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