dtgm / chocolatey-packages

chocolateynuget packages published on chocolatey.org
https://chocolatey.org/profiles/dtgm
Apache License 2.0
48 stars 109 forks source link

chocolateyUninstall.ps1: Remove superfluous try/catch statements #163

Closed dtgm closed 8 years ago

dtgm commented 8 years ago

Pre-fix

$packageName = '{{PackageName}}'
$packageSearch = "$packageName"
$installerType = 'exe'
$silentArgs = '/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-'
$validExitCodes = @(0)
try {
  Get-ItemProperty -Path @( 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*',
                            'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*',
                            'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' ) `
                   -ErrorAction:SilentlyContinue `
  | Where-Object   { $_.DisplayName -like "$packageSearch*" } `
  | ForEach-Object { Uninstall-ChocolateyPackage -PackageName "$packageName" `
                                                 -FileType "$installerType" `
                                                 -SilentArgs "$($silentArgs)" `
                                                 -File "$($_.UninstallString)" `
                                                 -ValidExitCodes $validExitCodes }
} catch {
  throw $_.Exception
}

ToDo

  1. Remove try {
  2. Unindent all lines until } catch {
  3. Remove multiple lines:
} catch {
  throw $_.Exception
}

Post-fix

$packageName = '{{PackageName}}'
$packageSearch = "$packageName"
$installerType = 'exe'
$silentArgs = '/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-'
$validExitCodes = @(0)

Get-ItemProperty -Path @( 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*',
                   'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*',
                   'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' ) `
                 -ErrorAction:SilentlyContinue `
| Where-Object   { $_.DisplayName -like "$packageSearch*" } `
| ForEach-Object { Uninstall-ChocolateyPackage -PackageName "$packageName" `
                                    -FileType "$installerType" `
                                    -SilentArgs "$($silentArgs)" `
                                    -File "$($_.UninstallString)" `
                                    -ValidExitCodes $validExitCodes }
dtgm commented 8 years ago

Attempt to auto fix 189 pkgs:

while read line; do 
echo $line >> /tmp/fixthrow-progress;
sed -i -r -e '/try \{/,/\} catch \{/{
    s/try \{//;
    s/  (.*)/\1/g;
    }' \
    -n -e '1h; 1!H; ${; g; s/\} catch \{\n  throw \$_.Exception\n\}//;p;}' $line; 
done < /tmp/fixthrow
dtgm commented 8 years ago

Still need to fix

$ while read line; do  grep '} catch {' $line -l | cut -d'/' -f1; done < /tmp/fixthrow
arduino
arduino-intel.handover
avastbrowsercleanup.new
freearc.install
iotdk.handover
jacksum
rssowl
thebrain.install.work