-
Notifications
You must be signed in to change notification settings - Fork 7
/
Publish.ps1
75 lines (65 loc) · 2.99 KB
/
Publish.ps1
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
[CmdletBinding(SupportsShouldProcess=$True, ConfirmImpact="High")]
param (
[string]$Filter = ""
,[string]$NugetAPIKey
,[string]$Source='nuget.org'
,[switch]$SaveAPIKey
)
$projectFolders = Get-ChildItem $PSScriptRoot\IntelliTect.* -Directory -Filter $filter | Where-Object {
$_.Name -notlike '*.Tests'
}
$pacakgesToPublish = @()
Write-Progress -Activity "Publish IntelliTect TestTools" -Status "Searching for projects ready to publish"
if(!$projectFolders) {
throw "Nothing matches the filter, '$Filter'"
}
foreach ($item in $projectFolders){
$projectName = $item.Name
$Nupkg = Get-ChildItem $projectName $projectName*.nupkg -Recurse | Sort-Object Name -Descending | Select-Object -First 1
if($Nupkg) {
$localVersion = $Nupkg.Name | Where-Object{
$_ -match "$projectName\.(?<Version>.*)\.nupkg"
} | ForEach-Object {
$matches.Version
}
Write-Progress -Activity "Publish IntelliTect TestTools" -Status "Checking for previously published versions of '$projectName'"
$latestPublishedProjectVersion = nuget.exe list $projectName
[string]$lastPublishedVersion=$null
if($latestPublishedProjectVersion) {
if($latestPublishedProjectVersion -match "$projectName\s*(?<Version>\d{1,4}\.\d{1,4}\.\d{1,4})") {
$lastPublishedVersion = $matches.Version
}
elseif($latestPublishedProjectVersion -ne 'No packages found.') {
throw "Unable to determine version of $projectName from nuget output '$latestPublishedProjectVersion'"
}
}
if($localVersion -gt $lastPublishedVersion) {
$verbosityOption = if($PSBoundParameters['Verbose']){'-Verbosity','detailed'}
$apiKeyOption = if($NugetAPIKey){'-ApiKey',$NugetAPIKey}
$sourceOption = '-Source',$Source
if($PSCmdlet.ShouldProcess("Should publish '$Nupkg'")) {
Write-Progress -Activity "Publish IntelliTect TestTools" -Status "Publishing '$Nupkg'"
Write-Verbose "Executing: & nuget.exe push $Nupkg $sourceOption $apiKeyOption $verbosityOption 2>&1"
$output = & nuget.exe push $Nupkg.FullName $sourceOption $apiKeyOption $verbosityOption 2>&1
if($LASTEXITCODE -ne 0) {
$stdErr = @()
$stdOut = @()
$output | ForEach-Object {
if( $_ -is [System.Management.Automation.ErrorRecord] ) {
$stdErr += $_
}
else {
# $stdOut += $_ # Not currently used anywhere
}
}
throw $stdErr
}
if($PSCmdlet.ShouldProcess("Should publish '$Nupkg'")) {
if($SaveAPIKey -and $NugetAPIKey) {
nuget.exe setApiKey $NugetAPIKey $source $verbosity
}
}
}
}
}
}