Skip to content

Commit

Permalink
Correct logging behavior introduced in 507a233
Browse files Browse the repository at this point in the history
This comment resolves issues relating to the creation
of an install log in 507a233 that ran afoul of the
rubric Chocolatey uses to parse those arguments
(recorded in chocolatey/choco#813). In short:
- Add functions to chocolateyInstall.ps1 to test the
  logging path and its ACL for writable status and try
  to correct them at runtime if insufficient.
- Create a tools/chocolateyUninstall.ps1 script to
  explicitly handle package removal, including the
  removal of the qalc.exe CLI moved upon install.
- Add SPDX licensing and copyright tags to all relevant
  files, just for the sake of being thorough.
- Run all PowerShell scripts through PSScriptAnalyzer
  linter and resolve all errors and warnings.

Signed-off-by: Peter J. Mello <[email protected]>
  • Loading branch information
RogueScholar committed Jan 24, 2025
1 parent bb6bef9 commit c41fe69
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 7 deletions.
9 changes: 8 additions & 1 deletion qalculate/qalculate.nuspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Do not remove this test for UTF-8: if “Ω” doesn’t appear as greek uppercase omega letter enclosed in quotation marks, you should use an editor that supports UTF-8, not this one. -->
<!--
SPDX-FileCopyrightText: © 2018–2020, Emrik Östling <[email protected]>
SPDX-License-Identifier: MIT
--><!--
Do not remove this test for UTF-8: if “Ω” doesn't appear as Greek uppercase
letter omega (Ohm unit symbol) enclosed in curly/"smart" quotation marks, you
should use a text editor that supports UTF-8, and not this one. -->
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>qalculate</id>
Expand Down
58 changes: 56 additions & 2 deletions qalculate/tools/chocolateyInstall.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,58 @@
$ErrorActionPreference = 'Stop'
# SPDX-FileCopyrightText: © 2018–2023, Emrik Östling <[email protected]>
# SPDX-FileCopyrightText: © 2024–2025, Peter J. Mello <[email protected]>
#
# SPDX-License-Identifier: MIT

$ErrorActionPreference = 'Stop'

$chocoPkgInstallLogFile = (Join-Path -Path "${Env:TEMP}" `
-ChildPath "${packageName}-${Env:chocolateyPackageVersion}-MsiInstall.log")
$chocoPkgInstallLogDir = (Split-Path "${chocoPkgInstallLogFile}" -Parent)

function Test-PkgInstallLogDirWritable {
[CmdletBinding()]
[OutputType([Int])]
param([string]${Directory})
Try {
if ((Test-Path -Path "${Directory}") -and (
Test-Path -Path "${Directory}" -PathType Container -Access Write
)) {
Return 0
}
}
Catch {
Return 1
}
}


function Set-PkgInstallLogDirWritable {
[CmdletBinding(supportsShouldProcess)]
[OutputType([Int])]
param([string]${Directory})
Try {
if (-not (Test-Path -Path "${Directory}")) {
New-Item -Path "${Directory}" -ItemType Directory
}
$logDirAcl = Get-Acl -Path "${Directory}"
$logDirRule = New-Object System.Security.AccessControl.FileSystemAccessRule(
"Everyone", "Write", "Allow"
)
${logDirAcl}.SetAccessRule(${logDirRule})
Set-Acl -Path "${Directory}" -AclObject "${logDirAcl}"
Return 0
}
Catch {
Return 1
}
}

if (Test-PkgInstallLogDirWritable -Directory "${chocoPkgInstallLogDir}") {
Write-Information "Logging directory exists and is writable; proceeding with installation…"
} elseif (Set-PkgInstallLogDirWritable -Directory "${chocoPkgInstallLogDir}") {
Write-Error "Unable to change logging directory permissions; aborting…"
Exit 1
}

$packageArgs = @{
packageName = 'qalculate'
Expand All @@ -10,7 +64,7 @@ $packageArgs = @{
checksumType = 'sha256'
checksumType64 = 'sha256'
softwareName = 'Qalculate!*'
silentArgs = '/qn /norestart /l*v `"$($Env:TEMP)\$($packageName).$($Env:chocolateyPackageVersion).MsiInstall.log`"'
silentArgs = "/qn /norestart /l*v ${chocoPkgInstallLogFile}"
validExitCodes = @(0)
}

Expand Down
37 changes: 37 additions & 0 deletions qalculate/tools/chocolateyUninstall.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# SPDX-FileCopyrightText: © 2025, Peter J. Mello <[email protected]>
#
# SPDX-License-Identifier: MIT

$ErrorActionPreference = 'Stop'

$packageName = 'qalculate'
$softwareName = 'qalculate*'
$installerType = 'MSI'
$silentArgs = '/qn /norestart'
$validExitCodes = @(0, 3010, 1605, 1614, 1641)

$local_key = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
$machine_key = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*'
$machine_key6432 = 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'

$key = Get-ItemProperty -Path @($machine_key6432,$machine_key, $local_key) `
-ErrorAction SilentlyContinue `
| Where-Object { $_.DisplayName -like "$softwareName" }

if (${key}.Count -eq 1) {
${key} | ForEach-Object {
$silentArgs = "$($_.PSChildName) ${silentArgs}"
}
Uninstall-ChocolateyPackage -PackageName ${packageName} `
-FileType ${installerType} `
-SilentArgs "${silentArgs}" `
-ValidExitCodes ${validExitCodes}
Uninstall-BinFile -Name qalc -Path "C:\Program Files\Qalculate\qalc.exe"
} elseif (${key}.Count -eq 0) {
Write-Warning "${packageName} has already been uninstalled by other means."
} elseif (${key}.Count -gt 1) {
Write-Warning "$key.Count matches found!"
Write-Warning "To prevent accidental data loss, no programs will be uninstalled."
Write-Warning "Please alert package maintainer the following keys were matched:"
$key | ForEach-Object { Write-Warning "- $_.DisplayName" }
}
14 changes: 10 additions & 4 deletions qalculate/update.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import-module au
# SPDX-FileCopyrightText: © 2018–2021, Emrik Östling <[email protected]>
# SPDX-FileCopyrightText: © 2025, Peter J. Mello <[email protected]>
#
# SPDX-License-Identifier: MIT
Import-Module -Name AU

$releases = 'https://github.com/Qalculate/qalculate-gtk/releases'

Expand All @@ -21,9 +25,11 @@ function global:au_BeforeUpdate() {
function global:au_GetLatest {
$download_page = Invoke-WebRequest -Uri $releases -UseBasicParsing

$url64 = $download_page.links | ? href -match '.msi$' | % href | select -First 1
$url64 = $download_page.links | Where-Object href -match '.msi$' |
ForEach-Object href | Select-Object -First 1
$url32 = $url64 -replace '-x64.msi$', '-i386.msi'
$version = ($url64 -split '/' | select -last 1) -split '-' | select -first 1 -skip 1
$version = ($url64 -split '/' | Select-Object -last 1) -split '-' |
Select-Object -first 1 -skip 1

@{
URL32 = $url32
Expand All @@ -32,4 +38,4 @@ function global:au_GetLatest {
}
}

update -ChecksumFor all
Update-Package -ChecksumFor all

0 comments on commit c41fe69

Please sign in to comment.