Skip to content

Commit

Permalink
Improves downloader script
Browse files Browse the repository at this point in the history
The command lookup for curl was conflicting with curl-config because of the wildcard. Switched to using exact names for lookup.

Also defaulted to using `unzip` as a default unzip tool - it avoids a nasty error in PowerShell's Archive module when the culture is not en_US.
  • Loading branch information
atruskie committed Mar 11, 2019
1 parent 209745d commit b6a34a5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
4 changes: 2 additions & 2 deletions build/download_ap.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ Describe 'AP.exe installer script' {
$result.AssetUrl | Should -BeLike '*github.com*'
}
It 'can download a build from AppVeyor' {
$result = Invoke -ci_build_number 515 -WhatIf
$result = Invoke -ci_build_number 678 -WhatIf
$result.Type | Should -Be "AppVeyor"
$result.Source | Should -Be "appveyor"
$result.ResolvedVersion | Should -Be '515'
$result.ResolvedVersion | Should -Be '678'
$result.AssetUrl | Should -BeLike '*appveyor.com*'
}
}
Expand Down
46 changes: 34 additions & 12 deletions build/download_ap.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ param(
$ErrorActionPreference = "Stop"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Import-Module "Microsoft.PowerShell.Archive" -Force

$build = "Release"
$type = $PsCmdlet.ParameterSetName
$exact_version = $null
Expand Down Expand Up @@ -188,22 +186,36 @@ Write-Output "Downloading asset $asset_url"
$downloaded_zip = "$destination/AP.zip"
if ($PsCmdlet.ShouldProcess($asset_url, "Downloading asset")) {
# use curl if available (faster)
$curl = Get-Command curl* -CommandType Application -TotalCount 1
$curl = Get-Command curl, curl.exe -CommandType Application | select -First 1
if ($curl) {
& $curl -L -o "$downloaded_zip" "$asset_url"

if ($LASTEXITCODE -ne 0) {
throw "Failed dowloading $asset_url using $curl"
}
}
else {
$asset_response = Invoke-WebRequest $asset_url -OutFile $downloaded_zip -PassThru
if ($asset_response.StatusCode -ne 200) {
throw "failed downloading $asset_url"
throw "Failed downloading $asset_url"
}
}
}

# extract asset
if ($PsCmdlet.ShouldProcess($downloaded_zip, "Extracting AP.exe zip")) {
Import-Module "Microsoft.PowerShell.Archive" -Force
Microsoft.PowerShell.Archive\Expand-Archive -LiteralPath $downloaded_zip -DestinationPath $destination -Force
if (Get-Command unzip -CommandType Application) {
unzip -q -o $downloaded_zip -d $destination

if ($LASTEXITCODE -ne 0) {
throw "Failed extracting $downloaded_zip using unzip"
}
}
else {
Import-Module "Microsoft.PowerShell.Archive" -Force
Microsoft.PowerShell.Archive\Expand-Archive -LiteralPath $downloaded_zip -DestinationPath $destination -Force

}
Remove-Item $downloaded_zip
}

Expand All @@ -227,27 +239,37 @@ if ($IsWin) {
}
# TODO Unix/MacOs symlinking

Write-Output "Checking environment has installed dependencies"
$check_environment = $null
$ErrorActionPreference = "Continue"
if ($PsCmdlet.ShouldProcess("AnalysisPrograms.exe", "Checking the install")) {
if ($IsWin) {
. "$destination/AnalysisPrograms.exe" CheckEnvironment
$check_environment = $LASTEXITCODE
$command = "$destination/AnalysisPrograms.exe CheckEnvironment"
if (! $IsWin) {

$command = "mono " + $command
}

if (! $IsWin -and $null -eq (Get-Command mono -CommandType Application -ErrorAction SilentlyContinue) ) {
Write-Error "Mono was not found on PATH. It must be isntalled for AP.exe to work"
$check_environment = -1
}
else {
mono "$destination/AnalysisPrograms.exe" CheckEnvironment
Invoke-Expression $command
$check_environment = $LASTEXITCODE
}

if ($check_environment -ne 0) {
throw "Unable to run AP.exe. There is some problem with your setup."
Write-Error "Unable to run AP.exe. There is some problem with your setup."
Write-Warning "You can run the environment check again by executing:`n`t$command"
}
}


return [PSCustomObject]@{
Type = $type
Source = $source
ResolvedVersion = $exact_version
Destination = $destination
AssetUrl = $asset_url
EnvironmentCheck = $check_environment
}
}

0 comments on commit b6a34a5

Please sign in to comment.