forked from microsoft/aspire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalhive.ps1
More file actions
233 lines (196 loc) · 8.11 KB
/
localhive.ps1
File metadata and controls
233 lines (196 loc) · 8.11 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
#!/usr/bin/env pwsh
<#!
.SYNOPSIS
Build local NuGet packages and create/update an Aspire CLI hive that points at them (Windows/PowerShell).
.DESCRIPTION
Mirrors localhive.sh behavior on Windows. Packs the repo, then either creates a symlink from
$HOME/.aspire/hives/<HiveName> to artifacts/packages/<Config>/Shipping or copies .nupkg files.
.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 Help
Show help and exit.
.EXAMPLE
.\localhive.ps1 -Configuration Release -Name local
.EXAMPLE
.\localhive.ps1 Debug my-feature
.NOTES
The hive is created at $HOME/.aspire/hives/<HiveName> so the Aspire CLI can discover a channel.
#>
[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,
[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
-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.
'@ | 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'
}
if ($Configuration) {
Write-Log "Building and packing NuGet packages [-c $Configuration] with versionsuffix '$VersionSuffix'"
& $buildScript -r -b -pack -c $Configuration "/p:VersionSuffix=$VersionSuffix" "/p:SkipTestProjects=true" "/p:SkipPlaygroundProjects=true"
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 -r -b -pack -c Release "/p:VersionSuffix=$VersionSuffix" "/p:SkipTestProjects=true" "/p:SkipPlaygroundProjects=true"
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'
$hivePath = Join-Path $hivesRoot $Name
Write-Log "Preparing hive directory: $hivesRoot"
New-Item -ItemType Directory -Path $hivesRoot -Force | Out-Null
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' to $pkgDir"
try {
if (Test-Path -LiteralPath $hivePath) {
$item = Get-Item -LiteralPath $hivePath -ErrorAction SilentlyContinue
if ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) {
# Remove existing link (symlink/junction)
Remove-Item -LiteralPath $hivePath -Force
}
}
# 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' -> $pkgDir (symlink)"
}
catch {
Write-Warn "Symlink not supported; attempting junction, else copying .nupkg files"
try {
if (Test-Path -LiteralPath $hivePath) { Remove-Item -LiteralPath $hivePath -Force -Recurse -ErrorAction SilentlyContinue }
New-Item -Path $hivePath -ItemType Junction -Target $pkgDir -Force | Out-Null
Write-Log "Created/updated hive '$Name' -> $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)."
}
}
}
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
Write-Log 'The Aspire CLI discovers channels automatically from the hives directory; no extra flags are required.'