Skip to content

Commit

Permalink
fixes for Powershell 5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaneri committed Nov 27, 2024
1 parent d6a026f commit 40d5bbc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pode.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ function Invoke-PodeBuildDotnetMonitorSrvBuild() {
$AssemblyVersion = ''
}

foreach ($target in @('win-x64', 'win-arm64' , 'linux-x64', 'linux-arm64', 'osx-x64', 'osx-arm64')) {
foreach ($target in @('win-x64', 'win-arm64' , 'linux-x64', 'linux-arm64', 'osx-x64', 'osx-arm64','linux-arm','win-x86','linux-musl-x64')) {
$DefineConstants = @()
$ParamConstants = ''

Expand Down
2 changes: 1 addition & 1 deletion src/PodeMonitor/PodeMonitor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PublishSingleFile>true</PublishSingleFile> <!-- Generate a single-file executable -->
<SelfContained>true</SelfContained> <!-- Bundle .NET runtime -->
<PublishTrimmed>false</PublishTrimmed> <!-- Disabled trimming for now -->
<RuntimeIdentifiers>win-x64;win-arm64;linux-x64;linux-arm64;osx-x64;osx-arm64</RuntimeIdentifiers> <!-- Target multiple platforms -->
<RuntimeIdentifiers>win-x64;win-arm64;linux-x64;linux-arm64;osx-x64;osx-arm64;linux-arm;win-x86;linux-musl-x64</RuntimeIdentifiers> <!-- Target multiple platforms -->
</PropertyGroup>

<ItemGroup>
Expand Down
48 changes: 48 additions & 0 deletions src/Private/Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4040,3 +4040,51 @@ function Convert-PodeMillisecondsToReadable {

return $parts -join ':'
}


<#
.SYNOPSIS
Determines the OS architecture for the current system.
.DESCRIPTION
This function detects the operating system's architecture and converts it into a format
compatible with PowerShell installation requirements. It handles both Windows and Unix-based
systems and maps various architecture identifiers to PowerShell-supported names (e.g., 'x64', 'arm64').
.OUTPUTS
[string] - The architecture string, such as 'x64', 'x86', 'arm64', or 'arm32'.
.EXAMPLE
$arch = Get-PodeOSPwshArchitecture
Write-Host "Current architecture: $arch"
.NOTES
- For Windows, the architecture is derived from the `PROCESSOR_ARCHITECTURE` environment variable.
- For Unix-based systems, the architecture is determined using the `uname -m` command.
- If the architecture is not supported, the function throws an exception.
#>
function Get-PodeOSPwshArchitecture {
# Initialize architecture variable
$arch = [string]::Empty

# Detect architecture on Unix-based systems (Linux/macOS)
if ($IsLinux -or $IsMacOS) {
$arch = uname -m
}else{
# Architecture on Windows
$arch = $env:PROCESSOR_ARCHITECTURE
}

# Convert detected architecture to a PowerShell-compatible format
switch ($arch.ToLowerInvariant()) {
'amd64' { return 'x64' } # 64-bit architecture (AMD64)
'x86' { return 'x86' } # 32-bit architecture
'x86_64' { return 'x64' } # 64-bit architecture (x86_64)
'armv7*' { return 'arm32' } # 32-bit ARM architecture
'aarch64*' { return 'arm64' } # 64-bit ARM architecture
'arm64' { return 'arm64' } # Explicit ARM64
'arm64*' { return 'arm64' } # Pattern matching for ARM64
'armv8*' { return 'arm64' } # ARM v8 series
default { throw "Unsupported architecture: $($arch)" } # Throw exception for unsupported architectures
}
}
34 changes: 22 additions & 12 deletions src/Public/Service.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ function Register-PodeService {
if (! (Test-Path -Path $SettingsPath -PathType Container)) {
$null = New-Item -Path $settingsPath -ItemType Directory
}

if (Test-PodeIsWindows) {
if ([string]::IsNullOrEmpty($WindowsUser)) {
if ( ($null -ne $Password)) {
Expand Down Expand Up @@ -205,7 +206,7 @@ function Register-PodeService {
$jsonContent | ConvertTo-Json | Set-Content -Path $settingsFile -Encoding UTF8

# Determine OS architecture and call platform-specific registration functions
$osArchitecture = ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture).ToString().ToLower()
$osArchitecture = Get-PodeOSPwshArchitecture

# Get the directory path where the Pode module is installed and store it in $binPath
$binPath = Join-Path -Path ((Get-Module -Name Pode).ModuleBase) -ChildPath 'Bin'
Expand All @@ -222,9 +223,20 @@ function Register-PodeService {
SecurityDescriptorSddl = $SecurityDescriptorSddl
OsArchitecture = "win-$osArchitecture"
}
$operation = Register-PodeMonitorWindowsService @param
$operation = Register-PodeMonitorWindowsService @param
}
elseif ($IsLinux) {
$musl = ''
if ($osArchitecture -eq 'x64') {
# Check for musl libc
if (Get-Command ldd -ErrorAction SilentlyContinue) {
$lddOutput = ldd --version 2>&1
if ($lddOutput -match 'musl') {
$musl = 'musl-'
}
}
}

$param = @{
Name = $Name
Description = $Description
Expand All @@ -233,7 +245,7 @@ function Register-PodeService {
User = $User
Group = $Group
Start = $Start
OsArchitecture = "linux-$osArchitecture"
OsArchitecture = "linux-$($musl)$($osArchitecture)"
}
$operation = Register-PodeLinuxService @param
}
Expand Down Expand Up @@ -818,10 +830,10 @@ function Unregister-PodeService {
Write-Verbose -Message "Service '$Name' unregistered successfully."

# Remove the service configuration
if ($service.PathName) {
$binaryPath = $service.PathName.trim('"').split('" "')
if ($binaryPath.Count -gt 1 -and (Test-Path -Path ($binaryPath[1]) -PathType Leaf)) {
Remove-Item -Path ($binaryPath[1]) -ErrorAction Break
if ($service.PathName -match '"([^"]+)" "([^"]+)"') {
$argument = $Matches[2]
if ( (Test-Path -Path ($argument) -PathType Leaf)) {
Remove-Item -Path ($argument) -ErrorAction SilentlyContinue
}
}
return $true
Expand All @@ -830,7 +842,6 @@ function Unregister-PodeService {

elseif ($IsLinux) {
if (! (Disable-PodeLinuxService -Name $Name)) {
# if (Get-PodeService -Name $Name -ErrorAction SilentlyContinue) {
Write-Verbose -Message "Service '$Name' unregistered failed."
throw ($PodeLocale.serviceUnRegistrationException -f $Name)
}
Expand Down Expand Up @@ -862,8 +873,7 @@ function Unregister-PodeService {

elseif ($IsMacOS) {
# Disable and unregister the service
Disable-PodeMacOsService -Name $Name
if (Get-PodeService -Name $Name -ErrorAction SilentlyContinue) {
if (!(Disable-PodeMacOsService -Name $Name)) {
Write-Verbose -Message "Service '$Name' unregistered failed."
throw ($PodeLocale.serviceUnRegistrationException -f $Name)
}
Expand Down Expand Up @@ -1020,8 +1030,8 @@ function Restart-PodeService {
throw ($PodeLocale.serviceIsNotRegisteredException -f $Name)
}

if (@('Running', 'Suspended' ) -inotcontains $service.Status ) {
Write-Verbose -Message "Service '$Name' is not Running or Suspended."
if ('Running' -ne $service.Status ) {
Write-Verbose -Message "Service '$Name' is not Running."
return $false
}
if (Test-PodeIsWindows) {
Expand Down

0 comments on commit 40d5bbc

Please sign in to comment.