Created
November 13, 2022 15:20
-
-
Save melMass/15823a7ca0c4fba7022a2e23a04ad754 to your computer and use it in GitHub Desktop.
WIP Cross Link
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Wrapper for cross platform symlinks, this uses soft links on all platforms. | |
def link [src:string, target:string, --dry, --verbose(-v), --absolute(-a) ] { | |
def _log [msg: string] { | |
print $"(ansi yellow)($msg)(ansi reset)" | |
} | |
# safe checks | |
let dir = ($src | path type | $in == dir) | |
let src_exist = ($src | path exists) | |
let target_exist = ($target | path exists) | |
let fam = $nu.os-info.family | |
if ($verbose) { | |
_log $"Family: ($fam) | Source is a directory: ($dir) | Target exist: ($target_exist)" | |
_log $"Will symlink ($src | path expand) in ($target | path expand)" | |
} | |
if $src_exist == false { | |
print $"(ansi red_bold)Source does not exists, aborting.(ansi reset)" | |
} | |
if $target_exist { | |
print $"(ansi red_bold)Target exists, aborting.(ansi reset)" | |
} | |
if $target_exist == false and $src_exist { | |
let src = if $absolute { | |
($src | path expand | str replace -sa '\' '/' ) | |
} else { | |
$src | |
} | |
let target = if $absolute { | |
($target | path expand | str replace -sa '\' '/' ) | |
} else { | |
$target | |
} | |
if $dry { print $"Would run:"} | |
if $nu.os-info.family == windows { | |
let src = ($src | str replace -sa '/' '\') | |
let target = ($target | str replace -sa '/' '\') | |
if $dir { | |
if $dry or $verbose { | |
print $"mklink /D \"($target)\" \"($src)\"" | |
} else { | |
mklink /D $target $src | |
} | |
} else { | |
if $dry or $verbose { | |
print $"mklink \"($target)\" \"($src)\"" | |
} | |
if $dry == false { | |
mklink $"($target)" $"($src)" | |
} | |
} | |
} else { | |
if $dry or $verbose { | |
print $"ln -s \"($src)\" \"($target)\"" | |
} else { | |
ln -s $"($src)" $"($target)" | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment