From: elijah Date: Mon, 14 Jan 2013 04:26:30 +0000 (-0800) Subject: add Util.relative_symlink X-Git-Url: https://gitweb.fluxo.info/?a=commitdiff_plain;h=bea336480bf90f7c24737809e27b0bd224f42233;p=leap%2Fleap_cli.git add Util.relative_symlink --- diff --git a/lib/leap_cli/util.rb b/lib/leap_cli/util.rb index d12c5a6..fb5eb52 100644 --- a/lib/leap_cli/util.rb +++ b/lib/leap_cli/util.rb @@ -1,6 +1,7 @@ require 'digest/md5' require 'paint' require 'fileutils' +require 'pathname' require 'erb' require 'pty' @@ -276,6 +277,24 @@ module LeapCli `which #{cmd}`.strip.chars.any? end + # + # creates a relative symlink from absolute paths, removing prior symlink if necessary + # + # symlink 'new' is created, pointing to 'old' + # + def relative_symlink(old, new) + relative_path = Pathname.new(old).relative_path_from(Pathname.new(new)) + if File.symlink?(new) + if File.readlink(new) != relative_path.to_s + File.unlink(new) + log :updated, 'symlink %s' % Path.relative_path(new) + end + else + log :created, 'symlink %s' % Path.relative_path(new) + end + FileUtils.ln_s(relative_path, new) + end + # # compares md5 fingerprints to see if the contents of a file match the string we have in memory #