-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathactrun.ps1
More file actions
167 lines (146 loc) · 5.1 KB
/
actrun.ps1
File metadata and controls
167 lines (146 loc) · 5.1 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
# actrun - Actionforge graph runner (PowerShell)
#
# This script is the PowerShell equivalent of:
# https://www.actionforge.dev/actrun.sh
#
# Usage:
# pwsh -Command "& ([scriptblock]::Create((irm https://www.actionforge.dev/actrun.ps1))) <file.act> [options]"
#
# Examples:
# pwsh -Command "& ([scriptblock]::Create((irm https://www.actionforge.dev/actrun.ps1))) hello-world.act"
# pwsh -Command "& ([scriptblock]::Create((irm https://www.actionforge.dev/actrun.ps1))) workflow.act --verbose"
# pwsh -Command "& ([scriptblock]::Create((irm https://www.actionforge.dev/actrun.ps1))) https://app.actionforge.dev/shared/wispy-paper-a49c664e.act"
# pwsh -Command "& ([scriptblock]::Create((irm https://www.actionforge.dev/actrun.ps1))) --help"
#
param(
[Parameter(ValueFromRemainingArguments = $true)]
[string[]]$Arguments
)
$ErrorActionPreference = 'Stop'
$DownloadBase = 'https://www.actionforge.dev/download'
$ApiUrl = 'https://app.actionforge.dev/api/v2/releases/list'
$ShareApiUrl = 'https://app.actionforge.dev/api/v2/share/graph/read'
$CacheDir = if ($env:LOCALAPPDATA) {
Join-Path $env:LOCALAPPDATA 'actrun'
} else {
Join-Path $HOME '.cache' 'actrun'
}
# Detect OS
if ($IsWindows -or $env:OS -eq 'Windows_NT') {
$OS = 'windows'
} elseif ($IsLinux) {
$OS = 'linux'
} elseif ($IsMacOS) {
$OS = 'macos'
} else {
Write-Error "Unsupported OS"
exit 1
}
# Detect architecture
$archRaw = if ($OS -eq 'windows') {
$env:PROCESSOR_ARCHITECTURE
} else {
(uname -m)
}
switch -Regex ($archRaw) {
'AMD64|x86_64|amd64' { $Arch = 'x64' }
'ARM64|arm64|aarch64' { $Arch = 'arm64' }
default { Write-Error "Unsupported architecture: $archRaw"; exit 1 }
}
# Get latest version from API (highest stable version)
$releases = Invoke-RestMethod -Uri $ApiUrl
$Version = $releases |
ForEach-Object { $_.version } |
Where-Object { $_ -match '^v\d+\.\d+\.\d+$' } |
Sort-Object { [version]($_ -replace '^v','') } |
Select-Object -Last 1
if (-not $Version) {
Write-Error "Failed to fetch latest version"
exit 1
}
# Check cache
$CacheVersionDir = Join-Path $CacheDir $Version
$BinaryName = if ($OS -eq 'windows') { 'actrun.exe' } else { 'actrun' }
$CachedBinary = Join-Path $CacheVersionDir $BinaryName
# Helper: resolve shared URL
function Invoke-SharedGraph {
param([string]$Url, [string]$Binary, [string[]]$Rest)
if ($Url -match '^https://app\.actionforge\.dev/shared/([a-zA-Z0-9_-]+\.act)$') {
$shareId = $Matches[1]
Write-Host "Fetching shared graph: $shareId"
$graphTmp = [System.IO.Path]::GetTempFileName() + '.act'
try {
Invoke-WebRequest -Uri "${ShareApiUrl}?id=${shareId}" -OutFile $graphTmp -UseBasicParsing
& $Binary $graphTmp @Rest
} finally {
Remove-Item -Force $graphTmp -ErrorAction SilentlyContinue
}
return $true
}
return $false
}
if (Test-Path $CachedBinary) {
Write-Host "Using cached actrun $Version"
if ($Arguments.Count -gt 0) {
$handled = Invoke-SharedGraph -Url $Arguments[0] -Binary $CachedBinary -Rest ($Arguments | Select-Object -Skip 1)
if (-not $handled) {
& $CachedBinary @Arguments
}
} else {
& $CachedBinary
}
return
}
# Determine file extension
$Ext = switch ($OS) {
'linux' { 'tar.gz' }
'windows' { 'zip' }
'macos' { 'pkg' }
}
$Filename = "actrun-${Version}.cli-${Arch}-${OS}.${Ext}"
$Url = "${DownloadBase}/${Filename}"
Write-Host "Downloading $Filename..."
$TmpDir = Join-Path ([System.IO.Path]::GetTempPath()) "actrun-$([guid]::NewGuid().ToString('N'))"
New-Item -ItemType Directory -Path $TmpDir -Force | Out-Null
try {
$DownloadPath = Join-Path $TmpDir $Filename
Invoke-WebRequest -Uri $Url -OutFile $DownloadPath -UseBasicParsing
Write-Host "Unpacking..."
switch ($OS) {
'linux' {
tar -xzf $DownloadPath -C $TmpDir
$Binary = Join-Path $TmpDir 'actrun'
}
'windows' {
Expand-Archive -Path $DownloadPath -DestinationPath $TmpDir -Force
$Binary = Join-Path $TmpDir 'actrun.exe'
}
'macos' {
$ExpandDir = Join-Path $TmpDir 'pkg_expanded'
pkgutil --expand-full $DownloadPath $ExpandDir
$Binary = Get-ChildItem -Path $ExpandDir -Recurse -Filter 'actrun' -File |
Select-Object -First 1 -ExpandProperty FullName
}
}
if (-not (Test-Path $Binary)) {
Write-Error "Failed to find actrun binary"
exit 1
}
# Cache the binary
New-Item -ItemType Directory -Path $CacheVersionDir -Force | Out-Null
Copy-Item -Path $Binary -Destination $CachedBinary -Force
if ($OS -ne 'windows') {
chmod +x $CachedBinary
}
Write-Host "Unpacked actrun $Version"
if ($Arguments.Count -gt 0) {
$handled = Invoke-SharedGraph -Url $Arguments[0] -Binary $CachedBinary -Rest ($Arguments | Select-Object -Skip 1)
if (-not $handled) {
& $CachedBinary @Arguments
}
} else {
& $CachedBinary
}
} finally {
Remove-Item -Recurse -Force $TmpDir -ErrorAction SilentlyContinue
}