-
Notifications
You must be signed in to change notification settings - Fork 0
/
nuget-version.ps1
115 lines (97 loc) · 3.25 KB
/
nuget-version.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
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
Param(
[string]$packageName
)
$nugetOriginalUrl = "http://dist.nuget.org/win-x86-commandline/latest/nuget.exe" #version 3.x
$buildDirectory = $env:BUILD_SOURCESDIRECTORY
# Helpers functions
function DownloadFile($source,$destination)
{
$client = New-Object System.Net.WebClient
$client.DownloadFile($source,$destination)
}
function disablePush()
{
}
function FindPackage($searchLoc, $packageFileName)
{
$findNuget = Get-ChildItem -Path $searchLoc -Filter $packageFileName -Recurse | Sort-Object LastWriteTime | Select-Object -Last 1
if($findNuget -eq $null)
{
Write-Host "Not found!" -ForegroundColor Red
return $null
}
return $findNuget.FullName
}
function FindNuget($searchLoc)
{
$findNuget = Get-ChildItem -Path $searchLoc -Filter "nuget.exe" -Recurse | Sort-Object LastWriteTime | Select-Object -Last 1
if($findNuget -eq $null)
{
Write-Host "Not found!" -ForegroundColor Red
return $null
}
return $findNuget.FullName
}
if(-not ($packageName -and $buildDirectory))
{
Write-Error "Running script from outside VSO Agent is currently not supported"
exit 1
}
Write-Host "Check if I can find default Nuget.exe" -NoNewline
$nuGetPath = ""
if(-not $nugetPath)
{
Write-Host "Not found!" -ForegroundColor Red
Write-Host "Check if I can find in working directory $PSScriptRoot " -NoNewline
$nugetPath = FindNuget -searchLoc $PSScriptRoot
if(-not $nugetPath)
{
Write-Host "Try to download from internet.." -NoNewline
DownloadFile -source $nugetOriginalUrl -destination $PSScriptRoot\nuget.exe
$nugetPath = FindNuget -searchLoc $PSScriptRoot
if(-not $nugetPath)
{
disablePush
throw("Can not find Nuget.exe")
}
}
Write-Host ""
}
Write-Host "Check last version of package $packageName online"
$OutputVariable = & $nugetPath list $packageName -PreRelease -NonInteractive -ForceEnglishOutput | Out-String
$firstLine = (($OutputVariable -split '\n')[0]).ToString().Trim()
$resultsPackageName = ($firstLine -split '\s+')[0]
$resultsPackageVersion = ($firstLine -split '\s+')[1]
$packageFileName = "$resultsPackageName.$resultsPackageVersion.nupkg"
if ($firstLine -contains "error")
{
disablePush
throw("ERROR: $firstLine")
}
if ($firstLine -eq "No packages found.")
{
Write-Host "Package not exists in Nuget Repository, you can push it."
Write-Output ("##vso[task.setvariable variable=NugetEnabled;]push")
}
if ($resultsPackageName -ne $packageName)
{
Write-Host "Package was not found. ERROR: $firstLine"
disablePush
}
else
{
Write-Host "The last version of package is $resultsPackageVersion"
Write-Host "Check if exists the same version on build dir $buildDirectory */ $packageFileName " -NoNewline
$packagePath = FindPackage -searchLoc $buildDirectory -packageFileName $packageFileName
Write-Host ""
if (-not $packagePath)
{
Write-Host "Package $packageName or file $packageFileName does not exists in this build, you can push it!"
Write-Output ("##vso[task.setvariable variable=NugetEnabled;]push")
}
else
{
Write-Warning "The package already exists on the build directory, you can't push the same package."
disablePush
}
}