Skip to content

Commit

Permalink
More bug fixes for installer script
Browse files Browse the repository at this point in the history
- Now uses curl if present on system.
- Fixes a bug with the IsWindows variable not being writeable in pwsh 6

[skip_ci]
  • Loading branch information
atruskie committed Aug 14, 2018
1 parent 627f200 commit ef883e7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
19 changes: 10 additions & 9 deletions build/download_ap.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/pwsh

#Requires -Version 6
#Requires -Version 5

# Tests for the download_ap.ps1 script

Expand Down Expand Up @@ -71,11 +71,14 @@ Describe 'AP.exe installer script' {

Context 'Installing the files' {
BeforeAll {
$here = "$PSScriptRoot/test_download"
$old_path = $env:Path
$env:Path = ""

(Get-Command "download_ap.ps1" -erroraction 'silentlycontinue' ) | Should -BeNullOrEmpty
$here = [System.IO.Path]::GetFullPath("$PSScriptRoot/test_download")

# remove any path modifications from previous test runs
$env:Path = $env:Path -replace "$here",''
$paths = [Environment]::GetEnvironmentVariables("User").Path -split [IO.Path]::PathSeparator
$paths = $paths | Where-Object { $_ -ne $here }
$user_path = ($paths -join [IO.Path]::PathSeparator)
[Environment]::SetEnvironmentVariable("Path", $user_path, "User")

$result_all = Invoke -Destination $here

Expand All @@ -85,8 +88,7 @@ Describe 'AP.exe installer script' {
$result.Destination | Should -BeIn ("/AP", "C:\AP")
}
It 'updates path on install' {
Write-Host ("Debugging: " + $env:Path)
$env:Path | Should -BeLike "*test_download*"
$env:Path | Should -BeLike "*$here*"
# Enable this test after we bootstrap the releases
#(Get-Command "download_ap.ps1" -erroraction 'silentlycontinue' ) | Should -Not -BeNullOrEmpty
}
Expand All @@ -108,7 +110,6 @@ Describe 'AP.exe installer script' {

AfterAll {
Remove-Item $here -Recurse
$env:Path = $old_path
}
}
}
44 changes: 27 additions & 17 deletions build/download_ap.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ param(
[ValidateSet('Stable', 'Weekly', 'Continuous')]
[string]$package = "Stable",

[Parameter(ParameterSetName = "GitHub", Mandatory=$true)]
[Parameter(ParameterSetName = "GitHub", Mandatory = $true)]
[string]$version,

[Parameter(ParameterSetName = "AppVeyor", Mandatory=$true)]
[Parameter(ParameterSetName = "AppVeyor", Mandatory = $true)]
[string]$ci_build_number,

[Parameter()]
Expand Down Expand Up @@ -172,9 +172,11 @@ finally {
$destination = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($destination)
if (Test-Path $destination) {
Write-Warning "Deleting old installation at '$destination' (but keeping log files)"
$script_name = split-path $PSCommandPath -Leaf
Remove-Item -Path "$destination/*" -Recurse -ErrorAction Stop -Exclude "$script_name","*log*"
Start-Sleep 1
if ($PsCmdlet.ShouldProcess($destination, "Deleting old install")) {
$script_name = split-path $PSCommandPath -Leaf
Remove-Item -Path "$destination/*" -Recurse -ErrorAction Stop -Exclude "$script_name", "*log*"
Start-Sleep 1
}
}
else {
New-Item $destination -ItemType Directory -ErrorAction Stop
Expand All @@ -185,9 +187,16 @@ else {
Write-Output "Downloading asset $asset_url"
$downloaded_zip = "$destination/AP.zip"
if ($PsCmdlet.ShouldProcess($asset_url, "Downloading asset")) {
$asset_response = Invoke-WebRequest $asset_url -OutFile $downloaded_zip -PassThru
if ($asset_response.StatusCode -ne 200) {
throw "failed downloading $asset_url"
# use curl if available (faster)
$curl = Get-Command curl* -CommandType Application -TotalCount 1
if ($curl) {
& $curl -L -o "$downloaded_zip" "$asset_url"
}
else {
$asset_response = Invoke-WebRequest $asset_url -OutFile $downloaded_zip -PassThru
if ($asset_response.StatusCode -ne 200) {
throw "failed downloading $asset_url"
}
}
}

Expand All @@ -201,25 +210,26 @@ if ($PsCmdlet.ShouldProcess($downloaded_zip, "Extracting AP.exe zip")) {
Write-Output "Download complete, installed to $destination"


$IsWindows = $IsWindows -or ($PSVersionTable.PSVersion.Major -lt 6)
if ($IsWindows) {
$IsWin = $IsWindows -or ($PSVersionTable.PSVersion.Major -lt 6)
if ($IsWin) {
$paths = [Environment]::GetEnvironmentVariables("User").Path -split [IO.Path]::PathSeparator
if ($paths -notcontains $destination) {
Write-Output "Adding AP.exe to PATH"
$paths = $paths | Where-Object { $_ }
$paths = $paths | Where-Object { $_ }
$paths += $destination
$user_path = ($paths -join [IO.Path]::PathSeparator)
[Environment]::SetEnvironmentVariable("Path", $user_path, "User")

# this change to so that the process env vars have access
$env:Path += ([IO.Path]::PathSeparator + $destination)
if ($PsCmdlet.ShouldProcess("PATH", "Adding $destionation to PATH")) {
[Environment]::SetEnvironmentVariable("Path", $user_path, "User")
# this change to so that the process env vars have access
$env:Path += ([IO.Path]::PathSeparator + $destination)
}
}
}
# TODO Unix/MacOs
# TODO Unix/MacOs symlinking

$check_environment = $null
if ($PsCmdlet.ShouldProcess("AnalysisPrograms.exe", "Checking the install")) {
if ($IsWindows) {
if ($IsWin) {
. "$destination/AnalysisPrograms.exe" CheckEnvironment
$check_environment = $LASTEXITCODE
}
Expand Down

0 comments on commit ef883e7

Please sign in to comment.