-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNew-PowerPackerPackage.ps1
More file actions
331 lines (278 loc) · 13.9 KB
/
Copy pathNew-PowerPackerPackage.ps1
File metadata and controls
331 lines (278 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
function New-PowerPackerPackage {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$DefinitionPath,
[Parameter(Mandatory = $true)]
[string]$DeployScriptPath,
[string]$OutputDirectory = (Join-Path $PSScriptRoot '..\Artifacts'),
[string]$PackageDirectoryName,
[string]$EntryScriptName = 'Invoke-AppDeployToolkit.ps1',
[string]$PsadtTemplateZipPath,
[string]$WingetVersion,
[string]$WingetSource,
[ValidateSet('auto', 'native', 'x64', 'x86', 'arm64')]
[string]$Architecture,
[string]$InstallerType,
[string]$Locale,
[ValidateSet('user', 'machine')]
[string]$Scope,
[switch]$SkipInstallerDownload,
[switch]$Force
)
$definition = Parse-PackageMd -Path $DefinitionPath
if (-not $definition.winget_id) {
throw "Definition '$DefinitionPath' does not contain a winget_id in YAML frontmatter."
}
$architecturePolicy = if ($PSBoundParameters.ContainsKey('Architecture') -and -not [string]::IsNullOrWhiteSpace($Architecture)) {
$Architecture.Trim().ToLowerInvariant()
}
else {
[string]$definition.architecture
}
$packageName = if ($PackageDirectoryName) {
$PackageDirectoryName
} else {
($definition.winget_id -replace '[<>:"/\\|?*]', '-')
}
$resolvedOutputDirectory = [System.IO.Path]::GetFullPath($OutputDirectory)
$artifactDirectory = Join-Path $resolvedOutputDirectory $packageName
if (Test-Path -LiteralPath $artifactDirectory) {
if (-not $Force) {
throw "Artifact directory '$artifactDirectory' already exists. Use -Force to overwrite it."
}
Remove-Item -LiteralPath $artifactDirectory -Recurse -Force
}
New-Item -ItemType Directory -Force -Path $artifactDirectory | Out-Null
$tempDirectory = Join-Path $artifactDirectory '.powerpacker-temp'
New-Item -ItemType Directory -Force -Path $tempDirectory | Out-Null
$psadtMetadata = $null
$wingetMetadata = $null
$installerDownload = $null
$wingetArchitectureJobs = @()
try {
$templateZipPath = $PsadtTemplateZipPath
if (-not $templateZipPath) {
$psadtMetadata = Get-PSADTTemplateAsset
$downloadedTemplate = Save-PSADTTemplate -TagName $psadtMetadata.TagName -AssetName $psadtMetadata.AssetName -Directory $tempDirectory -Repository $psadtMetadata.Repository
$templateZipPath = $downloadedTemplate.ZipPath
} else {
$templateZipPath = [System.IO.Path]::GetFullPath($templateZipPath)
if (-not (Test-Path -LiteralPath $templateZipPath -PathType Leaf)) {
throw "PSADT template zip '$templateZipPath' was not found."
}
}
Expand-Archive -Path $templateZipPath -DestinationPath $artifactDirectory -Force
foreach ($placeholder in @(
(Join-Path $artifactDirectory 'Files\Add Setup Files Here.txt'),
(Join-Path $artifactDirectory 'SupportFiles\Add Supporting Files Here.txt')
)) {
if (Test-Path -LiteralPath $placeholder) {
Remove-Item -LiteralPath $placeholder -Force
}
}
$entryScriptPath = Join-Path $artifactDirectory $EntryScriptName
Copy-Item -LiteralPath $DeployScriptPath -Destination $entryScriptPath -Force
$powerPackerSupportDirectory = Join-Path $artifactDirectory 'SupportFiles\PowerPacker'
New-Item -ItemType Directory -Force -Path $powerPackerSupportDirectory | Out-Null
if ((Split-Path -Leaf $DeployScriptPath) -ne $EntryScriptName) {
Copy-Item -LiteralPath $DeployScriptPath -Destination (Join-Path $powerPackerSupportDirectory (Split-Path -Leaf $DeployScriptPath)) -Force
}
if (-not $SkipInstallerDownload) {
$wingetMetadataParams = @{
Id = $definition.winget_id
Name = $definition.name
}
if ($WingetVersion) {
$wingetMetadataParams.Version = $WingetVersion
}
if ($WingetSource) {
$wingetMetadataParams.Source = $WingetSource
}
if ($InstallerType) {
$wingetMetadataParams.InstallerType = $InstallerType
}
if ($Locale) {
$wingetMetadataParams.Locale = $Locale
}
if ($Scope) {
$wingetMetadataParams.Scope = $Scope
}
if ($architecturePolicy -in @('auto', 'native')) {
$resolvedArchitectures = Resolve-WingetArchitectures @wingetMetadataParams
if (-not $resolvedArchitectures.Items -or $resolvedArchitectures.Items.Count -eq 0) {
throw "No winget installers were discovered for '$($definition.winget_id)' across probed architectures. Check the package id, scope, and source."
}
$wingetArchitectureJobs = @($resolvedArchitectures.Items)
}
else {
$lockedMetadata = Get-WingetPackageMetadata @wingetMetadataParams -Architecture $architecturePolicy
$canonicalLabel = switch ($architecturePolicy) {
'x64' { 'X64' }
'x86' { 'X86' }
'arm64' { 'ARM64' }
default { 'X64' }
}
$wingetArchitectureJobs = @(
[pscustomobject]@{
CanonicalLabel = $canonicalLabel
WingetArchitecture = $architecturePolicy
Metadata = $lockedMetadata
}
)
}
$wingetMetadata = $wingetArchitectureJobs[0].Metadata
}
$installerFilesByArchitecture = [ordered]@{}
$installerMetadataByArchitecture = [ordered]@{}
$availableArchitectures = [System.Collections.Generic.List[string]]::new()
$aggregatedInstallerFiles = [System.Collections.Generic.List[string]]::new()
$aggregatedManifestFiles = [System.Collections.Generic.List[string]]::new()
$lastRawDownloadOutput = $null
if (-not $SkipInstallerDownload) {
$filesDirectory = Join-Path $artifactDirectory 'Files'
foreach ($job in $wingetArchitectureJobs) {
$jobMetadata = $job.Metadata
$archSupportLabel = $job.WingetArchitecture
Set-Content -LiteralPath (Join-Path $powerPackerSupportDirectory "winget-show-$archSupportLabel.txt") -Value $jobMetadata.RawOutput
if ([string]::IsNullOrWhiteSpace($jobMetadata.InstallerSha256)) {
throw "winget metadata for '$($definition.winget_id)' ($archSupportLabel) is missing Installer SHA256; cannot verify downloaded payloads."
}
$installerDownloadParams = @{
Id = $definition.winget_id
Name = $definition.name
Directory = $filesDirectory
}
if ($WingetVersion) {
$installerDownloadParams.Version = $WingetVersion
}
if ($WingetSource) {
$installerDownloadParams.Source = $WingetSource
}
if ($InstallerType) {
$installerDownloadParams.InstallerType = $InstallerType
}
if ($Locale) {
$installerDownloadParams.Locale = $Locale
}
if ($Scope) {
$installerDownloadParams.Scope = $Scope
}
$installerDownloadParams.Architecture = $job.WingetArchitecture
$installerDownloadParams.ExpectedSha256 = $jobMetadata.InstallerSha256
$jobDownload = Save-WingetPackageInstaller @installerDownloadParams
$lastRawDownloadOutput = $jobDownload.RawOutput
Set-Content -LiteralPath (Join-Path $powerPackerSupportDirectory "winget-download-$archSupportLabel.txt") -Value $jobDownload.RawOutput
$primaryInstallerPath = $jobDownload.InstallerFiles | Select-Object -First 1
if ([string]::IsNullOrWhiteSpace([string]$primaryInstallerPath)) {
throw "winget download for '$($definition.winget_id)' ($archSupportLabel) did not produce an installer file."
}
$artifactRootFull = [System.IO.Path]::GetFullPath($artifactDirectory).TrimEnd('\')
$installerFullPath = [System.IO.Path]::GetFullPath([string]$primaryInstallerPath)
if (-not $installerFullPath.StartsWith($artifactRootFull + '\', [System.StringComparison]::OrdinalIgnoreCase)) {
throw "Installer path '$installerFullPath' is not under artifact directory '$artifactRootFull'."
}
$relativeInstallerPath = $installerFullPath.Substring($artifactRootFull.Length + 1)
$null = $availableArchitectures.Add($job.CanonicalLabel)
$installerFilesByArchitecture[$job.CanonicalLabel] = $relativeInstallerPath
$installerMetadataByArchitecture[$job.CanonicalLabel] = [ordered]@{
InstallerUrl = $jobMetadata.InstallerUrl
InstallerSha256 = $jobMetadata.InstallerSha256
InstallerType = $jobMetadata.InstallerType
ResolvedVersion = $jobMetadata.Version
Scope = $Scope
}
foreach ($path in $jobDownload.InstallerFiles) {
if (-not $aggregatedInstallerFiles.Contains($path)) {
$null = $aggregatedInstallerFiles.Add($path)
}
}
foreach ($path in $jobDownload.ManifestFiles) {
if (-not $aggregatedManifestFiles.Contains($path)) {
$null = $aggregatedManifestFiles.Add($path)
}
}
}
$installerDownload = [pscustomobject]@{
InstallerFiles = @($aggregatedInstallerFiles)
ManifestFiles = @($aggregatedManifestFiles)
RawOutput = if ($lastRawDownloadOutput) { $lastRawDownloadOutput } else { '' }
}
$legacyPriority = @('X64', 'ARM64', 'X86', 'NEUTRAL')
$legacyMetadata = $null
foreach ($label in $legacyPriority) {
$match = $wingetArchitectureJobs | Where-Object { $_.CanonicalLabel -eq $label } | Select-Object -First 1
if ($match) {
$legacyMetadata = $match.Metadata
break
}
}
if (-not $legacyMetadata) {
$legacyMetadata = $wingetArchitectureJobs[0].Metadata
}
$wingetMetadata = $legacyMetadata
}
$metadata = [ordered]@{
MetadataSchemaVersion = 2
ArchitecturePolicy = $architecturePolicy
AvailableArchitectures = @($availableArchitectures)
InstallerFilesByArchitecture = $installerFilesByArchitecture
InstallerMetadataByArchitecture = $installerMetadataByArchitecture
CreatedAt = (Get-Date).ToString('o')
ArtifactDirectory = $artifactDirectory
DefinitionPath = [System.IO.Path]::GetFullPath($DefinitionPath)
DeployScriptPath = [System.IO.Path]::GetFullPath($DeployScriptPath)
EntryScriptName = $EntryScriptName
Package = $definition
Psadt = if ($psadtMetadata) {
[ordered]@{
Repository = $psadtMetadata.Repository
TagName = $psadtMetadata.TagName
ReleaseName = $psadtMetadata.ReleaseName
AssetName = $psadtMetadata.AssetName
Digest = $psadtMetadata.Digest
}
}
else {
[ordered]@{
Source = 'Local override'
ZipPath = $templateZipPath
}
}
Installer = if ($SkipInstallerDownload) {
[ordered]@{
Downloaded = $false
}
}
else {
[ordered]@{
Downloaded = $true
WingetId = $wingetMetadata.ResolvedId
RequestedId = $definition.winget_id
RequestedVersion = $WingetVersion
RequestedScope = $Scope
ResolvedVersion = $wingetMetadata.Version
InstallerType = $wingetMetadata.InstallerType
InstallerUrl = $wingetMetadata.InstallerUrl
InstallerSha256 = $wingetMetadata.InstallerSha256
InstallerFiles = @($installerDownload.InstallerFiles)
ManifestFiles = @($installerDownload.ManifestFiles)
}
}
}
$metadataPath = Join-Path $powerPackerSupportDirectory 'artifact-metadata.json'
$metadata | ConvertTo-Json -Depth 8 | Set-Content -LiteralPath $metadataPath
[pscustomobject]@{
ArtifactDirectory = $artifactDirectory
EntryScriptPath = $entryScriptPath
MetadataPath = $metadataPath
InstallerFiles = if ($installerDownload) { $installerDownload.InstallerFiles } else { @() }
ManifestFiles = if ($installerDownload) { $installerDownload.ManifestFiles } else { @() }
}
}
finally {
if (Test-Path -LiteralPath $tempDirectory) {
Remove-Item -LiteralPath $tempDirectory -Recurse -Force
}
}
}