From bea336480bf90f7c24737809e27b0bd224f42233 Mon Sep 17 00:00:00 2001 From: elijah Date: Sun, 13 Jan 2013 20:26:30 -0800 Subject: add Util.relative_symlink --- lib/leap_cli/util.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'lib/leap_cli/util.rb') 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 # -- cgit v1.2.3