Skip to content

Commit

Permalink
Merge pull request #651 from axodotdev/powershell_fix_errors
Browse files Browse the repository at this point in the history
fix(powershell): replace errors with throw
  • Loading branch information
mistydemeo authored Dec 18, 2023
2 parents 7fc22a6 + dec41f6 commit 10471f5
Show file tree
Hide file tree
Showing 24 changed files with 672 additions and 385 deletions.
45 changes: 28 additions & 17 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,26 +268,35 @@ 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"
break
throw @"
Error: PowerShell 5 or later is required to install $app_name.
Upgrade PowerShell:

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

"@
}

# 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"
break
throw @"
Error: Installing $app_name requires at least .NET Framework 4.5
Please download and install it first:

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

"@
}
}

Expand All @@ -307,5 +312,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
}
44 changes: 28 additions & 16 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,26 +1048,35 @@ 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"
break
throw @"
Error: PowerShell 5 or later is required to install $app_name.
Upgrade PowerShell:

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

"@
}

# 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"
break
throw @"
Error: Installing $app_name requires at least .NET Framework 4.5
Please download and install it first:

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

"@
}
}

Expand All @@ -1087,7 +1092,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
44 changes: 28 additions & 16 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,26 +1048,35 @@ 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"
break
throw @"
Error: PowerShell 5 or later is required to install $app_name.
Upgrade PowerShell:

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

"@
}

# 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"
break
throw @"
Error: Installing $app_name requires at least .NET Framework 4.5
Please download and install it first:

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

"@
}
}

Expand All @@ -1087,7 +1092,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
44 changes: 28 additions & 16 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,26 +1049,35 @@ 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"
break
throw @"
Error: PowerShell 5 or later is required to install $app_name.
Upgrade PowerShell:

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

"@
}

# 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"
break
throw @"
Error: Installing $app_name requires at least .NET Framework 4.5
Please download and install it first:

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

"@
}
}

Expand All @@ -1088,7 +1093,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 10471f5

Please sign in to comment.