Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created June 4, 2014 17:24
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 ahoward/9f86e2bf62013cfaf263 to your computer and use it in GitHub Desktop.
Save ahoward/9f86e2bf62013cfaf263 to your computer and use it in GitHub Desktop.
~/dot/ify (should be in Dropbox for sanity)
#! /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