From cab950a8550538c5f525389210371ecc6b77c014 Mon Sep 17 00:00:00 2001 From: Franklin Yu Date: Tue, 27 Nov 2018 21:25:17 -0500 Subject: [PATCH 1/3] Migrate to portable package --- OpenJDK/openjdk.nuspec | 2 +- OpenJDK/tools/chocolateyinstall.ps1 | 63 ++++++++++++++++++++++++--- OpenJDK/tools/chocolateyuninstall.ps1 | 26 ----------- 3 files changed, 59 insertions(+), 32 deletions(-) diff --git a/OpenJDK/openjdk.nuspec b/OpenJDK/openjdk.nuspec index 34a03f3..cc12b99 100644 --- a/OpenJDK/openjdk.nuspec +++ b/OpenJDK/openjdk.nuspec @@ -2,7 +2,7 @@ openjdk - 11.0.1 + 11.0.1.20181126 https://github.com/johanjanssen/ChocolateyPackages/tree/master/OpenJDK OpenJDK Oracle diff --git a/OpenJDK/tools/chocolateyinstall.ps1 b/OpenJDK/tools/chocolateyinstall.ps1 index 1f7a7b0..b823180 100644 --- a/OpenJDK/tools/chocolateyinstall.ps1 +++ b/OpenJDK/tools/chocolateyinstall.ps1 @@ -1,9 +1,62 @@ $packageName = $env:ChocolateyPackageName -$targetDir = 'C:\Program Files\OpenJDK' +$toolsDir = Split-Path -Parent $MyInvocation.MyCommand.Definition +$legacyDir = 'C:\Program Files\OpenJDK' $url64 = 'https://download.java.net/java/GA/jdk11/13/GPL/openjdk-11.0.1_windows-x64_bin.zip' $checksum64 = '289dd06e06c2cbd5e191f2d227c9338e88b6963fd0c75bceb9be48f0394ede21' -Install-ChocolateyZipPackage $packageName $url64 $targetDir -Install-ChocolateyEnvironmentVariable 'JAVA_HOME' $targetDir\jdk-11.0.1 'Machine' -# The full path instead of the %JAVA_HOME% is needed so it can be removed with the Chocolatey Uninstall -Install-ChocolateyPath 'C:\Program Files\OpenJDK\jdk-11.0.1\bin' -PathType 'Machine' +#------------------------------------------------------------------------------- +# begin migration + +if (! (Get-Command -Name Uninstall-ChocolateyPath -ErrorAction SilentlyContinue)) { + function Uninstall-ChocolateyPath { + param( + [parameter(Mandatory=$true, Position=0)][string] $pathToUninstall, + [parameter(Mandatory=$false, Position=1)][EnvironmentVariableTarget] $pathType = [EnvironmentVariableTarget]::User + ) + if ($env:PATH.ToLower().Contains($pathToUnInstall.ToLower())) { + $statementTerminator = ';' + Write-Host "PATH environment variable contains $pathToUnInstall. Removing..." + [Collections.ArrayList]$actualPath = (Get-EnvironmentVariable -Name 'Path' -Scope $pathType -PreserveVariables).Split($statementTerminator) + + $actualPath.Remove($pathToUnInstall) + $newPath = $actualPath -Join $statementTerminator + + if ($pathType -eq [EnvironmentVariableTarget]::Machine) { + if (Test-ProcessAdminRights) { + Set-EnvironmentVariable -Name 'Path' -Value $newPath -Scope $pathType + } else { + $psArgs = "UnInstall-ChocolateyPath -pathToUnInstall `'$originalPathToUnInstall`' -pathType `'$pathType`'" + Start-ChocolateyProcessAsAdmin "$psArgs" + } + } else { + Set-EnvironmentVariable -Name 'Path' -Value $newPath -Scope $pathType + } + } + } +} + +if (Test-Path -Path $legacyDir -ErrorAction SilentlyContinue) { + Write-Debug "Removing legacy installation: $legacyDir" + Remove-Item -Recurse -Force -Path $legacyDir + $pathType = [EnvironmentVariableTarget]::Machine + Uninstall-ChocolateyPath -PathToUninstall "$legacyDir\jdk-11\bin" -PathType $pathType + Uninstall-ChocolateyPath -PathToUninstall "$legacyDir\jdk-11.0.1\bin" -PathType $pathType +} + +# end migration +#------------------------------------------------------------------------------- + +$pkgArgs = @{ + packageName = $packageName + unzipLocation = $toolsDir + url64bit = $url64 + checksum64 = $checksum64 + checksumType64 = 'sha256' +} +Install-ChocolateyZipPackage @pkgArgs +if (Test-ProcessAdminRights) { + $pathType = 'Machine' +} else { + $pathType = 'User' +} +Install-ChocolateyEnvironmentVariable 'JAVA_HOME' "$toolsDir\jdk-11.0.1" $pathType diff --git a/OpenJDK/tools/chocolateyuninstall.ps1 b/OpenJDK/tools/chocolateyuninstall.ps1 index d329053..934f47a 100644 --- a/OpenJDK/tools/chocolateyuninstall.ps1 +++ b/OpenJDK/tools/chocolateyuninstall.ps1 @@ -1,27 +1 @@ Uninstall-ChocolateyEnvironmentVariable 'JAVA_HOME' 'Machine' -rm -r 'C:\Program Files\OpenJDK\jdk-11.0.1' - -$pathToUnInstall = 'C:\Program Files\OpenJDK\jdk-11.0.1\bin' -$pathType = 'Machine' - -if ($env:PATH.ToLower().Contains($pathToUnInstall.ToLower())) -{ - $statementTerminator = ";" - Write-Host "PATH environment variable contains $pathToUnInstall. Removing..." - $actualPath = [System.Collections.ArrayList](Get-EnvironmentVariable -Name 'Path' -Scope $pathType).split($statementTerminator) - - $actualPath.Remove($pathToUnInstall) - $newPath = $actualPath -Join $statementTerminator - - if ($pathType -eq [System.EnvironmentVariableTarget]::Machine) { - if (Test-ProcessAdminRights) { - Set-EnvironmentVariable -Name 'Path' -Value $newPath -Scope $pathType - } else { - $psArgs = "UnInstall-ChocolateyPath -pathToUnInstall `'$originalPathToUnInstall`' -pathType `'$pathType`'" - Start-ChocolateyProcessAsAdmin "$psArgs" - } - } else { - Set-EnvironmentVariable -Name 'Path' -Value $newPath -Scope $pathType - } -} - From 158497788fae90cbe4462cfe04e77965ae44c525 Mon Sep 17 00:00:00 2001 From: Franklin Yu Date: Thu, 29 Nov 2018 10:24:46 -0500 Subject: [PATCH 2/3] Remove polyfill The API has not been settled; Having custom name is future-proof. --- OpenJDK/tools/chocolateyinstall.ps1 | 42 ++++++++++++++--------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/OpenJDK/tools/chocolateyinstall.ps1 b/OpenJDK/tools/chocolateyinstall.ps1 index b823180..04e3962 100644 --- a/OpenJDK/tools/chocolateyinstall.ps1 +++ b/OpenJDK/tools/chocolateyinstall.ps1 @@ -7,30 +7,28 @@ $checksum64 = '289dd06e06c2cbd5e191f2d227c9338e88b6963fd0c75bceb9be48f0394ede21 #------------------------------------------------------------------------------- # begin migration -if (! (Get-Command -Name Uninstall-ChocolateyPath -ErrorAction SilentlyContinue)) { - function Uninstall-ChocolateyPath { - param( - [parameter(Mandatory=$true, Position=0)][string] $pathToUninstall, - [parameter(Mandatory=$false, Position=1)][EnvironmentVariableTarget] $pathType = [EnvironmentVariableTarget]::User - ) - if ($env:PATH.ToLower().Contains($pathToUnInstall.ToLower())) { - $statementTerminator = ';' - Write-Host "PATH environment variable contains $pathToUnInstall. Removing..." - [Collections.ArrayList]$actualPath = (Get-EnvironmentVariable -Name 'Path' -Scope $pathType -PreserveVariables).Split($statementTerminator) +function Uninstall-MyChocolateyPath { + param( + [parameter(Mandatory=$true, Position=0)][string] $pathToUninstall, + [parameter(Mandatory=$false, Position=1)][EnvironmentVariableTarget] $pathType = [EnvironmentVariableTarget]::User + ) + if ($env:PATH.ToLower().Contains($pathToUnInstall.ToLower())) { + $statementTerminator = ';' + Write-Host "PATH environment variable contains $pathToUnInstall. Removing..." + [Collections.ArrayList]$actualPath = (Get-EnvironmentVariable -Name 'Path' -Scope $pathType -PreserveVariables).Split($statementTerminator) - $actualPath.Remove($pathToUnInstall) - $newPath = $actualPath -Join $statementTerminator + $actualPath.Remove($pathToUnInstall) + $newPath = $actualPath -Join $statementTerminator - if ($pathType -eq [EnvironmentVariableTarget]::Machine) { - if (Test-ProcessAdminRights) { - Set-EnvironmentVariable -Name 'Path' -Value $newPath -Scope $pathType - } else { - $psArgs = "UnInstall-ChocolateyPath -pathToUnInstall `'$originalPathToUnInstall`' -pathType `'$pathType`'" - Start-ChocolateyProcessAsAdmin "$psArgs" - } - } else { + if ($pathType -eq [EnvironmentVariableTarget]::Machine) { + if (Test-ProcessAdminRights) { Set-EnvironmentVariable -Name 'Path' -Value $newPath -Scope $pathType + } else { + $psArgs = "UnInstall-ChocolateyPath -pathToUnInstall `'$originalPathToUnInstall`' -pathType `'$pathType`'" + Start-ChocolateyProcessAsAdmin "$psArgs" } + } else { + Set-EnvironmentVariable -Name 'Path' -Value $newPath -Scope $pathType } } } @@ -39,8 +37,8 @@ if (Test-Path -Path $legacyDir -ErrorAction SilentlyContinue) { Write-Debug "Removing legacy installation: $legacyDir" Remove-Item -Recurse -Force -Path $legacyDir $pathType = [EnvironmentVariableTarget]::Machine - Uninstall-ChocolateyPath -PathToUninstall "$legacyDir\jdk-11\bin" -PathType $pathType - Uninstall-ChocolateyPath -PathToUninstall "$legacyDir\jdk-11.0.1\bin" -PathType $pathType + Uninstall-MyChocolateyPath -PathToUninstall "$legacyDir\jdk-11\bin" -PathType $pathType + Uninstall-MyChocolateyPath -PathToUninstall "$legacyDir\jdk-11.0.1\bin" -PathType $pathType } # end migration From fc343b99e32334a00d2744d83c8e0c3b1904f970 Mon Sep 17 00:00:00 2001 From: Franklin Yu Date: Thu, 29 Nov 2018 10:28:08 -0500 Subject: [PATCH 3/3] Make uninstallation portable --- OpenJDK/tools/chocolateyuninstall.ps1 | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/OpenJDK/tools/chocolateyuninstall.ps1 b/OpenJDK/tools/chocolateyuninstall.ps1 index 934f47a..09352ca 100644 --- a/OpenJDK/tools/chocolateyuninstall.ps1 +++ b/OpenJDK/tools/chocolateyuninstall.ps1 @@ -1 +1,6 @@ -Uninstall-ChocolateyEnvironmentVariable 'JAVA_HOME' 'Machine' +if (Test-ProcessAdminRights) { + $pathType = 'Machine' +} else { + $pathType = 'User' +} +Uninstall-ChocolateyEnvironmentVariable 'JAVA_HOME' $pathType