Press.psm1
using namespace System.IO function Build-ReleaseNotes { <# .SYNOPSIS Build release notes from commit logs using Keep a Changelog format #> [CmdletBinding()] param( #Path to the project folder where the git repository is located [Parameter(Mandatory)][String]$Path, #Version for the release notes. If not specified will be unreleased [String]$Version, #Output Path for the Changelog. If not specified it will be output directly as a string [String]$Destination, #By default, only builds the release notes since the last tag. Specify full to process the full operation. [Switch]$Full ) [String]$markdownResult = Get-MessagesSinceLastTag -Path $Path -All:$Full | Add-CommitType | ConvertTo-ReleaseNotesMarkdown -Version $Version if ($Destination) { Write-Verbose "Release Notes saved to $Destination" Out-File -FilePath $Destination -InputObject $markdownResult } else { return $markdownResult } } function Get-MessagesSinceLastTag ([String]$Path, [Switch]$All) { try { Push-Location -StackName GetMessagesSinceLastTag -Path $Path # Unicode (emoji) output from native commands cause issues on Windows $lastOutputEncoding = [console]::OutputEncoding [console]::OutputEncoding = [Text.Encoding]::UTF8 try { $LastErrorActionPreference = $ErrorActionPreference $ErrorActionPreference = 'Stop' [String]$currentCommitTag = & git describe --exact-match --tags 2>$null } catch { if ($PSItem -match 'no tag exactly matches') { #If this is not a direct tag that's fine $currentCommitTag = $null } elseif ($PSItem -match 'no names found, cannot describe anything.') { #This just means there are no tags $currentCommitTag = $null } else { throw } } finally { $ErrorActionPreference = $LastErrorActionPreference } #If this is a release tag, the release notes should be everything since the last release tag [String]$lastVersionTag = & git tag --list 'v*' --sort="version:refname" --merged | Where-Object { $PSItem -ne $currentCommitTag } | Select-Object -Last 1 if (-not $lastVersionTag) { Write-Verbose 'No version tags (vX.X.X) found in this repository, using all commits to generate release notes' $lastVersionCommit = $null } else { [String]$lastVersionCommit = (& git rev-list -n 1 $lastVersionTag) + '..' } if ($All) { $lastVersionCommit = $null } [String]$gitLogResult = (& git log --pretty=format:"|||%h||%B||%aL||%cL" $lastVersionCommit) -join "`n" } catch { throw } finally { [console]::OutputEncoding = $lastOutputEncoding Pop-Location -StackName GetMessagesSinceLastTag } $gitLogResult.Split('|||').where{ $PSItem }.foreach{ $logItem = $PSItem.Split('||') [PSCustomObject]@{ CommitId = $logItem[0] Message = $logItem[1].trim() Author = $logItem[2].trim() Committer = $logItem[3].trim() CommitType = $null } } } function Add-CommitIdIfNotPullRequest { [CmdletBinding()] param ( [Parameter(Mandatory,ValueFromPipeline)]$logEntry ) process { if ($logEntry.Message -notmatch '#\d+') { $logEntry.Message = ($logEntry.Message + ' ({0})') -f $logEntry.CommitId } $logEntry } } function Add-PullRequestContributorThanks { [CmdletBinding()] param ( [Parameter(Mandatory,ValueFromPipeline)]$logEntry ) process { #TODO: Make ignored committer configurable #TODO: Make PR match configurable if ($logEntry.Committer -ne 'noreply' -and #This is the default Github Author $logEntry.Author -ne $logEntry.Committer -and $logEntry.Message -match '#\d+') { [string[]]$multiLineMessage = $logEntry.Message.trim().split("`n") $multiLineMessage[0] = ($multiLineMessage[0] + ' - Thanks @{0}!') -f $logEntry.Author $logEntry.Message = $multiLineMessage -join "`n" } $logEntry } } function Add-CommitType { [CmdletBinding()] param ( [Parameter(ValueFromPipeline)]$logMessage ) begin { #TODO: Move this to PSSettings $commitTypes = @( @{ Name = 'Breaking Changes' Regex = '💥|:boom:|BREAKING CHANGE:|\+semver:\s?(breaking|major)' } @{ Name = 'New Features' Regex = '✨|:(feat|tada):|^feat:|\+semver:\s?(feature|minor)' } @{ Name = 'Minor Updates and Bug Fixes' Regex = '[📌🐛🩹🚑♻️🗑️🔥⚡🔒➕➖🔗⚙️]|:(bug|refactor|perf|security|add|remove|deps|config):|^(fix|refactor|perf|security|style|deps):|\+semver:\s?(fix|patch)' } @{ Name = 'Documentation Updates' Regex = '📝' } ) } process { foreach ($logItem in $logMessage) { foreach ($commitTypeItem in $commitTypes) { if ($LogItem -match $commitTypeItem.Regex) { $LogItem.CommitType = $commitTypeItem.Name break } #Last Resort $LogItem.CommitType = 'Other' } Write-Output $logItem } } } function ConvertTo-ReleaseNotesMarkdown { [CmdletBinding()] param ( #Log Item with commit Type [Parameter(ValueFromPipeline)]$InputObject, #Version to use [String]$Version ) begin { $messages = [Collections.ArrayList]::new() $markdown = [Text.StringBuilder]::new() #Top header $baseHeader = if ($Version) { $currentDate = Get-Date -Format 'yyyy-MM-dd' "## [$Version] - $currentDate" } else { '## [Unreleased]' } [void]$markdown.AppendLine($baseHeader) } process { [void]$messages.add($InputObject) } end { $sortOrder = 'Breaking Changes', 'New Features', 'Minor Updates and Bug Fixes', 'Documentation Updates' $messageGroups = $messages | Add-PullRequestContributorThanks | Add-CommitIdIfNotPullRequest | Group-Object CommitType | Sort-Object { #Sort by our custom sort order. Anything that doesn't match moves to the end $index = $sortOrder.IndexOf($PSItem.Name) if ($index -eq -1) { $index = [int]::MaxValue } $index } foreach ($messageGroupItem in $messageGroups) { #Header First [void]$markdown.AppendLine("### $($messageGroupItem.Name)") #Then the issue lines #TODO: Create links for PRs $messageGroupItem.Group.Message.foreach{ #Multiline List format, removing extra newlines [String]$ListBody = ($PSItem -split "`n").where{ $PSItem } -join " `n " [String]$ChangeItem = '- ' + $ListBody [void]$markdown.AppendLine($ChangeItem) } #Spacer [Void]$markdown.AppendLine() } return ([String]$markdown).trim() } } function Compress-Module { [CmdletBinding()] param( #Path to the directory to archive [Parameter(Mandatory)]$Path, #Output for Zip File Name [Parameter(Mandatory)]$Destination ) $CompressArchiveParams = @{ Path = $Path DestinationPath = $Destination } Compress-Archive @CompressArchiveParams Write-Verbose ('Zip File Output:' + $CompressArchiveParams.DestinationPath) } <# .SYNOPSIS This function prepares a powershell module from a source powershell module directory .DESCRIPTION This function can also optionally "compile" the module, which is place all relevant powershell code in a single .psm1 file. This improves module load performance. If you choose to compile, place any script lines you use to dot-source the other files in your .psm1 file into a #region SourceInit region block, and this function will replace it with the "compiled" scriptblock #> function Copy-ModuleFiles { [CmdletBinding()] param ( #Path to the Powershell Module Manifest representing the file you wish to compile [Parameter(Mandatory)]$PSModuleManifest, #Path to the build destination. This should be non-existent or deleted by Clean prior [Parameter(Mandatory)]$Destination, #By Default this command expects a nonexistent destination, specify this to allow for a "Dirty" copy [Switch]$Force, #By default, the build will consolidate all relevant module files into a single .psm1 file. This enables the module to load faster. Specify this if you want to instead copy the files as-is [Switch]$NoCompile, #If you chose compile, specify this for the region block in your .psm1 file to replace with the compiled code. If not specified, it will just append to the end of the file. Defaults to 'SourceInit' for #region SourceInit [String]$SourceRegionName = 'SourceInit', #Files that are considered for inclusion to the 'compiled' module. This by default includes .ps1 files only. Uses Filesystem Filter syntax [String[]]$PSFileInclude = '*.ps1', #Files that are considered for exclusion to the 'compiled' module. This excludes any files that have two periods before ps1 (e.g. .build.ps1, .tests.ps1). Uses Filesystem Filter syntax [String[]]$PSFileExclude = '*.*.ps1', #If a prerelease tag exists, the build will touch a prerelease warning file into the root of the module folder. Specify this parameter to disable this behavior. [Switch]$NoPreReleaseFile = (-not $PressSetting.PreRelease), #Additional files to include in the folder. These will be dropped directly into the resulting module folder. Paths should be relative to the module root. [String[]]$Include ) $SourceModuleDir = Split-Path $PSModuleManifest #Verify a clean build folder try { $DestinationDirectory = New-Item -ItemType Directory -Path $Destination -ErrorAction Stop } catch [IO.IOException] { if ($PSItem.exception.message -match 'already exists\.$') { if (-not $Force) { throw "Folder $Destination already exists. Make sure that you cleaned your Build Output directory. To override this behavior, specify -Force" } else { #Downgrade error to warning Write-Warning $PSItem } } else { throw $PSItem } } #TODO: Use this one command and sort out the items later #$FilesToCopy = Get-ChildItem -Path $PSModuleManifestDirectory -Filter '*.ps*1' -Exclude '*.tests.ps1' -Recurse $SourceManifest = Import-PowerShellDataFile -Path $PSModuleManifest #TODO: Allow .psm1 to be blank and generate it on-the-fly if (-not $SourceManifest.RootModule) { throw "The source manifest at $PSModuleManifest does not have a RootModule specified. This is required to build the module." } $SourceRootModulePath = Join-Path $SourceModuleDir $sourceManifest.RootModule $SourceRootModule = Get-Content -Raw $SourceRootModulePath #Cannot use Copy-Item Directly because the filtering isn't advanced enough (can't exclude) $SourceFiles = Get-ChildItem -Path $SourceModuleDir -Include $PSFileInclude -Exclude $PSFileExclude -File -Recurse if (-not $NoCompile) { #TODO: Apply ordering if important (e.g. classes) #Collate the files, pulling out using lines because these have to go first [String[]]$UsingLines = @() [String]$CombinedSourceFiles = ((Get-Content -Raw $SourceFiles) -split '\r?\n' | Where-Object { if ($_ -match '^using .+$') { $UsingLines += $_ return $false } return $true }) -join [Environment]::NewLine #If a SourceInit region was set, inject the files there, otherwise just append to the end. $sourceRegionRegex = "(?s)#region $SourceRegionName.+#endregion $SourceRegionName" if ($SourceRootModule -match $sourceRegionRegex) { #Need to escape the $ in the replacement string $RegexEscapedCombinedSourceFiles = [String]$CombinedSourceFiles.replace('$','$$') $SourceRootModule = $SourceRootModule -replace $sourceRegionRegex,$RegexEscapedCombinedSourceFiles } else { #Just add them to the end of the file $SourceRootModule += [Environment]::NewLine + $CombinedSourceFiles } #Use a stringbuilder to piece the portions of the config back together, with using statements up-front [Text.StringBuilder]$OutputRootModule = '' if ($UsingLines) { $UsingLines.trim() | Sort-Object -Unique | ForEach-Object { [void]$OutputRootModule.AppendLine($PSItem) } } [void]$OutputRootModule.AppendLine($SourceRootModule) [String]$SourceRootModule = $OutputRootModule #Strip non-help-related comments and whitespace #[String]$SourceRootModule = Remove-CommentsAndWhiteSpace $SourceRootModule } else { #TODO: Track all files in the source directory to ensure none get missed on the second step try { #In order to get relative paths we have to be in the directory we want to be relative to Push-Location (Split-Path $PSModuleManifest) $SourceFiles | ForEach-Object { #Powershell 6+ Preferred way. #TODO: Enable when dropping support for building on 5.x #$RelativePath = [io.path]::GetRelativePath($SourceModuleDir,$PSItem.fullname) #Powershell 3.x compatible "Ugly" Regex method #$RelativePath = $PSItem.FullName -replace [Regex]::Escape($SourceModuleDir),'' $RelativePath = Resolve-Path $PSItem.FullName -Relative #Copy-Item doesn't automatically create directory structures when copying files vs. directories $DestinationPath = Join-Path $DestinationDirectory $RelativePath $DestinationDir = Split-Path $DestinationPath if (-not (Test-Path $DestinationDir)) { New-Item -ItemType Directory $DestinationDir > $null } $copiedItems = Copy-Item -Path $PSItem -Destination $DestinationPath -PassThru #Update file timestamps for Invoke-Build Incremental Build detection $copiedItems.foreach{ $PSItem.LastWriteTime = [DateTime]::Now } } } catch { throw } finally { #Return after processing relative paths Pop-Location } } #Output the (potentially) modified Root Module $SourceRootModule | Out-File -FilePath (Join-Path $DestinationDirectory $SourceManifest.RootModule) #If there is a "lib" folder, copy that as-is if (Test-Path "$SourceModuleDir\lib") { Write-Verbose 'lib folder detected, copying entire contents' $copiedItems = Copy-Item -Recurse -Force -Path "$SourceModuleDir\lib" -Destination $DestinationDirectory -PassThru $copiedItems.foreach{ $PSItem.LastWriteTime = [DateTime]::Now } } #Copy the Module Manifest $OutputModuleManifest = Copy-Item -PassThru -Path $PSModuleManifest -Destination $DestinationDirectory $OutputModuleManifest.foreach{ $PSItem.LastWriteTime = [DateTime]::Now } $OutputModuleManifest = [String]$OutputModuleManifest #Additional files to include if ($Include) { $copiedItems = $Include | Copy-Item -Destination $DestinationDirectory -PassThru $copiedItems.foreach{ $PSItem.LastWriteTime = [DateTime]::Now } } #Add a prerelease if (-not $NoPreReleaseFile) { 'This is a prerelease build and not meant for deployment!' | Out-File -FilePath (Join-Path $DestinationDirectory "PRERELEASE-$($PressSetting.VersionLabel)") } return [PSCustomObject]@{ OutputModuleManifest = $OutputModuleManifest } } <# .SYNOPSIS Fetch the names of public functions in the specified folder using AST .DESCRIPTION This is a better method than grabbing the names of the .ps1 file and "hoping" they line up. This also only gets parent functions, child functions need not apply #> #TODO: Better Function handling: Require function to be the same name as the file. Accessory private functions are OK. function Get-PublicFunctions { [CmdletBinding()] param( #The path to the public module directory containing the modules. Defaults to the "Public" folder where the source module manifest resides. [Parameter(Mandatory)][String[]]$PublicModulePath ) $publicFunctionFiles = Get-ChildItem $PublicModulePath -Filter '*.ps1' | Where-Object Name -NotMatch '\.\w+?\.ps1$' #Exclude Tests.ps1, etc. #TODO: Make this a PSSetting foreach ($fileItem in $publicFunctionFiles) { $scriptContent = Get-Content -Raw $fileItem $functionNames = [ScriptBlock]::Create($scriptContent).AST.EndBlock.Statements | Where-Object { $PSItem -is [Management.Automation.Language.FunctionDefinitionAst] } | ForEach-Object Name $functionName = $FileItem.BaseName if ($functionName -notin $functionNames) { Write-Warning "$fileItem`: There is no function named $functionName in $fileItem, please ensure your public function is named the same as the file. Discovered functions: $functionNames" continue } Write-Verbose "Discovered public function $functionName in $fileItem" Write-Output $functionName } } function Get-Setting { [CmdletBinding()] param ( #Base Configuration Path to search for configurations [Parameter(Mandatory)][String]$ConfigBase, #Additional YAML configurations to add to the configuration [String[]]$YamlConfigurationPath, #Build Output Directory Name. Defaults to Get-BuildEnvironment Default which is 'BuildOutput' $BuildOutput = 'BuildOutput' ) $Settings = [ordered]@{} $Settings.BuildEnvironment = (Get-BuildEnvironment -BuildOutput $BuildOutput -As Hashtable).AsReadOnly() $Settings.General = [ordered]@{ # Root directory for the project ProjectRoot = $Settings.BuildEnvironment.ProjectPath # Root directory for the module SrcRootDir = $Settings.BuildEnvironment.ModulePath # The name of the module. This should match the basename of the PSD1 file ModuleName = $Settings.BuildEnvironment.ProjectName # Module version ModuleVersion = (Import-PowerShellDataFile -Path $Settings.BuildEnvironment.PSModuleManifest).ModuleVersion # Module manifest path ModuleManifestPath = $Settings.BuildEnvironment.PSModuleManifest } $Settings.Build = [ordered]@{ Dependencies = @('StageFiles', 'BuildHelp') # Default Output directory when building a module OutDir = $Settings.BuildEnvironment.BuildOutput # Module output directory # override the top-level 'OutDir' above and compute the full path to the module internally ModuleOutDir = Join-Path $Settings.BuildEnvironment.BuildOutput $Settings.BuildEnvironment.ProjectName # Controls whether to "compile" module into single PSM1 or not CompileModule = $true # List of files to exclude from output directory Exclude = @() } $Settings.Test = [ordered]@{ # Enable/disable Pester tests Enabled = $true # Directory containing Pester tests RootDir = Join-Path -Path $Settings.BuildEnvironment.ProjectPath -ChildPath tests # Specifies an output file path to send to Invoke-Pester's -OutputFile parameter. # This is typically used to write out test results so that they can be sent to a CI # system like AppVeyor. OutputFile = ([IO.Path]::Combine($Settings.Environment.BuildOutput,"$($Settings.Environment.ProjectName)-TestResults_PS$($psversiontable.psversion)`_$(get-date -format yyyyMMdd-HHmmss).xml")) # Specifies the test output format to use when the TestOutputFile property is given # a path. This parameter is passed through to Invoke-Pester's -OutputFormat parameter. OutputFormat = 'NUnitXml' ScriptAnalysis = [ordered]@{ # Enable/disable use of PSScriptAnalyzer to perform script analysis Enabled = $true # When PSScriptAnalyzer is enabled, control which severity level will generate a build failure. # Valid values are Error, Warning, Information and None. "None" will report errors but will not # cause a build failure. "Error" will fail the build only on diagnostic records that are of # severity error. "Warning" will fail the build on Warning and Error diagnostic records. # "Any" will fail the build on any diagnostic record, regardless of severity. FailBuildOnSeverityLevel = 'Error' # Path to the PSScriptAnalyzer settings file. SettingsPath = Join-Path $PSScriptRoot -ChildPath ScriptAnalyzerSettings.psd1 } CodeCoverage = [ordered]@{ # Enable/disable Pester code coverage reporting. Enabled = $false # Fail Pester code coverage test if below this threshold Threshold = .75 # CodeCoverageFiles specifies the files to perform code coverage analysis on. This property # acts as a direct input to the Pester -CodeCoverage parameter, so will support constructions # like the ones found here: https://github.com/pester/Pester/wiki/Code-Coverage. Files = @( Join-Path -Path $Settings.BuildEnvironment.ModulePath -ChildPath '*.ps1' Join-Path -Path $Settings.BuildEnvironment.ModulePath -ChildPath '*.psm1' ) } } $Settings.Help = [ordered]@{ # Path to updateable help CAB UpdatableHelpOutDir = Join-Path -Path $Settings.Build.ModuleOutDir -ChildPath 'UpdatableHelp' # Default Locale used for help generation, defaults to en-US DefaultLocale = (Get-UICulture).Name # Convert project readme into the module about file ConvertReadMeToAboutHelp = $false } $Settings.Docs = [ordered]@{ # Directory PlatyPS markdown documentation will be saved to RootDir = Join-Path -Path $Settings.Build.ModuleOutDir -ChildPath 'docs' } $Settings.Publish = [ordered]@{ # PowerShell repository name to publish modules to PSRepository = 'PSGallery' # API key to authenticate to PowerShell repository with PSRepositoryApiKey = $env:PSGALLERY_API_KEY # Credential to authenticate to PowerShell repository with PSRepositoryCredential = $null } # Enable/disable generation of a catalog (.cat) file for the module. # [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')] # $catalogGenerationEnabled = $true # # Select the hash version to use for the catalog file: 1 for SHA1 (compat with Windows 7 and # # Windows Server 2008 R2), 2 for SHA2 to support only newer Windows versions. # [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')] # $catalogVersion = 2 $PowerConfig = New-PowerConfig | Add-PowerConfigObject -Object $Settings -WarningAction silentlyContinue #TODO: Find and suppress the json depth warning | Add-PowerConfigYamlSource -Path (Join-Path (Resolve-Path $ConfigBase) '.config/press.yml') foreach ($path in $yamlConfigurationPath) { $ErrorActionPreference = 'Stop' $PowerConfig = $PowerConfig | Add-PowerConfigYamlSource -Mandatory -Path $path -ErrorAction Stop } #Environment variables are added last to take the highest precedence $PowerConfig = $PowerConfig | Add-PowerConfigEnvironmentVariableSource -Prefix 'PRESS_' return Get-PowerConfig $PowerConfig } function Get-Version { [CmdletBinding()] param( [Parameter(Mandatory)][String]$ProjectPath, [Version]$GitVersionVersion = '5.6.6', [String]$GitVersionConfigPath = $(Resolve-Path (Join-Path $MyInvocation.MyCommand.Module.ModuleBase '.\GitVersion.default.yml')) ) Write-Verbose "Using Gitversion Configuration $GitVersionConfigPath" $gitCommand = Get-Command -CommandType Application -Name 'git' if (-not $gitCommand) { throw 'git was not found in your path. Ensure git is installed and can be found.' } if (-not (Test-Path "$ProjectPath\.git")) { throw "No Git repository (.git folder) found in $ProjectPath. Please specify a folder with a git repository" } if (-not (Test-Path "$ProjectPath\.config\dotnet-tools.json")) { [String]$dotnetNewManifestStatus = & dotnet new tool-manifest -o $ProjectPath *>&1 if ($dotnetNewManifestStatus -notlike '*was created successfully*') { throw "There was an error creating a dotnet tool manifest: $dotnetNewManifestStatus" } if (-not (Get-Item "$ProjectPath\.config\dotnet-tools.json")) { throw "The manifest command completed successfully but $ProjectPath\.config\dotnet-tools.json still could not be found. This is probably a bug." } } #TODO: Direct Json check maybe? [String]$gitVersionToolCheck = & dotnet tool list if ($gitVersionToolCheck -notlike "*gitversion.tool*$GitVersionVersion*") { & dotnet tool uninstall gitversion.tool | Out-Null [String]$gitversionInstall = & dotnet tool install gitversion.tool --version $GitVersionVersion if ($gitVersionInstall -notlike '*gitversion.tool*was successfully installed*') { throw "Failed to install gitversion local tool with dotnet: $gitVersionInstall" } } #Output from a command is String in Windows and Object[] in Linux. Cast to string to normalize. [String]$dotnetToolRestoreStatus = & dotnet tool restore *>&1 $dotnetToolMatch = "*Tool 'gitversion.tool' (version '$GitVersionVersion') was restored.*" if ($dotnetToolRestoreStatus -notlike $dotnetToolMatch) { throw "GitVersion dotnet tool was not found. Ensure you have a .NET manifest. Message: $dotnetToolRestoreStatus" } #Reference Dotnet Local Tool directly rather than trying to go through .NET EXE #This appears to be an issue where dotnet is installed but the tools aren't added to the path for Linux # $GitVersionExe = "$HOME\.dotnet\tools/dotnet-gitversion" $DotNetExe = "dotnet" [String[]]$GitVersionParams = 'gitversion',$ProjectPath,'/nofetch' if (-not ( $ProjectPath -and (Test-Path ( Join-Path $ProjectPath 'GitVersion.yml' )) )) { #Use the Press Builtin $GitVersionParams += '/config' $GitVersionParams += $GitVersionConfigPath } try { if ($DebugPreference -eq 'Continue') { $GitVersionParams += '/diag' } [String[]]$GitVersionOutput = & $DotNetExe @GitVersionParams *>&1 if (-not $GitVersionOutput) { throw 'GitVersion returned no output. Are you sure it ran successfully?' } if ($LASTEXITCODE -ne 0) { if ($GitVersionOutput -like '*GitVersion.GitVersionException: No commits found on the current branch*') { #TODO: Auto-version calc maybe? throw 'There are no commits on your current git branch. Please make at least one commit before trying to calculate the version' } throw "GitVersion returned exit code $LASTEXITCODE. Output:`n$GitVersionOutput" } #Split Diagnostic Messages from Regex $i = 0 foreach ($lineItem in $GitVersionOutput) { if ($GitVersionOutput[$i] -eq '{') { break } $i++ } if ($i -ne 0) { [String[]]$diagMessages = $GitVersionOutput[0..($i - 1)] $diagMessages | Write-Debug } #Should not normally get this far if there are errors if ($diagMessages -match 'ERROR \[') { throw "An error occured when running GitVersion.exe in $ProjectPath. Diag Message: `n$diagMessages" } #There is some trailing debug info sometimes $jsonResult = $GitVersionOutput[$i..($GitVersionOutput.count - 1)] | Where-Object { $_ -notmatch 'Info.+Done writing' } $GitVersionInfo = $jsonResult | ConvertFrom-Json -ErrorAction stop #Fixup prerelease tag for Powershell modules if ($GitVersionInfo.NuGetPreReleaseTagV2 -match '[-.]') { Write-Verbose 'Detected invalid characters for Powershell Gallery Prerelease Tag. Fixing it up.' $GitVersionInfo.NuGetPreReleaseTagV2 = $GitVersionInfo.NuGetPreReleaseTagV2 -replace '[\-\.]','' $GitVersionInfo.NuGetVersionV2 = $GitVersionInfo.MajorMinorPatch,$GitVersionInfo.NuGetPreReleaseTagV2 -join '-' } $GitVersionResult = $GitVersionInfo | Select-Object BranchName, MajorMinorPatch, NuGetVersionV2, NuGetPreReleaseTagV2 | Format-List | Out-String Write-Verbose "Gitversion Result: `n$($GitVersionResult | Format-List | Out-String)" } catch { throw "There was an error when running GitVersion.exe $buildRoot`: $PSItem. The output of the command (if any) is below...`r`n$GitVersionOutput" } finally { #Restore the tag if it was present #TODO: Evaluate if this is still necessary # if ($currentTag) { # write-build DarkYellow "Task $($task.name) - Restoring tag $currentTag." # git tag $currentTag -a -m "Automatic GitVersion Release Tag Generated by Invoke-Build" # } } return $GitVersionInfo # #GA release detection # if ($BranchName -eq 'master') { # $Script:IsGARelease = $true # $Script:ProjectVersion = $ProjectBuildVersion # } else { # #The regex strips all hypens but the first one. This shouldn't be necessary per NuGet spec but Update-ModuleManifest fails on it. # $SCRIPT:ProjectPreReleaseVersion = $GitVersionInfo.nugetversion -replace '(?<=-.*)[-]' # $SCRIPT:ProjectVersion = $ProjectPreReleaseVersion # $SCRIPT:ProjectPreReleaseTag = $SCRIPT:ProjectPreReleaseVersion.split('-')[1] # } # write-build Green "Task $($task.name)` - Calculated Project Version: $ProjectVersion" # #Tag the release if this is a GA build # if ($BranchName -match '^(master|releases?[/-])') { # write-build Green "Task $($task.name)` - In Master/Release branch, adding release tag v$ProjectVersion to this build" # $SCRIPT:isTagRelease = $true # if ($BranchName -eq 'master') { # write-build Green "Task $($task.name)` - In Master branch, marking for General Availability publish" # [Switch]$SCRIPT:IsGARelease = $true # } # } # #Reset the build dir to the versioned release directory. TODO: This should probably be its own task. # $SCRIPT:BuildReleasePath = Join-Path $BuildProjectPath $ProjectBuildVersion # if (-not (Test-Path -pathtype Container $BuildReleasePath)) {New-Item -type Directory $BuildReleasePath | out-null} # $SCRIPT:BuildReleaseManifest = Join-Path $BuildReleasePath (split-path $env:BHPSModuleManifest -leaf) # write-build Green "Task $($task.name)` - Using Release Path: $BuildReleasePath" } function Invoke-Clean { [CmdletBinding()] param ( [Parameter(Mandatory)]$BuildOutputPath, [Parameter(Mandatory)]$buildProjectName, [Switch]$Prerequisites ) #Taken from Invoke-Build because it does not preserve the command in the scope this function normally runs #Copyright (c) Roman function Remove-BuildItem { [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '', Justification = 'Private Function')] param( [Parameter(Mandatory)][string[]]$Path ) if ($Path -match '^[.*/\\]*$') { *Die 'Not allowed paths.' 5 } $v = $PSBoundParameters['Verbose'] try { foreach ($_ in $Path) { if (Get-Item $_ -Force -ErrorAction 0) { if ($v) { Write-Verbose "remove: removing $_" -Verbose } Remove-Item $_ -Force -Recurse -ErrorAction stop } elseif ($v) { Write-Verbose "remove: skipping $_" -Verbose } } } catch { throw $_ } } #Reset the BuildOutput Directory if (Test-Path $buildOutputPath) { Write-Verbose "Removing and resetting Build Output Path: $buildOutputPath" Remove-BuildItem -Path $buildOutputPath } if ($Prerequisites) { $PrerequisitePath = (Join-Path ([Environment]::GetFolderpath('LocalApplicationData')) 'Press') Write-Verbose "Removing and resetting Press Prerequisites: $PrerequisitePath" Remove-BuildItem $PrerequisitePath -Verbose:$false } New-Item -Type Directory $BuildOutputPath > $null #META: Force reload Press if building press if ($buildProjectName -eq 'Press') { Write-Verbose 'Detected Press Meta-Build' Import-Module -Force -Global -Verbose -Name (Join-Path $MyInvocation.MyCommand.Module.ModuleBase 'Press.psd1') } else { #Unmount any modules named the same as our module Remove-Module $buildProjectName -Verbose:$false -ErrorAction SilentlyContinue } } function New-NugetPackage { [CmdletBinding(SupportsShouldProcess)] param ( #Path to the module to build [Parameter(Mandatory)][String]$Path, #Where to output the new module package. Specify a folder [Parameter(Mandatory)][String]$Destination ) $ModuleManifest = Get-Item $Path/*.psd1 | Where-Object { (Get-Content -Raw $PSItem) -match "ModuleVersion\s+?=\s+?\'[\d\.]+?\'" } | Select-Object -First 1 if (-not $ModuleManifest) { throw "No module manifest found in $Path. Please ensure a powershell module is present in this directory." } $ModuleName = $ModuleManifest.basename #TODO: Get this to work with older packagemanagement # $ModuleMetadata = Import-PowerShellDataFile $ModuleManifest # #Use some PowershellGet private methods to create a nuspec file and create a nupkg. This is much faster than the "slow" method referenced below # $NewNuSpecFileParams = @{ # OutputPath = $Path # Id = $ModuleName # Version = ($ModuleMetaData.ModuleVersion,$ModuleMetaData.PrivateData.PSData.Prerelease -join '-') # Description = $ModuleMetaData.Description # Authors = $ModuleMetaData.Author # } # #Fast Method but skips some metadata. Doesn't matter for non-powershell gallery publishes # #TODO: Add all the metadata from the publish process # $NuSpecPath = & (Get-Module PowershellGet) New-NuSpecFile @NewNuSpecFileParams # #$DotNetCommandPath = & (Get-Module PowershellGet) {$DotnetCommandPath} # #$NugetExePath = & (Get-Module PowershellGet) {$NugetExePath} # $NugetExePath = (command nuget -All -erroraction stop | where name -match 'nuget(.exe)?$').Source # $NewNugetPackageParams = @{ # NuSpecPath = $NuSpecPath # NuGetPackageRoot = $Destination # } # if ($DotNetCommandPath) { # $NewNugetPackageParams.UseDotNetCli = $true # } elseif ($NugetExePath) { # $NewNugetPackageParams.NugetExePath = $NuGetExePath # }else { # throw "Neither nuget or dotnet was detected by PowershellGet. Please check you have one or the other installed." # } # $nuGetPackagePath = & (Get-Module PowershellGet) New-NugetPackage @NewNugetPackageParams # write-verbose "Created NuGet Package at $nuGetPackagePath" # #Slow Method, maybe fallback to this # #Creates a temporary repository and registers it, uses publish-module which results in a nuget package try { $SCRIPT:tempRepositoryName = "$ModuleName-build-$(Get-Date -format 'yyyyMMdd-hhmmss')" Unregister-PSRepository -Name $tempRepositoryName -ErrorAction SilentlyContinue -Verbose:$false *>$null if ($PSCmdlet.ShouldProcess($Path, "Publish Nuget Package to $Destination")) { Register-PSRepository -Name $tempRepositoryName -SourceLocation ([String]$Destination) -Verbose:$false *>$null If (Get-Item -ErrorAction SilentlyContinue (Join-Path $Path "$ModuleName*.nupkg")) { Write-Debug Green "Nuget Package for $ModuleName already generated. Skipping. Delete the package to retry" } else { $CurrentProgressPreference = $GLOBAL:ProgressPreference $GLOBAL:ProgressPreference = 'SilentlyContinue' #TODO: Allow requiredmodules references in nuget packages Publish-Module -Repository $tempRepositoryName -Path $Path -Force -Verbose:$false $GLOBAL:ProgressPreference = $CurrentProgressPreference } } } catch { Write-Error $PSItem } finally { Unregister-PSRepository $tempRepositoryName *>$null if ($CurrentProgressPreference) {$GLOBAL:ProgressPreference = $CurrentProgressPreference} } } <# .SYNOPSIS Retrieves the dotnet dependencies for a powershell module .NOTES This process basically builds a C# Powershell Standard Library and identifies the resulting assemblies. There is probably a more lightweight way to do this. .EXAMPLE Get-PSModuleNugetDependencies @{'System.Text.Json'='4.6.0'} #> function Restore-NugetPackages { [CmdletBinding(SupportsShouldProcess,DefaultParameterSetName = 'String')] param ( #A list of nuget packages to include. You can specify a nuget-style version with a / separator e.g. yamldotnet/3.2.* [Parameter(ParameterSetName = 'String',Mandatory,Position = 0)][String[]]$PackageName, #Which packages and their associated versions to include, in hashtable form. Supports Nuget Versioning: https://docs.microsoft.com/en-us/nuget/concepts/package-versioning#version-ranges-and-wildcards [Parameter(ParameterSetName = 'Hashtable',Mandatory,Position = 0)][HashTable]$Packages, #Which .NET Framework target to use. Defaults to .NET Standard 2.0 and is what you should use for PS5+ compatible modules [String]$Target = 'netstandard2.0', #Where to output the resultant assembly files. Default is a new folder 'lib' in the current directory. [Parameter(Position = 1)][String]$Destination, #Which PS Standard library to use. Defaults to 7.0.0-preview.1. [String]$PowershellTarget = '7.0.0-preview.1', [String]$BuildPath = (Join-Path ([io.path]::GetTempPath()) "PSModuleDeps-$((New-Guid).Guid)"), #Name of the build project. You normally don't need to change this. [String]$BuildProjectName = 'PSModuleDeps', #Whether to output the resultant copied file paths [Switch]$PassThru, #Whether to do an online restore check of the dependencies. Disable this to speed up the process at the risk of compatibility. [Switch]$NoRestore ) if ($PSCmdlet.ParameterSetName -eq 'String') { $Packages = @{} $PackageName.Foreach{ $PackageVersion = $PSItem -split '/' if ($PackageVersion.count -eq 2) { $Packages[$PackageVersion[0]] = $PackageVersion[1] } else { $Packages[$PSItem] = '*' } } } #Add Powershell Standard Library $Packages['PowerShellStandard.Library'] = $PowershellTarget if (-not ([version](dotnet --version) -ge 2.2)) { throw 'dotnet 2.2 or later is required. Make sure you have the .net core SDK 2.x+ installed' } #Add starter Project for netstandard 2.0 $BuildProjectFile = Join-Path $BuildPath "$BuildProjectName.csproj" New-Item -ItemType Directory $BuildPath -Force > $null @" <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>$Target</TargetFramework> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> </PropertyGroup> <ItemGroup> <PackageReference Include="PowerShellStandard.Library" Version="$PowerShellTarget"> <PrivateAssets>All</PrivateAssets> </PackageReference> </ItemGroup> </Project> "@ | Out-File -FilePath $BuildProjectFile foreach ($ModuleItem in $Packages.keys) { $dotnetArgs = 'add',$BuildProjectFile,'package',$ModuleItem if ($Packages[$ModuleItem] -ne $true) { if ($NoRestore) { $dotNetArgs += '--no-restore' } $dotnetArgs += '--version' $dotnetArgs += $Packages[$ModuleItem] } Write-Verbose "Executing: dotnet $dotnetArgs" & dotnet $dotnetArgs | Write-Verbose } & dotnet publish -o $BuildPath $BuildProjectFile | Write-Verbose function ConvertFromModuleDeps ($Path) { $runtimeDeps = Get-Content -Raw $Path | ConvertFrom-Json $depResult = [ordered]@{} $TargetFullName = $runtimeDeps.targets[0].psobject.properties.name $runtimeDeps.targets.$TargetFullName.psobject.Properties.name | Where-Object { $PSItem -notlike "$BuildProjectName*" } | Sort-Object | ForEach-Object { $depInfo = $PSItem -split '/' $depResult[$depInfo[0]] = $depInfo[1] } return $depResult } #Use return to end script here and don't actually copy the files $ModuleDeps = ConvertFromModuleDeps -Path $BuildPath/obj/project.assets.json if (-not $Destination) { #Output the Module Dependencies and end here Remove-Item $BuildPath -Force -Recurse return $ModuleDeps } if ($PSCmdlet.ShouldProcess($Destination,'Copy Resultant DLL Assemblies')) { New-Item -ItemType Directory $Destination -Force > $null $CopyItemParams = @{ Path = "$BuildPath/*.dll" Exclude = "$BuildProjectName.dll" Destination = $Destination Force = $true } if ($PassThru) { $CopyItemParams.PassThru = $true } Copy-Item @CopyItemParams Remove-Item $BuildPath -Force -Recurse } } <# .SYNOPSIS Sets the version on a powershell Module #> function Set-Version { [CmdletBinding(SupportsShouldProcess)] param ( #Path to the module manifest to update [String][Parameter(Mandatory)]$Path, #Version to set for the module [Version][Parameter(Mandatory)]$Version, #Prerelease tag to add to the module, if any [AllowEmptyString()][String]$PreRelease ) #Default is to update version so no propertyname specified $Manifest = Import-PowerShellDataFile $Path $currentVersion = $Manifest.ModuleVersion # $currentVersion = Get-Metadata -Path $Path -PropertyName 'ModuleVersion' if ($currentVersion -ne $Version) { Write-Verbose "Current Manifest Version $currentVersion doesn't match $Version. Updating..." if ($PSCmdlet.ShouldProcess($Path, "Set ModuleVersion to $Version")) { Update-Metadata -Path $Path -PropertyName ModuleVersion -Value $Version } } $currentPreRelease = $Manifest.privatedata.psdata.prerelease if ($PreRelease) { if ($currentPreRelease -ne $PreRelease) { Write-Verbose "Current Manifest Prerelease Tag $currentPreRelease doesn't match $PreRelease. Updating..." #HACK: Do not use update-modulemanifest because https://github.com/PowerShell/PowerShellGetv2/issues/294 #TODO: AutoCreate prerelease metadata try { if ($PSCmdlet.ShouldProcess($Path, "Set Prerelease Version to $PreRelease")) { Update-Metadata -Path $Path -PropertyName PreRelease -Value $PreRelease } } catch { if ($PSItem -like "Can't find*") { throw 'Could not find the Prerelease field in your source manifest file. You must add this under PrivateData/PSData first' } } } } elseif ($CurrentPreRelease -ne '') { if ($PSCmdlet.ShouldProcess($Path, "Remove $PreRelease if it is present")) { Update-Metadata -Path $Path -PropertyName PreRelease -Value '' } } } function Test-Pester { [CmdletBinding(DefaultParameterSetName = 'Default')] param ( #Path where the Pester tests are located [Parameter(Mandatory,ParameterSetName = 'Default')][String]$Path, #Path where the coverage files should be output. Defaults to the build output path. [Parameter(Mandatory,ParameterSetName = 'Default')][String]$OutputPath, #A PesterConfiguration to use instead of the intelligent defaults. For advanced usage only. [Parameter(ParameterSetName = 'Configuration')]$Configuration ) #Load Pester Just-In-Time style, cannot use a requires because of module compilation Get-Module pester -ErrorAction SilentlyContinue | Where-Object version -LT '5.0.0' | Remove-Module -Force Import-Module Pester -MinimumVersion '5.0.0' | Write-Verbose #We can't do this in the param block because pester and its classes may not be loaded yet. [PesterConfiguration]$Configuration = $Configuration #FIXME: Allow for custom configurations once we figure out how to serialize them into a job if ($Configuration) { throw [NotSupportedException]'Custom Pester Configurations temporarily disabled while sorting out best way to run them in isolated job' } if (-not $Configuration) { $Configuration = @{} #If we are in vscode, add the VSCodeMarkers if ($host.name -match 'Visual Studio Code') { Write-Host -Fore Green '===Detected Visual Studio Code, Displaying Pester Test Links===' $Configuration.Debug.ShowNavigationMarkers = $true } $Configuration.Output.Verbosity = 'Detailed' $Configuration.Run.PassThru = $true $Configuration.Run.Path = $Path $Configuration.CodeCoverage.Enabled = $false $Configuration.CodeCoverage.OutputPath = "$OutputPath/CodeCoverage.xml" $Configuration.TestResult.Enabled = $true $Configuration.TestResult.OutputPath = "$OutputPath/TEST-Results.xml" #Exclude the output folder in case we dcopied any tests there to avoid duplicate testing. This should generally only matter for "meta" like PowerForge #FIXME: Specify just the directory instead of a path search when https://github.com/pester/Pester/issues/1575 is fixed $Configuration.Run.ExcludePath = [String[]](Get-ChildItem -Recurse $OutputPath -Include '*.Tests.ps1') } $TestResults = Invoke-Pester -Configuration $Configuration if ($TestResults.Result -ne 'Passed') { throw "Failed $($TestResults.FailedCount) tests" } return $TestResults } #requires -version 7 #because it uses System.Management.Automation.SemanticVersion function Update-GithubRelease { [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory)][String]$Owner, [Parameter(Mandatory)][String]$Repository, [Parameter(Mandatory)][SemVer]$Version, [Parameter(Mandatory)][String]$AccessToken, [Parameter(Mandatory)][String]$Body, [String[]]$ArtifactPath, #Skip cleanup of old drafts within the same major version [Switch]$NoCleanup ) $Tag = "v$Version" $gitHubParams = @{ OwnerName = $Owner RepositoryName = $Repository AccessToken = $accessToken Verbose = $Verbose } #Get all releases of the same major version [Collections.ArrayList]$existingReleases = @( Get-GitHubRelease @gitHubParams | Where-Object { #Skip nulls if ($null -eq $PSItem) { continue } #TODO: Allow custom version prefix [Semver]$releaseVersion = $PSItem.tag_name -replace 'v','' ($releaseVersion.Major -eq $Version.Major) } ) #Cleanup older drafts within the same major release if they exist if (-not $NoCleanup) { if ($PSCmdlet.ShouldProcess('Existing Github Releases', 'Remove all existing draft releases with same major version')) { $removedReleases = $existingReleases | Where-Object Draft | Where-Object tag_name -NE $Tag | ForEach-Object { Write-Verbose "Detected Older Draft Release for $($PSItem.tag_name) older minor version than $Version, removing" $PSItem | Remove-GitHubRelease @gitHubParams -ErrorAction Stop -Force Write-Output $PSItem } } } $removedReleases.foreach{ $existingReleases.Remove($removedReleases) } [Collections.ArrayList]$taggedRelease = @( $existingReleases | Where-Object tag_name -EQ $Tag ) #There should be only one release per tag if ($taggedRelease.count -gt 1) { Write-Warning "Multiple Releases found for $Tag. Will attempt to arrive at a single candidate release" #Attempt to resolve the "best" release and remove the rest $removedReleases = $taggedRelease | Sort-Object published_at,draft,created_at -Descending | Select-Object -Skip 1 | ForEach-Object { Write-Verbose "Detected duplicate Release for $($PSItem.tag_name) with ID $($PSItem.ReleaseID), removing" $PSItem | Remove-GitHubRelease @gitHubParams -ErrorAction Stop -Force Write-Output $PSItem } $removedReleases.foreach{ $taggedRelease.Remove($removedReleases) } #Should be zero or one at this point if ($taggedRelease.count -gt 1) { throw "Unable to resolve to a single release for tag $Tag. This is probably a bug. Items: $taggedRelease" } } $ghReleaseParams = $gitHubParams.Clone() $ghReleaseParams.Body = $Body $ghReleaseParams.Name = "$Repository $Tag" #If a release exists, update the existing one, otherwise create a new draft release #TODO: Add option to re-create so that the date of the release creation updates $releaseResult = if ($taggedRelease -and $taggedRelease.count -eq 1) { #Update Release Notes $taggedRelease | Set-GitHubRelease @ghReleaseParams -Tag $Tag -PassThru } else { New-GitHubRelease @ghReleaseParams -Draft -Tag $Tag } Write-Output $releaseResult #Update artifacts if required by pulling the existing artifacts and replacing them Write-Verbose "Github Artifacts: $artifactPath" if ($artifactPath) { Write-Verbose "Uploading Github Artifacts: $artifactPath" $releaseResult | Get-GitHubReleaseAsset @gitHubParams | Remove-GitHubReleaseAsset @gitHubParams -Force $artifactPath | New-GitHubReleaseAsset @gitHubParams -Release $releaseResult.releaseID | Out-Null } } <# .SYNOPSIS This function sets a module manifest for the various function exports that are present in a module such as private/public functions, classes, etc. #> function Update-PublicFunctions { [CmdletBinding(SupportsShouldProcess)] param( #Path to the module manifest to update [Parameter(Mandatory)][String]$Path, #Paths to the module public function files [String]$PublicFunctionPath, #Optionally Specify the list of functions to override auto-detection [String[]]$Functions ) if (-not $Functions) { $Functions = Get-PublicFunctions $PublicFunctionPath } if (-not $Functions) { write-warning "No functions found in the powershell module. Did you define any yet? Create a new one called something like New-MyFunction.ps1 in the Public folder" return } $currentFunctions = Get-Metadata -Path $Path -PropertyName FunctionsToExport if (Compare-Object $currentFunctions $functions) { Write-Verbose "Current Function List in manifest doesn't match. Current: $currentFunctions New: $Functions. Updating." #HACK: Don't use Update-ModuleManifest because of https://github.com/PowerShell/PowerShellGetv2/issues/294 if ($PSCmdlet.ShouldProcess($Path, "Add Functions $($Functions -join ', ')")) { BuildHelpers\Update-Metadata -Path $Path -PropertyName FunctionsToExport -Value $Functions } } } $taskAliasName = '.Tasks' Set-Alias -Name $taskAliasName -Value (Join-Path $PSScriptRoot 'Press.tasks.ps1') Export-ModuleMember -Alias $taskAliasName |