Public/Get-NextPSGalleryVersion.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
function Get-NextPSGalleryVersion {
    <#
    .SYNOPSIS
        Get the next version for a module or script in the PowerShell Gallery

    .FUNCTIONALITY
        CI/CD

    .DESCRIPTION
        Get the next version for 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)

        This requires the PowerShellGet module

    .PARAMETER Name
        Name of the PowerShell module or script

    .PARAMETER Type
        Module or script. Defaults to module.

    .EXAMPLE
        Get-NextPSGalleryVersion PSDeploy

    .EXAMPLE
        Get-NextPSGalleryVersion Open-ISEFunction -Type Script

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

    .LINK
        about_BuildHelpers
    #>

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

        [parameter(ValueFromPipelineByPropertyName=$True)]
        [ValidateSet('Module', 'Script')]
        [string]$Type = 'Module',

        [string]$Repository = 'PSGallery'
    )
    Process
    {
        foreach($Item in $Name)
        {
            Try
            {
                $Existing = $null
                if($Type -eq 'Module')
                {
                    $Existing = Find-Module -Name $Item -Repository $Repository -ErrorAction Stop
                }
                else # Script
                {
                    $Existing = Find-Script -Name $Item -Repository $Repository -ErrorAction Stop
                }
            }
            Catch
            {
                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-Error "Found no $Type matching '$Item'"
                continue
            }
            else
            {
                $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)
            }
        }
    }
}