Skip to content

Commit

Permalink
Warn on shim overwrite (#2033)
Browse files Browse the repository at this point in the history
  • Loading branch information
rasa authored and r15ch13 committed May 4, 2018
1 parent 7d3277b commit 0e2c253
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/core.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,38 @@ function movedir($from, $to) {
}
}

function get_app_name($path) {
if ($path -match '([^/\\]+)[/\\]current[/\\]') {
return $Matches[1].tolower()
}
return ""
}

function warn_on_overwrite($shim_ps1, $path) {
if (!([System.IO.File]::Exists($shim_ps1))) {
return
}
$reader = [System.IO.File]::OpenText($shim_ps1)
$line = $reader.ReadLine().replace("`r","").replace("`n","")
$reader.Close()
$shim_app = get_app_name $line
$path_app = get_app_name $path
if ($shim_app -eq $path_app) {
return
}
$filename = [System.IO.Path]::GetFileName($path)
warn "Overwriting shim to $filename installed from $shim_app"
}

function shim($path, $global, $name, $arg) {
if(!(test-path $path)) { abort "Can't shim '$(fname $path)': couldn't find '$path'." }
$abs_shimdir = ensure (shimdir $global)
if(!$name) { $name = strip_ext (fname $path) }

$shim = "$abs_shimdir\$($name.tolower())"

warn_on_overwrite "$shim.ps1" $path

# convert to relative path
Push-Location $abs_shimdir
$relative_path = resolve-path -relative $path
Expand Down

0 comments on commit 0e2c253

Please sign in to comment.