Skip to content

Instantly share code, notes, and snippets.

@rdamen
Created December 14, 2008 12:16
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 rdamen/35675 to your computer and use it in GitHub Desktop.
Save rdamen/35675 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Syler, the symlinking villain
#
# Recursivly symlinks every file from SRC to DST,
# creates corresponding directories if they don't exist.
require 'pathname'
module Syler
extend self
def pass(msg) puts "\e[32m\[\e[1mPASS\e[0m\e[32m\]\e[0m #{msg}"; true; end
def fail(msg) puts "\e[31m\[\e[1mFAIL\e[0m\e[31m\]\e[0m #{msg}"; false; end
def runner(arg)
src, dst = path(arg[0]), path(arg[1])
if src.directory? && dst.directory?
link_dir(src,dst)
else
fail("Both SRC and DST need to be directories.")
end
end
def path(path)
Pathname.new(File.expand_path(path))
end
def link_dir(src, dst, base=src)
# not using src.children here because it includes hidden files
kids(src).each do |child|
child_dst = path("#{dst}/#{child.relative_path_from(base)}")
child_src = child.relative_path_from(child_dst.dirname)
if child.directory?
run("mkdir #{child_dst}") unless child_dst.exist?
link_dir(child, dst, base)
else
if child_dst.exist?
fail("#{child_dst} already exists.")
else
run("ln -s #{child_src} #{child_dst}")
end
end
end
end
def kids(dir)
Dir.glob("#{dir}/*").map{|x| path(x)}
end
def run(cmd)
system(cmd)
pass(cmd)
end
end
if ARGV.size == 2
Syler.runner(ARGV)
else
Syler.fail("Usage: $ syler SRC DST")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment