public/Get-FixVersion.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<#
.SYNOPSIS
    Parse the specified version and create a fix version when needed.
 
.DESCRIPTION
    When a 4-part version number is used by the software authors, there
    may be a little hard at times to push out an updated version by manually
    creating the fix version.
    This script helps out with this when both a 4-part version is used, and
    when a prerelease have been passed.
 
.PARAMETER Version
    The version number to parse for a fix version
 
.PARAMETER OnlyFixBelowVersion
    The version to stop adding fix versions to
    (in case of padding revision number)
 
.PARAMETER AppendRevisionLength
    The number of zeros to append the revision number with, before adding the
    fix number
    (defaults to 1).
 
.PARAMETER NuspecFile
    The nuspec metadata file to parse for the existing version information
    (defaults to '.\*.nuspec')
 
.OUTPUTS
    Will output the full version number with a fix number when a 4-part version
    have been specified.
 
.OUTPUTS
    Will append the date to any pre-release versions.
 
.OUTPUTS
    Will just return the same version number if no fix is needed, or when the
    version number isn't a 4-part version.
 
.EXAMPLE
    PS> Get-FixVersion -Version '24.0.0.195'
 
    will output `24.0.0.19501` if the
    nuspec version is equal to `24.0.0.195` or `24.0.0.19500` and
    `$global:au_Force` is set to `$true`
 
.EXAMPLE
    PS> Get-FixVersion -Version '5.0-beta'
 
    will output `5.0-beta-20171123` (the current date)
 
.NOTES
    While the parameter `NuspecFile` accepts globbing patterns,
    it is expected to only match a single file.
 
.LINK
    https://wormiecorp.github.io/Wormies-AU-Helpers/docs/functions/get-fixversion
#>

function Get-FixVersion() {
    param(
        [ValidateScript( { Test-ValidVersion -Version $_ })]
        [parameter(Mandatory = $true)]
        [string]$Version,

        [string]$OnlyFixBelowVersion = $null,
        [Alias("AppendZeroes")]
        [int]$AppendRevisionLength = 1,
        [SupportsWildcards()]
        [string]$NuspecFile = "./*.nuspec"
    )
    function appendRevision {
        param($version, [int]$appendRevisionLength, [int]$existingRevision)

        [string]$newVersion = $version

        for ($i = $appendRevisionLength; $i -gt 0; $i--) {
            $newVersion += "0"
        }

        [int]$revision = $existingRevision + 1

        return $newVersion + $revision
    }

    function getExistingRevision {
        param($version, $existingVersion)

        $revision = $existingVersion -replace "^$version"
        if ($revision -match '^\d+$' -and $global:au_Force -eq $true) {
            return [int]$revision
        }
        elseif ($revision -match "^\d+$") {
            return ([int]$revision) - 1
        }
        elseif ($version -eq $existingVersion) {
            return 0
        }
        else {
            return -1
        }
    }

    if ($Version -match "^\d+(\.\d+){1,2}$") {
        return $Version
    }

    $NuspecFile = Resolve-Path $NuspecFile

    $existingVersion = Get-NuspecMetadata -nuspecFile $NuspecFile | ForEach-Object version

    if ($existingVersion -eq $Version -and $global:au_Force -ne $true) {
        return $Version
    }

    if ($Version -like '*-*') {
        [version]$mainVersion = $Version -split "-" | Select-Object -First 1
        $preRelease = "-" + ($Version -split "-" | Select-Object -First 1 -Skip 1)
    }
    else {
        [version]$mainVersion = $Version
        $preRelease = ""
    }

    if ($OnlyFixBelowVersion) {
        if ($OnlyFixBelowVersion -like '*-*') {
            [version]$belowVersion = $OnlyFixBelowVersion -split "-" | Select-Object -First 1
            $belowPreRelease = "-" + ($OnlyFixBelowVersion -split "-" | Select-Object -First 1 -Skip 1)
        }
        else {
            [version]$belowVersion = $OnlyFixBelowVersion
            $belowPreRelease = ""
        }

        if ($mainVersion -ge $belowVersion -and ($preRelease -ge $belowPreRelease)) {
            if (!$preRelease -and ([string]$existingVersion).StartsWith($mainVersion)) {
                return $existingVersion
            }
            elseif (([string]$existingVersion).StartsWith("${mainVersion}${preRelease}")) {
                if ($global:au_Force -ne $true) { return $existingVersion }
            }
            else {
                return $Version
            }
        }
        elseif ($mainVersion -eq $belowVersion -and !$preRelease -and $belowVersion) {
            if (([string]$existingVersion).StartsWith($mainVersion)) {
                return $existingVersion
            }
            else {
                return $Version
            }
        }
    }

    if (!($preRelease)) {
        $existingRevision = getExistingRevision -version $Version -existingVersion $existingVersion
        return appendRevision `
            -version $mainVersion `
            -existingRevision $existingRevision `
            -appendRevisionLength $AppendRevisionLength
    }
    else {
        return ([string]$mainVersion) + $preRelease + "-" + (Get-Date -UFormat "{0:yyyyMMdd}")
    }
}