| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Creates a Service.psd1 descriptor file. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Creates a Service.psd1 descriptor file used by Kestrun.Tool content-root service install flow. |
| | | 6 | | Required keys are FormatVersion, Name, Description, Version, and EntryPoint. |
| | | 7 | | Optional keys are ServiceLogPath and PreservePaths. |
| | | 8 | | .PARAMETER Path |
| | | 9 | | Target descriptor path. Accepts either a descriptor file path or a directory path. |
| | | 10 | | When a directory path is provided, Service.psd1 is appended automatically. |
| | | 11 | | .PARAMETER Name |
| | | 12 | | Immutable service name value written to the descriptor. |
| | | 13 | | .PARAMETER Description |
| | | 14 | | Service description value. |
| | | 15 | | .PARAMETER Version |
| | | 16 | | Service version. Must be compatible with System.Version. |
| | | 17 | | .PARAMETER EntryPoint |
| | | 18 | | Optional entry point path relative to the content root. Defaults to Service.ps1. |
| | | 19 | | .PARAMETER ServiceLogPath |
| | | 20 | | Optional default service log path. |
| | | 21 | | .PARAMETER PreservePaths |
| | | 22 | | Optional list of relative file/folder paths that must be preserved during service update. |
| | | 23 | | .PARAMETER Force |
| | | 24 | | Overwrite an existing descriptor file. |
| | | 25 | | .PARAMETER WhatIf |
| | | 26 | | Shows what would happen if the cmdlet runs. The cmdlet is not executed. |
| | | 27 | | .PARAMETER Confirm |
| | | 28 | | Prompts for confirmation before running the cmdlet. |
| | | 29 | | .EXAMPLE |
| | | 30 | | New-KrServiceDescriptor -Name demo -Description 'Demo service' -Version 1.2.0 |
| | | 31 | | #> |
| | | 32 | | function New-KrServiceDescriptor { |
| | | 33 | | [KestrunRuntimeApi('Everywhere')] |
| | | 34 | | [CmdletBinding(SupportsShouldProcess)] |
| | | 35 | | [OutputType([pscustomobject])] |
| | | 36 | | param( |
| | | 37 | | [Parameter()] |
| | | 38 | | [ValidateNotNullOrEmpty()] |
| | | 39 | | [string]$Path = 'Service.psd1', |
| | | 40 | | |
| | | 41 | | [Parameter(Mandatory)] |
| | | 42 | | [ValidateNotNullOrEmpty()] |
| | | 43 | | [string]$Name, |
| | | 44 | | |
| | | 45 | | [Parameter(Mandatory)] |
| | | 46 | | [ValidateNotNullOrEmpty()] |
| | | 47 | | [string]$Description, |
| | | 48 | | |
| | | 49 | | [Parameter(Mandatory)] |
| | | 50 | | [ValidateNotNullOrEmpty()] |
| | | 51 | | [version]$Version, |
| | | 52 | | |
| | | 53 | | [Parameter()] |
| | | 54 | | [string]$EntryPoint, |
| | | 55 | | |
| | | 56 | | [Parameter()] |
| | | 57 | | [string]$ServiceLogPath, |
| | | 58 | | |
| | | 59 | | [Parameter()] |
| | | 60 | | [string[]]$PreservePaths, |
| | | 61 | | |
| | | 62 | | [Parameter()] |
| | | 63 | | [switch]$Force |
| | | 64 | | ) |
| | | 65 | | |
| | 1 | 66 | | $fullPath = [System.IO.Path]::GetFullPath($Path) |
| | 1 | 67 | | if (Test-Path -LiteralPath $fullPath -PathType Container) { |
| | 1 | 68 | | $fullPath = Join-Path -Path $fullPath -ChildPath 'Service.psd1' |
| | | 69 | | } |
| | | 70 | | |
| | 1 | 71 | | if ([System.IO.Path]::GetExtension($fullPath) -ne '.psd1') { |
| | 0 | 72 | | throw "Path must target a .psd1 descriptor file or an existing directory: $fullPath" |
| | | 73 | | } |
| | | 74 | | |
| | 2 | 75 | | if ((Test-Path -LiteralPath $fullPath) -and -not $Force) { |
| | 0 | 76 | | throw "Descriptor file already exists: $fullPath. Use -Force to overwrite." |
| | | 77 | | } |
| | | 78 | | |
| | 1 | 79 | | $directory = [System.IO.Path]::GetDirectoryName($fullPath) |
| | 2 | 80 | | if (-not [string]::IsNullOrWhiteSpace($directory) -and -not (Test-Path -LiteralPath $directory)) { |
| | 0 | 81 | | $null = New-Item -ItemType Directory -Path $directory -Force |
| | | 82 | | } |
| | | 83 | | |
| | 1 | 84 | | $escapedName = $Name.Replace("'", "''") |
| | 1 | 85 | | $escapedDescription = $Description.Replace("'", "''") |
| | 1 | 86 | | $escapedVersion = $Version.ToString().Replace("'", "''") |
| | 3 | 87 | | $resolvedEntryPoint = if ([string]::IsNullOrWhiteSpace($EntryPoint)) { 'Service.ps1' } else { $EntryPoint } |
| | | 88 | | |
| | 1 | 89 | | if ([System.IO.Path]::IsPathRooted($resolvedEntryPoint)) { |
| | 0 | 90 | | throw "EntryPoint must be a relative path under the descriptor directory: $resolvedEntryPoint" |
| | | 91 | | } |
| | | 92 | | |
| | 2 | 93 | | $entryPointPath = [System.IO.Path]::GetFullPath((Join-Path -Path $directory -ChildPath $resolvedEntryPoint)) |
| | 2 | 94 | | $descriptorRootWithSeparator = if ($directory.EndsWith([System.IO.Path]::DirectorySeparatorChar)) { $directory } els |
| | 1 | 95 | | if (-not $entryPointPath.StartsWith($descriptorRootWithSeparator, [System.StringComparison]::OrdinalIgnoreCase)) { |
| | 1 | 96 | | throw "EntryPoint must resolve to a file under descriptor path: $resolvedEntryPoint" |
| | | 97 | | } |
| | | 98 | | |
| | 2 | 99 | | if (-not (Test-Path -LiteralPath $entryPointPath -PathType Leaf)) { |
| | 1 | 100 | | throw "EntryPoint file not found under descriptor path: $resolvedEntryPoint" |
| | | 101 | | } |
| | | 102 | | |
| | 1 | 103 | | $escapedEntryPoint = $resolvedEntryPoint.Replace("'", "''") |
| | | 104 | | |
| | 1 | 105 | | $contentLines = [System.Collections.Generic.List[string]]::new() |
| | 1 | 106 | | $contentLines.Add('@{') |
| | 1 | 107 | | $contentLines.Add(" FormatVersion = '1.0'") |
| | 1 | 108 | | $contentLines.Add(" Name = '$escapedName'") |
| | 1 | 109 | | $contentLines.Add(" Description = '$escapedDescription'") |
| | 1 | 110 | | $contentLines.Add(" Version = '$escapedVersion'") |
| | 1 | 111 | | $contentLines.Add(" EntryPoint = '$escapedEntryPoint'") |
| | | 112 | | |
| | 1 | 113 | | if (-not [string]::IsNullOrWhiteSpace($ServiceLogPath)) { |
| | 1 | 114 | | $escapedServiceLogPath = $ServiceLogPath.Replace("'", "''") |
| | 1 | 115 | | $contentLines.Add(" ServiceLogPath = '$escapedServiceLogPath'") |
| | | 116 | | } |
| | | 117 | | |
| | 1 | 118 | | if ($null -ne $PreservePaths -and $PreservePaths.Count -gt 0) { |
| | 1 | 119 | | $contentLines.Add(' PreservePaths = @(') |
| | 1 | 120 | | foreach ($preservePath in $PreservePaths) { |
| | 1 | 121 | | if ([string]::IsNullOrWhiteSpace($preservePath)) { |
| | | 122 | | continue |
| | | 123 | | } |
| | | 124 | | |
| | 1 | 125 | | $escapedPreservePath = $preservePath.Replace("'", "''") |
| | 1 | 126 | | $contentLines.Add(" '$escapedPreservePath'") |
| | | 127 | | } |
| | | 128 | | |
| | 1 | 129 | | $contentLines.Add(' )') |
| | | 130 | | } |
| | | 131 | | |
| | 1 | 132 | | $contentLines.Add('}') |
| | | 133 | | |
| | 1 | 134 | | if ($PSCmdlet.ShouldProcess($fullPath, 'Create Service.psd1 descriptor')) { |
| | 2 | 135 | | Set-Content -LiteralPath $fullPath -Value ($contentLines -join [Environment]::NewLine) -Encoding utf8NoBOM |
| | | 136 | | } |
| | | 137 | | |
| | 1 | 138 | | Get-KrServiceDescriptor -Path $fullPath |
| | | 139 | | } |