Skip to content

Commit d779a7d

Browse files
haatabenmusson
andauthored
Add BinMod.exe support (#15)
* Cleanup whitespace * Add BinMod.exe support - Uses the release tag of ahk2exe to find the correct BinMod.ahk to download * This file is not bundled with the release, so we need to dig a bit more to download it directly from the source tree * Determining the 'latest' tag requires an additional request (as we need the actual tag name to retrieve BinMod.ahk) - Install BinMode.exe along side ahk2exe and upx (where it's expected to be) * pr revisions - Use shared function for getting release information - Check for previously installed BinMod - Validate BinMod compilation - Add tests --------- Co-authored-by: Ben Musson <ben.d.musson@gmail.com>
1 parent 5831750 commit d779a7d

File tree

4 files changed

+155
-27
lines changed

4 files changed

+155
-27
lines changed

.github/workflows/test.yml

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ jobs:
100100
ahk-tag: v1.1.37.02
101101
github-token: ${{ secrets.GITHUB_TOKEN }}
102102

103-
- name: Test v1.1 x64
103+
- name: Test v1.1 x86
104104
uses: ./
105105
with:
106106
in: .\testing\v1.1.ahk
@@ -111,6 +111,28 @@ jobs:
111111
ahk-tag: v1.1.37.02
112112
github-token: ${{ secrets.GITHUB_TOKEN }}
113113

114+
- name: Test v2.0 w/ BinMod
115+
uses: ./
116+
with:
117+
in: .\testing\v2.0_binmod.ahk
118+
out: .\output\v2.0_binmod_x64.exe
119+
icon: .\testing\icons\test.ico
120+
compression: upx
121+
target: x64
122+
github-token: ${{ secrets.GITHUB_TOKEN }}
123+
124+
- name: Test v1.1 w/ BinMod
125+
uses: ./
126+
with:
127+
in: .\testing\v1.1_binmod.ahk
128+
out: .\output\v1.1_binmod_x64.exe
129+
icon: .\testing\icons\test.ico
130+
compression: upx
131+
target: x64
132+
ahk-tag: v1.1.37.02
133+
github-token: ${{ secrets.GITHUB_TOKEN }}
134+
135+
114136
- name: Check for Failed Test
115137
run: |
116138
if(
@@ -122,7 +144,9 @@ jobs:
122144
[System.IO.File]::Exists('.\output\v2.0_x86_resourceid.exe') &&
123145
[System.IO.File]::Exists('.\output\v2.0_x64_tagged.exe') &&
124146
[System.IO.File]::Exists('.\output\v1.1_x86.exe') &&
125-
[System.IO.File]::Exists('.\output\v1.1_x64.exe')
147+
[System.IO.File]::Exists('.\output\v1.1_x64.exe') &&
148+
[System.IO.File]::Exists('.\output\v2.0_x64_binmod.exe') &&
149+
[System.IO.File]::Exists('.\output\v1.1_x64_binmod.exe')
126150
) {
127151
Write-Output "$($PSStyle.Foreground.Green)All Tests Passed!$($PSStyle.Reset)"
128152
} else {

action.ps1

Lines changed: 119 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,35 @@ function Show-Message {
3333
Write-Host "$message_style$message$($PSStyle.Reset)"
3434
}
3535

36+
function Get-GitHubReleaseInformation {
37+
param (
38+
[string]$Repository,
39+
[string]$ReleaseTag = 'latest'
40+
)
41+
42+
$repositoryOwner, $repositoryName = ($Repository -split "/")[0, 1]
43+
if ([string]::IsNullOrEmpty($repositoryOwner)) { Throw "Invalid repository path, missing repository owner."}
44+
if ([string]::IsNullOrEmpty($repositoryName)) { Throw "Invalid repository path, missing repository name."}
45+
46+
$displayPath = "$repositoryOwner/$repositoryName/$ReleaseTag"
47+
$previousHeader = Set-MessageHeader "Query-$displayPath"
48+
49+
if ($ReleaseTag -like 'latest') {
50+
$apiUrl = "https://api.github.com/repos/$repositoryOwner/$repositoryName/releases/latest"
51+
} else {
52+
$apiUrl = "https://api.github.com/repos/$repositoryOwner/$repositoryName/releases/tags/$ReleaseTag"
53+
}
54+
55+
Show-Message "Getting release information..." $StyleAction
56+
$headers = @{ "User-Agent" = "PowerShell-ahk2exe-action" }
57+
if (![string]::IsNullOrEmpty($env:GitHubToken)) { $headers["Authorization"] = "token $env:GitHubToken" }
58+
59+
$release = Invoke-RestMethod -Uri $apiUrl -Method Get -Headers $headers
60+
61+
[void](Set-MessageHeader $previousHeader)
62+
return $release
63+
}
64+
3665
function Get-GitHubReleaseAssets {
3766
param (
3867
[string]$Repository,
@@ -49,7 +78,7 @@ function Get-GitHubReleaseAssets {
4978

5079
$downloadFolderName = $displayPath.Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
5180
$downloadFolder = Join-Path $PathDownloads $downloadFolderName
52-
if (Test-Path -Path $downloadFolder) {
81+
if (Test-Path -Path $downloadFolder) {
5382
if ((Get-ChildItem -Path "$downloadFolder" | Measure-Object).Count -gt 0) {
5483
Show-Message "$displayPath is already present, skipping re-download..." $StyleQuiet
5584

@@ -58,18 +87,9 @@ function Get-GitHubReleaseAssets {
5887
}
5988
}
6089

61-
if ($ReleaseTag -like 'latest') {
62-
$apiUrl = "https://api.github.com/repos/$repositoryOwner/$repositoryName/releases/latest"
63-
} else {
64-
$apiUrl = "https://api.github.com/repos/$repositoryOwner/$repositoryName/releases/tags/$ReleaseTag"
65-
}
6690

67-
Show-Message "Getting release information..." $StyleAction
68-
$headers = @{ "User-Agent" = "PowerShell-ahk2exe-action" }
69-
if (![string]::IsNullOrEmpty($env:GitHubToken)) { $headers["Authorization"] = "token $env:GitHubToken" }
91+
$release = Get-GitHubReleaseInformation -Repository $Repository -ReleaseTag $ReleaseTag
7092

71-
$release = Invoke-RestMethod -Uri $apiUrl -Method Get -Headers $headers
72-
7393
$assets = $release.assets
7494
if ($assets.Count -eq 0) { Throw "No assets found for release '$displayPath'" }
7595

@@ -99,6 +119,48 @@ function Get-GitHubReleaseAssets {
99119
return $downloadFolder
100120
}
101121

122+
function Get-GitHubReleaseFile {
123+
param(
124+
[string]$Repository,
125+
[string]$ReleaseTag = 'latest',
126+
[string]$FileName
127+
)
128+
129+
$repositoryOwner, $repositoryName = ($Repository -split "/")[0, 1]
130+
if ([string]::IsNullOrEmpty($repositoryOwner)) { Throw "Invalid repository path, missing repository owner."}
131+
if ([string]::IsNullOrEmpty($repositoryName)) { Throw "Invalid repository path, missing repository name."}
132+
133+
$displayPath = "$repositoryOwner/$repositoryName/$ReleaseTag/$FileName"
134+
$previousHeader = Set-MessageHeader "Download-$displayPath"
135+
136+
$downloadFolderName = $displayPath.Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
137+
$downloadFolder = Join-Path $PathDownloads $downloadFolderName
138+
$downloadDestination = Join-Path $downloadFolder $FileName
139+
if (Test-Path -Path $downloadFolder) {
140+
if ((Get-ChildItem -Path "$downloadFolder" | Measure-Object).Count -gt 0) {
141+
Show-Message "$displayPath is already present, skipping re-download..." $StyleQuiet
142+
143+
[void](Set-MessageHeader $previousHeader)
144+
return $downloadDestination
145+
}
146+
}
147+
148+
$release = Get-GitHubReleaseInformation -Repository $Repository -ReleaseTag $ReleaseTag
149+
$tag = $release.tag_name
150+
151+
$downloadUrl = " https://raw.githubusercontent.com/$repositoryOwner/$repositoryName/refs/tags/$tag/$FileName"
152+
153+
$previousHeaderAsset = Set-MessageHeader "File-$FileName"
154+
Show-Message "Downloading..." $StyleAction
155+
Show-Message "Source: $downloadUrl" $StyleCommand
156+
Show-Message "Destination: $downloadDestination" $StyleCommand
157+
[void](New-Item -ItemType Directory -Path $downloadFolder -Force)
158+
[void](New-Object System.Net.WebClient).DownloadFile($downloadUrl, $downloadDestination)
159+
Show-Message "Download completed" $StyleStatus
160+
161+
return $downloadDestination
162+
}
163+
102164
function Invoke-UnzipAllInPlace {
103165
param (
104166
[string]$FolderPath
@@ -115,24 +177,24 @@ function Invoke-UnzipAllInPlace {
115177

116178
function Install-AutoHotkey {
117179
$previousHeader = Set-MessageHeader "Install-Autohotkey"
118-
180+
119181
Show-Message "Installing..." $StyleAction
120182
$downloadFolder = Get-GitHubReleaseAssets -Repository "$env:AutoHotkeyRepo" -ReleaseTag "$env:AutoHotkeyTag" -FileTypeFilter "*.zip"
121183

122184
switch ($env:Target) {
123-
'x64' {
124-
$exeName = 'AutoHotkey64.exe'
185+
'x64' {
186+
$exeName = 'AutoHotkey64.exe'
125187
$searchFilter = 'AutoHotkey(U)?64\.exe'
126188
}
127-
'x86' {
128-
$exeName = 'AutoHotkey32.exe'
189+
'x86' {
190+
$exeName = 'AutoHotkey32.exe'
129191
$searchFilter = 'AutoHotkey(U)?32\.exe'
130192
}
131193
Default { Throw "Unsupported Architecture: '$target'. Valid Options: x64, x86" }
132194
}
133195

134196
$installPath = (Get-ChildItem -Path $downloadFolder -Recurse | Where-Object { $_.Name -match "^$searchFilter$" } | Select-Object -First 1)
135-
if ([System.IO.File]::Exists($installPath)) {
197+
if ([System.IO.File]::Exists($installPath)) {
136198
Show-Message "Autohotkey is already installed, skipping re-installation..." $StyleQuiet
137199

138200
[void](Set-MessageHeader $previousHeader)
@@ -160,7 +222,7 @@ function Install-Ahk2Exe {
160222
$exeName = 'Ahk2Exe.exe'
161223

162224
$installPath = (Get-ChildItem -Path $downloadFolder -Recurse -Filter $exeName | Select-Object -First 1)
163-
if ([System.IO.File]::Exists($installPath)) {
225+
if ([System.IO.File]::Exists($installPath)) {
164226
Show-Message "Ahk2Exe is already installed, skipping re-installation..." $StyleQuiet
165227

166228
[void](Set-MessageHeader $previousHeader)
@@ -179,6 +241,36 @@ function Install-Ahk2Exe {
179241
return $installPath
180242
}
181243

244+
function Install-BinMod {
245+
param (
246+
[string]$Ahk2ExePath
247+
)
248+
$previousHeader = Set-MessageHeader "Install-BinMod"
249+
250+
Show-Message "Installing..." $StyleAction
251+
$downloadFile = Get-GitHubReleaseFile -Repository "$env:Ahk2ExeRepo" -ReleaseTag "$env:Ahk2ExeTag" -FileName "BinMod.ahk"
252+
253+
$exeName = 'BinMod.exe'
254+
$ahk2exeFolder = Split-Path -Path $Ahk2ExePath -Parent
255+
$installPath = Join-Path $ahk2exeFolder $exeName
256+
if ([System.IO.File]::Exists($installPath)) {
257+
Show-Message "BinMod is already installed, skipping re-installation..." $StyleQuiet
258+
259+
[void](Set-MessageHeader $previousHeader)
260+
return
261+
}
262+
263+
Invoke-Ahk2Exe -Path "$ahk2exePath" -Base "$ahk2exePath" -In "$downloadFile" -Out "$installPath" -Icon "" -Compression "none" -ResourceId ""
264+
265+
Show-Message "Verifying installation..." $StyleAction
266+
if (![System.IO.File]::Exists($installPath)) { Throw "Missing BinMod Executable '$exeName'." }
267+
Show-Message "Installation path: $installPath" $StyleCommand
268+
Show-Message "Installation completed" $StyleStatus
269+
270+
[void](Set-MessageHeader $previousHeader)
271+
return $installPath
272+
}
273+
182274
function Install-UPX {
183275
param (
184276
[string]$Ahk2ExePath
@@ -190,7 +282,7 @@ function Install-UPX {
190282
$downloadFolder = Get-GitHubReleaseAssets -Repository "$env:UPXRepo" -ReleaseTag "$env:UPXTag" -FileTypeFilter "*win64.zip"
191283

192284
$exeName = 'upx.exe'
193-
$ahk2exeFolder = Split-Path -Path $Ahk2ExePath -Parent
285+
$ahk2exeFolder = Split-Path -Path $Ahk2ExePath -Parent
194286

195287
$installPath = Join-Path $ahk2exeFolder $exeName
196288
if ([System.IO.File]::Exists($installPath)) {
@@ -239,13 +331,13 @@ function Invoke-Ahk2Exe {
239331

240332
Switch ($compression) {
241333
'none' { $ahk2exe_args += " /compress 0" }
242-
'upx' { $ahk2exe_args += " /compress 2" }
334+
'upx' { $ahk2exe_args += " /compress 2" }
243335
Default { Throw "Unsupported Compression Type: '$compression'. Valid Options: none, upx"}
244336
}
245337

246-
if (![string]::IsNullOrEmpty($Out)) {
338+
if (![string]::IsNullOrEmpty($Out)) {
247339
[void](New-Item -Path $Out -ItemType File -Force)
248-
$ahk2exe_args += " /out `"$Out`""
340+
$ahk2exe_args += " /out `"$Out`""
249341
}
250342
$ahk2exe_args += if (![string]::IsNullOrEmpty($Icon)) { " /icon `"$Icon`"" }
251343
$ahk2exe_args += if (![string]::IsNullOrEmpty($ResourceId)) { " /resourceid `"$ResourceId`"" }
@@ -267,13 +359,15 @@ function Invoke-Ahk2Exe {
267359
function Invoke-Action {
268360
Show-Message "Starting..." $StyleAction
269361

270-
if ([string]::IsNullOrEmpty($env:GitHubToken)) {
271-
Show-Message "GitHubToken environment variable is not set. API calls may be rate limited." $StyleAlert
362+
if ([string]::IsNullOrEmpty($env:GitHubToken)) {
363+
Show-Message "GitHubToken environment variable is not set. API calls may be rate limited." $StyleAlert
272364
}
273365

274366
$ahkPath = Install-AutoHotkey
275367
$ahk2exePath = Install-Ahk2Exe
276368

369+
[void](Install-BinMod -Ahk2ExePath $ahk2exePath)
370+
277371
if ("$env:Compression" -eq "upx") {
278372
[void](Install-UPX -Ahk2ExePath $ahk2exePath)
279373
}
@@ -282,4 +376,4 @@ function Invoke-Action {
282376
Show-Message "Finished" $StyleStatus
283377
}
284378

285-
Invoke-Action
379+
Invoke-Action

testing/v1.1_binmod.ahk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#Requires AutoHotkey v1.1
2+
3+
;@Ahk2Exe-PostExec BinMod.exe "%A_WorkFileName%"
4+
5+
MsgBox "Compiled using benmusson/ahk2exe-action."

testing/v2.0_binmod.ahk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#Requires AutoHotkey v2.0
2+
3+
;@Ahk2Exe-PostExec BinMod.exe "%A_WorkFileName%"
4+
5+
MsgBox "Compiled using benmusson/ahk2exe-action."

0 commit comments

Comments
 (0)