Skip to content

Commit

Permalink
Cleanup / Fixup not on PATH test.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirois committed Sep 8, 2024
1 parent 971443c commit f21ede2
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,23 @@
#>

param (
[Alias("h")]
[Alias('h')]
[switch]$Help,

[Alias("d")]
[Alias('d')]
[string]$BinDir = (
# N.B.: PowerShell>=6 supports varargs, but to retain compatibility with older PowerShell, we
# just Join-Path twice.
# See: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/join-path?view=powershell-7.4#-additionalchildpath
Join-Path (
Join-Path ([Environment]::GetFolderPath("LocalApplicationData")) "Programs") "Science"
Join-Path ([Environment]::GetFolderPath('LocalApplicationData')) 'Programs'
) 'Science'
),

[switch]$NoModifyPath,

[Alias("V")]
[string]$Version = "latest/download"
[Alias('V')]
[string]$Version = 'latest/download'
)

$ErrorActionPreference = 'Stop'
Expand Down Expand Up @@ -87,8 +88,8 @@ function Die {

function TemporaryDirectory {
$Tmp = [System.IO.Path]::GetTempPath()
$Unique = (New-Guid).ToString("N")
$TempDir = New-Item -ItemType Directory -Path (Join-Path $Tmp "$Unique")
$Unique = (New-Guid).ToString('N')
$TempDir = New-Item -ItemType Directory -Path (Join-Path $Tmp $Unique)
trap {
Remove-Item $TempDir -Recurse
}
Expand All @@ -107,18 +108,18 @@ function Fetch {
function InstallFromUrl {
param (
[string]$Url,
$DestDir
[string]$DestDir
)

$Sha256Url = "$Url.sha256"

$Workdir = TemporaryDirectory
$ScienceExeFile = "$Workdir\science.exe"
$Sha256File = "$Workdir\science.exe.sha256"
$ScienceExeFile = Join-Path $Workdir 'science.exe'
$Sha256File = Join-Path $Workdir 'science.exe.sha256'

Fetch -Url $Url -DestFile $ScienceExeFile
Fetch -Url $Sha256Url -DestFile $Sha256File
Green "Download completed successfully"
Green 'Download completed successfully'

$ExpectedHash = ((Get-Content $Sha256File).Trim().ToLower() -Split '\s+',2)[0]
$ActualHash = (Get-FileHash $ScienceExeFile -Algorithm SHA256).Hash.ToLower()
Expand All @@ -132,6 +133,7 @@ function InstallFromUrl {
New-Item $DestDir -ItemType Directory | Out-Null
}
Move-Item $ScienceExeFile $DestDir -Force
Join-Path $BinDir 'science.exe'
}

if ($Help) {
Expand All @@ -152,21 +154,19 @@ $Arch = switch -Wildcard ((Get-CimInstance Win32_operatingsystem).OSArchitecture
$DownloadURL = "https://github.com/a-scie/lift/releases/$Version/science-fat-windows-$Arch.exe"

Green "Download URL is: $DownloadURL"
InstallFromUrl -Url $DownloadURL -DestDir $BinDir
$ScienceExe = InstallFromUrl -Url $DownloadURL -DestDir $BinDir

$User = [System.EnvironmentVariableTarget]::User
$Path = [System.Environment]::GetEnvironmentVariable('Path', $User)
if (!(";$Path;".ToLower() -like "*;$BinDir;*".ToLower())) {
if ($NoModifyPath) {
Warn "WARNING: $BinDir is not detected on `$PATH"
Warn (
"You'll either need to invoke $BinDir\science.exe explicitly or else add $BinDir to your " +
"You'll either need to invoke $ScienceExe explicitly or else add $BinDir to your " +
"PATH."
)
} else {
$User = [System.EnvironmentVariableTarget]::User
$Path = [System.Environment]::GetEnvironmentVariable('Path', $User)
if (!(";$Path;".ToLower() -like "*;$BinDir;*".ToLower())) {
[System.Environment]::SetEnvironmentVariable('Path', "$Path;$BinDir", $User)
$Env:Path += ";$BinDir"
}
[System.Environment]::SetEnvironmentVariable('Path', "$Path;$BinDir", $User)
$Env:Path += ";$BinDir"
}
}

0 comments on commit f21ede2

Please sign in to comment.