Skip to content

Commit

Permalink
fix(powershell): replace errors with throw
Browse files Browse the repository at this point in the history
Co-authored-by: Aria Beingessner <[email protected]>

Fixes #484.
  • Loading branch information
mistydemeo committed Dec 18, 2023
1 parent 7fc22a6 commit 5d9ec4d
Show file tree
Hide file tree
Showing 24 changed files with 672 additions and 337 deletions.
43 changes: 28 additions & 15 deletions cargo-dist/templates/installer/installer.ps1.j2
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,11 @@ function Install-Binary($install_args) {
Get-Help $PSCommandPath -Detailed
Exit
}
$old_erroractionpreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'

Initialize-Environment

# Platform info injected by cargo-dist
$platforms = @{
$platforms = @{
{%- for artifact in artifacts %}
"{{ artifact.target_triples[0] }}" = @{
"artifact_name" = "{{ artifact.id }}"
Expand All @@ -73,8 +71,6 @@ function Install-Binary($install_args) {
$fetched = Download "$ArtifactDownloadUrl" $platforms
# FIXME: add a flag that lets the user not do this step
Invoke-Installer $fetched "$install_args"

$ErrorActionPreference = $old_erroractionpreference
}

function Get-TargetTriple() {
Expand Down Expand Up @@ -272,25 +268,36 @@ function Add-Path($OrigPathToAdd) {

function Initialize-Environment() {
If (($PSVersionTable.PSVersion.Major) -lt 5) {
Write-Error "PowerShell 5 or later is required to install $app_name."
Write-Error "Upgrade PowerShell: https://docs.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell"
throw @"
PowerShell 5 or later is required to install $app_name.
Upgrade PowerShell:

https://docs.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell

"@
break
}

# show notification to change execution policy:
$allowedExecutionPolicy = @('Unrestricted', 'RemoteSigned', 'ByPass')
If ((Get-ExecutionPolicy).ToString() -notin $allowedExecutionPolicy) {
Write-Error "PowerShell requires an execution policy in [$($allowedExecutionPolicy -join ", ")] to run $app_name."
Write-Error "For example, to set the execution policy to 'RemoteSigned' please run :"
Write-Error "'Set-ExecutionPolicy RemoteSigned -scope CurrentUser'"
break
throw @"
Error: PowerShell requires an execution policy in [$($allowedExecutionPolicy -join ", ")] to run $app_name. For example, to set the execution policy to 'RemoteSigned' please run:

Set-ExecutionPolicy RemoteSigned -scope CurrentUser

"@
}

# GitHub requires TLS 1.2
If ([System.Enum]::GetNames([System.Net.SecurityProtocolType]) -notcontains 'Tls12') {
Write-Error "Installing $app_name requires at least .NET Framework 4.5"
Write-Error "Please download and install it first:"
Write-Error "https://www.microsoft.com/net/download"
throw @"
Installing $app_name requires at least .NET Framework 4.5
Please download and install it first:

https://www.microsoft.com/net/download

"@
break
}
}
Expand All @@ -307,5 +314,11 @@ function New-Temp-Dir() {
$Null = $ArtifactDownloadUrl, $NoModifyPath, $Help
# Make Write-Information statements be visible
$InformationPreference = "Continue"
Install-Binary "$Args"

# The default interactive handler
try {
Install-Binary "$Args"
} catch {
Write-Information $_
exit 1
}
42 changes: 28 additions & 14 deletions cargo-dist/tests/snapshots/akaikatana_basic.snap
Original file line number Diff line number Diff line change
Expand Up @@ -852,8 +852,6 @@ function Install-Binary($install_args) {
Get-Help $PSCommandPath -Detailed
Exit
}
$old_erroractionpreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'

Initialize-Environment

Expand All @@ -869,8 +867,6 @@ function Install-Binary($install_args) {
$fetched = Download "$ArtifactDownloadUrl" $platforms
# FIXME: add a flag that lets the user not do this step
Invoke-Installer $fetched "$install_args"

$ErrorActionPreference = $old_erroractionpreference
}

function Get-TargetTriple() {
Expand Down Expand Up @@ -1052,25 +1048,36 @@ function Add-Path($OrigPathToAdd) {

function Initialize-Environment() {
If (($PSVersionTable.PSVersion.Major) -lt 5) {
Write-Error "PowerShell 5 or later is required to install $app_name."
Write-Error "Upgrade PowerShell: https://docs.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell"
throw @"
PowerShell 5 or later is required to install $app_name.
Upgrade PowerShell:

https://docs.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell

"@
break
}

# show notification to change execution policy:
$allowedExecutionPolicy = @('Unrestricted', 'RemoteSigned', 'ByPass')
If ((Get-ExecutionPolicy).ToString() -notin $allowedExecutionPolicy) {
Write-Error "PowerShell requires an execution policy in [$($allowedExecutionPolicy -join ", ")] to run $app_name."
Write-Error "For example, to set the execution policy to 'RemoteSigned' please run :"
Write-Error "'Set-ExecutionPolicy RemoteSigned -scope CurrentUser'"
break
throw @"
Error: PowerShell requires an execution policy in [$($allowedExecutionPolicy -join ", ")] to run $app_name. For example, to set the execution policy to 'RemoteSigned' please run:

Set-ExecutionPolicy RemoteSigned -scope CurrentUser

"@
}

# GitHub requires TLS 1.2
If ([System.Enum]::GetNames([System.Net.SecurityProtocolType]) -notcontains 'Tls12') {
Write-Error "Installing $app_name requires at least .NET Framework 4.5"
Write-Error "Please download and install it first:"
Write-Error "https://www.microsoft.com/net/download"
throw @"
Installing $app_name requires at least .NET Framework 4.5
Please download and install it first:

https://www.microsoft.com/net/download

"@
break
}
}
Expand All @@ -1087,7 +1094,14 @@ function New-Temp-Dir() {
$Null = $ArtifactDownloadUrl, $NoModifyPath, $Help
# Make Write-Information statements be visible
$InformationPreference = "Continue"
Install-Binary "$Args"

# The default interactive handler
try {
Install-Binary "$Args"
} catch {
Write-Information $_
exit 1
}

================ dist-manifest.json ================
{
Expand Down
42 changes: 28 additions & 14 deletions cargo-dist/tests/snapshots/akaikatana_repo_with_dot_git.snap
Original file line number Diff line number Diff line change
Expand Up @@ -852,8 +852,6 @@ function Install-Binary($install_args) {
Get-Help $PSCommandPath -Detailed
Exit
}
$old_erroractionpreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'

Initialize-Environment

Expand All @@ -869,8 +867,6 @@ function Install-Binary($install_args) {
$fetched = Download "$ArtifactDownloadUrl" $platforms
# FIXME: add a flag that lets the user not do this step
Invoke-Installer $fetched "$install_args"

$ErrorActionPreference = $old_erroractionpreference
}

function Get-TargetTriple() {
Expand Down Expand Up @@ -1052,25 +1048,36 @@ function Add-Path($OrigPathToAdd) {

function Initialize-Environment() {
If (($PSVersionTable.PSVersion.Major) -lt 5) {
Write-Error "PowerShell 5 or later is required to install $app_name."
Write-Error "Upgrade PowerShell: https://docs.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell"
throw @"
PowerShell 5 or later is required to install $app_name.
Upgrade PowerShell:

https://docs.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell

"@
break
}

# show notification to change execution policy:
$allowedExecutionPolicy = @('Unrestricted', 'RemoteSigned', 'ByPass')
If ((Get-ExecutionPolicy).ToString() -notin $allowedExecutionPolicy) {
Write-Error "PowerShell requires an execution policy in [$($allowedExecutionPolicy -join ", ")] to run $app_name."
Write-Error "For example, to set the execution policy to 'RemoteSigned' please run :"
Write-Error "'Set-ExecutionPolicy RemoteSigned -scope CurrentUser'"
break
throw @"
Error: PowerShell requires an execution policy in [$($allowedExecutionPolicy -join ", ")] to run $app_name. For example, to set the execution policy to 'RemoteSigned' please run:

Set-ExecutionPolicy RemoteSigned -scope CurrentUser

"@
}

# GitHub requires TLS 1.2
If ([System.Enum]::GetNames([System.Net.SecurityProtocolType]) -notcontains 'Tls12') {
Write-Error "Installing $app_name requires at least .NET Framework 4.5"
Write-Error "Please download and install it first:"
Write-Error "https://www.microsoft.com/net/download"
throw @"
Installing $app_name requires at least .NET Framework 4.5
Please download and install it first:

https://www.microsoft.com/net/download

"@
break
}
}
Expand All @@ -1087,7 +1094,14 @@ function New-Temp-Dir() {
$Null = $ArtifactDownloadUrl, $NoModifyPath, $Help
# Make Write-Information statements be visible
$InformationPreference = "Continue"
Install-Binary "$Args"

# The default interactive handler
try {
Install-Binary "$Args"
} catch {
Write-Information $_
exit 1
}

================ dist-manifest.json ================
{
Expand Down
42 changes: 28 additions & 14 deletions cargo-dist/tests/snapshots/axolotlsay_abyss.snap
Original file line number Diff line number Diff line change
Expand Up @@ -853,8 +853,6 @@ function Install-Binary($install_args) {
Get-Help $PSCommandPath -Detailed
Exit
}
$old_erroractionpreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'

Initialize-Environment

Expand All @@ -870,8 +868,6 @@ function Install-Binary($install_args) {
$fetched = Download "$ArtifactDownloadUrl" $platforms
# FIXME: add a flag that lets the user not do this step
Invoke-Installer $fetched "$install_args"

$ErrorActionPreference = $old_erroractionpreference
}

function Get-TargetTriple() {
Expand Down Expand Up @@ -1053,25 +1049,36 @@ function Add-Path($OrigPathToAdd) {

function Initialize-Environment() {
If (($PSVersionTable.PSVersion.Major) -lt 5) {
Write-Error "PowerShell 5 or later is required to install $app_name."
Write-Error "Upgrade PowerShell: https://docs.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell"
throw @"
PowerShell 5 or later is required to install $app_name.
Upgrade PowerShell:

https://docs.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell

"@
break
}

# show notification to change execution policy:
$allowedExecutionPolicy = @('Unrestricted', 'RemoteSigned', 'ByPass')
If ((Get-ExecutionPolicy).ToString() -notin $allowedExecutionPolicy) {
Write-Error "PowerShell requires an execution policy in [$($allowedExecutionPolicy -join ", ")] to run $app_name."
Write-Error "For example, to set the execution policy to 'RemoteSigned' please run :"
Write-Error "'Set-ExecutionPolicy RemoteSigned -scope CurrentUser'"
break
throw @"
Error: PowerShell requires an execution policy in [$($allowedExecutionPolicy -join ", ")] to run $app_name. For example, to set the execution policy to 'RemoteSigned' please run:

Set-ExecutionPolicy RemoteSigned -scope CurrentUser

"@
}

# GitHub requires TLS 1.2
If ([System.Enum]::GetNames([System.Net.SecurityProtocolType]) -notcontains 'Tls12') {
Write-Error "Installing $app_name requires at least .NET Framework 4.5"
Write-Error "Please download and install it first:"
Write-Error "https://www.microsoft.com/net/download"
throw @"
Installing $app_name requires at least .NET Framework 4.5
Please download and install it first:

https://www.microsoft.com/net/download

"@
break
}
}
Expand All @@ -1088,7 +1095,14 @@ function New-Temp-Dir() {
$Null = $ArtifactDownloadUrl, $NoModifyPath, $Help
# Make Write-Information statements be visible
$InformationPreference = "Continue"
Install-Binary "$Args"

# The default interactive handler
try {
Install-Binary "$Args"
} catch {
Write-Information $_
exit 1
}

================ npm-package.tar.gz/package/.gitignore ================
/node_modules
Expand Down
Loading

0 comments on commit 5d9ec4d

Please sign in to comment.