Merge pull request #4362 from blueelvis/bug/2587-path-being-destroyed-when-too-long

Windows installer: Use PowerShell to update PATH value to avoid 1024 char truncation
pull/3835/head^2
Thomas Strömberg 2019-05-28 17:06:57 -07:00 committed by GitHub
commit 05332bdf9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 51 deletions

View File

@ -43,6 +43,8 @@ OutFile "minikube-installer.exe"
!define MUI_WELCOMEFINISHPAGE_BITMAP "logo.bmp"
!define MUI_UNWELCOMEFINISHPAGE_BITMAP "logo.bmp"
!define MUI_HEADERIMAGE_BITMAP "logo.bmp"
!define MUI_FINISHPAGE_NOAUTOCLOSE
!define MUI_UNFINISHPAGE_NOAUTOCLOSE
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE "LICENSE.txt"
@ -124,7 +126,7 @@ Section "Install"
# Files added here should be removed by the uninstaller (see section "uninstall")
File "minikube.exe"
File "logo.ico"
File "update_path.bat"
File "update_path.ps1"
# Add any other files for the install directory (license files, app data, etc) here
# Uninstaller - See function un.onInit and section "uninstall" for configuration
@ -147,17 +149,20 @@ Section "Install"
WriteRegStr HKLM "${UNINSTALLDIR}" "DisplayVersion" "$\"${VERSIONMAJOR}.${VERSIONMINOR}.${VERSIONBUILD}$\""
WriteRegDWORD HKLM "${UNINSTALLDIR}" "VersionMajor" ${VERSIONMAJOR}
WriteRegDWORD HKLM "${UNINSTALLDIR}" "VersionMinor" ${VERSIONMINOR}
# There is no option for modifying or repairing the install
# There is no option for modifying or repairing the install
WriteRegDWORD HKLM "${UNINSTALLDIR}" "NoModify" 1
WriteRegDWORD HKLM "${UNINSTALLDIR}" "NoRepair" 1
# Set the INSTALLSIZE constant (!defined at the top of this script) so Add/Remove Programs can accurately report the size
# Set the INSTALLSIZE constant (!defined at the top of this script) so Add/Remove Programs can accurately report the size
WriteRegDWORD HKLM "${UNINSTALLDIR}" "EstimatedSize" ${INSTALLSIZE}
# Add installed executable to PATH
# Cannot uset EnvVarUpdate since the path can be too long
# this is explicitly warned in the documentation page
# http://nsis.sourceforge.net/Environmental_Variables:_append,_prepend,_and_remove_entries
nsExec::Exec '"$INSTDIR\update_path.bat" add $INSTDIR'
# Cannot uset EnvVarUpdate since the path can be too long
# this is explicitly warned in the documentation page
# http://nsis.sourceforge.net/Environmental_Variables:_append,_prepend,_and_remove_entries
nsExec::ExecToLog 'powershell -ExecutionPolicy Bypass -WindowStyle Hidden -File "$INSTDIR\update_path.ps1" -Add -Path "$INSTDIR"'
SectionEnd
Section "Uninstall"
@ -168,12 +173,12 @@ Section "Uninstall"
RmDir /REBOOTOK "$SMPROGRAMS\${COMPANYNAME}"
# Remove uninstalled executable from PATH
nsExec::Exec '"$INSTDIR\update_path.bat" remove $INSTDIR' ; appends to the system path
nsExec::ExecToLog 'powershell -ExecutionPolicy Bypass -WindowStyle Hidden -File "$INSTDIR\update_path.ps1" -Remove -Path "$INSTDIR"' ; appends to the system path
# Remove files
Delete /REBOOTOK $INSTDIR\minikube.exe
Delete /REBOOTOK $INSTDIR\logo.ico
Delete /REBOOTOK $INSTDIR\update_path.bat
Delete /REBOOTOK $INSTDIR\update_path.ps1
# Always delete uninstaller as the last action
Delete /REBOOTOK $INSTDIR\uninstall.exe

View File

@ -1,42 +0,0 @@
@echo off
setlocal EnableExtensions EnableDelayedExpansion
if [%1] == [] (
goto :usage
)
if [%2] == [] (
goto :usage
)
set TARGET_PATH=%2 %3 %4 %5 %6 %7 %8 %9
:: Remove trailing spaces
for /f "tokens=* delims= " %%a in ("!TARGET_PATH!") do set "TARGET_PATH=%%a"
for /l %%a in (1,1,100) do if "!TARGET_PATH:~-1!"==" " set "TARGET_PATH=!TARGET_PATH:~0,-1!"
:: Remove trailing ; if any
if "%PATH:~-1%"==";" (
set PATH=!PATH:~0,-1!
)
if "%1" == "add" (
set "PATH=!PATH!;%TARGET_PATH%"
goto :update
)
if "%1" == "remove" (
set "PATH=!PATH:;%TARGET_PATH%=!"
goto :update
)
:usage
echo Script to add or remove to path environment variable
echo Usage:
echo %0 [add^|remove] path
exit /b 1
:update
call Setx PATH "!PATH!" /m

View File

@ -0,0 +1,70 @@
<#
.DESCRIPTION
This script is used to add/remove the installation path of Minikube in the PATH Environment variable as part of installation/uninstallation of Minikube.
The script assumes that the PATH exists before running.
.PARAMETER Add
This is a Switch parameter which tells the script to ADD the path supplied to the System's PATH Environment variable.
.PARAMETER Remove
This is a Switch parameter which tells the script to REMOVE the path supplied from the System's PATH Environment variable.
.PARAMETER Path
This parameter accepts a string which needs to be added/removed from the System's PATH Environment Variable.
#>
param(
[cmdletbinding()]
# This parameter dictates if the path needs to be added
[Parameter(Mandatory=$false,ParameterSetName="EnvironmentVariableAddOperation")]
[switch]
$Add,
# This parameter dictates if the path needs to be removed
[Parameter(Mandatory=$false,ParameterSetName="EnvironmentVariableRemoveOperation")]
[switch]
$Remove,
# This parameter tells us the path inside the $PATH Environment Variable for which the operation needs to be performed
[Parameter(Mandatory=$true)]
[string]
$Path
)
$currentSystemPath = [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine)
try {
if ($Add) {
Write-Output "Path needs to be added."
Write-Output "Checking if the given path already exists or not"
if ($currentSystemPath -match [Regex]::Escape($Path)) {
Write-Output "The provided path already exists in the system. Exiting now."
} else {
Write-Output "The given path was not found. Adding it now."
if ($currentSystemPath.EndsWith(";")) {
$newSystemPath = $currentSystemPath + $Path.Trim() + ";"
} else {
$newSystemPath = $currentSystemPath + ";" + $Path.Trim() + ";"
}
[Environment]::SetEnvironmentVariable("Path", $newSystemPath, [EnvironmentVariableTarget]::Machine)
Write-Output "Path has been added successfully."
}
} else {
Write-Output "Path needs to be added."
Write-Output "Checking if the given path already exists or not"
if ($currentSystemPath -match [Regex]::Escape($Path)) {
Write-Output "The provided path exists in the system. Removing now."
$newSystemPath = $currentSystemPath.Replace(($Path.Trim() + ";"), "")
[Environment]::SetEnvironmentVariable("Path", $newSystemPath, [EnvironmentVariableTarget]::Machine)
} else {
Write-Output "The given path was not found. Exiting now."
}
}
}
catch {
Write-Output "[Error]:: There was an error while execution. Please see the details below. Ensure that the script is running with administrator privileges."
Write-Output $_
}