Skip to content

Instantly share code, notes, and snippets.

@tatey
Created May 20, 2011 06:37
Show Gist options
  • Select an option

  • Save tatey/982454 to your computer and use it in GitHub Desktop.

Select an option

Save tatey/982454 to your computer and use it in GitHub Desktop.
require 'rake'
require 'erb'
UNLINKABLES = %w[Rakefile]
desc "install the dot files into user's home directory"
task :install do
replace_all = false
Dir['*'].each do |file|
next if UNLINKABLES.include? file
if File.exist?(File.join(ENV['HOME'], ".#{file.sub('.erb', '')}"))
if File.identical? file, File.join(ENV['HOME'], ".#{file.sub('.erb', '')}")
puts "identical ~/.#{file.sub('.erb', '')}"
elsif replace_all
replace_file(file)
else
print "overwrite ~/.#{file.sub('.erb', '')}? [ynaq] "
case $stdin.gets.chomp
when 'a'
replace_all = true
replace_file(file)
when 'y'
replace_file(file)
when 'q'
exit
else
puts "skipping ~/.#{file.sub('.erb', '')}"
end
end
else
link_file(file)
end
end
end
desc "uninstall the dot files from the user's home directory"
task :uninstall do
Dir['*'].each do |file|
next if UNLINKABLES.include? file
puts "unlinking ~/.#{file}"
system %Q{rm "$HOME/.#{file.sub('.erb', '')}"}
end
end
def replace_file(file)
system %Q{rm "$HOME/.#{file.sub('.erb', '')}"}
link_file(file)
end
def link_file(file)
if file =~ /.erb$/
puts "generating ~/.#{file.sub('.erb', '')}"
File.open(File.join(ENV['HOME'], ".#{file.sub('.erb', '')}"), 'w') do |new_file|
new_file.write ERB.new(File.read(file)).result(binding)
end
else
puts "linking ~/.#{file}"
system %Q{ln -s "$PWD/#{file}" "$HOME/.#{file}"}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment