-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage.ps1
More file actions
376 lines (326 loc) · 14.5 KB
/
package.ps1
File metadata and controls
376 lines (326 loc) · 14.5 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
#Requires -Version 7.0
param(
[string]$Version = $null,
[switch]$SkipTests,
[switch]$SkipChangelog,
[switch]$NonInteractive,
[string[]]$Runtimes = @('win-x64', 'linux-x64', 'linux-arm64', 'osx-x64')
)
$ErrorActionPreference = 'Stop'
$DistDir = Join-Path $PSScriptRoot '.dist'
$BuildDir = Join-Path $PSScriptRoot '.build'
#region Tool Installation Functions
function Ensure-DotnetTool {
param([string]$Package, [string]$Command = $Package)
if (-not (Get-Command $Command -ErrorAction SilentlyContinue)) {
Write-Host "Installing $Package via dotnet tool..." -ForegroundColor Yellow
dotnet tool install -g $Package
}
}
#endregion
#region Platform-Specific Runtime Filtering
function Remove-NonMatchingRuntimes {
<#
.SYNOPSIS
Removes platform-specific runtime directories that don't match the target runtime.
.DESCRIPTION
Scans all plugin directories for 'runtimes' folders and removes subdirectories
that don't match the target platform. This significantly reduces package size.
.PARAMETER PluginsDir
Path to the plugins directory to scan.
.PARAMETER TargetRuntime
Target runtime identifier (e.g., 'linux-x64', 'win-x64', 'osx-arm64').
.RETURNS
Number of directories removed.
#>
param(
[Parameter(Mandatory)]
[string]$PluginsDir,
[Parameter(Mandatory)]
[string]$TargetRuntime
)
$removedCount = 0
# Parse target runtime: "linux-x64" -> os="linux", arch="x64"
$parts = $TargetRuntime.Split('-')
$targetOs = $parts[0].ToLowerInvariant()
$targetArch = if ($parts.Length -gt 1) { $parts[1].ToLowerInvariant() } else { $null }
# Define which runtime directories to keep for each target OS
# Be precise: exact match + base/generic versions only
$keepExact = switch ($targetOs) {
'linux' {
@(
"linux-$targetArch", # Exact match (e.g., linux-x64)
'linux', # Base linux (no arch)
'unix' # Unix generic
)
}
'win' {
@(
"win-$targetArch", # Exact match (e.g., win-x64)
'win', # Base windows (no arch)
'windows' # Windows generic
)
}
'osx' {
@(
"osx-$targetArch", # Exact match (e.g., osx-arm64)
'osx', # Base macOS (no arch)
'unix' # Unix generic (macOS is unix-like)
)
}
default {
@($TargetRuntime) # Just keep exact match
}
}
# Find all 'runtimes' directories in all plugins
$runtimesDirs = Get-ChildItem -Path $PluginsDir -Directory -Recurse -Filter 'runtimes' -ErrorAction SilentlyContinue
foreach ($runtimesDir in $runtimesDirs) {
# Get all platform subdirectories
$platformDirs = Get-ChildItem -Path $runtimesDir.FullName -Directory -ErrorAction SilentlyContinue
foreach ($platformDir in $platformDirs) {
$platformName = $platformDir.Name.ToLowerInvariant()
# Check if this platform should be kept (exact match only, no wildcards)
$shouldKeep = $keepExact -contains $platformName
if (-not $shouldKeep) {
Remove-Item -Path $platformDir.FullName -Recurse -Force -ErrorAction SilentlyContinue
$removedCount++
}
}
# If runtimes directory is now empty, remove it too
$remaining = Get-ChildItem -Path $runtimesDir.FullName -ErrorAction SilentlyContinue
if ($remaining.Count -eq 0) {
Remove-Item -Path $runtimesDir.FullName -Force -ErrorAction SilentlyContinue
}
}
return $removedCount
}
#endregion
#region Version Management
function Get-NextVersion {
# Use conventional commits to calculate version
$versionScript = Join-Path $PSScriptRoot 'scripts' 'get-version.ps1'
if (Test-Path $versionScript) {
$version = & $versionScript
if ($version -and $version -match '^\d+\.\d+\.\d+$') {
return $version
}
}
# Fallback: try to get version from git tags
$lastTag = git describe --tags --abbrev=0 2>$null
if ($lastTag) {
$current = [Version]($lastTag -replace '^v', '')
return "$($current.Major).$($current.Minor).$($current.Build + 1)"
}
return "0.1.0"
}
function Update-VersionInFiles {
param([string]$NewVersion)
# Update Directory.Build.props
$propsFile = Join-Path $PSScriptRoot 'Directory.Build.props'
if (Test-Path $propsFile) {
$content = Get-Content $propsFile -Raw
if ($content -match '<Version>') {
$content = $content -replace '<Version>[^<]+</Version>', "<Version>$NewVersion</Version>"
Set-Content $propsFile $content -NoNewline
Write-Host "Updated version in Directory.Build.props" -ForegroundColor Green
}
}
# Update release-please manifest
$manifestFile = Join-Path $PSScriptRoot '.github' 'release-please-manifest.json'
if (Test-Path $manifestFile) {
$manifest = Get-Content $manifestFile -Raw | ConvertFrom-Json
$manifest.'.' = $NewVersion
$manifest | ConvertTo-Json | Set-Content $manifestFile
Write-Host "Updated version in release-please-manifest.json" -ForegroundColor Green
}
}
#endregion
#region Changelog Management
function Update-Changelog {
param([string]$NewVersion)
$changelogPath = Join-Path $PSScriptRoot 'CHANGELOG.md'
$today = Get-Date -Format 'yyyy-MM-dd'
if (Test-Path $changelogPath) {
$content = Get-Content $changelogPath -Raw
# Replace [Unreleased] section
$content = $content -replace '\[Unreleased\]', "[$NewVersion] - $today`n`n## [Unreleased]"
Set-Content $changelogPath $content -NoNewline
Write-Host "Updated CHANGELOG.md with version $NewVersion" -ForegroundColor Green
}
}
#endregion
Push-Location $PSScriptRoot
try {
Write-Host "=== LCDPossible Packaging ===" -ForegroundColor Cyan
# Determine version
if (-not $Version) {
$Version = Get-NextVersion
Write-Host "Auto-detected version: $Version" -ForegroundColor Yellow
if (-not $NonInteractive) {
$confirm = Read-Host "Use this version? (y/n or enter custom version)"
if ($confirm -ne 'y' -and $confirm -ne 'Y' -and $confirm -ne '') {
$Version = $confirm
}
}
}
Write-Host "Packaging version: $Version" -ForegroundColor Green
# Clean dist folder
if (Test-Path $DistDir) {
Remove-Item $DistDir -Recurse -Force
}
New-Item -ItemType Directory -Path $DistDir -Force | Out-Null
# Update version in files (skip in CI - Release Please handles this)
if (-not $NonInteractive) {
Update-VersionInFiles -NewVersion $Version
}
# Update changelog (skip in CI - Release Please handles this)
if (-not $SkipChangelog -and -not $NonInteractive) {
Update-Changelog -NewVersion $Version
}
# Run build
Write-Host "`n=== Building ===" -ForegroundColor Cyan
& (Join-Path $PSScriptRoot 'build.ps1')
if ($LASTEXITCODE -ne 0) { throw "Build failed" }
# Run tests
if (-not $SkipTests) {
Write-Host "`n=== Testing ===" -ForegroundColor Cyan
& (Join-Path $PSScriptRoot 'test-full.ps1')
if ($LASTEXITCODE -ne 0) { throw "Tests failed" }
}
# Publish for each target runtime
Write-Host "`n=== Publishing ===" -ForegroundColor Cyan
foreach ($runtime in $Runtimes) {
Write-Host "Publishing for $runtime..." -ForegroundColor Yellow
$outputDir = Join-Path $DistDir 'LCDPossible' $runtime
dotnet publish src/LCDPossible/LCDPossible.csproj `
--configuration Release `
--runtime $runtime `
--self-contained true `
--output $outputDir `
-p:Version=$Version `
-p:PublishSingleFile=true `
-p:IncludeNativeLibrariesForSelfExtract=true
if ($LASTEXITCODE -ne 0) { throw "Publish failed for $runtime" }
# Copy plugins from build output (they're not included in single-file publish)
$buildPluginsDir = Join-Path $BuildDir 'LCDPossible' 'bin' 'Release' 'net10.0' $runtime 'plugins'
$publishPluginsDir = Join-Path $outputDir 'plugins'
if (Test-Path $buildPluginsDir) {
Copy-Item -Path $buildPluginsDir -Destination $publishPluginsDir -Recurse -Force
Write-Host " Copied plugins to publish output" -ForegroundColor Gray
# Remove platform-specific runtime files that don't match the target
$removedCount = Remove-NonMatchingRuntimes -PluginsDir $publishPluginsDir -TargetRuntime $runtime
if ($removedCount -gt 0) {
Write-Host " Removed $removedCount non-matching runtime directories" -ForegroundColor Gray
}
} else {
Write-Host " [WARN] Plugins directory not found in build output: $buildPluginsDir" -ForegroundColor Yellow
}
# Copy SDK and Core assemblies to output directory (plugins need them on disk, not just embedded in single-file)
$buildOutputDir = Join-Path $BuildDir 'LCDPossible' 'bin' 'Release' 'net10.0' $runtime
foreach ($sharedDll in @('LCDPossible.Sdk.dll', 'LCDPossible.Core.dll')) {
$dllPath = Join-Path $buildOutputDir $sharedDll
if (Test-Path $dllPath) {
Copy-Item -Path $dllPath -Destination $outputDir -Force
Write-Host " Copied $sharedDll for plugin compatibility" -ForegroundColor Gray
} else {
Write-Host " [WARN] Assembly not found: $dllPath" -ForegroundColor Yellow
}
}
# Copy html_assets folder (needed for HTML panel rendering - CSS, JS, etc.)
$buildAssetsDir = Join-Path $buildOutputDir 'html_assets'
$publishAssetsDir = Join-Path $outputDir 'html_assets'
if (Test-Path $buildAssetsDir) {
Copy-Item -Path $buildAssetsDir -Destination $publishAssetsDir -Recurse -Force
Write-Host " Copied html_assets for panel rendering" -ForegroundColor Gray
} else {
Write-Host " [WARN] html_assets directory not found: $buildAssetsDir" -ForegroundColor Yellow
}
# Rename executable to lowercase for Linux/macOS (case-sensitive filesystems)
if ($runtime -notlike 'win-*') {
$exePath = Join-Path $outputDir 'LCDPossible'
$lowerExePath = Join-Path $outputDir 'lcdpossible'
if (Test-Path $exePath) {
Move-Item $exePath $lowerExePath -Force
Write-Host " Renamed executable to lowercase for $runtime" -ForegroundColor Gray
}
}
# Create archive
$archiveName = "lcdpossible-$Version-$runtime"
if ($runtime -like 'win-*') {
$archivePath = Join-Path $DistDir "$archiveName.zip"
Compress-Archive -Path "$outputDir\*" -DestinationPath $archivePath -Force
Write-Host " Created: $archivePath" -ForegroundColor Gray
} else {
# For Linux/macOS, we'd need tar (available in Git Bash or WSL)
$tarPath = Join-Path $DistDir "$archiveName.tar.gz"
if (Get-Command tar -ErrorAction SilentlyContinue) {
Push-Location $outputDir
# Use relative path for tar (avoids Windows C: being interpreted as remote host)
$relativeTarPath = "../../$archiveName.tar.gz"
# Exclude obj folder (intermediate files that shouldn't be published)
tar -czvf $relativeTarPath --exclude='obj' --exclude='obj/*' *
if ($LASTEXITCODE -eq 0) {
Write-Host " Created: $tarPath" -ForegroundColor Gray
} else {
Write-Host " [WARN] Failed to create tarball" -ForegroundColor Yellow
}
Pop-Location
}
}
}
# Package NuGet packages (if any library projects)
Write-Host "`n=== NuGet Packages ===" -ForegroundColor Cyan
$nugetDir = Join-Path $DistDir 'nuget'
New-Item -ItemType Directory -Path $nugetDir -Force | Out-Null
$libraryProjects = Get-ChildItem -Path 'src' -Recurse -Filter '*.csproj' |
Where-Object {
$content = Get-Content $_.FullName -Raw
$content -match '<IsPackable>true</IsPackable>' -or
(-not ($content -match '<OutputType>Exe</OutputType>') -and
-not ($content -match 'Microsoft\.NET\.Sdk\.Web') -and
-not ($content -match 'Microsoft\.NET\.Sdk\.Worker'))
}
foreach ($project in $libraryProjects) {
Write-Host "Packing $($project.BaseName)..." -ForegroundColor Yellow
dotnet pack $project.FullName `
--configuration Release `
--output $nugetDir `
-p:Version=$Version `
--no-build
}
# Copy documentation
Write-Host "`n=== Documentation ===" -ForegroundColor Cyan
$docsDistDir = Join-Path $DistDir 'docs'
if (Test-Path 'docs') {
Copy-Item -Path 'docs' -Destination $docsDistDir -Recurse
}
Copy-Item -Path 'README.md' -Destination $DistDir -ErrorAction SilentlyContinue
Copy-Item -Path 'LICENSE' -Destination $DistDir -ErrorAction SilentlyContinue
Copy-Item -Path 'CHANGELOG.md' -Destination $DistDir -ErrorAction SilentlyContinue
# Copy installer scripts
Write-Host "`n=== Installer Scripts ===" -ForegroundColor Cyan
$installerDistDir = Join-Path $DistDir 'installer'
if (Test-Path 'installer') {
Copy-Item -Path 'installer' -Destination $installerDistDir -Recurse
}
# Create version file
@{
Version = $Version
BuildDate = (Get-Date -Format 'o')
GitCommit = (git rev-parse HEAD 2>$null)
GitBranch = (git rev-parse --abbrev-ref HEAD 2>$null)
} | ConvertTo-Json | Set-Content (Join-Path $DistDir 'version.json')
Write-Host "`n=== Packaging Complete ===" -ForegroundColor Green
Write-Host "Distribution folder: $DistDir" -ForegroundColor Green
Write-Host "Version: $Version" -ForegroundColor Green
# List contents
Write-Host "`nContents:" -ForegroundColor Cyan
Get-ChildItem $DistDir -Recurse -File | ForEach-Object {
$relativePath = $_.FullName.Replace($DistDir, '.dist')
$sizeKB = [math]::Round($_.Length / 1KB, 1)
Write-Host " $relativePath ($sizeKB KB)" -ForegroundColor Gray
}
}
finally {
Pop-Location
}