| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Updates a Service.psd1 descriptor file. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Updates Description, Version, EntryPoint, ServiceLogPath, PreservePaths, and ApplicationDataFolders values in Servic |
| | | 6 | | Name is immutable and cannot be changed by this cmdlet. |
| | | 7 | | .PARAMETER Path |
| | | 8 | | Descriptor path. Accepts either a descriptor file path or a directory path. |
| | | 9 | | When a directory path is provided, Service.psd1 is appended automatically. |
| | | 10 | | .PARAMETER Description |
| | | 11 | | New description value. |
| | | 12 | | .PARAMETER Version |
| | | 13 | | New version value compatible with System.Version. |
| | | 14 | | .PARAMETER EntryPoint |
| | | 15 | | New entry point path value. |
| | | 16 | | .PARAMETER ServiceLogPath |
| | | 17 | | New default service log path. |
| | | 18 | | .PARAMETER ClearServiceLogPath |
| | | 19 | | Removes ServiceLogPath from the descriptor. |
| | | 20 | | .PARAMETER PreservePaths |
| | | 21 | | Replaces PreservePaths with a new list of relative file/folder paths. |
| | | 22 | | .PARAMETER ClearPreservePaths |
| | | 23 | | Removes PreservePaths from the descriptor. |
| | | 24 | | .PARAMETER ApplicationDataFolders |
| | | 25 | | Replaces ApplicationDataFolders with a new list of relative application-data folder paths. |
| | | 26 | | .PARAMETER ClearApplicationDataFolders |
| | | 27 | | Removes ApplicationDataFolders from the descriptor. |
| | | 28 | | .PARAMETER WhatIf |
| | | 29 | | Shows what would happen if the cmdlet runs. The cmdlet is not executed. |
| | | 30 | | .PARAMETER Confirm |
| | | 31 | | Prompts for confirmation before running the cmdlet. |
| | | 32 | | .EXAMPLE |
| | | 33 | | Set-KrServiceDescriptor -Path .\Service.psd1 -Description 'Updated' -Version 1.2.1 |
| | | 34 | | #> |
| | | 35 | | function Set-KrServiceDescriptor { |
| | | 36 | | [KestrunRuntimeApi('Everywhere')] |
| | | 37 | | [CmdletBinding(SupportsShouldProcess)] |
| | | 38 | | [OutputType([pscustomobject])] |
| | | 39 | | param( |
| | | 40 | | [Parameter()] |
| | | 41 | | [ValidateNotNullOrEmpty()] |
| | | 42 | | [string]$Path = 'Service.psd1', |
| | | 43 | | |
| | | 44 | | [Parameter()] |
| | | 45 | | [string]$Description, |
| | | 46 | | |
| | | 47 | | [Parameter()] |
| | | 48 | | [version]$Version, |
| | | 49 | | |
| | | 50 | | [Parameter()] |
| | | 51 | | [string]$EntryPoint, |
| | | 52 | | |
| | | 53 | | [Parameter()] |
| | | 54 | | [string]$ServiceLogPath, |
| | | 55 | | |
| | | 56 | | [Parameter()] |
| | | 57 | | [string[]]$PreservePaths, |
| | | 58 | | |
| | | 59 | | [Parameter()] |
| | | 60 | | [string[]]$ApplicationDataFolders, |
| | | 61 | | |
| | | 62 | | [Parameter()] |
| | | 63 | | [switch]$ClearServiceLogPath, |
| | | 64 | | |
| | | 65 | | [Parameter()] |
| | | 66 | | [switch]$ClearPreservePaths, |
| | | 67 | | |
| | | 68 | | [Parameter()] |
| | | 69 | | [switch]$ClearApplicationDataFolders |
| | | 70 | | ) |
| | | 71 | | |
| | 1 | 72 | | $updateRequested = $PSBoundParameters.ContainsKey('Description') -or |
| | | 73 | | $PSBoundParameters.ContainsKey('Version') -or |
| | | 74 | | $PSBoundParameters.ContainsKey('EntryPoint') -or |
| | | 75 | | $PSBoundParameters.ContainsKey('ServiceLogPath') -or |
| | | 76 | | $PSBoundParameters.ContainsKey('PreservePaths') -or |
| | | 77 | | $PSBoundParameters.ContainsKey('ApplicationDataFolders') -or |
| | | 78 | | $ClearServiceLogPath -or |
| | | 79 | | $ClearPreservePaths -or |
| | | 80 | | $ClearApplicationDataFolders |
| | | 81 | | |
| | 1 | 82 | | if (-not $updateRequested) { |
| | 0 | 83 | | throw 'No updates requested. Specify one or more updatable fields.' |
| | | 84 | | } |
| | | 85 | | |
| | 1 | 86 | | if ($PSBoundParameters.ContainsKey('ServiceLogPath') -and $ClearServiceLogPath) { |
| | 0 | 87 | | throw 'Cannot use -ServiceLogPath and -ClearServiceLogPath together.' |
| | | 88 | | } |
| | | 89 | | |
| | 1 | 90 | | if ($PSBoundParameters.ContainsKey('PreservePaths') -and $ClearPreservePaths) { |
| | 0 | 91 | | throw 'Cannot use -PreservePaths and -ClearPreservePaths together.' |
| | | 92 | | } |
| | | 93 | | |
| | 1 | 94 | | if ($PSBoundParameters.ContainsKey('ApplicationDataFolders') -and $ClearApplicationDataFolders) { |
| | 0 | 95 | | throw 'Cannot use -ApplicationDataFolders and -ClearApplicationDataFolders together.' |
| | | 96 | | } |
| | | 97 | | |
| | 1 | 98 | | $current = Get-KrServiceDescriptor -Path $Path |
| | | 99 | | |
| | 3 | 100 | | $nextDescription = if ($PSBoundParameters.ContainsKey('Description')) { $Description } else { $current.Description } |
| | 1 | 101 | | $nextVersion = if ($PSBoundParameters.ContainsKey('Version')) { |
| | 1 | 102 | | if ($null -eq $Version) { |
| | 1 | 103 | | $null |
| | | 104 | | } else { |
| | 1 | 105 | | $Version.ToString() |
| | | 106 | | } |
| | | 107 | | } else { |
| | 1 | 108 | | $current.Version |
| | | 109 | | } |
| | 3 | 110 | | $nextEntryPoint = if ($PSBoundParameters.ContainsKey('EntryPoint')) { $EntryPoint } else { $current.EntryPoint } |
| | 4 | 111 | | $nextServiceLogPath = if ($ClearServiceLogPath) { $null } elseif ($PSBoundParameters.ContainsKey('ServiceLogPath')) |
| | 7 | 112 | | $nextPreservePaths = if ($ClearPreservePaths) { @() } elseif ($PSBoundParameters.ContainsKey('PreservePaths')) { @($ |
| | 7 | 113 | | $nextApplicationDataFolders = if ($ClearApplicationDataFolders) { @() } elseif ($PSBoundParameters.ContainsKey('Appl |
| | | 114 | | |
| | 1 | 115 | | if ([string]::IsNullOrWhiteSpace($nextDescription)) { |
| | 1 | 116 | | if ($PSBoundParameters.ContainsKey('Description')) { |
| | 1 | 117 | | throw 'Parameter -Description cannot be null or empty.' |
| | | 118 | | } |
| | | 119 | | |
| | 1 | 120 | | throw 'Service descriptor is missing a valid Description. Update the descriptor or pass -Description with a non- |
| | | 121 | | } |
| | | 122 | | |
| | 1 | 123 | | if ([string]::IsNullOrWhiteSpace($nextVersion)) { |
| | 1 | 124 | | if ($PSBoundParameters.ContainsKey('Version')) { |
| | 1 | 125 | | throw 'Parameter -Version cannot be null or empty and must be compatible with [System.Version].' |
| | | 126 | | } |
| | | 127 | | |
| | 1 | 128 | | throw 'Service descriptor is missing a valid Version. Pass -Version with a value compatible with [System.Version |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | try { |
| | 1 | 132 | | [void][version]$nextVersion |
| | | 133 | | } catch { |
| | 0 | 134 | | if ($PSBoundParameters.ContainsKey('Version')) { |
| | 0 | 135 | | throw 'Parameter -Version cannot be null or empty and must be compatible with [System.Version].' |
| | | 136 | | } |
| | | 137 | | |
| | 0 | 138 | | throw 'Service descriptor is missing a valid Version. Pass -Version with a value compatible with [System.Version |
| | | 139 | | } |
| | | 140 | | |
| | 1 | 141 | | if ([string]::IsNullOrWhiteSpace($nextEntryPoint)) { |
| | 0 | 142 | | if ($PSBoundParameters.ContainsKey('EntryPoint')) { |
| | 0 | 143 | | throw 'Parameter -EntryPoint cannot be null or empty.' |
| | | 144 | | } |
| | | 145 | | |
| | 0 | 146 | | throw 'Service descriptor is missing a valid EntryPoint. Update the descriptor or pass -EntryPoint with a non-em |
| | | 147 | | } |
| | | 148 | | |
| | 1 | 149 | | if ([System.IO.Path]::IsPathRooted($nextEntryPoint)) { |
| | 0 | 150 | | throw "EntryPoint must be a relative path under the descriptor directory: $nextEntryPoint" |
| | | 151 | | } |
| | | 152 | | |
| | 1 | 153 | | $descriptorDirectory = [System.IO.Path]::GetDirectoryName($current.Path) |
| | 2 | 154 | | $entryPointPath = [System.IO.Path]::GetFullPath((Join-Path -Path $descriptorDirectory -ChildPath $nextEntryPoint)) |
| | 2 | 155 | | $descriptorRootWithSeparator = if ($descriptorDirectory.EndsWith([System.IO.Path]::DirectorySeparatorChar)) { $descr |
| | 1 | 156 | | if (-not $entryPointPath.StartsWith($descriptorRootWithSeparator, [System.StringComparison]::OrdinalIgnoreCase)) { |
| | 0 | 157 | | throw "EntryPoint must resolve to a file under descriptor path: $nextEntryPoint" |
| | | 158 | | } |
| | | 159 | | |
| | 2 | 160 | | if (-not (Test-Path -LiteralPath $entryPointPath -PathType Leaf)) { |
| | 1 | 161 | | throw "EntryPoint file not found under descriptor path: $nextEntryPoint" |
| | | 162 | | } |
| | | 163 | | |
| | 1 | 164 | | $escapedName = $current.Name.Replace("'", "''") |
| | 1 | 165 | | $escapedDescription = $nextDescription.Replace("'", "''") |
| | 1 | 166 | | $escapedVersion = $nextVersion.Replace("'", "''") |
| | | 167 | | |
| | 1 | 168 | | $contentLines = [System.Collections.Generic.List[string]]::new() |
| | 1 | 169 | | $contentLines.Add('@{') |
| | 1 | 170 | | $contentLines.Add(" FormatVersion = '1.0'") |
| | 1 | 171 | | $contentLines.Add(" Name = '$escapedName'") |
| | 1 | 172 | | $contentLines.Add(" Description = '$escapedDescription'") |
| | 1 | 173 | | $contentLines.Add(" Version = '$escapedVersion'") |
| | | 174 | | |
| | 1 | 175 | | $escapedEntryPoint = $nextEntryPoint.Replace("'", "''") |
| | 1 | 176 | | $contentLines.Add(" EntryPoint = '$escapedEntryPoint'") |
| | | 177 | | |
| | 1 | 178 | | if (-not [string]::IsNullOrWhiteSpace($nextServiceLogPath)) { |
| | 1 | 179 | | $escapedServiceLogPath = $nextServiceLogPath.Replace("'", "''") |
| | 1 | 180 | | $contentLines.Add(" ServiceLogPath = '$escapedServiceLogPath'") |
| | | 181 | | } |
| | | 182 | | |
| | 1 | 183 | | if ($null -ne $nextPreservePaths -and $nextPreservePaths.Count -gt 0) { |
| | 1 | 184 | | $contentLines.Add(' PreservePaths = @(') |
| | 1 | 185 | | foreach ($preservePath in $nextPreservePaths) { |
| | 1 | 186 | | if ([string]::IsNullOrWhiteSpace($preservePath)) { |
| | | 187 | | continue |
| | | 188 | | } |
| | | 189 | | |
| | 1 | 190 | | $escapedPreservePath = $preservePath.Replace("'", "''") |
| | 1 | 191 | | $contentLines.Add(" '$escapedPreservePath'") |
| | | 192 | | } |
| | | 193 | | |
| | 1 | 194 | | $contentLines.Add(' )') |
| | | 195 | | } |
| | | 196 | | |
| | 1 | 197 | | if ($null -ne $nextApplicationDataFolders -and $nextApplicationDataFolders.Count -gt 0) { |
| | 1 | 198 | | $contentLines.Add(' ApplicationDataFolders = @(') |
| | 1 | 199 | | foreach ($applicationDataFolder in $nextApplicationDataFolders) { |
| | 1 | 200 | | if ([string]::IsNullOrWhiteSpace($applicationDataFolder)) { |
| | | 201 | | continue |
| | | 202 | | } |
| | | 203 | | |
| | 1 | 204 | | $escapedApplicationDataFolder = $applicationDataFolder.Replace("'", "''") |
| | 1 | 205 | | $contentLines.Add(" '$escapedApplicationDataFolder'") |
| | | 206 | | } |
| | | 207 | | |
| | 1 | 208 | | $contentLines.Add(' )') |
| | | 209 | | } |
| | | 210 | | |
| | 1 | 211 | | $contentLines.Add('}') |
| | | 212 | | |
| | 1 | 213 | | if ($PSCmdlet.ShouldProcess($current.Path, 'Update Service.psd1 descriptor (Name is immutable)')) { |
| | 2 | 214 | | Set-Content -LiteralPath $current.Path -Value ($contentLines -join [Environment]::NewLine) -Encoding utf8NoBOM |
| | | 215 | | } |
| | | 216 | | |
| | 1 | 217 | | Get-KrServiceDescriptor -Path $current.Path |
| | | 218 | | } |