--- trunk/blazew.ps1 2024/09/07 14:52:39 628 +++ trunk/blazew.ps1 2024/09/07 14:55:53 629 @@ -1,145 +1,116 @@ #!/usr/bin/env powershell -$blazeDir = (Join-Path (Get-Location) ".blaze").Replace("\", "/") -$blazewDir = (Join-Path (Get-Location) "blaze/wrapper").Replace("\", "/") -$propertiesFile = Join-Path $blazewDir "blaze_wrapper.properties" -$wrapperJSFile = Join-Path $blazewDir "blaze_wrapper.js" -$bunBinDir = Join-Path $blazeDir "bun/bin" -$bunPath = Join-Path $bunBinDir "bun.exe" -$debugMode = $false - -if ($env:BLAZEW_DEBUG -eq "1") { - $debugMode = $true -} - -function Debug-Log { - param([string]$message) - - if ($debugMode) { - Write-Host "[debug] $message" +$projectdir = Split-Path -parent $MyInvocation.MyCommand.Definition +$tmp_dir = $projectdir + "/.blaze" +$blazew_dir = $projectdir + "/blaze/wrapper" +$blazew_properties = $blazew_dir + "/blaze_wrapper.properties" +$bun_path = $tmp_dir + "/bun/bin/bun" + +enum LogLevel { + Info + Warn + Error + Debug +} + +function Write-Log { + param ( + [string]$message, + [LogLevel]$type = [LogLevel]::Info + ) + + if ($type -eq [LogLevel]::Debug -and $env:BLAZEW_DEBUG -ne "1") { + return + } + + switch ($type) { + Info { + Write-Host "info " -NoNewline -ForegroundColor Green + Write-Host "$message" + } + Warn { + Write-Host "warn " -NoNewline -ForegroundColor Yellow + Write-Warning "$message" + } + Error { + Write-Host "error " -NoNewline -ForegroundColor Red + Write-Error "$message" + } + Debug { + Write-Host "debug " -NoNewline -ForegroundColor Cyan + Write-Verbose "$message" + } } } -function Get-Property { - param([string]$key) +if (-not (Test-Path $blazew_properties)) { + Write-Log "blaze_wrapper.properties file could not be found in $blazew_dir. Are you sure this is BlazeBuild project?" + exit 1 +} - $value = Get-Content $propertiesFile | Select-String -Pattern "$key=" | ForEach-Object { $_ -replace "$key=" } - return $value +if (-not (Test-Path $tmp_dir)) { + New-Item -ItemType directory -Path $tmp_dir } -function Start-Blaze { - if (-not (Test-Path $blazeDir)) { - Debug-Log "Creating .blaze/ directory" - 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 - } +function Get-BlazeProperty { + param ( + [string]$property + ) - if (-not (Test-Path $propertiesFile)) { - Write-Error "blaze_wrapper.properties file not found. Please run this script from the root of a BlazeBuild project." - exit 1 + $properties = Get-Content $blazew_properties + $properties | ForEach-Object { + if ($_ -match "$property=(.*)") { + $matches[1] + } } } -# These checks were taken from the Bun installation script. -function Test-Bun { - $bunRevision = "$(& "${bunPath}" --revision)" - - if ($LASTEXITCODE -eq 1073741795) { - # STATUS_ILLEGAL_INSTRUCTION - 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 - } +function Install-Bun() { + Write-Log "Installing Bun $bun_version" + $env:BUN_INSTALL = $tmp_dir + "/bun" + $env:SHELL = "blazew" + $env:PATH = $tmp_dir + "/bun/bin" + ";" + $env:PATH + ";" + $tmp_dir + "/bun/bin" + & ([scriptblock]::Create((irm bun.sh/install.ps1))) -Version $bun_version -NoPathUpdate -NoRegisterInstallation -NoCompletions -DownloadWithoutCurl if ($LASTEXITCODE -ne 0) { - Write-Output "Install Failed - could not verify bun.exe" - Write-Output "The command '${bunPath} --revision' exited with code ${LASTEXITCODE}`n" + Write-Log "Failed to install Bun $bun_version" Error exit 1 } - - return $bunRevision } -Start-Blaze - -$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 +function Check-Bun() { + $bun_version = Get-BlazeProperty "bun.version" - if ($version -eq $bunVersion) { - Write-Host "Bun is up to date" - # TODO - exit 0 + if (-not $bun_version) { + Write-Log "bun.version property could not be found in $blazew_properties." + exit 1 } -} - -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 -} -Write-Host "Downloading Bun from $bunDownloadURL" -Invoke-WebRequest -Uri $bunDownloadURL -OutFile $zipPath - -$bunInstallPath = Join-Path $blazeDir "bun" - -if (Test-Path $bunInstallPath) { - Remove-Item -Recurse -Force $bunInstallPath -} - -Write-Host "Installing Bun to $bunInstallPath" -Expand-Archive -Path $zipPath -DestinationPath $bunInstallPath -Remove-Item -Force $zipPath -Rename-Item -Path (Join-Path $bunInstallPath "bun-windows-x64") -NewName $bunBinDir - -Test-Bun - -Write-Host "Bun installed successfully" - -# Prepare and execute the BlazeBuild wrapper - -if (-not (Test-Path $wrapperJSFile)) { - Write-Error "blaze_wrapper.js file not found. Please run this script from the root of a BlazeBuild project." - exit 1 + Write-Log "Checking if Bun is already installed" Debug + + if (-not (Test-Path $tmp_dir/bun/bin)) { + Write-Log "Could not find Bun installation" + Install-Bun + $current_version = & $bun_path --version + Write-Log "Installed Bun $current_version" + } + else { + $current_version = & $bun_path --version + + if ($current_version -ne $bun_version) { + Write-Log "Bun $current_version is installed, but required version is $bun_version" Warn + Write-log "Reinstalling Bun $bun_version" Info + Remove-Item -Recurse $tmp_dir/bun + Install-Bun + $current_version = & $bun_path --version + Write-Log "Installed Bun $current_version" + } + else { + Write-Log "Bun $current_version is already installed and meets the requirements" Debug + } + } } -$env:Path = "$bunBinDir;$env:Path" +Check-Bun -$blazeBuildArgs = $args -join " " -Debug-Log "Executing BlazeBuild with arguments: $blazeBuildArgs" -& $bunPath run $wrapperJSFile $args -exit $LASTEXITCODE \ No newline at end of file +& $bun_path $blazew_dir/blaze_wrapper.js $args