-
-
Save ahoward/9f86e2bf62013cfaf263 to your computer and use it in GitHub Desktop.
~/dot/ify (should be in Dropbox for sanity)
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 | |
# usage | |
# | |
usage = <<-__ | |
dot/ify | |
keep *all* your dot files in one directory. link them back into your home | |
dir. for example | |
~/dot/foo -> ~/.foo | |
~/dot/bar -> ~/.bar | |
__ | |
abort(usage) if ARGV.delete('-h')||ARGV.delete('--help') | |
# setup | |
# | |
require 'fileutils' | |
require 'pathname' | |
home = Dir.home | |
dotdir = File.join(home, 'dot') | |
dotify = File.join(dotdir, 'ify') | |
dot = '.' | |
dirname, basename = File.split(File.expand_path(__FILE__)) | |
this = File.join(dirname, basename) | |
realpath = lambda{|path| Pathname.new(path).realpath.to_s rescue nil} | |
identical = lambda{|a,b| realpath[a] == realpath[b]} | |
# ensure the dotdir exists | |
# | |
FileUtils.mkdir_p(dotdir) unless test(?e, dotdir) | |
abort("#{ dotdir } does not exist!") unless test(?d, dotdir) | |
# ensure dot/ify script is linked into the dotdir | |
# | |
unless identical[this, dotify] | |
src, dst = this, dotify | |
FileUtils.ln_s(src, dst) | |
end | |
# move any files specified on the command line into the dotdir | |
# | |
unless ARGV.empty? | |
srcs = ARGV | |
srcs.each do |src| | |
basename = File.basename(src).sub(/^[.]/, '') | |
dst = File.join(dotdir, basename) | |
FileUtils.mv(src, dst) unless identical[src, dst] | |
end | |
end | |
# link dot files back into homedir. eg. ~/dot/foo -> ~/.foo | |
# | |
glob = File.join(dirname, '*') | |
entries = Dir.glob(glob) | |
entries.delete_if{|entry| entry == this} | |
entries.each do |src| | |
dst = File.join(home, dot + File.basename(src)) | |
if test(?e, dst) | |
unless identical[src, dst] | |
abort("#{ dst } exists!") | |
end | |
else | |
#FileUtils.ln_s(src, dst) | |
`ln -sf #{ src.inspect } #{ dst.inspect }` | |
puts "#{ src } #=> #{ dst }" | |
end | |
end | |
# hack in support for Dir.home (not in ruby 1.8.x) | |
# | |
BEGIN { | |
unless Dir.respond_to?(:home) | |
def Dir.home | |
home = | |
catch :home do | |
["HOME", "USERPROFILE"].each do |key| | |
throw(:home, ENV[key]) if ENV[key] | |
end | |
if ENV["HOMEDRIVE"] and ENV["HOMEPATH"] | |
throw(:home, "#{ ENV['HOMEDRIVE'] }:#{ ENV['HOMEPATH'] }") | |
end | |
File.expand_path("~") rescue(File::ALT_SEPARATOR ? "C:/" : "/") | |
end | |
File.expand_path(home) | |
end | |
end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment