|
| 1 | +module Path |
| 2 | + module_function |
| 3 | + |
| 4 | + def clean(path) |
| 5 | + path = "#{path}/".gsub(/(\A|\/)(?:\.\/)+/, '\1').tr_s('/', '/') |
| 6 | + nil while path.sub!(/[^\/]+\/\.\.\//, '') |
| 7 | + path |
| 8 | + end |
| 9 | + |
| 10 | + def relative(path, base) |
| 11 | + path = clean(path) |
| 12 | + base = clean(base) |
| 13 | + path, base = [path, base].map{|s|s.split("/")} |
| 14 | + until path.empty? or base.empty? or path[0] != base[0] |
| 15 | + path.shift |
| 16 | + base.shift |
| 17 | + end |
| 18 | + path, base = [path, base].map{|s|s.join("/")} |
| 19 | + if base.empty? |
| 20 | + path |
| 21 | + elsif base.start_with?("../") or File.absolute_path?(base) |
| 22 | + File.expand_path(path) |
| 23 | + else |
| 24 | + base.gsub!(/[^\/]+/, '..') |
| 25 | + File.join(base, path) |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + def clean_link(src, dest) |
| 30 | + begin |
| 31 | + link = File.readlink(dest) |
| 32 | + rescue |
| 33 | + else |
| 34 | + return if link == src |
| 35 | + File.unlink(dest) |
| 36 | + end |
| 37 | + yield src, dest |
| 38 | + end |
| 39 | + |
| 40 | + # Extensions to FileUtils |
| 41 | + |
| 42 | + module Mswin |
| 43 | + def ln_safe(src, dest, *opt) |
| 44 | + cmd = ["mklink", dest.tr("/", "\\"), src.tr("/", "\\")] |
| 45 | + cmd[1, 0] = opt |
| 46 | + return if system("cmd", "/c", *cmd) |
| 47 | + # TODO: use RUNAS or something |
| 48 | + puts cmd.join(" ") |
| 49 | + end |
| 50 | + |
| 51 | + def ln_dir_safe(src, dest) |
| 52 | + ln_safe(src, dest, "/d") |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + module HardlinkExcutable |
| 57 | + def ln_exe(src, dest) |
| 58 | + ln(src, dest, force: true) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + def ln_safe(src, dest) |
| 63 | + ln_sf(src, dest) |
| 64 | + rescue Errno::ENOENT |
| 65 | + # Windows disallows to create broken symboic links, probably because |
| 66 | + # it is a kind of reparse points. |
| 67 | + raise if File.exist?(src) |
| 68 | + end |
| 69 | + |
| 70 | + alias ln_dir_safe ln_safe |
| 71 | + alias ln_exe ln_safe |
| 72 | + |
| 73 | + def ln_relative(src, dest, executable = false) |
| 74 | + return if File.identical?(src, dest) |
| 75 | + parent = File.dirname(dest) |
| 76 | + File.directory?(parent) or mkdir_p(parent) |
| 77 | + if executable |
| 78 | + return (ln_exe(src, dest) if File.exist?(src)) |
| 79 | + end |
| 80 | + clean_link(relative(src, parent), dest) {|s, d| ln_safe(s, d)} |
| 81 | + end |
| 82 | + |
| 83 | + def ln_dir_relative(src, dest) |
| 84 | + return if File.identical?(src, dest) |
| 85 | + parent = File.dirname(dest) |
| 86 | + File.directory?(parent) or mkdir_p(parent) |
| 87 | + clean_link(relative(src, parent), dest) {|s, d| ln_dir_safe(s, d)} |
| 88 | + end |
| 89 | + |
| 90 | + case (CROSS_COMPILING || RUBY_PLATFORM) |
| 91 | + when /linux|darwin|solaris/ |
| 92 | + prepend HardlinkExcutable |
| 93 | + extend HardlinkExcutable |
| 94 | + when /mingw|mswin/ |
| 95 | + unless File.respond_to?(:symlink) |
| 96 | + prepend Mswin |
| 97 | + extend Mswin |
| 98 | + end |
| 99 | + else |
| 100 | + end |
| 101 | +end |
0 commit comments