-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathbuild.ps1
More file actions
230 lines (203 loc) · 9.19 KB
/
build.ps1
File metadata and controls
230 lines (203 loc) · 9.19 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Root build orchestrator for the Copilot Token Tracker mono-repo.
.DESCRIPTION
Builds one or more sub-projects from the repo root so that nothing gets missed.
Projects:
vscode-extension – VS Code extension (TypeScript / Node.js)
cli – Command-line tool (TypeScript / Node.js)
sharing – Self-hosted sharing server (TypeScript / Node.js)
visualstudio-extension – Visual Studio extension (C# / .NET) [future]
.PARAMETER Project
Which project(s) to build. Accepts: all | vscode | cli | sharing | visualstudio
Default: all
.PARAMETER Target
Which build target to run. Accepts: build | package | test | clean
Default: build
.EXAMPLE
./build.ps1
# builds vscode-extension and cli (default: all, build)
.EXAMPLE
./build.ps1 -Project vscode -Target test
# runs tests for the VS Code extension only
#>
param(
[ValidateSet('all', 'vscode', 'cli', 'visualstudio', 'sharing')]
[string] $Project = 'all',
[ValidateSet('build', 'package', 'test', 'clean')]
[string] $Target = 'build'
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
function Write-Step([string]$msg) { Write-Host "`n==> $msg" -ForegroundColor Cyan }
function Write-Ok([string]$msg) { Write-Host " $msg" -ForegroundColor Green }
function Write-Err([string]$msg) { Write-Host " ERROR: $msg" -ForegroundColor Red }
# ---------------------------------------------------------------------------
# VS Code Extension
# ---------------------------------------------------------------------------
function Build-VsCode {
Write-Step "vscode-extension: $Target"
Push-Location "$PSScriptRoot/vscode-extension"
try {
switch ($Target) {
'build' { npm ci; npm run compile }
'package' { npm ci; npm run package; npx vsce package }
'test' { npm ci; npm run compile-tests; npm test }
'clean' { Remove-Item -Recurse -Force dist, out -ErrorAction SilentlyContinue }
}
Write-Ok "vscode-extension done."
}
finally { Pop-Location }
}
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
function Build-Cli {
Write-Step "cli: $Target"
Push-Location "$PSScriptRoot/cli"
try {
switch ($Target) {
'build' { npm ci; npm run build }
'package' { npm ci; npm run build:production; & pwsh -NoProfile -File bundle-exe.ps1 -SkipBuild }
'test' { Write-Host " (no CLI tests yet)" }
'clean' { Remove-Item -Recurse -Force dist -ErrorAction SilentlyContinue }
}
Write-Ok "cli done."
}
finally { Pop-Location }
}
# ---------------------------------------------------------------------------
# CLI Bundled Executable (for embedding in Visual Studio extension)
# ---------------------------------------------------------------------------
function Build-CliExe {
Write-Step "cli: bundle-exe"
Push-Location "$PSScriptRoot/cli"
try {
npm ci
& pwsh -NoProfile -File bundle-exe.ps1
if ($LASTEXITCODE -ne 0) { throw "CLI exe bundling failed" }
Write-Ok "cli exe bundled."
}
finally { Pop-Location }
}
# ---------------------------------------------------------------------------
# Visual Studio Extension
# ---------------------------------------------------------------------------
function Build-VisualStudio {
Write-Step "visualstudio-extension: $Target"
$slnFiles = Get-ChildItem -Path "$PSScriptRoot/visualstudio-extension" -Filter '*.sln' -Recurse -ErrorAction SilentlyContinue
if (-not $slnFiles) {
Write-Host " (visualstudio-extension not yet scaffolded – skipping)" -ForegroundColor Yellow
return
}
# Always rebuild the vscode-extension webview bundles so the VS extension
# gets the latest compiled JS (labels, strings, etc.)
Write-Step "vscode-extension: compile (for VS webview bundles)"
Push-Location "$PSScriptRoot/vscode-extension"
try {
npm ci
npm run compile
if ($LASTEXITCODE -ne 0) { throw "vscode-extension compile failed" }
Write-Ok "vscode-extension compiled."
}
finally { Pop-Location }
# Copy compiled webview bundles into the VS extension project
$webviewSrc = Join-Path $PSScriptRoot 'vscode-extension' 'dist' 'webview'
$webviewDst = Join-Path $PSScriptRoot 'visualstudio-extension' 'src' 'CopilotTokenTracker' 'webview'
if (-not (Test-Path $webviewDst)) { New-Item -ItemType Directory -Path $webviewDst -Force | Out-Null }
foreach ($bundle in @('details', 'chart', 'usage', 'diagnostics', 'environmental', 'maturity')) {
$src = Join-Path $webviewSrc "$bundle.js"
if (Test-Path $src) {
Copy-Item $src (Join-Path $webviewDst "$bundle.js") -Force
}
}
Write-Ok "Copied webview bundles to visualstudio-extension/webview/"
# Always rebuild the CLI exe from source so the VS extension has the latest
# maturity scoring labels and other runtime logic.
Build-CliExe
# Copy the CLI exe and its runtime assets into the VS extension project
$cliExe = Join-Path $PSScriptRoot 'cli' 'dist' 'copilot-token-tracker.exe'
$vsCliDir = Join-Path $PSScriptRoot 'visualstudio-extension' 'src' 'CopilotTokenTracker' 'cli-bundle'
if (-not (Test-Path $vsCliDir)) { New-Item -ItemType Directory -Path $vsCliDir -Force | Out-Null }
Copy-Item $cliExe (Join-Path $vsCliDir 'copilot-token-tracker.exe') -Force
# sql.js WASM binary is loaded at runtime from the same directory as the exe
$wasmSrc = Join-Path $PSScriptRoot 'cli' 'dist' 'sql-wasm.wasm'
if (Test-Path $wasmSrc) {
Copy-Item $wasmSrc (Join-Path $vsCliDir 'sql-wasm.wasm') -Force
}
Write-Ok "Copied CLI exe + sql-wasm.wasm to cli-bundle/"
$sln = $slnFiles[0].FullName
# VSIX projects require Visual Studio's MSBuild (not dotnet build) because
# the VSSDK build targets depend on VS-specific assemblies.
$msbuild = & "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" `
-latest -requires Microsoft.Component.MSBuild `
-find 'MSBuild\**\Bin\MSBuild.exe' 2>$null | Select-Object -First 1
if (-not $msbuild) {
# Fallback: try the well-known VS 18 (2024+) path
$msbuild = "${env:ProgramFiles}\Microsoft Visual Studio\18\Enterprise\MSBuild\Current\Bin\MSBuild.exe"
}
if (-not (Test-Path $msbuild)) {
Write-Err "MSBuild not found — install the Visual Studio 'VSIX development' workload"
return
}
switch ($Target) {
'build' {
# Restore SDK-style test project (needs dotnet restore, not nuget restore)
dotnet restore "$PSScriptRoot/visualstudio-extension/src/CopilotTokenTracker.Tests/CopilotTokenTracker.Tests.csproj"
& $msbuild $sln /p:Configuration=Release /t:Build /v:minimal
}
'package' { & $msbuild $sln /p:Configuration=Release /t:Rebuild /v:minimal }
'test' {
# 1. Restore SDK-style test project first, then build the full solution with MSBuild
dotnet restore "$PSScriptRoot/visualstudio-extension/src/CopilotTokenTracker.Tests/CopilotTokenTracker.Tests.csproj"
& $msbuild $sln /p:Configuration=Release /t:Build /v:minimal
if ($LASTEXITCODE -ne 0) { throw "MSBuild failed before running tests" }
# 2. Run tests with dotnet test --no-build (avoids re-invoking VSSDK build targets)
Push-Location "$PSScriptRoot/visualstudio-extension"
try {
$testProj = "src/CopilotTokenTracker.Tests/CopilotTokenTracker.Tests.csproj"
dotnet test $testProj `
--no-build `
--configuration Release `
--collect:"XPlat Code Coverage" `
--results-directory TestResults `
--logger "console;verbosity=normal"
if ($LASTEXITCODE -ne 0) { throw "Unit tests failed" }
}
finally { Pop-Location }
}
'clean' { & $msbuild $sln /p:Configuration=Release /t:Clean /v:minimal }
}
Write-Ok "visualstudio-extension done."
}
# ---------------------------------------------------------------------------
# Sharing Server
# ---------------------------------------------------------------------------
function Build-Sharing {
Write-Step "sharing-server: $Target"
Push-Location "$PSScriptRoot/sharing-server"
try {
switch ($Target) {
'build' { npm ci; npm run build }
'package' { npm ci; npm run build:production }
'test' { Write-Host " (no sharing-server tests yet)" }
'clean' { Remove-Item -Recurse -Force dist -ErrorAction SilentlyContinue }
}
Write-Ok "sharing-server done."
}
finally { Pop-Location }
}
switch ($Project) {
'all' {
Build-VsCode
Build-Cli
Build-Sharing
Build-VisualStudio
}
'vscode' { Build-VsCode }
'cli' { Build-Cli }
'sharing' { Build-Sharing }
'visualstudio'{ Build-VisualStudio }
}
Write-Host "`nBuild complete." -ForegroundColor Green