-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaunch-browser.ps1
More file actions
190 lines (161 loc) · 7.12 KB
/
launch-browser.ps1
File metadata and controls
190 lines (161 loc) · 7.12 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# launch-browser.ps1 -- Launch Crissy's Style Tool (imgui-browser) on Windows.
#
# Usage:
# .\launch-browser.ps1
# .\launch-browser.ps1 --url https://example.com
# .\launch-browser.ps1 --clean # wipe build dir, reconfigure, rebuild
#
# Checks whether the source is newer than the built exe and prompts you to
# build before opening if so. Requires Visual Studio 2022 or CMake in PATH.
param(
[string]$Url = "",
[switch]$Clean
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$Root = $PSScriptRoot
$Exe = Join-Path $Root "imgui-browser\build\Release\imgui_browser.exe"
$SrcDir = Join-Path $Root "imgui-browser\src"
$CmkList = Join-Path $Root "imgui-browser\CMakeLists.txt"
$BuildDir= Join-Path $Root "imgui-browser\build"
# ── Find a build tool ────────────────────────────────────────────────────────
function Find-MSBuild {
# 1. vswhere (VS installer ships this)
$vsWhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
if (Test-Path $vsWhere) {
$found = & $vsWhere -latest -requires Microsoft.Component.MSBuild `
-find "MSBuild\**\Bin\MSBuild.exe" 2>$null |
Select-Object -First 1
if ($found -and (Test-Path $found)) { return $found }
}
# 2. Known VS 2022 default paths
$candidates = @(
"C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\MSBuild.exe",
"C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\MSBuild.exe",
"C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\MSBuild.exe",
"C:\Program Files\Microsoft Visual Studio\2022\BuildTools\MSBuild\Current\Bin\MSBuild.exe"
)
foreach ($c in $candidates) {
if (Test-Path $c) { return $c }
}
return $null
}
function Find-CMake {
$c = Get-Command cmake.exe -ErrorAction SilentlyContinue
if ($c) { return $c.Source }
return $null
}
# ── Check whether a rebuild is needed ───────────────────────────────────────
function Get-NeedsRebuild {
if (-not (Test-Path $Exe)) { return $true }
$exeTime = (Get-Item $Exe).LastWriteTimeUtc
$watchPaths = @($SrcDir, $CmkList)
foreach ($wp in $watchPaths) {
if (-not (Test-Path $wp)) { continue }
$newer = Get-ChildItem $wp -Recurse -File -ErrorAction SilentlyContinue |
Where-Object { $_.LastWriteTimeUtc -gt $exeTime } |
Select-Object -First 1
if ($newer) { return $true }
}
return $false
}
# ── Configure (cmake ..) ─────────────────────────────────────────────────────
function Invoke-Configure {
$cmake = Find-CMake
if (-not $cmake) {
Write-Host "cmake not found -- skipping configure step."
return $false
}
Write-Host "Configuring with CMake..."
if (-not (Test-Path $BuildDir)) { New-Item -ItemType Directory -Path $BuildDir | Out-Null }
$args = @("..", "-DCMAKE_BUILD_TYPE=Release")
& $cmake @args
$ok = $LASTEXITCODE -eq 0
if (-not $ok) { Write-Host "cmake configure failed." }
return $ok
}
# ── Build ────────────────────────────────────────────────────────────────────
function Invoke-Build {
$slnPath = Join-Path $BuildDir "imgui_browser.sln"
# If there is no solution / cache yet, configure first.
if (-not (Test-Path (Join-Path $BuildDir "CMakeCache.txt"))) {
if (-not (Invoke-Configure)) { return $false }
}
$msbuild = Find-MSBuild
if ($msbuild) {
Write-Host "Using MSBuild: $msbuild"
& $msbuild $slnPath `
/p:Configuration=Release /p:Platform=x64 `
/t:imgui_browser /m /nologo /v:minimal
return $LASTEXITCODE -eq 0
}
$cmake = Find-CMake
if ($cmake) {
Write-Host "Using CMake: $cmake"
& $cmake --build $BuildDir --config Release --target imgui_browser
return $LASTEXITCODE -eq 0
}
Write-Host ""
Write-Host "No build tool found. Options:"
Write-Host " - Install Visual Studio 2022 (any edition) with the C++ workload"
Write-Host " - Or open imgui-browser\build\imgui_browser.sln in Visual Studio manually"
return $false
}
# ── Clean ────────────────────────────────────────────────────────────────────
function Invoke-Clean {
if (Test-Path $BuildDir) {
Write-Host "Removing $BuildDir ..."
Remove-Item $BuildDir -Recurse -Force
Write-Host "Build directory cleared."
}
}
# ── Launch ───────────────────────────────────────────────────────────────────
function Start-Browser {
if (-not (Test-Path $Exe)) {
Write-Host "Exe not found. Cannot open."
return
}
$params = @{ FilePath = $Exe; WorkingDirectory = (Split-Path $Exe) }
if ($Url -ne "") { $params["ArgumentList"] = @("--url", $Url) }
Start-Process @params
Write-Host "Browser launched."
}
# ── Main ─────────────────────────────────────────────────────────────────────
Write-Host ""
Write-Host "--- Crissy's Style Tool ---"
if ($Clean) {
Invoke-Clean
Write-Host "Rebuilding from scratch..."
if (Invoke-Build) { Start-Browser } else { Write-Host "Build failed. Check output above." }
exit
}
$needsBuild = Get-NeedsRebuild
$exeExists = Test-Path $Exe
if (-not $exeExists) {
Write-Host "No binary found. A build is required before the browser can open."
Write-Host ""
Write-Host " [B] Build now"
Write-Host " [C] Cancel"
Write-Host ""
$choice = (Read-Host "Choice").Trim().ToUpper()
if ($choice -eq "B") {
if (Invoke-Build) { Start-Browser } else { Write-Host "Build failed. Check output above." }
}
} elseif ($needsBuild) {
$exeAge = (Get-Item $Exe).LastWriteTime.ToString("yyyy-MM-dd HH:mm")
Write-Host "Source files are newer than the built binary (exe from $exeAge)."
Write-Host ""
Write-Host " [B] Build and open"
Write-Host " [O] Open existing binary without rebuilding"
Write-Host " [C] Cancel"
Write-Host ""
$choice = (Read-Host "Choice").Trim().ToUpper()
switch ($choice) {
"B" { if (Invoke-Build) { Start-Browser } else { Write-Host "Build failed. Check output above." } }
"O" { Start-Browser }
}
} else {
$exeAge = (Get-Item $Exe).LastWriteTime.ToString("yyyy-MM-dd HH:mm")
Write-Host "Binary is up to date (built $exeAge)."
Start-Browser
}