From 814852f85ec06f1084d5ca03a0291f6af6fb6fa9 Mon Sep 17 00:00:00 2001 From: Alden Flatt Date: Wed, 20 Mar 2013 11:50:38 -0600 Subject: [PATCH 1/2] Add the ability to remove shortcuts on uninstall When a Chocolatey-Uninstall is done, the bash and batch shortcuts will now be removed using Remove-BinFile I've also added a message in Generate-BinFile that the bash shortcut is getting added, as this was missing. I've also added a message to Chocolatey-Uninstall describing which directory is removed on uninstall. --- src/functions/Chocolatey-Uninstall.ps1 | 2 ++ src/functions/Generate-BinFile.ps1 | 1 + src/functions/Get-ChocolateyBins.ps1 | 15 +++++++++++--- src/functions/Remove-BinFile.ps1 | 27 ++++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 src/functions/Remove-BinFile.ps1 diff --git a/src/functions/Chocolatey-Uninstall.ps1 b/src/functions/Chocolatey-Uninstall.ps1 index 6db9ba2..2543d55 100644 --- a/src/functions/Chocolatey-Uninstall.ps1 +++ b/src/functions/Chocolatey-Uninstall.ps1 @@ -24,6 +24,8 @@ param( else { Write-Debug "Looking for $($package).$($versions.found)" $packageFolder = Join-Path $nugetLibPath "$($package).$($versions.found)" + Write-host "Uninstalling from folder $packageFolder" + Get-ChocolateyBins $packageFolder -uninstall Run-ChocolateyPS1 $packageFolder $package "uninstall" Remove-Item -Recurse -Force $packageFolder } diff --git a/src/functions/Generate-BinFile.ps1 b/src/functions/Generate-BinFile.ps1 index fc5aa5f..74d2ab0 100644 --- a/src/functions/Generate-BinFile.ps1 +++ b/src/functions/Generate-BinFile.ps1 @@ -11,6 +11,7 @@ param( $path = $path.ToLower().Replace($nugetPath.ToLower(), "%DIR%..\").Replace("\\","\") $pathBash = $path.Replace("%DIR%..\","`$DIR/../").Replace("\","/") Write-Host "Adding $packageBatchFileName and pointing to `'$path`'." -ForegroundColor $Note + Write-Host "Adding $packageBashFileName and pointing to `'$path`'." -ForegroundColor $Note if ($useStart) { Write-Host "Setting up $name as a non-command line application." -ForegroundColor $Note "@echo off diff --git a/src/functions/Get-ChocolateyBins.ps1 b/src/functions/Get-ChocolateyBins.ps1 index c9dfe79..123962c 100644 --- a/src/functions/Get-ChocolateyBins.ps1 +++ b/src/functions/Get-ChocolateyBins.ps1 @@ -1,6 +1,7 @@ function Get-ChocolateyBins { param( - [string] $packageFolder + [string] $packageFolder, + [switch] $uninstall ) Write-Debug "Running 'Get-ChocolateyBins' for $packageFolder"; @@ -17,9 +18,17 @@ Adding batch files for any executables found to a location on PATH. In other wor foreach ($file in $files) { if (!(test-path($file.FullName + '.ignore'))) { if (test-path($file.FullName + '.gui')) { - Generate-BinFile $file.Name.Replace(".exe","").Replace(".EXE","") $file.FullName -useStart + if ($uninstall) { + Remove-BinFile $file.Name.Replace(".exe","").Replace(".EXE","") $file.FullName + } else { + Generate-BinFile $file.Name.Replace(".exe","").Replace(".EXE","") $file.FullName -useStart + } } else { - Generate-BinFile $file.Name.Replace(".exe","").Replace(".EXE","") $file.FullName + if ($uninstall) { + Remove-BinFile $file.Name.Replace(".exe","").Replace(".EXE","") $file.FullName + } else { + Generate-BinFile $file.Name.Replace(".exe","").Replace(".EXE","") $file.FullName + } } $batchCreated = $true } diff --git a/src/functions/Remove-BinFile.ps1 b/src/functions/Remove-BinFile.ps1 new file mode 100644 index 0000000..1fcc9fc --- /dev/null +++ b/src/functions/Remove-BinFile.ps1 @@ -0,0 +1,27 @@ +function Remove-BinFile { +param( + [string] $name, + [string] $path +) + Write-Debug "Running 'Remove-BinFile' for $name with path:`'$path`'"; + + $packageBatchFileName = Join-Path $nugetExePath "$name.bat" + $packageBashFileName = Join-Path $nugetExePath "$name" + $path = $path.ToLower().Replace($nugetPath.ToLower(), "%DIR%..\").Replace("\\","\") + $pathBash = $path.Replace("%DIR%..\","`$DIR/../").Replace("\","/") + Write-Debug "Attempting to remove the batch and bash shortcuts: $packageBatchFileName and $packageBashFileName" + if (Test-Path $packageBatchFileName) { + Write-Host "Removing batch file $packageBatchFileName which pointed to `'$path`'." -ForegroundColor $Note + Remove-Item $packageBatchFileName + } + else { + Write-Host "Tried to remove batch file $packageBatchFileName but it was already removed." -ForegroundColor $Note + } + if (Test-Path $packageBashFileName) { + Write-Host "Removing bash file $packageBashFileName which pointed to `'$path`'." -ForegroundColor $Note + Remove-Item $packageBashFileName + } + else { + Write-Host "Tried to remove bash file $packageBashFileName but it was already removed." -ForegroundColor $Note + } +} From 24b905602d2d457da978a315bfef6215f48b6659 Mon Sep 17 00:00:00 2001 From: Alden Flatt Date: Thu, 18 Apr 2013 14:45:35 -0600 Subject: [PATCH 2/2] Quote args to shell script When the args contain spaces, the existing shell script would fail. If we quote the args this will fix the problem --- src/functions/Generate-BinFile.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/functions/Generate-BinFile.ps1 b/src/functions/Generate-BinFile.ps1 index 74d2ab0..46a2ffd 100644 --- a/src/functions/Generate-BinFile.ps1 +++ b/src/functions/Generate-BinFile.ps1 @@ -20,7 +20,7 @@ start """" ""$path"" %*" | Out-File $packageBatchFileName -encoding ASCII "#!/bin/sh DIR=`${0%/*} -""$pathBash"" `$* &" | Out-File $packageBashFileName -encoding ASCII +""$pathBash"" ""`$*"" &" | Out-File $packageBashFileName -encoding ASCII } else { "@echo off @@ -29,6 +29,6 @@ SET DIR=%~dp0% "#!/bin/sh DIR=`${0%/*} -""$pathBash"" `$*" | Out-File $packageBashFileName -encoding ASCII +""$pathBash"" ""`$*""" | Out-File $packageBashFileName -encoding ASCII } } \ No newline at end of file