-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.ps1
101 lines (78 loc) · 3.77 KB
/
build.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
#!/usr/bin/env pwsh
[CmdletBinding()]
param(
[Parameter()]
[switch] $DownloadRamMap,
[Parameter()]
[switch] $CompileExecutable,
[Parameter()]
[switch] $Force
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Import modules
try { Import-Module SmartLogging } catch { Install-Module SmartLogging -Scope CurrentUser -Force; Import-Module SmartLogging }
try { Import-Module Execution } catch { Install-Module Execution -Scope CurrentUser -Force; Import-Module Execution }
Set-ScriptArgs $MyInvocation.BoundParameters $MyInvocation.UnboundArguments
# Invoke-SelfElevation
function Get-FileFromUrl([string] $name, [string] $url) {
Log info "Download '$name' from '$url'..."
$fileName = [System.IO.Path]::GetFileName($url)
$downloadedFile = Join-Path $env:TEMP $fileName
Invoke-WebRequest -Uri $url -Method Get -OutFile $downloadedFile -UseBasicParsing
Log trace "Saved to '$downloadedFile'."
return $downloadedFile
}
function New-DirectoryIfNotExists([string] $directory) {
if (-not (Test-Path $directory -PathType Container)) {
Log trace "Directory '$directory' not exists. Create it..."
New-Item -Path $directory -ItemType Directory -Force:$Force.IsPresent > $null
}
}
try {
# Code goes here
$outputDir = Join-Path $PSScriptRoot '_build'
if ($DownloadRamMap) {
$ramMapDownloadUrl = 'https://live.sysinternals.com/RAMMap.exe'
$ramMapFileName = [System.IO.Path]::GetFileName($ramMapDownloadUrl)
New-DirectoryIfNotExists $outputDir
$ramMapDestination = Join-Path $outputDir $ramMapFileName
if ((Test-Path -Path $ramMapDestination) -and -not $Force.IsPresent) {
Log info "'$ramMapDestination' already exists."
} else {
$ramMapFile = Get-FileFromUrl -name 'RAMMap' -url $ramMapDownloadUrl
Move-Item -Path $ramMapFile -Destination $ramMapDestination -Force:$Force.IsPresent
Log info "Download complete: '$ramMapDestination'"
}
}
if (-not $CompileExecutable.IsPresent -or $CompileExecutable) {
$ahkDownloadUrl = 'https://www.autohotkey.com/download/ahk.zip'
$ahkPath = Join-Path $outputDir 'ahk'
New-DirectoryIfNotExists $ahkPath
$ahkCompilerFile = Join-Path $ahkPath 'Compiler/Ahk2Exe.exe'
$ahkBinaryFileName = if ([Environment]::Is64BitOperatingSystem) { 'Unicode 64-bit.bin' } else { 'Unicode 32-bit.bin' }
$ahkBinaryFile = Join-Path $ahkPath "Compiler/$ahkBinaryFileName"
if ((Test-Path -Path $ahkCompilerFile) -and (Test-Path -Path $ahkBinaryFile) -and -not $Force.IsPresent) {
Log info 'AutoHotKey compiler already exists.'
} else {
$ahkFile = Get-FileFromUrl -name 'AutoHotKey' -url $ahkDownloadUrl
Expand-Archive -Path $ahkFile -DestinationPath $ahkPath -Force:$Force.IsPresent
Log info "Download complete: '$ahkPath'"
}
$ahkSrcFile = Join-Path $PSScriptRoot 'src/rammap-cleanup.ahk'
$ahkSrcFileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($ahkSrcFile)
$exeFile = Join-Path $outputDir "$ahkSrcFileNameWithoutExtension.exe"
Start-NativeExecution $ahkCompilerFile /in "$ahkSrcFile" /out "$exeFile" /bin $ahkBinaryFile /mpress 1 /cp 65001
Log info "Compilation complete: '$exeFile'"
}
Log info "Successfully"
Exit-WithAndWaitOnExplorer 0
} catch {
Log error "Something went wrong: $_"
Log trace "Exception: $($_.Exception)"
Log trace "StackTrace: $($_.ScriptStackTrace)"
Exit-WithAndWaitOnExplorer 1
} finally {
# Cleanup goes here
}