public
Created

  • Download Gist
dotfiles.thor
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
require 'erb'
 
# inspired by https://github.com/ryanb/dotfiles/blob/master/Rakefile
 
 
class Dotfiles < Thor
include Thor::Actions
include Thor::Util
desc 'install', 'install dotfiles into home directory'
method_option :replace_all, :aliases => '-r', :desc => 'Replace files without asking.'
def install
replace_all = options[:replace_all]
`git submodule init && git submodule init`
Dir['*'].each do |file|
next if %w[Rakefile README.rdoc README.textile LICENSE vim vimrc].include? file
if File.exist?(File.expand_path("~/.#{file.sub('.erb', '')}"))
if File.identical? file, File.expand_path("~/.#{file.sub('.erb', '')}")
say_status 'identical', "~/.#{file.sub('.erb', '')}", :green
elsif replace_all
replace_file(file)
else
case ask "overwrite ~/.#{file.sub('.erb', '')}? [ynaq] "
when 'a'
replace_all = true
replace_file(file)
when 'y'
replace_file(file)
when 'q'
exit
else
say_status 'skipping', "~/.#{file.sub('.erb', '')}", :green
end
end
else
prepare_file(file)
end
end
copy_file 'vim', '~/.vim'
copy_file 'vimrc', '~/.vimrc'
inside '~/.vim' do
`git submodule init && git submodule update`
end
end
desc 'add FILE', 'add FILE from your home directory to your dotfiles repo'
def add(file)
if file.starts_with? '.'
file = file.sub('.', '')
end
if File.exist? File.expand_path("~/.#{file}")
copy_file "~/#{file}", file
remove_file "~/#{file}"
prepare_file file
link_file file
end
end
no_tasks do
def replace_file(file)
remove_file "~/.#{file.sub('.erb', '')}"
prepare_file(file)
end
def prepare_file(file)
if file =~ /.erb$/
say_status 'generating', "~/.#{file.sub('.erb', '')}", :green
template file, "~/.#{file.sub('.erb', '')}"
else
say_status 'linking', "~/.#{file}", :green
link_file file, "~/.#{file}"
end
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.