-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathufbt-fm.ps1
115 lines (104 loc) · 5.25 KB
/
ufbt-fm.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
param ([Parameter(Mandatory)]$command, [Parameter(Mandatory)]$arg1, [switch]$prune)
Push-Location $PSScriptRoot
$ErrorActionPreference = "Stop"
function Get-UfbtState {
$ufbt_state_file = Join-Path (Resolve-Path "~/") ".ufbt/current/ufbt_state.json"
if (Test-Path $ufbt_state_file) {
return Get-Content $ufbt_state_file -Raw | ConvertFrom-Json
}
return $null
}
if ($command -eq 'use') {
$firmwares_raw_text = Get-Content (Resolve-Path './firmwares.json') -Raw
$firmwares = $firmwares_raw_text | ConvertFrom-Json
$matching_firmware = ($firmwares.firmwares | Where-Object { $_.alias -contains $arg1 })[0]
$ufbt_command = ''
if (!$matching_firmware) {
throw "Unable to recognize which firmware to use"
}
if ($matching_firmware.type -eq 'index') {
$ufbt_command = "ufbt update --index-url=$($matching_firmware.index_url) --channel=$($matching_firmware.channel)"
}
elseif ($matching_firmware.type -eq 'git-action') {
$workflow_runs_uri = "https://api.github.com/repos/$($matching_firmware.git_repo)/actions/workflows/$($matching_firmware.git_action)/runs?per_page=1&status=success"
if ($matching_firmware.git_branch) {
$workflow_runs_uri += "&branch=$($matching_firmware.git_branch)"
}
if ($matching_firmware.git_event) {
$workflow_runs_uri += "&event=$($matching_firmware.git_event)"
}
$workflow_runs = Invoke-RestMethod -Uri $workflow_runs_uri
$last_success_run = $workflow_runs.workflow_runs | Select-Object -Index 0
$artifact_dir = Join-Path (Resolve-Path "~/") ".ufbt/.ufbt_fm/$($matching_firmware.git_repo)/$($matching_firmware.git_action)/$($matching_firmware.git_branch)"
$sdk_zip_path = Join-Path $artifact_dir "sdk-$($last_success_run.id).zip"
if (-not (Test-Path -Path $sdk_zip_path -PathType Leaf)) {
New-Item -ItemType Directory -Path $artifact_dir -Force
$artifacts = Invoke-RestMethod -Uri $last_success_run.artifacts_url
$artifact = $artifacts.artifacts | Where-Object { $_.name -match $matching_firmware.git_artifact } | Select-Object -Index 0
Write-Host "New Github action artifacts found ($($last_success_run.html_url))"
if ($prune) {
Remove-Item -Path "$artifact_dir/*" -Recurse -Force
}
$artifact_archive_path = Join-Path $artifact_dir "artifact.zip"
Write-Host "Downloading Github action artifacts"
$artifact_download_uri = "https://nightly.link/$($matching_firmware.git_repo)/suites/$($last_success_run.check_suite_id)/artifacts/$($artifact.id)"
try {
$process = Start-Process -FilePath "curl" -ArgumentList $artifact_download_uri, "-L", "-o", $artifact_archive_path -Wait -NoNewWindow -PassThru
if ($process.ExitCode -ne 0) {
throw "cURL exited with non-zero code"
}
}
catch {
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $artifact_download_uri -OutFile $artifact_archive_path -UseBasicParsing
}
Write-Host "Extracting Github action artifacts"
$sdk_filename_pattern = $matching_firmware.sdk_filename_pattern
if (-not ($sdk_filename_pattern)) {
$sdk_filename_pattern = "sdk.*\.zip$"
}
Add-Type -Assembly System.IO.Compression.FileSystem
$zip_archive = [System.IO.Compression.ZipFile]::OpenRead($artifact_archive_path)
try {
$sdk_zip_entry = $zip_archive.Entries | Where-Object { $_.Name -match $sdk_filename_pattern } | Select-Object -Index 0
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($sdk_zip_entry, $sdk_zip_path, $true)
}
finally {
$zip_archive.Dispose()
Remove-Item $artifact_archive_path -Force
}
}
$ufbt_state = Get-UfbtState
if ($ufbt_state.file_path -ne $sdk_zip_path) {
$ufbt_command = "ufbt update `"--local=$sdk_zip_path`" --hw-target=f7"
}
}
else {
$sdk_uri = ''
if ($matching_firmware.type -eq 'git-release') {
$sdk_filename_pattern = $matching_firmware.sdk_filename_pattern
if (-not ($sdk_filename_pattern)) {
$sdk_filename_pattern = "sdk.*\.zip$"
}
$release_info = Invoke-RestMethod -Uri "https://api.github.com/repos/$($matching_firmware.git_repo)/releases/$($matching_firmware.git_release)"
$sdk_uri = ($release_info.assets | Where-Object { $_.name -match $sdk_filename_pattern } | Select-Object -Index 0).browser_download_url
}
elseif ($matching_firmware.type -eq 'direct-uri') {
$sdk_uri = $matching_firmware.uri
}
if ($sdk_uri) {
$ufbt_state = Get-UfbtState
if ($ufbt_state.url -ne $sdk_uri) {
$ufbt_command = "ufbt update `"--url=$sdk_uri`" --hw-target=f7"
}
}
}
if ($ufbt_command) {
Invoke-Expression $ufbt_command
}
Write-Host "Updated `"$($matching_firmware.description)`" firmware for uFBT"
}
else {
throw "Unknown command"
}
Pop-Location