@@ -29,9 +29,18 @@ function Build-Module {
2929 Build-Module -Prefix "using namespace System.Management.Automation"
3030
3131 This example shows how to build a simple module from it's manifest, adding a using statement at the top as a prefix
32+
33+ . Example
34+ $gitVersion = gitversion | ConvertFrom-Json | Select -Expand InformationalVersion
35+ Build-Module -SemVer $gitVersion
36+
37+ This example shows how to use a semantic version from gitversion to version your build.
38+ Note, this is how we version ModuleBuilder, so if you want to see it in action, check out our azure-pipelines.yml
39+ https://github.com/PoshCode/ModuleBuilder/blob/master/azure-pipelines.yml
3240 #>
3341 [Diagnostics.CodeAnalysis.SuppressMessageAttribute (" PSUseApprovedVerbs" , " " , Justification= " Build is approved now" )]
3442 [Diagnostics.CodeAnalysis.SuppressMessageAttribute (" PSUseCmdletCorrectly" , " " )]
43+ [CmdletBinding (DefaultParameterSetName = " SemanticVersion" )]
3544 param (
3645 # The path to the module folder, manifest or build.psd1
3746 [Parameter (Position = 0 , ValueFromPipelineByPropertyName )]
@@ -50,8 +59,26 @@ function Build-Module {
5059 [Alias (" Destination" )]
5160 [string ]$OutputDirectory ,
5261
62+ # Semantic version, like 1.0.3-beta01+sha.22c35ffff166f34addc49a3b80e622b543199cc5
63+ # If the SemVer has metadata (after a +), then the full Semver will be added to the ReleaseNotes
64+ [Parameter (ParameterSetName = " SemanticVersion" )]
65+ [string ]$SemVer ,
66+
67+ # The module version (must be a valid System.Version such as PowerShell supports for modules)
5368 [Alias (" ModuleVersion" )]
54- [version ]$Version ,
69+ [Parameter (ParameterSetName = " ModuleVersion" , Mandatory )]
70+ [version ]$Version = $ (if ($V = $SemVer.Split (" +" )[0 ].Split(" -" )[0 ]){$V }),
71+
72+ # Setting pre-release forces the release to be a pre-release.
73+ # Must be valid pre-release tag like PowerShellGet supports
74+ [Parameter (ParameterSetName = " ModuleVersion" )]
75+ [string ]$Prerelease = $ ($SemVer.Split (" +" )[0 ].Split(" -" )[1 ]),
76+
77+ # Build metadata (like the commit sha or the date).
78+ # If a value is provided here, then the full Semantic version will be inserted to the release notes:
79+ # Like: ModuleName v(Version(-Prerelease?)+BuildMetadata)
80+ [Parameter (ParameterSetName = " ModuleVersion" )]
81+ [string ]$BuildMetadata = $ ($SemVer.Split (" +" )[1 ]),
5582
5683 # Folders which should be copied intact to the module output
5784 # Can be relative to the module folder
@@ -101,6 +128,18 @@ function Build-Module {
101128 }
102129 process {
103130 try {
131+ # BEFORE we InitializeBuild we need to "fix" the version
132+ if ($PSCmdlet.ParameterSetName -ne " SemanticVersion" ) {
133+ Write-Verbose " Calculate the Semantic Version from the $Version - $Prerelease + $BuildMetadata "
134+ $SemVer = $Version
135+ if ($Prerelease ) {
136+ $SemVer = $Version + ' -' + $Prerelease
137+ }
138+ if ($BuildMetadata ) {
139+ $SemVer = $SemVer + ' +' + $BuildMetadata
140+ }
141+ }
142+
104143 # Push into the module source (it may be a subfolder)
105144 $ModuleInfo = InitializeBuild $SourcePath
106145 # Output file names
@@ -159,10 +198,43 @@ function Build-Module {
159198 }
160199 }
161200
162- Write-Verbose " Update Manifest to $OutputManifest "
201+ try {
202+ if ($Version ) {
203+ Write-Verbose " Update Manifest at $OutputManifest with version: $Version "
204+ Update-Metadata - Path $OutputManifest - PropertyName ModuleVersion - Value $Version
205+ }
206+ } catch {
207+ Write-Warning " Failed to update version to $Version . $_ "
208+ }
209+
210+ if ($Prerelease ) {
211+ Write-Verbose " Update Manifest at $OutputManifest with Prerelease: $Prerelease "
212+ Update-Metadata - Path $OutputManifest - PropertyName PrivateData.PSData.Prerelease - Value $Prerelease
213+ } else {
214+ Update-Metadata - Path $OutputManifest - PropertyName PrivateData.PSData.Prerelease - Value " "
215+ }
163216
164- if ($Version ) {
165- Update-Metadata - Path $OutputManifest - PropertyName ModuleVersion - Value $Version
217+ if ($BuildMetadata ) {
218+ Write-Verbose " Update Manifest at $OutputManifest with metadata: $BuildMetadata from $SemVer "
219+ $RelNote = Get-Metadata - Path $OutputManifest - PropertyName PrivateData.PSData.ReleaseNotes - ErrorAction SilentlyContinue
220+ if ($null -ne $RelNote ) {
221+ $Line = " $ ( $ModuleInfo.Name ) v$ ( $SemVer ) "
222+ if ([string ]::IsNullOrWhiteSpace($RelNote )) {
223+ Write-Verbose " New ReleaseNotes:`n $Line "
224+ Update-Metadata - Path $OutputManifest - PropertyName PrivateData.PSData.ReleaseNotes - Value $Line
225+ } elseif ($RelNote -match " ^\s*\n" ) {
226+ # Leading whitespace includes newlines
227+ Write-Verbose " Existing ReleaseNotes:$RelNote "
228+ $RelNote = $RelNote -replace " ^(?s)(\s*)\S.*$|^$" , " `$ {1}$ ( $Line ) `$ _"
229+ Write-Verbose " New ReleaseNotes:$RelNote "
230+ Update-Metadata - Path $OutputManifest - PropertyName PrivateData.PSData.ReleaseNotes - Value $RelNote
231+ } else {
232+ Write-Verbose " Existing ReleaseNotes:`n $RelNote "
233+ $RelNote = $RelNote -replace " ^(?s)(\s*)\S.*$|^$" , " `$ {1}$ ( $Line ) `n`$ _"
234+ Write-Verbose " New ReleaseNotes:`n $RelNote "
235+ Update-Metadata - Path $OutputManifest - PropertyName PrivateData.PSData.ReleaseNotes - Value $RelNote
236+ }
237+ }
166238 }
167239
168240 # This is mostly for testing ...
0 commit comments