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,8 +1,8 @@
[CmdletBinding()]
param(
[string]$SourceScript = (Join-Path $PSScriptRoot 'caffeine.ps1'),
[string]$OutputDir = (Join-Path $PSScriptRoot 'dist'),
[string]$BuildDir = (Join-Path $PSScriptRoot 'build'),
[string]$SourceScript,
[string]$OutputDir,
[string]$BuildDir,
[string]$ExeName = 'PortableCaffeine.exe',
[switch]$ConsoleTarget,
[switch]$KeepExtractedSource
@@ -11,6 +11,26 @@ 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($SourceScript)) {
$SourceScript = Join-Path $scriptRoot 'caffeine.ps1'
}
if ([string]::IsNullOrWhiteSpace($OutputDir)) {
$OutputDir = Join-Path $scriptRoot 'dist'
}
if ([string]::IsNullOrWhiteSpace($BuildDir)) {
$BuildDir = Join-Path $scriptRoot 'build'
}
function Get-CSharpFromCaffeineScript {
param(
[Parameter(Mandatory = $true)]

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."
}

5
tasks/lessons.md Normal file
View File

@@ -0,0 +1,5 @@
# Lessons
## 2026-02-24
- PowerShell 5.1 compatibility: do not use `$PSScriptRoot` (or other script-location variables) inside `param()` default expressions. Resolve script-relative defaults after the `param` block instead.