Public/Get-NextNugetPackageVersion.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
116
117
118
119
120
121
function Get-NextNugetPackageVersion {
    <#
    .SYNOPSIS
        Get the next version for a nuget package, such as a module or script in the PowerShell Gallery

    .FUNCTIONALITY
        CI/CD

    .DESCRIPTION
        Get the next version for a nuget package, such as a module or script in the PowerShell Gallery

        Uses the versioning scheme adopted by the user

        Where possible, users should stick to semver: http://semver.org/ (Major.Minor.Patch, given restrictions .NET Version class)

        If no existing module is found, we return 0.0.1

    .PARAMETER Name
        Name of the nuget package

    .PARAMETER PackageSourceUrl
        Nuget PackageSourceUrl to query.
            PSGallery Module URL: https://www.powershellgallery.com/api/v2/ (default)
            PSGallery Script URL: https://www.powershellgallery.com/api/v2/items/psscript/

    .PARAMETER Credential
        Use if repository requires basic authentication

    .EXAMPLE
        Get-NextNugetPackageVersion PSDeploy

    .EXAMPLE
        Get-NextNugetPackageVersion Open-ISEFunction -PackageSourceUrl https://www.powershellgallery.com/api/v2/items/psscript/

    .LINK
        https://github.com/RamblingCookieMonster/BuildHelpers

    .LINK
        about_BuildHelpers
    #>

    [cmdletbinding()]
    param(
        [parameter(ValueFromPipelineByPropertyName=$True)]
        [string[]]$Name,

        [string]$PackageSourceUrl = 'https://www.powershellgallery.com/api/v2/',

        [PSCredential]$Credential 
    )
    Process
    {
        foreach($Item in $Name)
        {
            Try
            {
                $params = @{
                    Name = $Item
                }
                if($PSBoundParameters.ContainsKey('Credential')){
                    $Params.add('Credential', $Credential)
                }
                $Existing = $null
                $Existing = Find-NugetPackage @params -PackageSourceUrl $PackageSourceUrl -IsLatest -ErrorAction Stop
            }
            Catch
            {
                if($_ -match "No match was found for the specified search criteria")
                {
                    New-Object System.Version (0,0,1)
                }
                else
                {
                    Write-Error $_
                }
                continue
            }

            if($Existing.count -gt 1)
            {
                Write-Error "Found more than one $Type matching '$Item': Did you use a wildcard?"
                continue
            }
            elseif($Existing.count -eq 0)
            {
                Write-Verbose "Found no $Type matching '$Item'"
                New-Object System.Version (0,0,1)
                continue
            }
            else
            {
                $Version = [System.Version]$Existing.Version
            }

            # using revision
            if($Version.Revision -ge 0)
            {
                $Build = if($Version.Build -le 0) { 0 } else { $Version.Build }
                $Revision = if($Version.Revision -le 0) { 1 } else { $Version.Revision + 1 }
                New-Object System.Version ($Version.Major, $Version.Minor, $Build, $Revision)
            }
            # using build
            elseif($Version.Build -ge 0)
            {
                $Build = if($Version.Build -le 0) { 1 } else { $Version.Build + 1 }
                New-Object System.Version ($Version.Major, $Version.Minor, $Build)
            }
            # using minor. wat?
            elseif($Version.Minor -ge 0)
            {
                $Minor = if($Version.Minor -le 0) { 1 } else { $Version.Minor + 1 }
                New-Object System.Version ($Version.Major, $Minor)
            }
            # using major only. I don't even.
            else
            {
                New-Object System.Version ($Version.Major + 1, 0)
            }
        }
    }
}