]> gitweb.fluxo.info Git - leap/leap_cli.git/commitdiff
add Util.relative_symlink
authorelijah <elijah@riseup.net>
Mon, 14 Jan 2013 04:26:30 +0000 (20:26 -0800)
committerelijah <elijah@riseup.net>
Mon, 14 Jan 2013 04:26:30 +0000 (20:26 -0800)
lib/leap_cli/util.rb

index d12c5a67ef012a8dbbbc05d05caa46f3ec08bb00..fb5eb52bb49c9b5ffd3fbac15a869f49e4879a6b 100644 (file)
@@ -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
     #