-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish.ps1
More file actions
189 lines (156 loc) · 5.71 KB
/
publish.ps1
File metadata and controls
189 lines (156 loc) · 5.71 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
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[ValidateSet("patch", "minor", "major")]
[string]$Bump,
[Parameter(Mandatory = $true)]
[string]$ApiKey
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
function Get-VersionFromPropsFile {
param([Parameter(Mandatory = $true)][string]$PropsPath)
$text = Get-Content -Path $PropsPath -Raw
$match = [regex]::Match($text, "<Version>(?<version>[^<]+)</Version>")
if (-not $match.Success) {
throw "Could not find <Version> in $PropsPath"
}
return $match.Groups["version"].Value.Trim()
}
function Get-BumpedVersion {
param(
[Parameter(Mandatory = $true)][string]$CurrentVersion,
[Parameter(Mandatory = $true)][ValidateSet("patch", "minor", "major")][string]$Bump
)
if ($CurrentVersion -notmatch "^(\d+)\.(\d+)\.(\d+)$") {
throw "Unsupported version format '$CurrentVersion'. Expected 'major.minor.patch'."
}
$major = [int]$Matches[1]
$minor = [int]$Matches[2]
$patch = [int]$Matches[3]
switch ($Bump) {
"patch" {
$patch++
}
"minor" {
$minor++
$patch = 0
}
"major" {
$major++
$minor = 0
$patch = 0
}
}
return "$major.$minor.$patch"
}
function Set-VersionInPropsFile {
param(
[Parameter(Mandatory = $true)][string]$PropsPath,
[Parameter(Mandatory = $true)][string]$Version
)
$assemblyAndFileVersion = "$Version.0"
$text = Get-Content -Path $PropsPath -Raw
$text = $text -replace "<Version>[^<]+</Version>", "<Version>$Version</Version>"
if ($text -match "<AssemblyVersion>[^<]+</AssemblyVersion>") {
$text = $text -replace "<AssemblyVersion>[^<]+</AssemblyVersion>", "<AssemblyVersion>$assemblyAndFileVersion</AssemblyVersion>"
}
if ($text -match "<FileVersion>[^<]+</FileVersion>") {
$text = $text -replace "<FileVersion>[^<]+</FileVersion>", "<FileVersion>$assemblyAndFileVersion</FileVersion>"
}
Set-Content -Path $PropsPath -Value $text -Encoding utf8
}
function Get-PackageIdFromPropsFile {
param([Parameter(Mandatory = $true)][string]$PropsPath)
# Check Directory.Build.props first
$text = Get-Content -Path $PropsPath -Raw
$match = [regex]::Match($text, "<PackageId>(?<id>[^<]+)</PackageId>")
if ($match.Success) {
return $match.Groups["id"].Value.Trim()
}
# Check .csproj files in src directory
$repoRoot = Split-Path -Parent $PropsPath
$srcPath = Join-Path $repoRoot "src"
if (Test-Path $srcPath) {
$csprojFiles = @(Get-ChildItem -Path $srcPath -Recurse -Filter "*.csproj" -File -ErrorAction SilentlyContinue)
foreach ($csproj in $csprojFiles) {
$csprojText = Get-Content -Path $csproj.FullName -Raw
$csprojMatch = [regex]::Match($csprojText, "<PackageId>(?<id>[^<]+)</PackageId>")
if ($csprojMatch.Success) {
return $csprojMatch.Groups["id"].Value.Trim()
}
}
}
# If no PackageId is specified, derive from directory name
$dirName = Split-Path -Leaf $repoRoot
return $dirName
}
# Main script
$repoRoot = $PSScriptRoot
$propsPath = Join-Path $repoRoot "Directory.Build.props"
if (-not (Test-Path $propsPath)) {
throw "Missing file: $propsPath"
}
$packageId = Get-PackageIdFromPropsFile -PropsPath $propsPath
$currentVersion = Get-VersionFromPropsFile -PropsPath $propsPath
$targetVersion = Get-BumpedVersion -CurrentVersion $currentVersion -Bump $Bump
Write-Host "Building and publishing $packageId package..." -ForegroundColor Cyan
Write-Host "Version: $currentVersion -> $targetVersion"
Write-Host ""
# Update version
Set-VersionInPropsFile -PropsPath $propsPath -Version $targetVersion
try {
# Find solution or project file
$slnFiles = @(Get-ChildItem -Path $repoRoot -Filter "*.sln" -File -ErrorAction SilentlyContinue)
if ($slnFiles.Count -eq 1) {
$packTarget = $slnFiles[0].FullName
} else {
$srcPath = Join-Path $repoRoot "src"
$csprojFiles = @(
Get-ChildItem -Path $srcPath -Recurse -Filter "*.csproj" -File -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -notmatch "\\(bin|obj)\\" }
)
if ($csprojFiles.Count -eq 1) {
$packTarget = $csprojFiles[0].FullName
} else {
throw "Could not find a single .sln or .csproj file"
}
}
$artifactsDir = Join-Path $repoRoot "artifacts"
New-Item -ItemType Directory -Force -Path $artifactsDir | Out-Null
# Step 1: Restore
Write-Host "Step 1: Restoring dependencies..." -ForegroundColor Yellow
dotnet restore $packTarget
if ($LASTEXITCODE -ne 0) { throw "dotnet restore failed" }
# Step 2: Build
Write-Host ""
Write-Host "Step 2: Building..." -ForegroundColor Yellow
dotnet build $packTarget -c Release --no-restore
if ($LASTEXITCODE -ne 0) { throw "dotnet build failed" }
# Step 3: Pack
Write-Host ""
Write-Host "Step 3: Packing..." -ForegroundColor Yellow
dotnet pack $packTarget -c Release -o $artifactsDir --no-build /p:ContinuousIntegrationBuild=true
if ($LASTEXITCODE -ne 0) { throw "dotnet pack failed" }
# Step 4: Push
Write-Host ""
Write-Host "Step 4: Pushing version $targetVersion to NuGet.org..." -ForegroundColor Yellow
$nupkgPath = Join-Path $artifactsDir "$packageId.$targetVersion.nupkg"
if (-not (Test-Path $nupkgPath)) {
throw "Package not found: $nupkgPath"
}
dotnet nuget push $nupkgPath --api-key $ApiKey --source "https://api.nuget.org/v3/index.json" --skip-duplicate
if ($LASTEXITCODE -eq 0) {
Write-Host ""
Write-Host "Successfully published $packageId $targetVersion to NuGet.org!" -ForegroundColor Green
} else {
$exitCode = $LASTEXITCODE
throw "Failed to push package (exit code: $exitCode)"
}
}
catch {
# Rollback version on failure
Set-VersionInPropsFile -PropsPath $propsPath -Version $currentVersion
Write-Warning "Rolled back version change in Directory.Build.props due to failure."
throw
}