/[sudobot]/trunk/blazew.ps1
ViewVC logotype

Diff of /trunk/blazew.ps1

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.627  
changed lines
  Added in v.628

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26