Skip to content

Commit

Permalink
Merge pull request #264 from henno/improve-powershell-install-script
Browse files Browse the repository at this point in the history
Improve PATH handling in Powershell, fixes #263
  • Loading branch information
ducaale committed Apr 9, 2023
2 parents 38b6c73 + 7669339 commit 20f834a
Showing 1 changed file with 37 additions and 40 deletions.
77 changes: 37 additions & 40 deletions install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,64 @@
$ProgressPreference = 'SilentlyContinue'
$release = Invoke-RestMethod -Method Get -Uri "https://api.github.com/repos/ducaale/xh/releases/latest"
$asset = $release.assets | Where-Object name -like *x86_64-pc-windows*.zip
$destdir = "$home\bin\"
$zipfile = "$env:TEMP\$($asset.name)"
$destdir = "$home\bin"
$zipfile = "$env:TEMP\$( $asset.name )"
$zipfilename = [System.IO.Path]::GetFileNameWithoutExtension("$zipfile")

Write-Output "Downloading: $($asset.name)"
Write-Output "Downloading: $( $asset.name )"
Invoke-RestMethod -Method Get -Uri $asset.browser_download_url -OutFile $zipfile

# Checks if an older version of xh.exe (includes xhs.exe) exists in '$destdir', if yes, then delete it, if not, then download latest zip to extract from.

$xhPath = "${destdir}xh.exe"
$xhsPath = "${destdir}xhs.exe"
if (Test-Path -Path $xhPath -PathType Leaf) {
"Removing previous installation of xh from $($destdir)"
# Check if an older version of xh.exe (includes xhs.exe) exists in '$destdir', if yes, then delete it, if not then download latest zip to extract from
$xhPath = "${destdir}\xh.exe"
$xhsPath = "${destdir}\xhs.exe"
if (Test-Path -Path $xhPath -PathType Leaf)
{
Write-Output "Removing previous installation of xh from $destdir"
Remove-Item -r -fo $xhPath
Remove-Item -r -fo $xhsPath
}

# xh.exe extraction start.
# Create dir for result of extraction
New-Item -ItemType Directory -Path $destdir -Force | Out-Null

# Decompress the zip file to the destination directory
Add-Type -Assembly System.IO.Compression.FileSystem

$zip = [IO.Compression.ZipFile]::OpenRead($zipfile)
$entries = $zip.Entries | Where-Object { $_.FullName -like '*.exe' }
$entries | ForEach-Object { [IO.Compression.ZipFileExtensions]::ExtractToFile($_, $destdir + "\" + $_.Name) }

# Create dir for result of extraction.

New-Item -ItemType Directory -Path $destdir -Force | Out-Null

# Extraction.

$entries | ForEach-Object { [IO.Compression.ZipFileExtensions]::ExtractToFile( $_, $destdir + $_.Name) }

# Free the zipfile.

# Free the zipfile
$zip.Dispose()

Remove-Item -Path $zipfile

# Copy xh.exe as xhs.exe into bin.

# Copy xh.exe as xhs.exe into bin
Copy-Item $xhPath $xhsPath

# Add to environment variables.
# Get version from zip file name.
$xhVersion = $($zipfilename.trim("xh-v -x86_64-pc-windows-msvc.zip") )

# Inform user where the executables have been put
Write-Output "xh v$( $xhVersion ) has been installed to:`n - $xhPath`n - $xhsPath"

$p = [System.Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::User)
if (!$p.ToLower().Contains($destdir.ToLower())) {
# Make sure destdir is in the path
$userPath = [System.Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::User)
$machinePath = [System.Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::Machine)

# Path to "user"/bin.
# If userPath AND machinePath both do not contain bin, then add it to user path
if (!($userPath.ToLower().Contains($destdir.ToLower())) -and !($machinePath.ToLower().Contains($destdir.ToLower())))
{
# Update userPath
$userPath = $userPath.Trim(";") + ";$destdir"

Write-Output "Adding $destdir to your Path"

$p += "$destdir"
[System.Environment]::SetEnvironmentVariable('Path', $p, [System.EnvironmentVariableTarget]::User)
$Env:Path = [System.Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::Machine) + ";" + $p

# Path to xhs.exe.
# Modify PATH for new windows
Write-Output "`nAdding $destdir directory to the PATH variable."
[System.Environment]::SetEnvironmentVariable('Path', $userPath, [System.EnvironmentVariableTarget]::User)

Write-Host "PATH environment variable changed (restart your applications that use command line)." -foreground yellow
}
# Modify PATH for current terminal
Write-Output "`nRefreshing current terminal's PATH for you."
$Env:Path = $Env:Path.Trim(";") + ";$destdir"

# Get version from zip file name.
# Instruct how to modify PATH for other open terminals
Write-Output "`nFor other terminals, restart them (or the entire IDE if they're within one).`n"

$xhVersion = $($zipfilename.trim("xh-v -x86_64-pc-windows-msvc.zip"))
Write-Output "xh v$($xhVersion) has been installed to:`n - $xhPath`n - $xhsPath"
}

0 comments on commit 20f834a

Please sign in to comment.