Skip to content

Instantly share code, notes, and snippets.

@gmurphey
Last active December 17, 2015 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmurphey/5645397 to your computer and use it in GitHub Desktop.
Save gmurphey/5645397 to your computer and use it in GitHub Desktop.
require 'rake'
require 'erb'
namespace :setup do
desc "links dotfiles into home directory"
task :dotfiles do
replace_all = false
files = Dir['*'] - %w[Rakefile README.md oh-my-zsh resources packages]
files.each do |file|
system %Q{mkdir -p "$HOME/.#{File.dirname{file}}"} if file =~ /\//
target_file = File.join(ENV["HOME"], ".#{file.sub(/\.erb$/, '')}")
if File.exist?(target_file)
if File.identical? file, target_file
puts "using #{file}"
elsif replace_all
replace_file(file)
else
print "overwrite #{target_file}? [ynaq] "
case $stdin.gets.chomp
when "a"
replace_all = true
replace_file(file)
when "y"
replace_file(file)
when "q"
exit
else
puts "skipping #{target_file}"
end
end
else
link_file(file)
end
end
end
end
def replace_file(file)
system %Q{rm -rf "$HOME/.#{file.sub(/\.erb$/, '')}"}
link_file(file)
end
def link_file(file)
if file =~ /.erb$/
puts "creating ~/.#{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