-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-release-metadata.ps1
95 lines (69 loc) · 2.55 KB
/
update-release-metadata.ps1
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
<#
.SYNOPSIS
Create the metadata JSON for a release in a checkout of the OpenTabletDriver/Plugin-Repository repo.
.PARAMETER GitHubToken
GitHub API token
.PARAMETER PluginRepositoryPath
Path to a checkout of OpenTabletDriver/Plugin-Repository
.EXAMPLE
In A GitHub Actions workflow:
./update-release-metadata.ps1 -GitHubToken ${{github.token}} -PluginRepositoryPath ..\Plugin-Repository
#>
[cmdletbinding()]
param(
[string] $GitHubToken="",
[string] $PluginRepositoryPath
)
$Repo = "OpenKneeboard/OTD-IPC"
$AssetPrefix = "OpenKneeboard-OTD-IPC-"
$OutputName = "$($Repo.Split('/')[-1]).json"
$DownloadHeaders = @{
"User-Agent" = "${Repo} - $((Get-Item $PSCommandPath).Name) v0.0.0-filehash.$((Get-FileHash $PSCommandPath).Hash)";
}
$ApiHeaders = $DownloadHeaders + @{
"Accept" = "application/vnd.github+json";
"X-GitHub-Api-Version" = "2022-11-28";
}
if ("${GitHubToken}" -ne "") {
$ApiHeaders['Authorization'] = $GitHubToken
$DownloadHeaders['Authorization'] = $GitHubToken
}
$Release = (Invoke-WebRequest -Uri https://api.github.com/repos/${Repo}/releases -Headers $ApiHeaders).Content
| ConvertFrom-Json
| Where-Object -Not -Property 'prerelease'
| Sort-Object -Descending -Property { [System.Management.Automation.SemanticVersion] ($_.tag_name -replace '^v' -replace 'beta','beta.') }
| Select-Object -First 1
$FilePattern = "${AssetPrefix}$($Release.tag_name)*.zip"
$Asset = $Release.assets
| Where-Object -FilterScript { $_.name -like $FilePattern }
$DownloadUrl = $Asset.browser_download_url
$DownloadPath = Join-Path $env:TEMP $Asset.name
Invoke-WebRequest `
-Uri $DownloadUrl `
-Headers $DownloadHeaders `
-OutFile $DownloadPath
$BaseName = (Get-Item $DownloadPath).BaseName
$Extracted = Join-Path $env:TEMP $BaseName
Expand-Archive -Path $DownloadPath -DestinationPath $Extracted
$Metadata = Get-Content (Join-Path $Extracted "metadata.json")
| ConvertFrom-Json -AsHashtable
$Metadata += @{
"DownloadUrl" = $DownloadUrl;
"CompressionFormat" = "zip";
"SHA256" = (Get-FileHash -Algorithm SHA256 -Path $DownloadPath).Hash.ToLower()
}
Write-Output "Release Metadata:"
Write-Output $Metadata
$OutputDirectory = Join-Path `
(Get-Item $PluginRepositoryPath).FullName `
"Repository" `
$Metadata.SupportedDriverVersion `
$Repo
$OutputPath = Join-Path $OutputDirectory $OutputName
Write-Output "`nOutput path: ${OutputPath}"
if (-not (Test-Path $OutputDirectory)) {
New-Item -Path $OutputDirectory -ItemType Directory | Out-Null
}
$Metadata
| ConvertTo-Json -Depth 8
| Out-File -Encoding utf8NoBOM -FilePath $OutputPath