diff --git a/build-caffeine-exe.ps1 b/build-caffeine-exe.ps1 index f6838a8..58ff385 100644 --- a/build-caffeine-exe.ps1 +++ b/build-caffeine-exe.ps1 @@ -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)] diff --git a/sign-caffeine-exe.ps1 b/sign-caffeine-exe.ps1 index b62ccf9..7ae85a2 100644 --- a/sign-caffeine-exe.ps1 +++ b/sign-caffeine-exe.ps1 @@ -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." } diff --git a/tasks/lessons.md b/tasks/lessons.md new file mode 100644 index 0000000..b300d57 --- /dev/null +++ b/tasks/lessons.md @@ -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.