src/module/manifest/Update-SemVer.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using module .\..\Get-MKModuleInfo.ps1
using module .\Get-GitBranchName.ps1

# RegEx pattern is from here: https://regex101.com/r/gG8cK7/1
$script:SemVerRegEx = "^(?'MAJOR'0|(?:[1-9]\d*))\.(?'MINOR'0|(?:[1-9]\d*))\.(?'PATCH'0|(?:[1-9]\d*))(?:-(?'prerelease'(?:0|(?:[1-9A-Za-z-][0-9A-Za-z-]*))(?:\.(?:0|(?:[1-9A-Za-z-][0-9A-Za-z-]*)))*))?(?:\+(?'build'(?:0|(?:[1-9A-Za-z-][0-9A-Za-z-]*))(?:\.(?:0|(?:[1-9A-Za-z-][0-9A-Za-z-]*)))*))?$"

function Update-SemVer {
    [CmdletBinding(PositionalBinding = $true, 
        DefaultParameterSetName = "ByPath")]
    Param
    (
        [Parameter(Mandatory = $false,
            Position = 0,
            ValueFromPipeline = $false, 
            ParameterSetName = "ByPath")]
        [string]$Path = '.',
        
        [Parameter(Mandatory = $false, 
            ParameterSetName = "ByValue")]
        [ValidatePattern("^(?'MAJOR'0|(?:[1-9]\d*))\.(?'MINOR'0|(?:[1-9]\d*))\.(?'PATCH'0|(?:[1-9]\d*))(?:-(?'prerelease'(?:0|(?:[1-9A-Za-z-][0-9A-Za-z-]*))(?:\.(?:0|(?:[1-9A-Za-z-][0-9A-Za-z-]*)))*))?(?:\+(?'build'(?:0|(?:[1-9A-Za-z-][0-9A-Za-z-]*))(?:\.(?:0|(?:[1-9A-Za-z-][0-9A-Za-z-]*)))*))?$")]
        [Parameter(ParameterSetName = "ByPath")]
        [Parameter(ParameterSetName = "ByName")]
        [String]$Value,

        [Parameter(Mandatory = $false,
            Position = 1, 
            ParameterSetName = "ByNumbers")]
        [Parameter(Mandatory = $false,
            Position = 1, 
            ParameterSetName = "ByPath")]
        [Parameter(Mandatory = $false,
            Position = 1, 
            ParameterSetName = "ByName")]
        [int]$Major = -1,

        [Parameter(Mandatory = $false,
            Position = 2, 
            ParameterSetName = "ByNumbers")]
        [Parameter(Mandatory = $false,
            Position = 2, 
            ParameterSetName = "ByPath")]
        [Parameter(Mandatory = $false,
            Position = 2, 
            ParameterSetName = "ByName")]
        [int]$Minor = -1,

        [Parameter(Mandatory = $false,
            Position = 3, 
            ParameterSetName = "ByNumbers")]
        [Parameter(Mandatory = $false,
            Position = 3, 
            ParameterSetName = "ByPath")]
        [Parameter(Mandatory = $false,
            Position = 3, 
            ParameterSetName = "ByName")]
        [int]$Patch = -1,

        [Parameter(Mandatory = $false,
            Position = 4)]
        [string]$SourceFolderPath = 'src',

        [Parameter(ParameterSetName = "ByBumping")]
        [Parameter(ParameterSetName = "ByPath")]
        [Parameter(ParameterSetName = "ByName")]
        [switch]$BumpMajor,

        [Parameter(ParameterSetName = "ByBumping")]
        [Parameter(ParameterSetName = "ByPath")]
        [Parameter(ParameterSetName = "ByName")]
        [switch]$BumpMinor,
        
        [Parameter(ParameterSetName = "ByBumping")]
        [Parameter(ParameterSetName = "ByPath")]
        [Parameter(ParameterSetName = "ByName")]
        [switch]$BumpPatch,

        [Parameter(ParameterSetName = "ByAutoUpdateSemVer")]
        [Parameter(ParameterSetName = "ByPath")]
        [Parameter(ParameterSetName = "ByName")]
        [switch]$AutoUpdate
    )
    
    DynamicParam {
        return GetModuleNameSet -Position 0 -Mandatory 
    }

    begin {
        $Name = $PSBoundParameters['Name']
    }

    end {
        if ($PSBoundParameters.Name) {
            $ModInfo = Get-MKModuleInfo -Name $Name
        }
        else {
            $ModInfo = Get-MKModuleInfo -Path $Path
        }

        # PowerShellGet's Update-ModuleManifest currently has a bug that is reverting previous
        # changed values. https://github.com/PowerShell/PowerShell/issues/7181
        $ManifestContentRaw = Get-Content -Path $ModInfo.ManifestFilePath -Raw
            
        if ($AutoUpdate.IsPresent) {
            $CurrentBranchName = Get-GitBranchName -Path ($ModInfo.Path)

            if ($CurrentBranchName -match $script:SemVerRegEx) {
                $Version = $ModInfo.Version
                # using '-gt' comparison because developer may have bumped or explictly set semver
                # to something greater than what Git branch they are on. although if they explictly
                # set a lower value it will be overwritten.
                if (($Matches.MAJOR -gt $Version.Major) -or `
                    ($Matches.MINOR -gt $Version.Minor) -or `
                    ($Matches.PATCH -gt $Version.Build)) {
                    $Value = $CurrentBranchName 
                }
            }
            
            # since $AutoUpdate is present, lets update URIs
            # Using Import-PowerShellDataFile for convenience to check keys
            $Manifest = Import-PowerShellDataFile -Path $ModInfo.ManifestFilePath
            
            $Name = $ModInfo.Name

            # expecting: ...<ModuleName>/tree/<AnyValue>
            if ($Manifest.ContainsKey('HelpInfoURI')) {
                if ($ManifestContentRaw -match "(?<=HelpInfoURI)(?:[\s]*[=][\s]*)(\'.*\')") {
                    $NuHelpInfoUri = [regex]::Replace($Matches[1].Trim(), "(?<=$Name[\\|\/]tree[\\|\/]).*(?=\')", $CurrentBranchName)
                    $ManifestContentRaw = [regex]::Replace($ManifestContentRaw, "(?<=HelpInfoURI)(?:[\s]*[=][\s]*)(\'.*\')", " = $NuHelpInfoUri")
                }
            }
    
            if ($Manifest.ContainsKey('PrivateData')) {
                $PSData = $Manifest.PrivateData.PSData

                # expecting: .../<AnyValue>/LICENSE
                if ($PSData.ContainsKey('LicenseUri')) {
                    if ($ManifestContentRaw -match "(?<=LicenseUri)(?:[\s]*[=][\s]*)(\'.*\')") {
                        $NuLicenseUri = [regex]::Replace($Matches[1].Trim(), "[^\\|\/]+?(?=[\\|\/]LICENSE)", $CurrentBranchName)
                        $ManifestContentRaw = [regex]::Replace($ManifestContentRaw, "(?<=LicenseUri)(?:[\s]*[=][\s]*)(\'.*\')", " = $NuLicenseUri")

                    }
                }

                # expecting: ...<ModuleName>/tree/<AnyValue>
                if ($PSData.ContainsKey('IconUri')) {
                    if ($ManifestContentRaw -match "(?<=IconUri)(?:[\s]*[=][\s]*)(\'.*\')") {
                        $NuIconUri = [regex]::Replace($Matches[1].Trim(), "(?<=$Name[\\|\/]tree[\\|\/]).*(?=\')", $CurrentBranchName)
                        $ManifestContentRaw = [regex]::Replace($ManifestContentRaw, "(?<=IconUri)(?:[\s]*[=][\s]*)(\'.*\')", " = $NuIconUri")
                    }
                }
            }
        }
        elseif (-not $Value) {
            $Version = $ModInfo.Version

            if ($Major -eq -1) {
                $Major = $Version.Major
            }

            if ($Minor -eq -1) {
                $Minor = $Version.Minor
            }

            if ($Patch -eq -1) {
                $Patch = $Version.Build
            }

            if ($BumpMajor.IsPresent) {
                $Major += 1
            }
        
            if ($BumpMinor.IsPresent) {
                $Minor += 1
            }
        
            if ($BumpPatch.IsPresent) {
                $Patch += 1
            }

            $Value = "$Major.$Minor.$Patch"
        }

        if ($Value) {
            if ($ManifestContentRaw -match "(?<=ModuleVersion).*") {
                $ManifestContentRaw = [regex]::Replace($ManifestContentRaw, $Matches[0], " = '$Value'")
            }

            Set-Content -Path $ModInfo.ManifestFilePath -Value $ManifestContentRaw -NoNewline

            if ($AutoUpdate.IsPresent) {
                Write-Host "Module version has been changed to '$Value'" -ForegroundColor Green
            }
            else {
                $Value
            }
        }
    }
}