Created
July 20, 2013 21:04
-
-
Save AndrewVos/6046434 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "rainbow" | |
require "ostruct" | |
require "fileutils" | |
if really = ARGV.include?("--really") | |
puts "You specified --really so I'm going to go ahead and makes these changes to your files. Good luck!".color(:red), "" | |
else | |
puts "You didn't specify --really so these changes won't really happen.".color(:green), "" | |
end | |
symlinks = Dir.glob("**/*.symlink", File::FNM_DOTMATCH).map do |s| | |
source = s | |
target = File.join("~", File.basename(s)).gsub(/\.symlink/, "") | |
full_source = File.expand_path(source) | |
full_target = File.expand_path(target) | |
ignore = File.exist?(full_target) && File.symlink?(full_target) && File.readlink(full_target) == File.expand_path(source) | |
overwrite = File.exist?(full_target) || File.symlink?(full_target) && !ignore | |
create = !ignore && !overwrite | |
OpenStruct.new(:source => source, :target => target, :full_source => full_source, :full_target => full_target, :ignore? => ignore, :overwrite? => overwrite, :create? => create) | |
end | |
widest_target = symlinks.map {|s| s.target.length}.max | |
symlinks.each do |symlink| | |
if symlink.ignore? | |
puts "ignore #{symlink.target.ljust(widest_target)} => #{symlink.source}".color(:green) | |
elsif symlink.overwrite? | |
puts "overwrite #{symlink.target.ljust(widest_target)} => #{symlink.source}".color(:red) | |
if really | |
FileUtils.remove_entry symlink.full_target | |
File.symlink(symlink.full_source, symlink.full_target) | |
end | |
elsif symlink.create? | |
puts "create #{symlink.target.ljust(widest_target)} => #{symlink.source}".color(:blue) | |
if really | |
File.symlink(symlink.full_source, symlink.full_target) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment