1 |
#!/usr/bin/env powershell |
#!/usr/bin/env powershell |
2 |
|
|
3 |
$blazeDir = (Join-Path (Get-Location) ".blaze").Replace("\", "/") |
$projectdir = Split-Path -parent $MyInvocation.MyCommand.Definition |
4 |
$blazewDir = (Join-Path (Get-Location) "blaze/wrapper").Replace("\", "/") |
$tmp_dir = $projectdir + "/.blaze" |
5 |
$propertiesFile = Join-Path $blazewDir "blaze_wrapper.properties" |
$blazew_dir = $projectdir + "/blaze/wrapper" |
6 |
$wrapperJSFile = Join-Path $blazewDir "blaze_wrapper.js" |
$blazew_properties = $blazew_dir + "/blaze_wrapper.properties" |
7 |
$bunBinDir = Join-Path $blazeDir "bun/bin" |
$bun_path = $tmp_dir + "/bun/bin/bun" |
8 |
$bunPath = Join-Path $bunBinDir "bun.exe" |
|
9 |
$debugMode = $false |
enum LogLevel { |
10 |
|
Info |
11 |
if ($env:BLAZEW_DEBUG -eq "1") { |
Warn |
12 |
$debugMode = $true |
Error |
13 |
} |
Debug |
14 |
|
} |
15 |
function Debug-Log { |
|
16 |
param([string]$message) |
function Write-Log { |
17 |
|
param ( |
18 |
if ($debugMode) { |
[string]$message, |
19 |
Write-Host "[debug] $message" |
[LogLevel]$type = [LogLevel]::Info |
20 |
|
) |
21 |
|
|
22 |
|
if ($type -eq [LogLevel]::Debug -and $env:BLAZEW_DEBUG -ne "1") { |
23 |
|
return |
24 |
|
} |
25 |
|
|
26 |
|
switch ($type) { |
27 |
|
Info { |
28 |
|
Write-Host "info " -NoNewline -ForegroundColor Green |
29 |
|
Write-Host "$message" |
30 |
|
} |
31 |
|
Warn { |
32 |
|
Write-Host "warn " -NoNewline -ForegroundColor Yellow |
33 |
|
Write-Warning "$message" |
34 |
|
} |
35 |
|
Error { |
36 |
|
Write-Host "error " -NoNewline -ForegroundColor Red |
37 |
|
Write-Error "$message" |
38 |
|
} |
39 |
|
Debug { |
40 |
|
Write-Host "debug " -NoNewline -ForegroundColor Cyan |
41 |
|
Write-Verbose "$message" |
42 |
|
} |
43 |
} |
} |
44 |
} |
} |
45 |
|
|
46 |
function Get-Property { |
if (-not (Test-Path $blazew_properties)) { |
47 |
param([string]$key) |
Write-Log "blaze_wrapper.properties file could not be found in $blazew_dir. Are you sure this is BlazeBuild project?" |
48 |
|
exit 1 |
49 |
|
} |
50 |
|
|
51 |
$value = Get-Content $propertiesFile | Select-String -Pattern "$key=" | ForEach-Object { $_ -replace "$key=" } |
if (-not (Test-Path $tmp_dir)) { |
52 |
return $value |
New-Item -ItemType directory -Path $tmp_dir |
53 |
} |
} |
54 |
|
|
55 |
function Start-Blaze { |
function Get-BlazeProperty { |
56 |
if (-not (Test-Path $blazeDir)) { |
param ( |
57 |
Debug-Log "Creating .blaze/ directory" |
[string]$property |
58 |
New-Item -ItemType Directory -Path $blazeDir | Out-Null |
) |
|
} |
|
|
|
|
|
if (-not (Test-Path $blazewDir)) { |
|
|
Write-Error "blaze/wrapper/ directory not found. Please run this script from the root of a BlazeBuild project." |
|
|
exit 1 |
|
|
} |
|
59 |
|
|
60 |
if (-not (Test-Path $propertiesFile)) { |
$properties = Get-Content $blazew_properties |
61 |
Write-Error "blaze_wrapper.properties file not found. Please run this script from the root of a BlazeBuild project." |
$properties | ForEach-Object { |
62 |
exit 1 |
if ($_ -match "$property=(.*)") { |
63 |
|
$matches[1] |
64 |
|
} |
65 |
} |
} |
66 |
} |
} |
67 |
|
|
68 |
# These checks were taken from the Bun installation script. |
function Install-Bun() { |
69 |
function Test-Bun { |
Write-Log "Installing Bun $bun_version" |
70 |
$bunRevision = "$(& "${bunPath}" --revision)" |
$env:BUN_INSTALL = $tmp_dir + "/bun" |
71 |
|
$env:SHELL = "blazew" |
72 |
if ($LASTEXITCODE -eq 1073741795) { |
$env:PATH = $tmp_dir + "/bun/bin" + ";" + $env:PATH + ";" + $tmp_dir + "/bun/bin" |
73 |
# STATUS_ILLEGAL_INSTRUCTION |
& ([scriptblock]::Create((irm bun.sh/install.ps1))) -Version $bun_version -NoPathUpdate -NoRegisterInstallation -NoCompletions -DownloadWithoutCurl |
|
Write-Output "Install Failed - bun.exe is not compatible with your CPU. This should have been detected before downloading.`n" |
|
|
exit 1 |
|
|
} |
|
|
|
|
|
if (($LASTEXITCODE -eq 3221225781) -or ($LASTEXITCODE -eq -1073741515)) { |
|
|
# STATUS_DLL_NOT_FOUND |
|
|
Write-Output "Install Failed - You are missing a DLL required to run bun.exe" |
|
|
Write-Output "This can be solved by installing the Visual C++ Redistributable from Microsoft:`nSee https://learn.microsoft.com/cpp/windows/latest-supported-vc-redist`nDirect Download -> https://aka.ms/vs/17/release/vc_redist.x64.exe`n`n" |
|
|
Write-Output "The command '${bunPath} --revision' exited with code ${LASTEXITCODE}`n" |
|
|
exit 1 |
|
|
} |
|
74 |
|
|
75 |
if ($LASTEXITCODE -ne 0) { |
if ($LASTEXITCODE -ne 0) { |
76 |
Write-Output "Install Failed - could not verify bun.exe" |
Write-Log "Failed to install Bun $bun_version" Error |
|
Write-Output "The command '${bunPath} --revision' exited with code ${LASTEXITCODE}`n" |
|
77 |
exit 1 |
exit 1 |
78 |
} |
} |
|
|
|
|
return $bunRevision |
|
79 |
} |
} |
80 |
|
|
81 |
Start-Blaze |
function Check-Bun() { |
82 |
|
$bun_version = Get-BlazeProperty "bun.version" |
|
$bunVersion = Get-Property "bun.version" |
|
|
|
|
|
if (-not $bunVersion) { |
|
|
Write-Error "bun.version property not found or is empty in blaze_wrapper.properties file." |
|
|
exit 1 |
|
|
} |
|
|
|
|
|
if (Test-Path $bunPath) { |
|
|
Write-Host "Found bun installation at $bunPath" |
|
|
Test-Bun |
|
|
|
|
|
$version = & $bunPath --version |
|
83 |
|
|
84 |
if ($version -eq $bunVersion) { |
if (-not $bun_version) { |
85 |
Write-Host "Bun is up to date" |
Write-Log "bun.version property could not be found in $blazew_properties." |
86 |
# TODO |
exit 1 |
|
exit 0 |
|
87 |
} |
} |
|
} |
|
|
|
|
|
if (-not (Get-Command Get-CimInstance -ErrorAction SilentlyContinue)) { |
|
|
Write-Output "Cannot Install Bun" |
|
|
Write-Output "Bun for Windows requires PowerShell 3.0 or later.`n" |
|
|
exit 1 |
|
|
} |
|
|
|
|
|
if (-not ((Get-CimInstance Win32_ComputerSystem)).SystemType -match "x64-based") { |
|
|
Write-Output "Cannot Install Bun" |
|
|
Write-Output "Bun for Windows is currently only available for x86 64-bit Windows.`n" |
|
|
exit 1 |
|
|
} |
|
|
|
|
|
$bunDownloadURL = "https://github.com/oven-sh/bun/releases/download/bun-v$bunVersion/bun-windows-x64.zip" |
|
|
$zipPath = Join-Path $blazeDir "bun.zip" |
|
|
|
|
|
if (Test-Path $zipPath) { |
|
|
Remove-Item -Force $zipPath |
|
|
} |
|
88 |
|
|
89 |
Write-Host "Downloading Bun from $bunDownloadURL" |
Write-Log "Checking if Bun is already installed" Debug |
90 |
Invoke-WebRequest -Uri $bunDownloadURL -OutFile $zipPath |
|
91 |
|
if (-not (Test-Path $tmp_dir/bun/bin)) { |
92 |
$bunInstallPath = Join-Path $blazeDir "bun" |
Write-Log "Could not find Bun installation" |
93 |
|
Install-Bun |
94 |
if (Test-Path $bunInstallPath) { |
$current_version = & $bun_path --version |
95 |
Remove-Item -Recurse -Force $bunInstallPath |
Write-Log "Installed Bun $current_version" |
96 |
} |
} |
97 |
|
else { |
98 |
Write-Host "Installing Bun to $bunInstallPath" |
$current_version = & $bun_path --version |
99 |
Expand-Archive -Path $zipPath -DestinationPath $bunInstallPath |
|
100 |
Remove-Item -Force $zipPath |
if ($current_version -ne $bun_version) { |
101 |
Rename-Item -Path (Join-Path $bunInstallPath "bun-windows-x64") -NewName $bunBinDir |
Write-Log "Bun $current_version is installed, but required version is $bun_version" Warn |
102 |
|
Write-log "Reinstalling Bun $bun_version" Info |
103 |
Test-Bun |
Remove-Item -Recurse $tmp_dir/bun |
104 |
|
Install-Bun |
105 |
Write-Host "Bun installed successfully" |
$current_version = & $bun_path --version |
106 |
|
Write-Log "Installed Bun $current_version" |
107 |
# Prepare and execute the BlazeBuild wrapper |
} |
108 |
|
else { |
109 |
if (-not (Test-Path $wrapperJSFile)) { |
Write-Log "Bun $current_version is already installed and meets the requirements" Debug |
110 |
Write-Error "blaze_wrapper.js file not found. Please run this script from the root of a BlazeBuild project." |
} |
111 |
exit 1 |
} |
112 |
} |
} |
113 |
|
|
114 |
$env:Path = "$bunBinDir;$env:Path" |
Check-Bun |
115 |
|
|
|
$blazeBuildArgs = $args -join " " |
|
|
Debug-Log "Executing BlazeBuild with arguments: $blazeBuildArgs" |
|
|
& $bunPath run $wrapperJSFile $args |
|
|
exit $LASTEXITCODE |
|
116 |
|
& $bun_path $blazew_dir/blaze_wrapper.js $args |