Refactor parameter handling in build and sign scripts for PowerShell 5.1 compatibility

This commit is contained in:
2026-02-25 08:34:44 +11:00
parent 3fb8e98eed
commit 5c5a8b3b4c
3 changed files with 47 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
[CmdletBinding()]
param(
[string]$ExePath = (Join-Path $PSScriptRoot 'dist\PortableCaffeine.exe'),
[string]$ExePath,
[string]$Subject = 'CN=Portable Caffeine Dev Code Signing',
[string]$FriendlyName = 'Portable Caffeine Dev Code Signing (Self-Signed)',
[ValidateSet('None', 'CurrentUser', 'LocalMachine')]
@@ -10,7 +10,7 @@ param(
[int]$YearsValid = 5,
[switch]$ForceNewCertificate,
[string]$TimestampServer,
[string]$CerExportPath = (Join-Path $PSScriptRoot 'dist\PortableCaffeine-dev-signing.cer'),
[string]$CerExportPath,
[string]$PfxExportPath,
[securestring]$PfxPassword
)
@@ -18,6 +18,23 @@ param(
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
# In Windows PowerShell 5.1, $PSScriptRoot may be empty during param default evaluation.
# Resolve script-relative defaults after the param block for compatibility.
$scriptRoot = if (-not [string]::IsNullOrWhiteSpace($PSScriptRoot)) {
$PSScriptRoot
} elseif ($MyInvocation.MyCommand.Path) {
Split-Path -Parent $MyInvocation.MyCommand.Path
} else {
(Get-Location).Path
}
if ([string]::IsNullOrWhiteSpace($ExePath)) {
$ExePath = Join-Path $scriptRoot 'dist\PortableCaffeine.exe'
}
if ([string]::IsNullOrWhiteSpace($CerExportPath)) {
$CerExportPath = Join-Path $scriptRoot 'dist\PortableCaffeine-dev-signing.cer'
}
if ([Environment]::OSVersion.Platform -ne [PlatformID]::Win32NT) {
throw "This script is intended for Windows only."
}