| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Creates a Kestrun service package (.krpack). |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Creates a .krpack archive from either: |
| | | 6 | | - A source folder that already contains Service.psd1 (validated before packaging), or |
| | | 7 | | - A script file plus Name/Version metadata (a Service.psd1 descriptor is generated automatically). |
| | | 8 | | |
| | | 9 | | For generated descriptors, FormatVersion is set to '1.0' and EntryPoint is set to the script file name. |
| | | 10 | | .PARAMETER SourceFolder |
| | | 11 | | Folder to package. Must contain a valid Service.psd1 descriptor. |
| | | 12 | | .PARAMETER ScriptPath |
| | | 13 | | Script file to package. A Service.psd1 descriptor is generated automatically. |
| | | 14 | | .PARAMETER Name |
| | | 15 | | Service name used when generating Service.psd1 from ScriptPath. |
| | | 16 | | If omitted, defaults to the script filename without extension. |
| | | 17 | | .PARAMETER Version |
| | | 18 | | Service version used when generating Service.psd1 from ScriptPath. |
| | | 19 | | .PARAMETER Description |
| | | 20 | | Optional description used when generating Service.psd1 from ScriptPath. |
| | | 21 | | Defaults to Name. |
| | | 22 | | .PARAMETER ServiceLogPath |
| | | 23 | | Optional ServiceLogPath written to generated Service.psd1. |
| | | 24 | | .PARAMETER PreservePaths |
| | | 25 | | Optional relative file/folder paths to preserve during service update. |
| | | 26 | | .PARAMETER ApplicationDataFolders |
| | | 27 | | Optional relative application-data folders to preserve during service update. |
| | | 28 | | .PARAMETER ExcludeApplicationDataFolders |
| | | 29 | | In SourceFolder mode, excludes files under descriptor ApplicationDataFolders from the package archive. |
| | | 30 | | The descriptor values are kept unchanged so those folders can still be preserved during service update. |
| | | 31 | | .PARAMETER ExcludePaths |
| | | 32 | | In SourceFolder mode, excludes specific relative files or folders from the package archive. |
| | | 33 | | Paths must stay under SourceFolder and cannot exclude Service.psd1 or the EntryPoint file. |
| | | 34 | | .PARAMETER OutputPath |
| | | 35 | | Output .krpack path. |
| | | 36 | | Defaults: |
| | | 37 | | - SourceFolder mode: <SourceFolderName>.krpack in current directory |
| | | 38 | | - ScriptPath mode: <Name>-<Version>.krpack in current directory |
| | | 39 | | .PARAMETER Force |
| | | 40 | | Overwrite an existing output file. |
| | | 41 | | .PARAMETER WhatIf |
| | | 42 | | Shows what would happen if the cmdlet runs. The cmdlet is not executed. |
| | | 43 | | .PARAMETER Confirm |
| | | 44 | | Prompts for confirmation before running the cmdlet. |
| | | 45 | | .EXAMPLE |
| | | 46 | | New-KrServicePackage -SourceFolder .\my-service -OutputPath .\my-service.krpack |
| | | 47 | | .EXAMPLE |
| | | 48 | | New-KrServicePackage -ScriptPath .\server.ps1 -Name demo -Version 1.2.0 -OutputPath .\demo.krpack |
| | | 49 | | .EXAMPLE |
| | | 50 | | New-KrServicePackage -ScriptPath .\server.ps1 -Version 1.2.0 |
| | | 51 | | .EXAMPLE |
| | | 52 | | New-KrServicePackage -SourceFolder .\my-service -ExcludeApplicationDataFolders -ExcludePaths @('secrets/dev.json', ' |
| | | 53 | | #> |
| | | 54 | | function New-KrServicePackage { |
| | | 55 | | [KestrunRuntimeApi('Everywhere')] |
| | | 56 | | [CmdletBinding(DefaultParameterSetName = 'FromFolder', SupportsShouldProcess)] |
| | | 57 | | [OutputType([pscustomobject])] |
| | | 58 | | param( |
| | | 59 | | [Parameter(Mandatory, ParameterSetName = 'FromFolder')] |
| | | 60 | | [ValidateNotNullOrEmpty()] |
| | | 61 | | [string]$SourceFolder, |
| | | 62 | | |
| | | 63 | | [Parameter(Mandatory, ParameterSetName = 'FromScript')] |
| | | 64 | | [ValidateNotNullOrEmpty()] |
| | | 65 | | [string]$ScriptPath, |
| | | 66 | | |
| | | 67 | | [Parameter(ParameterSetName = 'FromScript')] |
| | | 68 | | [ValidateNotNullOrEmpty()] |
| | | 69 | | [string]$Name, |
| | | 70 | | |
| | | 71 | | [Parameter(Mandatory, ParameterSetName = 'FromScript')] |
| | | 72 | | [ValidateNotNullOrEmpty()] |
| | | 73 | | [version]$Version, |
| | | 74 | | |
| | | 75 | | [Parameter(ParameterSetName = 'FromScript')] |
| | | 76 | | [string]$Description, |
| | | 77 | | |
| | | 78 | | [Parameter(ParameterSetName = 'FromScript')] |
| | | 79 | | [string]$ServiceLogPath, |
| | | 80 | | |
| | | 81 | | [Parameter(ParameterSetName = 'FromScript')] |
| | | 82 | | [string[]]$PreservePaths, |
| | | 83 | | |
| | | 84 | | [Parameter(ParameterSetName = 'FromScript')] |
| | | 85 | | [string[]]$ApplicationDataFolders, |
| | | 86 | | |
| | | 87 | | [Parameter(ParameterSetName = 'FromFolder')] |
| | | 88 | | [switch]$ExcludeApplicationDataFolders, |
| | | 89 | | |
| | | 90 | | [Parameter(ParameterSetName = 'FromFolder')] |
| | | 91 | | [string[]]$ExcludePaths, |
| | | 92 | | |
| | | 93 | | [Parameter()] |
| | | 94 | | [ValidateNotNullOrEmpty()] |
| | | 95 | | [string]$OutputPath, |
| | | 96 | | |
| | | 97 | | [Parameter()] |
| | | 98 | | [switch]$Force |
| | | 99 | | ) |
| | | 100 | | |
| | | 101 | | <# |
| | | 102 | | .SYNOPSIS |
| | | 103 | | Resolves the output path for the .krpack file. |
| | | 104 | | .DESCRIPTION |
| | | 105 | | Determines the full path for the output .krpack file based on the provided path or a default base name. |
| | | 106 | | .PARAMETER ProvidedOutputPath |
| | | 107 | | The user-provided output path. |
| | | 108 | | .PARAMETER DefaultBaseName |
| | | 109 | | The default base name to use if no output path is provided. |
| | | 110 | | .EXAMPLE |
| | | 111 | | Get-KrResolvedOutputPath -ProvidedOutputPath .\output.krpack -DefaultBaseName demo |
| | | 112 | | .EXAMPLE |
| | | 113 | | Get-KrResolvedOutputPath -ProvidedOutputPath '' -DefaultBaseName demo |
| | | 114 | | .EXAMPLE |
| | | 115 | | Get-KrResolvedOutputPath -ProvidedOutputPath $null -DefaultBaseName demo |
| | | 116 | | .OUTPUTS |
| | | 117 | | [string] The resolved full path for the .krpack file. |
| | | 118 | | #> |
| | | 119 | | function Get-KrResolvedOutputPath { |
| | | 120 | | param( |
| | | 121 | | [string]$ProvidedOutputPath, |
| | | 122 | | [string]$DefaultBaseName |
| | | 123 | | ) |
| | | 124 | | |
| | 1 | 125 | | $resolved = if ([string]::IsNullOrWhiteSpace($ProvidedOutputPath)) { |
| | 2 | 126 | | [System.IO.Path]::Combine((Get-Location).Path, "$DefaultBaseName.krpack") |
| | | 127 | | } else { |
| | 1 | 128 | | [System.IO.Path]::GetFullPath($ProvidedOutputPath) |
| | | 129 | | } |
| | | 130 | | |
| | 1 | 131 | | if (-not $resolved.EndsWith('.krpack', [System.StringComparison]::OrdinalIgnoreCase)) { |
| | 0 | 132 | | $resolved = "$resolved.krpack" |
| | | 133 | | } |
| | | 134 | | |
| | 1 | 135 | | return $resolved |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | <# |
| | | 139 | | .SYNOPSIS |
| | | 140 | | Validates and resolves a relative package path. |
| | | 141 | | .DESCRIPTION |
| | | 142 | | Resolves a relative path under the package root and optionally requires that the path exists. |
| | | 143 | | .PARAMETER PackageRoot |
| | | 144 | | The package root directory. |
| | | 145 | | .PARAMETER RelativePath |
| | | 146 | | The relative file or folder path to validate. |
| | | 147 | | .PARAMETER PathLabel |
| | | 148 | | Label used in error messages. |
| | | 149 | | .PARAMETER RequireExisting |
| | | 150 | | Requires the path to exist under the package root. |
| | | 151 | | .PARAMETER RejectPackageRoot |
| | | 152 | | Rejects paths that resolve to the package root itself. |
| | | 153 | | .OUTPUTS |
| | | 154 | | [pscustomobject] with FullPath, RelativePath, and IsDirectory properties. |
| | | 155 | | #> |
| | | 156 | | function Get-KrPackagePathInfo { |
| | | 157 | | param( |
| | | 158 | | [string]$PackageRoot, |
| | | 159 | | [string]$RelativePath, |
| | | 160 | | [string]$PathLabel, |
| | | 161 | | [switch]$RequireExisting, |
| | | 162 | | [switch]$RejectPackageRoot |
| | | 163 | | ) |
| | | 164 | | |
| | 1 | 165 | | if ([string]::IsNullOrWhiteSpace($RelativePath)) { |
| | 0 | 166 | | throw "$PathLabel cannot contain empty values." |
| | | 167 | | } |
| | | 168 | | |
| | 1 | 169 | | if ([System.IO.Path]::IsPathRooted($RelativePath)) { |
| | 0 | 170 | | throw "$PathLabel entry '$RelativePath' must be a relative path." |
| | | 171 | | } |
| | | 172 | | |
| | 1 | 173 | | $packageRootFullPath = [System.IO.Path]::GetFullPath($PackageRoot) |
| | 1 | 174 | | $packageRootNormalized = [System.IO.Path]::TrimEndingDirectorySeparator($packageRootFullPath) |
| | 1 | 175 | | $combinedPath = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($packageRootFullPath, $RelativePath)) |
| | 1 | 176 | | $relativeToRoot = [System.IO.Path]::GetRelativePath($packageRootNormalized, $combinedPath) |
| | | 177 | | |
| | 1 | 178 | | if ([System.IO.Path]::IsPathRooted($relativeToRoot) -or |
| | | 179 | | [string]::Equals($relativeToRoot, '..', [System.StringComparison]::Ordinal) -or |
| | 1 | 180 | | $relativeToRoot.StartsWith("..$([System.IO.Path]::DirectorySeparatorChar)", [System.StringComparison]::Ordin |
| | 1 | 181 | | $relativeToRoot.StartsWith("..$([System.IO.Path]::AltDirectorySeparatorChar)", [System.StringComparison]::Or |
| | 0 | 182 | | throw "$PathLabel entry '$RelativePath' escapes the package root." |
| | | 183 | | } |
| | | 184 | | |
| | 1 | 185 | | if ($RejectPackageRoot -and [string]::Equals($relativeToRoot, '.', [System.StringComparison]::Ordinal)) { |
| | 0 | 186 | | throw "$PathLabel entry '$RelativePath' resolves to the package root and cannot be excluded." |
| | | 187 | | } |
| | | 188 | | |
| | 1 | 189 | | $pathExists = Test-Path -LiteralPath $combinedPath |
| | 1 | 190 | | if ($RequireExisting -and -not $pathExists) { |
| | 0 | 191 | | throw "$PathLabel entry '$RelativePath' was not found under '$PackageRoot'." |
| | | 192 | | } |
| | | 193 | | |
| | 1 | 194 | | $isDirectory = if ($pathExists) { |
| | 1 | 195 | | Test-Path -LiteralPath $combinedPath -PathType Container |
| | | 196 | | } else { |
| | 0 | 197 | | $RelativePath.EndsWith([System.IO.Path]::DirectorySeparatorChar) -or |
| | | 198 | | $RelativePath.EndsWith([System.IO.Path]::AltDirectorySeparatorChar) |
| | | 199 | | } |
| | | 200 | | |
| | 1 | 201 | | [pscustomobject]@{ |
| | 1 | 202 | | FullPath = $combinedPath |
| | 1 | 203 | | RelativePath = $relativeToRoot -replace '\\', '/' |
| | 1 | 204 | | IsDirectory = $isDirectory |
| | | 205 | | } |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | <# |
| | | 209 | | .SYNOPSIS |
| | | 210 | | Tests whether a path is covered by package exclusions. |
| | | 211 | | .DESCRIPTION |
| | | 212 | | Matches both exact file exclusions and directory exclusions that cover descendant files. |
| | | 213 | | .PARAMETER Path |
| | | 214 | | The file path to test. |
| | | 215 | | .PARAMETER ExcludedEntries |
| | | 216 | | Exclusion entries returned by Get-KrPackagePathInfo. |
| | | 217 | | .OUTPUTS |
| | | 218 | | [bool] True when the path should be excluded. |
| | | 219 | | #> |
| | | 220 | | function Test-KrPathIsExcluded { |
| | | 221 | | param( |
| | | 222 | | [string]$Path, |
| | | 223 | | [object[]]$ExcludedEntries |
| | | 224 | | ) |
| | | 225 | | |
| | 1 | 226 | | if ($null -eq $ExcludedEntries -or $ExcludedEntries.Count -eq 0) { |
| | 1 | 227 | | return $false |
| | | 228 | | } |
| | | 229 | | |
| | 1 | 230 | | $candidatePath = [System.IO.Path]::TrimEndingDirectorySeparator([System.IO.Path]::GetFullPath($Path)) |
| | 1 | 231 | | foreach ($entry in $ExcludedEntries) { |
| | 1 | 232 | | $excludedPath = [System.IO.Path]::TrimEndingDirectorySeparator([System.IO.Path]::GetFullPath($entry.FullPath |
| | 1 | 233 | | $relativeToExcluded = [System.IO.Path]::GetRelativePath($excludedPath, $candidatePath) |
| | | 234 | | |
| | 1 | 235 | | if ($entry.IsDirectory) { |
| | 1 | 236 | | if ([string]::Equals($relativeToExcluded, '.', [System.StringComparison]::Ordinal)) { |
| | 0 | 237 | | return $true |
| | | 238 | | } |
| | | 239 | | |
| | 1 | 240 | | if (-not [System.IO.Path]::IsPathRooted($relativeToExcluded) -and |
| | | 241 | | -not [string]::Equals($relativeToExcluded, '..', [System.StringComparison]::Ordinal) -and |
| | 1 | 242 | | -not $relativeToExcluded.StartsWith("..$([System.IO.Path]::DirectorySeparatorChar)", [System.StringC |
| | 1 | 243 | | -not $relativeToExcluded.StartsWith("..$([System.IO.Path]::AltDirectorySeparatorChar)", [System.Stri |
| | 1 | 244 | | return $true |
| | | 245 | | } |
| | | 246 | | |
| | | 247 | | continue |
| | | 248 | | } |
| | | 249 | | |
| | 1 | 250 | | if ([string]::Equals($candidatePath, $excludedPath, [System.StringComparison]::OrdinalIgnoreCase)) { |
| | 1 | 251 | | return $true |
| | | 252 | | } |
| | | 253 | | } |
| | | 254 | | |
| | 1 | 255 | | return $false |
| | | 256 | | } |
| | | 257 | | |
| | | 258 | | <# |
| | | 259 | | .SYNOPSIS |
| | | 260 | | Builds normalized package exclusion entries. |
| | | 261 | | .DESCRIPTION |
| | | 262 | | Combines ApplicationDataFolders and explicit ExcludePaths into a deduplicated exclusion list. |
| | | 263 | | .PARAMETER PackageRoot |
| | | 264 | | The package root directory. |
| | | 265 | | .PARAMETER DescriptorInfo |
| | | 266 | | The validated descriptor info object. |
| | | 267 | | .PARAMETER DescriptorPath |
| | | 268 | | The Service.psd1 path. |
| | | 269 | | .PARAMETER ExcludeApplicationDataFolders |
| | | 270 | | Excludes descriptor ApplicationDataFolders from the archive. |
| | | 271 | | .PARAMETER ExcludePaths |
| | | 272 | | Additional relative file or folder paths to exclude. |
| | | 273 | | .OUTPUTS |
| | | 274 | | An array of exclusion entry objects. |
| | | 275 | | #> |
| | | 276 | | function Get-KrPackageExclusion { |
| | | 277 | | param( |
| | | 278 | | [string]$PackageRoot, |
| | | 279 | | [pscustomobject]$DescriptorInfo, |
| | | 280 | | [string]$DescriptorPath, |
| | | 281 | | [switch]$ExcludeApplicationDataFolders, |
| | | 282 | | [string[]]$ExcludePaths |
| | | 283 | | ) |
| | | 284 | | |
| | 1 | 285 | | $entries = [System.Collections.Generic.List[object]]::new() |
| | 1 | 286 | | $seenKeys = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase) |
| | | 287 | | |
| | 1 | 288 | | $addEntry = { |
| | | 289 | | param([pscustomobject]$Entry) |
| | | 290 | | |
| | 2 | 291 | | $entryKey = '{0}|{1}' -f ([System.IO.Path]::TrimEndingDirectorySeparator($Entry.FullPath)), $Entry.IsDirecto |
| | 1 | 292 | | if ($seenKeys.Add($entryKey)) { |
| | 1 | 293 | | $entries.Add($Entry) |
| | | 294 | | } |
| | | 295 | | } |
| | | 296 | | |
| | 2 | 297 | | if ($ExcludeApplicationDataFolders -and ($null -ne $DescriptorInfo.ApplicationDataFolders)) { |
| | 2 | 298 | | foreach ($applicationDataFolder in @($DescriptorInfo.ApplicationDataFolders)) { |
| | 1 | 299 | | if ([string]::IsNullOrWhiteSpace($applicationDataFolder)) { |
| | | 300 | | continue |
| | | 301 | | } |
| | | 302 | | |
| | 2 | 303 | | & $addEntry (Get-KrPackagePathInfo -PackageRoot $PackageRoot -RelativePath $applicationDataFolder -PathL |
| | | 304 | | } |
| | | 305 | | } |
| | | 306 | | |
| | 1 | 307 | | if ($null -ne $ExcludePaths) { |
| | 2 | 308 | | foreach ($excludePath in @($ExcludePaths)) { |
| | 1 | 309 | | if ([string]::IsNullOrWhiteSpace($excludePath)) { |
| | | 310 | | continue |
| | | 311 | | } |
| | | 312 | | |
| | 2 | 313 | | & $addEntry (Get-KrPackagePathInfo -PackageRoot $PackageRoot -RelativePath $excludePath -PathLabel 'Excl |
| | | 314 | | } |
| | | 315 | | } |
| | | 316 | | |
| | 1 | 317 | | if ($entries.Count -eq 0) { |
| | 1 | 318 | | return @() |
| | | 319 | | } |
| | | 320 | | |
| | 1 | 321 | | if (Test-KrPathIsExcluded -Path $DescriptorPath -ExcludedEntries $entries) { |
| | 0 | 322 | | throw 'Requested package exclusions cannot exclude Service.psd1 because the service descriptor is required.' |
| | | 323 | | } |
| | | 324 | | |
| | 1 | 325 | | $entryPointPath = [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($PackageRoot, $DescriptorInfo.EntryPoi |
| | 1 | 326 | | if (Test-KrPathIsExcluded -Path $entryPointPath -ExcludedEntries $entries) { |
| | 2 | 327 | | throw "Requested package exclusions cannot exclude the EntryPoint '$($DescriptorInfo.EntryPoint)'." |
| | | 328 | | } |
| | | 329 | | |
| | 1 | 330 | | return $entries.ToArray() |
| | | 331 | | } |
| | | 332 | | |
| | | 333 | | <# |
| | | 334 | | .SYNOPSIS |
| | | 335 | | Creates a zip archive from a directory. |
| | | 336 | | .DESCRIPTION |
| | | 337 | | Compresses the contents of a directory into a .krpack file. |
| | | 338 | | .PARAMETER DirectoryPath |
| | | 339 | | The path of the directory to compress. |
| | | 340 | | .PARAMETER DestinationPath |
| | | 341 | | The path of the resulting .krpack file. |
| | | 342 | | .EXAMPLE |
| | | 343 | | Invoke-KrZipFromDirectory -DirectoryPath .\my-service -DestinationPath .\my-service.krpack |
| | | 344 | | .OUTPUTS |
| | | 345 | | None. Creates a .krpack file at the specified destination. |
| | | 346 | | #> |
| | | 347 | | function Invoke-KrZipFromDirectory { |
| | | 348 | | param( |
| | | 349 | | [string]$DirectoryPath, |
| | | 350 | | [string]$DestinationPath, |
| | | 351 | | [object[]]$ExcludedEntries = @() |
| | | 352 | | ) |
| | | 353 | | |
| | 1 | 354 | | Add-Type -AssemblyName System.IO.Compression |
| | 1 | 355 | | Add-Type -AssemblyName System.IO.Compression.FileSystem |
| | | 356 | | |
| | 1 | 357 | | $destinationDirectory = [System.IO.Path]::GetDirectoryName($DestinationPath) |
| | 2 | 358 | | if (-not [string]::IsNullOrWhiteSpace($destinationDirectory) -and -not (Test-Path -LiteralPath $destinationDirec |
| | 0 | 359 | | $null = New-Item -ItemType Directory -Path $destinationDirectory -Force |
| | | 360 | | } |
| | | 361 | | |
| | 1 | 362 | | if (Test-Path -LiteralPath $DestinationPath -PathType Leaf) { |
| | 0 | 363 | | Remove-Item -LiteralPath $DestinationPath -Force |
| | | 364 | | } |
| | | 365 | | |
| | 1 | 366 | | $zip = [System.IO.Compression.ZipFile]::Open($DestinationPath, [System.IO.Compression.ZipArchiveMode]::Create) |
| | | 367 | | try { |
| | 2 | 368 | | foreach ($file in (Get-ChildItem -LiteralPath $DirectoryPath -File -Recurse -Force)) { |
| | 1 | 369 | | if (Test-KrPathIsExcluded -Path $file.FullName -ExcludedEntries $ExcludedEntries) { |
| | | 370 | | continue |
| | | 371 | | } |
| | | 372 | | |
| | 1 | 373 | | $relativePath = [System.IO.Path]::GetRelativePath($DirectoryPath, $file.FullName) |
| | 1 | 374 | | $normalizedRelativePath = $relativePath -replace '\\', '/' |
| | 2 | 375 | | [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $file.FullName, $normalizedRelative |
| | | 376 | | } |
| | | 377 | | } finally { |
| | 1 | 378 | | $zip.Dispose() |
| | | 379 | | } |
| | | 380 | | } |
| | | 381 | | |
| | 1 | 382 | | $stagingRoot = $null |
| | 1 | 383 | | $packageRoot = $null |
| | 1 | 384 | | $descriptorInfo = $null |
| | 1 | 385 | | $packageExclusions = @() |
| | | 386 | | |
| | | 387 | | try { |
| | 1 | 388 | | if ($PSCmdlet.ParameterSetName -eq 'FromFolder') { |
| | 1 | 389 | | $packageRoot = [System.IO.Path]::GetFullPath($SourceFolder) |
| | 2 | 390 | | if (-not (Test-Path -LiteralPath $packageRoot -PathType Container)) { |
| | 0 | 391 | | throw "Source folder not found: $packageRoot" |
| | | 392 | | } |
| | | 393 | | |
| | 1 | 394 | | $descriptorPath = [System.IO.Path]::Combine($packageRoot, 'Service.psd1') |
| | 2 | 395 | | if (-not (Test-Path -LiteralPath $descriptorPath -PathType Leaf)) { |
| | 1 | 396 | | throw "Service descriptor not found: $descriptorPath" |
| | | 397 | | } |
| | | 398 | | |
| | 1 | 399 | | $descriptorData = Import-PowerShellDataFile -LiteralPath $descriptorPath |
| | 1 | 400 | | $descriptorInfo = Test-KrServiceDescriptorData -Descriptor $descriptorData -DescriptorPath $descriptorPath - |
| | 1 | 401 | | $packageExclusions = Get-KrPackageExclusion -PackageRoot $packageRoot -DescriptorInfo $descriptorInfo ` |
| | | 402 | | -DescriptorPath $descriptorPath -ExcludeApplicationDataFolders:$ExcludeApplicationDataFolders -ExcludePa |
| | | 403 | | |
| | 1 | 404 | | $defaultBaseName = [System.IO.Path]::GetFileName($packageRoot) |
| | 1 | 405 | | $resolvedOutputPath = Get-KrResolvedOutputPath -ProvidedOutputPath $OutputPath -DefaultBaseName $defaultBase |
| | | 406 | | } else { |
| | 1 | 407 | | $resolvedScriptPath = [System.IO.Path]::GetFullPath($ScriptPath) |
| | 2 | 408 | | if (-not (Test-Path -LiteralPath $resolvedScriptPath -PathType Leaf)) { |
| | 0 | 409 | | throw "Script file not found: $resolvedScriptPath" |
| | | 410 | | } |
| | | 411 | | |
| | 1 | 412 | | if (-not $resolvedScriptPath.EndsWith('.ps1', [System.StringComparison]::OrdinalIgnoreCase)) { |
| | 0 | 413 | | throw 'ScriptPath must point to a .ps1 file.' |
| | | 414 | | } |
| | | 415 | | |
| | 1 | 416 | | $scriptFileName = [System.IO.Path]::GetFileName($resolvedScriptPath) |
| | 3 | 417 | | $effectiveName = if ([string]::IsNullOrWhiteSpace($Name)) { [System.IO.Path]::GetFileNameWithoutExtension($r |
| | 3 | 418 | | $effectiveDescription = if ([string]::IsNullOrWhiteSpace($Description)) { $effectiveName } else { $Descripti |
| | | 419 | | |
| | 2 | 420 | | $stagingRoot = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), "kestrun-krpack-$([Guid]::NewGuid( |
| | 1 | 421 | | $null = New-Item -ItemType Directory -Path $stagingRoot -Force |
| | 1 | 422 | | $packageRoot = $stagingRoot |
| | | 423 | | |
| | 2 | 424 | | Copy-Item -LiteralPath $resolvedScriptPath -Destination ([System.IO.Path]::Combine($packageRoot, $scriptFile |
| | | 425 | | |
| | 1 | 426 | | $escapedName = $effectiveName.Replace("'", "''") |
| | 1 | 427 | | $escapedDescription = $effectiveDescription.Replace("'", "''") |
| | 1 | 428 | | $escapedVersion = $Version.ToString().Replace("'", "''") |
| | 1 | 429 | | $escapedEntryPoint = $scriptFileName.Replace("'", "''") |
| | | 430 | | |
| | 1 | 431 | | $descriptorLines = [System.Collections.Generic.List[string]]::new() |
| | 1 | 432 | | $descriptorLines.Add('@{') |
| | 1 | 433 | | $descriptorLines.Add(" FormatVersion = '1.0'") |
| | 1 | 434 | | $descriptorLines.Add(" Name = '$escapedName'") |
| | 1 | 435 | | $descriptorLines.Add(" Description = '$escapedDescription'") |
| | 1 | 436 | | $descriptorLines.Add(" Version = '$escapedVersion'") |
| | 1 | 437 | | $descriptorLines.Add(" EntryPoint = '$escapedEntryPoint'") |
| | | 438 | | |
| | 1 | 439 | | if (-not [string]::IsNullOrWhiteSpace($ServiceLogPath)) { |
| | 0 | 440 | | $escapedServiceLogPath = $ServiceLogPath.Replace("'", "''") |
| | 0 | 441 | | $descriptorLines.Add(" ServiceLogPath = '$escapedServiceLogPath'") |
| | | 442 | | } |
| | | 443 | | |
| | 1 | 444 | | if ($null -ne $PreservePaths -and $PreservePaths.Count -gt 0) { |
| | 1 | 445 | | $descriptorLines.Add(' PreservePaths = @(') |
| | 1 | 446 | | foreach ($preservePath in $PreservePaths) { |
| | 1 | 447 | | if ([string]::IsNullOrWhiteSpace($preservePath)) { |
| | | 448 | | continue |
| | | 449 | | } |
| | | 450 | | |
| | 1 | 451 | | $escapedPreservePath = $preservePath.Replace("'", "''") |
| | 1 | 452 | | $descriptorLines.Add(" '$escapedPreservePath'") |
| | | 453 | | } |
| | | 454 | | |
| | 1 | 455 | | $descriptorLines.Add(' )') |
| | | 456 | | } |
| | | 457 | | |
| | 1 | 458 | | if ($null -ne $ApplicationDataFolders -and $ApplicationDataFolders.Count -gt 0) { |
| | 1 | 459 | | $descriptorLines.Add(' ApplicationDataFolders = @(') |
| | 1 | 460 | | foreach ($applicationDataFolder in $ApplicationDataFolders) { |
| | 1 | 461 | | if ([string]::IsNullOrWhiteSpace($applicationDataFolder)) { |
| | | 462 | | continue |
| | | 463 | | } |
| | | 464 | | |
| | 1 | 465 | | $escapedApplicationDataFolder = $applicationDataFolder.Replace("'", "''") |
| | 1 | 466 | | $descriptorLines.Add(" '$escapedApplicationDataFolder'") |
| | | 467 | | } |
| | | 468 | | |
| | 1 | 469 | | $descriptorLines.Add(' )') |
| | | 470 | | } |
| | | 471 | | |
| | 1 | 472 | | $descriptorLines.Add('}') |
| | | 473 | | |
| | 1 | 474 | | $descriptorPath = [System.IO.Path]::Combine($packageRoot, 'Service.psd1') |
| | 2 | 475 | | Set-Content -LiteralPath $descriptorPath -Value ($descriptorLines -join [Environment]::NewLine) -Encoding ut |
| | | 476 | | |
| | 1 | 477 | | $descriptorData = Import-PowerShellDataFile -LiteralPath $descriptorPath |
| | 1 | 478 | | $descriptorInfo = Test-KrServiceDescriptorData -Descriptor $descriptorData -DescriptorPath $descriptorPath - |
| | | 479 | | |
| | 2 | 480 | | $defaultBaseName = "$effectiveName-$($Version.ToString())" |
| | 1 | 481 | | $resolvedOutputPath = Get-KrResolvedOutputPath -ProvidedOutputPath $OutputPath -DefaultBaseName $defaultBase |
| | | 482 | | } |
| | | 483 | | |
| | 2 | 484 | | if ((Test-Path -LiteralPath $resolvedOutputPath -PathType Leaf) -and -not $Force) { |
| | 0 | 485 | | throw "Output package already exists: $resolvedOutputPath. Use -Force to overwrite." |
| | | 486 | | } |
| | | 487 | | |
| | 1 | 488 | | if ($PSCmdlet.ShouldProcess($resolvedOutputPath, 'Create .krpack package')) { |
| | 1 | 489 | | Invoke-KrZipFromDirectory -DirectoryPath $packageRoot -DestinationPath $resolvedOutputPath -ExcludedEntries |
| | | 490 | | } |
| | | 491 | | |
| | 2 | 492 | | [pscustomobject]([ordered]@{ |
| | 1 | 493 | | PackagePath = $resolvedOutputPath |
| | 1 | 494 | | Name = $descriptorInfo.Name |
| | 1 | 495 | | FormatVersion = $descriptorInfo.FormatVersion |
| | 1 | 496 | | EntryPoint = $descriptorInfo.EntryPoint |
| | 1 | 497 | | Description = $descriptorInfo.Description |
| | 1 | 498 | | Version = $descriptorInfo.Version |
| | 1 | 499 | | ServiceLogPath = $descriptorInfo.ServiceLogPath |
| | 2 | 500 | | PreservePaths = @($descriptorInfo.PreservePaths) |
| | 2 | 501 | | ApplicationDataFolders = @($descriptorInfo.ApplicationDataFolders) |
| | | 502 | | } |
| | | 503 | | ) |
| | | 504 | | } finally { |
| | 2 | 505 | | if (-not [string]::IsNullOrWhiteSpace($stagingRoot) -and (Test-Path -LiteralPath $stagingRoot -PathType Containe |
| | 1 | 506 | | Remove-Item -LiteralPath $stagingRoot -Recurse -Force -ErrorAction SilentlyContinue |
| | | 507 | | } |
| | | 508 | | } |
| | | 509 | | } |