forked from ppn-systems/nalix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush-nuget.ps1
More file actions
141 lines (113 loc) · 4.1 KB
/
Copy pathpush-nuget.ps1
File metadata and controls
141 lines (113 loc) · 4.1 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
<#
.SYNOPSIS
Pushes NuGet packages (.nupkg) to a NuGet feed using nuget.exe.
.DESCRIPTION
- Reads API key from a local file (api.txt)
- Pushes all .nupkg in the artifacts folder
- Skips symbol packages by default (.snupkg)
- Can skip duplicates (recommended for CI)
.PREREQUISITES
- nuget.exe is present next to this script (or specify -NuGetExe)
- api.txt is present next to this script (or specify -ApiKeyFile)
- Packages exist in the artifacts folder (default: .\artifacts\nuget)
.USAGE
pwsh .\push-nuget.ps1
pwsh .\push-nuget.ps1 -Source "https://api.nuget.org/v3/index.json"
pwsh .\push-nuget.ps1 -WhatIf
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $false)]
[string]$PackagesDir = ".\artifacts\nuget",
[Parameter(Mandatory = $false)]
[string]$Source = "https://api.nuget.org/v3/index.json",
[Parameter(Mandatory = $false)]
[string]$NuGetExe = ".\nugets\nuget.exe",
[Parameter(Mandatory = $false)]
[string]$ApiKeyFile = ".\nugets\nuget.key",
[Parameter(Mandatory = $false)]
[switch]$SkipDuplicates,
[Parameter(Mandatory = $false)]
[switch]$IncludeSymbols
)
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
function Write-Section {
param([Parameter(Mandatory = $true)][string]$Title)
Write-Host ""
Write-Host ("=" * 72) -ForegroundColor DarkGray
Write-Host (" " + $Title) -ForegroundColor Cyan
Write-Host ("=" * 72) -ForegroundColor DarkGray
}
function Assert-FileExists {
param([Parameter(Mandatory = $true)][string]$Path, [Parameter(Mandatory = $true)][string]$Name)
if (-not (Test-Path -LiteralPath $Path)) {
throw "$Name not found: $Path"
}
}
try {
Write-Section "Nalix NuGet Push"
Assert-FileExists -Path $NuGetExe -Name "nuget.exe"
Assert-FileExists -Path $ApiKeyFile -Name "API key file (api.txt)"
if (-not (Test-Path -LiteralPath $PackagesDir)) {
throw "Packages directory not found: $PackagesDir"
}
$apiKey = (Get-Content -LiteralPath $ApiKeyFile -Raw).Trim()
if ([string]::IsNullOrWhiteSpace($apiKey)) {
throw "API key file is empty: $ApiKeyFile"
}
$pkgDirFull = (Resolve-Path -LiteralPath $PackagesDir).Path
$nugetFull = (Resolve-Path -LiteralPath $NuGetExe).Path
Write-Host "PackagesDir : $pkgDirFull"
Write-Host "Source : $Source"
Write-Host "nuget.exe : $nugetFull"
Write-Host "SkipDuplicates: $($SkipDuplicates.IsPresent)"
Write-Host "IncludeSymbols: $($IncludeSymbols.IsPresent)"
Write-Section "Discover Packages"
$packages = @(Get-ChildItem -LiteralPath $pkgDirFull -File |
Where-Object {
$_.Extension -ieq ".nupkg" -and
($IncludeSymbols.IsPresent -or $_.Name -notmatch '\.symbols\.nupkg$') -and
$_.Name -notmatch '\.snupkg$'
} |
Sort-Object Name)
if ($packages.Count -eq 0) {
Write-Host "No .nupkg packages found in: $pkgDirFull" -ForegroundColor Yellow
exit 0
}
foreach ($p in $packages) {
Write-Host ("- " + $p.Name) -ForegroundColor Green
}
Write-Section "Push"
foreach ($p in $packages) {
$args = @(
"push",
$p.FullName,
"-ApiKey", $apiKey,
"-Source", $Source,
"-NonInteractive"
)
if ($SkipDuplicates.IsPresent) {
# nuget.exe supports -SkipDuplicate (singular)
$args += "-SkipDuplicate"
}
$cmdLine = "nuget.exe " + ($args | ForEach-Object {
if ($_ -match '\s') { '"' + $_ + '"' } else { $_ }
} | Join-String -Separator " ")
if ($PSCmdlet.ShouldProcess($p.FullName, "Push package to $Source")) {
Write-Host ">> $cmdLine" -ForegroundColor DarkGray
& $nugetFull @args
if ($LASTEXITCODE -ne 0) {
throw "Push failed (exit $LASTEXITCODE): $($p.Name)"
}
}
}
Write-Host ""
Write-Host "Done." -ForegroundColor Green
}
catch {
Write-Host ""
Write-Host "FAILED: $($_.Exception.Message)" -ForegroundColor Red
Write-Host $_.ScriptStackTrace -ForegroundColor DarkGray
exit 1
}