Skip to content

Commit d883424

Browse files
committed
Fixed release note bug. Added automatic increment of version with tests
1 parent dc17abe commit d883424

3 files changed

Lines changed: 178 additions & 64 deletions

File tree

PSModuleBuild.tests.ps1

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#Set up
22
$ScriptPath = $PSScriptRoot
3-
. $ScriptPath\Source\Invoke-PSModuleBuild.ps1
3+
. $ScriptPath\Source\Public\Invoke-PSModuleBuild.ps1
4+
. $ScriptPath\Source\Private\CreateUpdateManifest.ps1
45

56
#Clean up
67
Remove-Item $ScriptPath\Test-Module -Recurse -Force -ErrorAction SilentlyContinue
@@ -57,6 +58,10 @@ Function check-m2e{ <#this is a test#> }
5758
$Search = Select-String -Path $ScriptPath\Test-Module\Test-Module.psd1 -Pattern "PowerShellVersion = '3.0'"
5859
$Search.Count | Should Be 1
5960
}
61+
It "Module Version set to 1.0" {
62+
$Search = Select-String -Path $ScriptPath\Test-Module\Test-Module.psd1 -Pattern "ModuleVersion = '1.0'"
63+
$Search.Count | Should Be 1
64+
}
6065
It "Private function exists in module" {
6166
$Search = Select-String -Path $ScriptPath\Test-Module\Test-Module.psm1 -Pattern "Function Private-Function { <#this is a test#> }"
6267
$Search.Count | Should Be 1
@@ -99,6 +104,10 @@ Function Test2
99104
$Search = Select-String -Path $ScriptPath\Test-Module\Test-Module.psd1 -Pattern "PowerShellVersion = '3.0'"
100105
$Search.Count | Should Be 1
101106
}
107+
It "Module Version set to 1.1" {
108+
$Search = Select-String -Path $ScriptPath\Test-Module\Test-Module.psd1 -Pattern "ModuleVersion = '1.1'"
109+
$Search.Count | Should Be 1
110+
}
102111
It "Private function exists in module" {
103112
$Search = Select-String -Path $ScriptPath\Test-Module\Test-Module.psm1 -Pattern "Function Private-Function { <#this is a test#> }"
104113
$Search.Count | Should Be 1
@@ -112,6 +121,50 @@ Function Test2
112121
$Search.Count | Should Be 1
113122
}
114123
}
124+
Context "Automatic Version increments" {
125+
It "Update Build - No version increment" {
126+
{ Invoke-PSModuleBuild -Path $ScriptPath\Test-Module -IncrementVersion None } | Should Not Throw
127+
}
128+
It "ModuleVersion still 1.1" {
129+
$Search = Select-String -Path $ScriptPath\Test-Module\Test-Module.psd1 -Pattern "ModuleVersion = '1.1'"
130+
$Search.Count | Should Be 1
131+
}
132+
It "Update Build - increment revision, when build and revision don't exist" {
133+
{ Invoke-PSModuleBuild -Path $ScriptPath\Test-Module -IncrementVersion Revision } | Should Not Throw
134+
}
135+
It "ModuleVersion set to 1.1.0.1" {
136+
$Search = Select-String -Path $ScriptPath\Test-Module\Test-Module.psd1 -Pattern "ModuleVersion = '1.1.0.1'"
137+
$Search.Count | Should Be 1
138+
}
139+
It "Update Build - increment Major" {
140+
{ Invoke-PSModuleBuild -Path $ScriptPath\Test-Module -IncrementVersion Major } | Should Not Throw
141+
}
142+
It "ModuleVersion set to 2.1.0.1" {
143+
$Search = Select-String -Path $ScriptPath\Test-Module\Test-Module.psd1 -Pattern "ModuleVersion = '2.1.0.1'"
144+
$Search.Count | Should Be 1
145+
}
146+
It "Update Build - increment Minor" {
147+
{ Invoke-PSModuleBuild -Path $ScriptPath\Test-Module -IncrementVersion Minor } | Should Not Throw
148+
}
149+
It "ModuleVersion set to 2.2.0.1" {
150+
$Search = Select-String -Path $ScriptPath\Test-Module\Test-Module.psd1 -Pattern "ModuleVersion = '2.2.0.1'"
151+
$Search.Count | Should Be 1
152+
}
153+
It "Update Build - increment Build" {
154+
{ Invoke-PSModuleBuild -Path $ScriptPath\Test-Module -IncrementVersion Build } | Should Not Throw
155+
}
156+
It "ModuleVersion set to 2.2.1.1" {
157+
$Search = Select-String -Path $ScriptPath\Test-Module\Test-Module.psd1 -Pattern "ModuleVersion = '2.2.1.1'"
158+
$Search.Count | Should Be 1
159+
}
160+
It "Update Build - increment Revision" {
161+
{ Invoke-PSModuleBuild -Path $ScriptPath\Test-Module -IncrementVersion Revision } | Should Not Throw
162+
}
163+
It "ModuleVersion set to 2.2.1.2" {
164+
$Search = Select-String -Path $ScriptPath\Test-Module\Test-Module.psd1 -Pattern "ModuleVersion = '2.2.1.2'"
165+
$Search.Count | Should Be 1
166+
}
167+
}
115168
Context "Specify Module Name build" {
116169
It "Update Build" {
117170
{ Invoke-PSModuleBuild -Path $ScriptPath\Test-Module -ModuleName MyModule } | Should Not Throw
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
Function CreateUpdateManifest
2+
{
3+
[CmdletBinding()]
4+
Param (
5+
[hashtable]$Manifest,
6+
[string]$OldManifestPath
7+
)
8+
9+
If (Test-Path $OldManifestPath)
10+
{
11+
$OldManifest = Import-LocalizedData -BaseDirectory (Split-Path $OldManifestPath) -FileName (Split-Path $OldManifestPath -Leaf)
12+
If ([version]$OldManifest.PowerShellVersion -gt $HighVersion)
13+
{
14+
$HighVersion = [version]$OldManifest.PowerShellVersion
15+
}
16+
If ($Manifest.PowerShellVersion -gt $HighVersion)
17+
{
18+
$HighVersion = $Manifest.PowerShellVersion
19+
}
20+
$Manifest.ReleaseNotes = $ReleaseNotes + $OldManifest.PrivateData.PSData.ReleaseNotes
21+
$Manifest.Path = $OldManifestPath
22+
$Manifest.PowerShellVersion = $HighVersion
23+
$Manifest.FunctionsToExport = $FunctionNames | Where Private -eq $false | Select -ExpandProperty Name
24+
If (-not $Manifest.ModuleVersion)
25+
{
26+
$VersionNum = [ordered]@{}
27+
$VersionFields = "Major","Minor","Build","Revision"
28+
$Count = 0
29+
ForEach ($VersionField in $VersionFields)
30+
{
31+
$VersionNum.Add($VersionField,$Count)
32+
$Count ++
33+
}
34+
Try {
35+
$OldModuleVersion = [version]$OldManifest.ModuleVersion
36+
}
37+
Catch {}
38+
If ($OldModuleVersion -is [version])
39+
{
40+
$Versions = @()
41+
ForEach ($Num in (0..3))
42+
{
43+
$VF = $VersionFields[$Num]
44+
If ($OldModuleVersion.$VF -lt 0 -and $VersionNum[$IncrementVersion] -gt $VersionNum[$Num])
45+
{
46+
$Versions += 0
47+
}
48+
ElseIf ($OldModuleVersion.$VF -lt 0 -and $IncrementVersion -eq $VF)
49+
{
50+
$Versions += 1
51+
}
52+
ElseIf ($OldModuleVersion.$VF -ge 0)
53+
{
54+
If ($VF -eq $IncrementVersion)
55+
{
56+
$Versions += $OldModuleVersion.$VF + 1
57+
}
58+
Else
59+
{
60+
$Versions += $OldModuleVersion.$VF
61+
}
62+
}
63+
}
64+
If ($IncrementVersion -eq "Last")
65+
{
66+
$Versions[-1] ++
67+
}
68+
$Manifest.ModuleVersion = $Versions -join "."
69+
}
70+
}
71+
72+
Update-ModuleManifest @Manifest
73+
}
74+
Else
75+
{
76+
$Manifest.RootModule = $ModuleName
77+
$Manifest.Path = $OldManifestPath
78+
$Manifest.PowerShellVersion = "$($HighVersion.Major).$($HighVersion.Minor)"
79+
$Manifest.FunctionsToExport = $FunctionNames | Where Private -eq $false | Select -ExpandProperty Name
80+
If ($ReleaseNotes)
81+
{
82+
$Manifest.ReleaseNotes = $ReleaseNotes | Out-String
83+
}
84+
New-ModuleManifest @Manifest
85+
}
86+
87+
Return $Manifest
88+
}
Lines changed: 36 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ Function Invoke-PSModuleBuild {
4848
Any release notes you want to include in the module manifest. If a manifest file already exists Invoke-PSModuleBuild will
4949
read the release notes from it and join the new release notes together.
5050
51+
.PARAMETER IncrementVersion
52+
If a previous manifest file is located, PSModuleBuild will automatically increment the version number (unless you set this parameter to
53+
"None"). Set which field you want to increment "major", "minor", "build" or "revision" or set it to "last" and it will increment the last
54+
field that previous existed. For example:
55+
56+
1.1 would become 1.2
57+
1.0.0.3 would become 1.0.0.4
58+
59+
Default settings is "Last"
60+
5161
.INPUTS
5262
None
5363
@@ -88,6 +98,7 @@ Function Invoke-PSModuleBuild {
8898
1.0.14 Rename to Invoke-PSModuleBuild and create module named PSModuleBuild. Added ReleaseNotes support (New and Update-ModuleManifest treat ReleaseNotes differently)
8999
1.0.15 Updated comment based help
90100
1.1 Added multiple target paths
101+
1.1.38 Fixed bug with release notes. Added IncrementVersion
91102
.LINK
92103
https://github.com/martin9700/PSModuleBuild
93104
#>
@@ -98,7 +109,9 @@ Function Invoke-PSModuleBuild {
98109
[string[]]$TargetPath,
99110
[string]$ModuleName,
100111
[switch]$Passthru,
101-
[string[]]$ReleaseNotes
112+
[string[]]$ReleaseNotes,
113+
[ValidateSet("None","Last","Major","Minor","Build","Revision")]
114+
[string]$IncrementVersion = "Last"
102115
)
103116
DynamicParam {
104117
# Create the dictionary that this scriptblock will return:
@@ -129,7 +142,7 @@ Function Invoke-PSModuleBuild {
129142
$DynParamDictionary
130143
}
131144

132-
PROCESS {
145+
END {
133146
Write-Verbose "$(Get-Date): Invoke-PSModuleBuild started"
134147

135148
If (-not $Path)
@@ -216,79 +229,39 @@ Function Invoke-PSModuleBuild {
216229

217230
#Create the manifest file
218231
Write-Verbose "$(Get-Date): Creating/Updating module manifest and module file"
219-
$Manifest = @{}
232+
$NewManifest = @{}
220233

221234

222-
ForEach ($Key in ($PSBoundParameters.GetEnumerator() | Where { $_.Key -NotMatch "Path|Passthru|ModuleName" -and $CommonParams -notcontains $_.Key }))
235+
ForEach ($Key in ($PSBoundParameters.GetEnumerator() | Where { $_.Key -NotMatch "Path|Passthru|ModuleName|IncrementVersion" -and $CommonParams -notcontains $_.Key }))
223236
{
224-
$Manifest.Add($Key.Key,$Key.Value)
237+
$NewManifest.Add($Key.Key,$Key.Value)
225238
}
226239
ForEach ($TP in $TargetPath)
227240
{
241+
#Save the manifest
228242
$ManifestPath = Join-Path -Path $TP -ChildPath "$ModuleName.psd1"
229-
If (Test-Path $ManifestPath)
230-
{
231-
$OldManifest = Import-LocalizedData -BaseDirectory (Split-Path $ManifestPath) -FileName (Split-Path $ManifestPath -Leaf)
232-
If ([version]$OldManifest.PowerShellVersion -gt $HighVersion)
233-
{
234-
$HighVersion = [version]$OldManifest.PowerShellVersion
235-
}
236-
If ($OldManifest.PrivateData.PSData.ReleaseNotes)
237-
{
238-
$Manifest.ReleaseNotes = $ReleaseNotes + $OldManifest.PrivateData.PSData.ReleaseNotes
239-
}
240-
If ($Manifest.PowerShellVersion -gt $HighVersion)
241-
{
242-
$HighVersion = $Manifest.PowerShellVersion
243-
}
244-
$Manifest.Path = $ManifestPath
245-
$Manifest.PowerShellVersion = $HighVersion
246-
$Manifest.FunctionsToExport = $FunctionNames | Where Private -eq $false | Select -ExpandProperty Name
247-
If ($BuildVersion)
248-
{
249-
$Manifest.Add("ModuleVersion",$BuildVersion)
250-
}
251-
Update-ModuleManifest @Manifest
252-
}
253-
Else
254-
{
255-
$Manifest.RootModule = $ModuleName
256-
$Manifest.Path = $ManifestPath
257-
$Manifest.PowerShellVersion = "$($HighVersion.Major).$($HighVersion.Minor)"
258-
$Manifest.FunctionsToExport = $FunctionNames | Where Private -eq $false | Select -ExpandProperty Name
259-
If ($BuildVersion)
260-
{
261-
$Manifest.Add("ModuleVersion",$BuildVersion)
262-
}
263-
If ($ReleaseNotes)
264-
{
265-
$Manifest.ReleaseNotes = $ReleaseNotes | Out-String
266-
}
267-
New-ModuleManifest @Manifest
268-
}
269-
}
243+
$ResultManifest = CreateUpdateManifest -Manifest $NewManifest.Clone() -OldManifestPath $ManifestPath
270244

271-
#Save the Module file
272-
ForEach ($TP in $TargetPath)
273-
{
245+
#Save the module file
274246
$ModulePath = Join-Path -Path $TP -ChildPath "$ModuleName.psm1"
275247
$Module | Out-File $ModulePath -Encoding ascii
276248
Write-Verbose "Module created at: $TP as $ModuleName" -Verbose
277-
}
278249

279-
#Passthru
280-
If ($Passthru)
281-
{
282-
[PSCustomObject]@{
283-
Name = $ModuleName
284-
SourcePath = $Path
285-
TargetPath = $TargetPath
286-
ManifestPath = $ManifestPath
287-
ModulePath = $ModulePath
288-
RequiredVersion = $Manifest.PowerShellVersion
289-
PublicFunctions = @($FunctionNames | Where Private -eq $false | Select -ExpandProperty Name)
290-
PrivateFunctions = @($FunctionNames | Where Private -eq $true | Select -ExpandProperty Name)
291-
ReleaseNotes = $Manifest.ReleaseNotes
250+
#Passthru
251+
If ($Passthru)
252+
{
253+
[PSCustomObject]@{
254+
Name = $ModuleName
255+
SourcePath = $Path
256+
TargetPath = $TargetPath
257+
ManifestPath = $ManifestPath
258+
ModulePath = $ModulePath
259+
ModuleVersion = $ResultManifest.ModuleVersion
260+
RequiredVersion = $ResultManifest.PowerShellVersion
261+
PublicFunctions = @($FunctionNames | Where Private -eq $false | Select -ExpandProperty Name)
262+
PrivateFunctions = @($FunctionNames | Where Private -eq $true | Select -ExpandProperty Name)
263+
ReleaseNotes = $ResultManifest.ReleaseNotes
264+
}
292265
}
293266
}
294267

0 commit comments

Comments
 (0)