From d75cc3f939046937d48520f08fc3d84f3f0c12d4 Mon Sep 17 00:00:00 2001 From: reubenmiller Date: Sun, 11 Apr 2021 19:05:03 +0000 Subject: [PATCH] setting powershell module version from git tag --- tools/PSc8y/tools/build.psm1 | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tools/PSc8y/tools/build.psm1 b/tools/PSc8y/tools/build.psm1 index 70a26acfb..ebfe6a02b 100644 --- a/tools/PSc8y/tools/build.psm1 +++ b/tools/PSc8y/tools/build.psm1 @@ -65,6 +65,25 @@ function New-ModulePSMFile { Get-Content -Path "$ModuleRoot\tools\modulefile\PartTwo.ps1" | Out-File -FilePath $moduleFile -Append } +Function Get-GitVersion { + Param() + $version = git describe + + if ($LASTEXITCODE -ne 0) { + if ($Env:GITHUB_REF) { + $version = $Env:GITHUB_REF -replace ".+/", "" + } + } + + $parts = $Version -split "-", 2 + + [pscustomobject]@{ + Version = $parts[0] -replace "^v", "" + PreRelease = ($parts[1] -split "-", 2)[0] + IsPrerelease = $parts[1].Length -gt 0 + } +} + function Update-ModuleManifestFunctions { # Update the psd1 file with the /public/psgetfunctions # Update-ModuleManifest is not used because a) it is not availabe for ps version <5.0 and b) it is destructive. @@ -93,6 +112,24 @@ function Update-ModuleManifestFunctions { $ManifestFileContent = $ManifestFileContent.Replace('FunctionsToExport = "*"', $ManifestFunctionExportString) Set-Content -Path "$ManifestFile" -Value $ManifestFileContent.TrimEnd() } + +Function Set-ModuleManifestVersion { + Param( + [Parameter( + Mandatory = $true + )] + [string] $Path + ) + + # Update version info from the git tag + $VersionInfo = Get-GitVersion + Update-ModuleManifest -Path $Path -ModuleVersion $VersionInfo.Version + + if ($VersionInfo.IsPrerelease) { + Update-ModuleManifest -Path $Path -Prerelease $VersionInfo.PreRelease + } +} + function Remove-ModuleManifestFunctions ($Path) { # Utility method to remove the list of functions from a manifest. This is specific to this modules manifest and # assumes the next item in the manifest file after the functions is a comment containing 'VariablesToExport'. @@ -135,6 +172,9 @@ function Publish-ModuleArtifacts { # Construct the distributed .psm1 file. New-ModulePSMFile + # Update version + Set-ModuleManifestVersion -Path "$ArtifactRoot\$ModuleName\$ModuleName.psd1" + # Package the module in /dist $zipFileName = "$ModuleName.zip" $artifactZipFile = Join-Path -Path $ArtifactRoot -ChildPath $zipFileName