-
Notifications
You must be signed in to change notification settings - Fork 40
/
trunk.ps1
72 lines (64 loc) · 2.53 KB
/
trunk.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
$ErrorActionPreference = 'Stop'
$Env:TRUNK_LAUNCHER_VERSION="1.2.4" # warning: this line is auto-updated
$Env:TRUNK_LAUNCHER_PATH = $PSCommandPath
# Try to get the version from trunk.yaml.
function TryGetTrunkVersion {
$currentDir = (Get-Location).Path
while ($currentDir -ne '') {
$trunkYamlPath = "$currentDir\.trunk\trunk.yaml"
if (Test-Path "$trunkYamlPath") {
$yamlContent = Get-Content "$trunkYamlPath" -Raw
return $yamlContent -replace '(?s).*version:\s*([0-9a-z.-]+).*','$1'
}
$currentDir = Split-Path $currentDir -Parent
}
return $null
}
# Get the latest version of trunk.
function GetLatestTrunkVersion {
$latestReleaseInfo = Invoke-RestMethod 'https://trunk.io/releases/latest' -UseBasicParsing
return $latestReleaseInfo -replace '(?s).*version:\s*([0-9a-z.-]+).*','$1'
}
# Download a particular version of trunk to the specified directory.
function DownloadTrunk($version, $trunkDir) {
$null = New-Item -Type Directory -Force -Path (Split-Path $trunkDir -Parent)
$guid = [System.Guid]::NewGuid()
$downloadPath = "$trunkDir.$guid.zip"
$destinationPath = "$trunkDir.$guid"
try {
# Download the zip file.
$zipFile = "trunk-$version.windows.zip"
$zipFilePath = "$tempDir\$zipFile"
$downloadUrl = "https://trunk.io/releases/$version/$zipFile"
Invoke-RestMethod -Uri $downloadUrl -OutFile $downloadPath
# Extract the zip file to a uniquely named directory.
Expand-Archive -Path $downloadPath -DestinationPath $destinationPath
# Remove the trunk directory if it already exists.
if (Test-Path $trunkDir) {
Remove-Item -Path $trunkDir -Recurse -Force
}
# Move the uniquely named trunk directory to the final location.
Move-Item -Path $destinationPath -Destination $trunkDir
}
finally {
# Cleanup.
Remove-Item -Path $downloadPath -Force -ea 0
Remove-Item -Path $destinationPath -Recurse -Force -ea 0
}
}
# Get the version to run.
$version = TryGetTrunkVersion
if ($version -eq $null) {
$version = GetLatestTrunkVersion
}
# Determine the expected path to trunk.exe.
$localApplicationData = [Environment]::GetFolderPath('LocalApplicationData')
$trunkDir = "$localApplicationData\trunk\cli\trunk-${version}-windows"
$trunkExe = "$trunkDir\trunk.exe"
# Download trunk.exe if it doesn't exist.
if (!(Test-Path $trunkExe)) {
DownloadTrunk $version $trunkDir
}
# Execute trunk with all arguments
& $trunkExe $args
exit $LastExitCode