-
Notifications
You must be signed in to change notification settings - Fork 847
Expand file tree
/
Copy pathlocalhive.ps1
More file actions
415 lines (350 loc) · 15.7 KB
/
localhive.ps1
File metadata and controls
415 lines (350 loc) · 15.7 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#!/usr/bin/env pwsh
<#!
.SYNOPSIS
Build local NuGet packages, Aspire CLI, and bundle, then create/update a hive and install everything (Windows/PowerShell).
.DESCRIPTION
Mirrors localhive.sh behavior on Windows. Packs the repo, creates a symlink from
$HOME/.aspire/hives/<HiveName> to artifacts/packages/<Config>/Shipping (or copies .nupkg files),
installs the locally-built Aspire CLI to $HOME/.aspire/bin, and builds/installs the bundle
(aspire-managed + DCP) to $HOME/.aspire so the CLI can auto-discover it.
.PARAMETER Configuration
Build configuration: Release or Debug (positional parameter 0). If omitted, the script tries Release then falls back to Debug.
.PARAMETER Name
Hive name (positional parameter 1). Default: local.
.PARAMETER VersionSuffix
Prerelease version suffix. If omitted, auto-generates: local.YYYYMMDD.tHHmmss (UTC)
.PARAMETER Copy
Copy .nupkg files instead of linking the hive directory.
.PARAMETER SkipCli
Skip installing the locally-built CLI to $HOME/.aspire/bin.
.PARAMETER SkipBundle
Skip building and installing the bundle (aspire-managed + DCP) to $HOME/.aspire.
.PARAMETER NativeAot
Build and install the native AOT CLI (self-extracting binary with embedded bundle) instead of the dotnet tool version.
.PARAMETER Help
Show help and exit.
.EXAMPLE
.\localhive.ps1 -Configuration Release -Name local
.EXAMPLE
.\localhive.ps1 Debug my-feature
.EXAMPLE
.\localhive.ps1 -SkipCli
.NOTES
The hive is created at $HOME/.aspire/hives/<HiveName> so the Aspire CLI can discover a channel.
The CLI is installed to $HOME/.aspire/bin so it can be used directly.
#>
[CmdletBinding(PositionalBinding=$true)]
param(
[Alias('c')]
[Parameter(Position=0)]
[string] $Configuration,
[Alias('n','hive','hiveName')]
[Parameter(Position=1)]
[string] $Name = 'local',
[Alias('v')]
[string] $VersionSuffix,
[switch] $Copy,
[switch] $SkipCli,
[switch] $SkipBundle,
[switch] $NativeAot,
[Alias('h')]
[switch] $Help
)
$ErrorActionPreference = 'Stop'
function Show-Usage {
@'
Usage:
.\localhive.ps1 [options]
.\localhive.ps1 [Release|Debug] [HiveName]
Positional parameters:
[Release|Debug] Optional build configuration (Position 0). If omitted, attempts Release then Debug.
[HiveName] Optional hive name (Position 1). Defaults to 'local'.
Options:
-Configuration (-c) Build configuration: Release or Debug
-Name (-n) Hive name (default: local)
-VersionSuffix (-v) Prerelease version suffix (default: auto-generates local.YYYYMMDD.tHHmmss)
-Copy Copy .nupkg files instead of creating a symlink
-SkipCli Skip installing the locally-built CLI to $HOME\.aspire\bin
-SkipBundle Skip building and installing the bundle (aspire-managed + DCP)
-NativeAot Build native AOT CLI (self-extracting with embedded bundle)
-Help (-h) Show this help and exit
Examples:
.\localhive.ps1 -c Release -n local
.\localhive.ps1 Debug my-feature
.\localhive.ps1 -c Release -n demo -v local.20250811.t033324
.\localhive.ps1 # Packs (tries Release then Debug) -> hive 'local'
.\localhive.ps1 Debug # Packs Debug -> hive 'local'
.\localhive.ps1 Release demo
This will pack NuGet packages into artifacts\packages\<Config>\Shipping and create/update
a hive at $HOME\.aspire\hives\<HiveName> so the Aspire CLI can use it as a channel.
It also installs the locally-built CLI to $HOME\.aspire\bin (unless -SkipCli is specified).
'@ | Write-Host
}
function Write-Log { param([string]$m) Write-Host "[localhive] $m" }
function Write-Warn { param([string]$m) Write-Warning "[localhive] $m" }
function Write-Err { param([string]$m) Write-Error "[localhive] $m" }
if ($Help) { Show-Usage; exit 0 }
# Normalize configuration casing if provided (case-insensitive) and allow common abbreviations.
if ($Configuration) {
switch ($Configuration.ToLowerInvariant()) {
'release' { $Configuration = 'Release' }
'debug' { $Configuration = 'Debug' }
default { Write-Err "Unsupported configuration '$Configuration'. Use Release or Debug."; exit 1 }
}
}
# Compute repo root based on script location
$RepoRoot = (Resolve-Path -LiteralPath $PSScriptRoot).Path
function Test-VersionSuffix {
param([Parameter(Mandatory)][string]$Suffix)
# Must be dot-separated identifiers containing only 0-9A-Za-z- per SemVer2.
if ($Suffix -notmatch '^[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*$') { return $false }
$parts = $Suffix -split '\.'
foreach ($p in $parts) {
if ($p -match '^[0-9]+$' -and $p.Length -gt 1 -and $p.StartsWith('0')) { return $false }
}
return $true
}
# Auto-generate version suffix if not specified
if (-not $VersionSuffix) {
$utc = [DateTime]::UtcNow
$VersionSuffix = 'local.{0}.t{1}' -f $utc.ToString('yyyyMMdd'), $utc.ToString('HHmmss')
}
if (-not (Test-VersionSuffix -Suffix $VersionSuffix)) {
Write-Err "Invalid versionsuffix '$VersionSuffix'. It must be dot-separated identifiers using [0-9A-Za-z-] only; numeric identifiers cannot have leading zeros."
Write-Warn "Examples: preview.1, rc.2, local.20250811.t033324"
exit 1
}
Write-Log "Using prerelease version suffix: $VersionSuffix"
# Build and pack
$pkgDir = $null
# Use build.cmd on Windows, build.sh otherwise (PowerShell is cross-platform)
if ($IsWindows) {
$buildScript = Join-Path $RepoRoot 'build.cmd'
}
else {
$buildScript = Join-Path $RepoRoot 'build.sh'
}
function Get-PackagesPath {
param([Parameter(Mandatory)][string]$Config)
Join-Path (Join-Path (Join-Path (Join-Path $RepoRoot 'artifacts') 'packages') $Config) 'Shipping'
}
$effectiveConfig = if ($Configuration) { $Configuration } else { 'Release' }
# Skip native AOT during pack unless user will build it separately via -NativeAot + Bundle.proj
$aotArg = if (-not $NativeAot) { "/p:PublishAot=false" } else { "" }
if ($Configuration) {
Write-Log "Building and packing NuGet packages [-c $Configuration] with versionsuffix '$VersionSuffix'"
& $buildScript -restore -build -pack -c $Configuration "/p:VersionSuffix=$VersionSuffix" "/p:SkipTestProjects=true" "/p:SkipPlaygroundProjects=true" $aotArg
if ($LASTEXITCODE -ne 0) {
Write-Err "Build failed for configuration $Configuration."
exit 1
}
$pkgDir = Get-PackagesPath -Config $Configuration
if (-not (Test-Path -LiteralPath $pkgDir)) {
Write-Err "Could not find packages path $pkgDir for CONFIG=$Configuration"
exit 1
}
}
else {
Write-Log "Building and packing NuGet packages [-c Release] with versionsuffix '$VersionSuffix'"
& $buildScript -restore -build -pack -c Release "/p:VersionSuffix=$VersionSuffix" "/p:SkipTestProjects=true" "/p:SkipPlaygroundProjects=true" $aotArg
if ($LASTEXITCODE -ne 0) {
Write-Err "Build failed for configuration Release."
exit 1
}
$pkgDir = Get-PackagesPath -Config 'Release'
if (-not (Test-Path -LiteralPath $pkgDir)) {
Write-Err "Could not find packages path $pkgDir for CONFIG=Release"
exit 1
}
}
# Ensure there are .nupkg files
$packages = Get-ChildItem -LiteralPath $pkgDir -Filter *.nupkg -File -ErrorAction SilentlyContinue
if (-not $packages -or $packages.Count -eq 0) {
Write-Err "No .nupkg files found in $pkgDir. Did the pack step succeed?"
exit 1
}
Write-Log ("Found {0} packages in {1}" -f $packages.Count, $pkgDir)
$hivesRoot = Join-Path (Join-Path $HOME '.aspire') 'hives'
$hiveRoot = Join-Path $hivesRoot $Name
$hivePath = Join-Path $hiveRoot 'packages'
Write-Log "Preparing hive directory: $hivesRoot"
New-Item -ItemType Directory -Path $hivesRoot -Force | Out-Null
# Remove previous hive content (handles both old layout junctions and stale data)
if (Test-Path -LiteralPath $hiveRoot) {
Write-Log "Removing previous hive '$Name'"
Remove-Item -LiteralPath $hiveRoot -Force -Recurse -ErrorAction SilentlyContinue
}
function Copy-PackagesToHive {
param([string]$Source,[string]$Destination)
New-Item -ItemType Directory -Path $Destination -Force | Out-Null
Get-ChildItem -LiteralPath $Source -Filter *.nupkg -File | Copy-Item -Destination $Destination -Force
}
if ($Copy) {
Write-Log "Populating hive '$Name' by copying .nupkg files"
Copy-PackagesToHive -Source $pkgDir -Destination $hivePath
Write-Log "Created/updated hive '$Name' at $hivePath (copied packages)."
}
else {
Write-Log "Linking hive '$Name/packages' to $pkgDir"
New-Item -ItemType Directory -Path $hiveRoot -Force | Out-Null
try {
# Try symlink first (requires Developer Mode or elevated privilege)
New-Item -Path $hivePath -ItemType SymbolicLink -Target $pkgDir -Force | Out-Null
Write-Log "Created/updated hive '$Name/packages' -> $pkgDir (symlink)"
}
catch {
Write-Warn "Symlink not supported; attempting junction, else copying .nupkg files"
try {
New-Item -Path $hivePath -ItemType Junction -Target $pkgDir -Force | Out-Null
Write-Log "Created/updated hive '$Name/packages' -> $pkgDir (junction)"
}
catch {
Write-Warn "Link creation failed; copying .nupkg files instead"
Copy-PackagesToHive -Source $pkgDir -Destination $hivePath
Write-Log "Created/updated hive '$Name' at $hivePath (copied packages)."
}
}
}
# Determine the RID for the current platform
if ($IsWindows) {
$bundleRid = if ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture -eq [System.Runtime.InteropServices.Architecture]::Arm64) { 'win-arm64' } else { 'win-x64' }
} elseif ($IsMacOS) {
$bundleRid = if ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture -eq [System.Runtime.InteropServices.Architecture]::Arm64) { 'osx-arm64' } else { 'osx-x64' }
} else {
$bundleRid = if ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture -eq [System.Runtime.InteropServices.Architecture]::Arm64) { 'linux-arm64' } else { 'linux-x64' }
}
$aspireRoot = Join-Path $HOME '.aspire'
$cliBinDir = Join-Path $aspireRoot 'bin'
# Build the bundle (aspire-managed + DCP, and optionally native AOT CLI)
if (-not $SkipBundle) {
$bundleProjPath = Join-Path $RepoRoot "eng" "Bundle.proj"
$skipNativeArg = if ($NativeAot) { '' } else { '/p:SkipNativeBuild=true' }
Write-Log "Building bundle (aspire-managed + DCP$(if ($NativeAot) { ' + native AOT CLI' }))..."
$buildArgs = @($bundleProjPath, '-c', $effectiveConfig, "/p:VersionSuffix=$VersionSuffix")
if (-not $NativeAot) {
$buildArgs += '/p:SkipNativeBuild=true'
}
& dotnet build @buildArgs
if ($LASTEXITCODE -ne 0) {
Write-Err "Bundle build failed."
exit 1
}
$bundleLayoutDir = Join-Path $RepoRoot "artifacts" "bundle" $bundleRid
if (-not (Test-Path -LiteralPath $bundleLayoutDir)) {
Write-Err "Bundle layout not found at $bundleLayoutDir"
exit 1
}
# Copy managed/ and dcp/ to $HOME/.aspire so the CLI auto-discovers them
foreach ($component in @('managed', 'dcp')) {
$sourceDir = Join-Path $bundleLayoutDir $component
$destDir = Join-Path $aspireRoot $component
if (Test-Path -LiteralPath $sourceDir) {
if (Test-Path -LiteralPath $destDir) {
Remove-Item -LiteralPath $destDir -Force -Recurse
}
Write-Log "Copying $component/ to $destDir"
Copy-Item -LiteralPath $sourceDir -Destination $destDir -Recurse -Force
} else {
Write-Warn "$component/ not found in bundle layout at $sourceDir"
}
}
Write-Log "Bundle installed to $aspireRoot (managed/ + dcp/)"
}
# Install the CLI to $HOME/.aspire/bin
if (-not $SkipCli) {
$cliExeName = if ($IsWindows) { 'aspire.exe' } else { 'aspire' }
if ($NativeAot) {
# Native AOT CLI is produced by Bundle.proj's _PublishNativeCli target
$cliPublishDir = Join-Path $RepoRoot "artifacts" "bin" "Aspire.Cli" $effectiveConfig "net10.0" $bundleRid "native"
if (-not (Test-Path -LiteralPath $cliPublishDir)) {
$cliPublishDir = Join-Path $RepoRoot "artifacts" "bin" "Aspire.Cli" $effectiveConfig "net10.0" $bundleRid "publish"
}
} else {
# Framework-dependent CLI from dotnet tool build
$cliPublishDir = Join-Path $RepoRoot "artifacts" "bin" "Aspire.Cli.Tool" $effectiveConfig "net10.0" "publish"
if (-not (Test-Path -LiteralPath $cliPublishDir)) {
$cliPublishDir = Join-Path $RepoRoot "artifacts" "bin" "Aspire.Cli.Tool" $effectiveConfig "net10.0"
}
}
$cliSourcePath = Join-Path $cliPublishDir $cliExeName
if (Test-Path -LiteralPath $cliSourcePath) {
Write-Log "Installing Aspire CLI$(if ($NativeAot) { ' (native AOT)' }) to $cliBinDir"
New-Item -ItemType Directory -Path $cliBinDir -Force | Out-Null
# Backup existing CLI executable if it's locked (same pattern as aspire update --self)
$targetExePath = Join-Path $cliBinDir $cliExeName
$backupPath = $null
if (Test-Path -LiteralPath $targetExePath) {
$timestamp = [DateTimeOffset]::UtcNow.ToUnixTimeSeconds()
$backupPath = "$targetExePath.old.$timestamp"
try {
Move-Item -LiteralPath $targetExePath -Destination $backupPath -Force -ErrorAction Stop
Write-Log "Backed up existing CLI to $backupPath"
}
catch {
Write-Warn "Could not backup existing CLI (may be in use). Attempting direct overwrite."
$backupPath = $null
}
}
try {
# Copy all files from the publish directory (CLI and its dependencies)
# Use -ErrorAction SilentlyContinue for individual files that may be locked by running processes
$copyErrors = @()
Get-ChildItem -LiteralPath $cliPublishDir -File | ForEach-Object {
try {
Copy-Item $_.FullName -Destination $cliBinDir -Force -ErrorAction Stop
}
catch {
$copyErrors += $_.Exception.Message
}
}
if ($copyErrors.Count -gt 0) {
Write-Warn "$($copyErrors.Count) file(s) could not be overwritten (likely locked by a running process). The CLI executable was updated successfully."
}
# Clean up old backup files
Get-ChildItem -LiteralPath $cliBinDir -Filter "$cliExeName.old.*" -ErrorAction SilentlyContinue |
ForEach-Object { Remove-Item $_.FullName -Force -ErrorAction SilentlyContinue }
}
catch {
# Restore backup if copy failed
if ($backupPath -and (Test-Path -LiteralPath $backupPath)) {
Write-Warn "Copy failed, restoring backup"
Move-Item -LiteralPath $backupPath -Destination $targetExePath -Force
}
throw
}
$installedCliPath = Join-Path $cliBinDir $cliExeName
Write-Log "Aspire CLI installed to: $installedCliPath"
# Set the channel to the local hive so templates and packages resolve from it
& $installedCliPath config set channel $Name -g 2>$null
Write-Log "Set global channel to '$Name'"
# Check if the bin directory is in PATH
$pathSeparator = [System.IO.Path]::PathSeparator
$currentPathArray = $env:PATH.Split($pathSeparator, [StringSplitOptions]::RemoveEmptyEntries)
if ($currentPathArray -notcontains $cliBinDir) {
Write-Warn "The CLI bin directory is not in your PATH."
Write-Log "Add it to your PATH with: `$env:PATH = '$cliBinDir' + '$pathSeparator' + `$env:PATH"
}
}
else {
Write-Warn "Could not find CLI at $cliSourcePath. Skipping CLI installation."
Write-Warn "You may need to build the CLI separately or use 'dotnet tool install' for the Aspire.Cli package."
}
}
Write-Host
Write-Log 'Done.'
Write-Host
Write-Log "Aspire CLI will discover a channel named '$Name' from:"
Write-Log " $hivePath"
Write-Host
Write-Log "Channel behavior: Aspire* comes from the hive; others from nuget.org."
Write-Host
if (-not $SkipCli) {
Write-Log "The locally-built CLI was installed to: $(Join-Path (Join-Path $HOME '.aspire') 'bin')"
Write-Host
}
if (-not $SkipBundle) {
Write-Log "Bundle (aspire-managed + DCP) installed to: $(Join-Path $HOME '.aspire')"
Write-Log " The CLI at ~/.aspire/bin/ will auto-discover managed/ and dcp/ in the parent directory."
Write-Host
}
Write-Log 'The Aspire CLI discovers channels automatically from the hives directory; no extra flags are required.'