SemanticRelease.psm1
|
Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' # Source: src/public/Get-SemanticReleaseCommitMessage.ps1 function Get-SemanticReleaseCommitMessage { [CmdletBinding()] param( [string]$ProjectRoot = (Get-Location).Path ) return Read-SemanticReleaseCommitMessage -ProjectRoot $ProjectRoot } # Source: src/public/Get-SemanticReleaseCommitRange.ps1 function Get-SemanticReleaseCommitRange { [CmdletBinding()] param( [string]$ProjectRoot = (Get-Location).Path ) return Get-SemanticReleaseCommitRangeBasis -ProjectRoot $ProjectRoot } # Source: src/public/Get-SemanticReleaseLabel.ps1 function Get-SemanticReleaseLabel { [CmdletBinding()] param( [AllowEmptyCollection()][string[]]$Message = @() ) return Resolve-SemanticReleaseVersionLabel -Message $Message } # Source: src/public/Get-SemanticReleaseNextVersion.ps1 function Get-SemanticReleaseNextVersion { [CmdletBinding()] param( [Parameter(Mandatory)][semver]$CurrentVersion, [ValidateSet('Major', 'Minor', 'Patch')] [string]$Label = 'Patch', [switch]$Preview ) return Get-SemanticReleaseVersionPlan -CurrentVersion $CurrentVersion -Label $Label -PreviewRelease:$Preview } # Source: src/public/Get-SemanticReleasePropertiesVersion.ps1 function Get-SemanticReleasePropertiesVersion { [CmdletBinding()] param( [Parameter(Mandatory)][string]$Path, [string]$Key = 'version' ) $propertiesData = Read-SemanticReleasePropertiesData -PropertiesPath $Path -Key $Key return [pscustomobject]@{ Path = $Path Key = $Key Version = $propertiesData.PreviousVersion } } # Source: src/public/Set-SemanticReleaseJsonVersion.ps1 function Set-SemanticReleaseJsonVersion { [CmdletBinding(SupportsShouldProcess = $true)] param( [Parameter(Mandatory)][string]$Path, [string]$Key = 'Version', [ValidateSet('Major', 'Minor', 'Patch')] [string]$Label = 'Patch', [switch]$Preview ) $jsonData = Read-SemanticReleaseJsonData -JsonPath $Path if (-not $jsonData.ContainsKey($Key)) { throw "JSON key '$Key' was not found in $Path" } $previousVersion = [string]$jsonData[$Key] $versionPlan = Get-SemanticReleaseVersionPlan -CurrentVersion ([semver]$previousVersion) -Label $Label -PreviewRelease:$Preview $newVersion = $versionPlan.NewVersion.ToString() $target = [System.IO.Path]::GetFileName($Path) $action = "Set $Key to $newVersion" $changeInfo = @{ Path = $Path Key = $Key PreviousVersion = $previousVersion NewVersion = $newVersion } if (-not $PSCmdlet.ShouldProcess($target, $action)) { return Get-SemanticReleaseJsonVersionWriteResult -ChangeInfo $changeInfo } $jsonData[$Key] = $newVersion Write-SemanticReleaseJsonData -JsonPath $Path -Data $jsonData return Get-SemanticReleaseJsonVersionWriteResult -ChangeInfo $changeInfo -Applied } # Source: src/public/Set-SemanticReleasePropertiesVersion.ps1 function Set-SemanticReleasePropertiesVersion { [CmdletBinding(SupportsShouldProcess = $true)] param( [Parameter(Mandatory)][string]$Path, [string]$Key = 'version', [ValidateSet('Major', 'Minor', 'Patch')] [string]$Label = 'Patch', [switch]$Preview ) $propertiesData = Read-SemanticReleasePropertiesData -PropertiesPath $Path -Key $Key $versionPlan = Get-SemanticReleaseVersionPlan -CurrentVersion ([semver]$propertiesData.PreviousVersion) -Label $Label -PreviewRelease:$Preview $newVersion = $versionPlan.NewVersion.ToString() $changeInfo = @{ Path = $Path Key = $Key PreviousVersion = $propertiesData.PreviousVersion NewVersion = $newVersion } $target = [System.IO.Path]::GetFileName($Path) $action = "Set $Key to $newVersion" if (-not $PSCmdlet.ShouldProcess($target, $action)) { return Get-SemanticReleasePropertiesVersionWriteResult -ChangeInfo $changeInfo } Write-SemanticReleasePropertiesData -Data $propertiesData -NewVersion $newVersion return Get-SemanticReleasePropertiesVersionWriteResult -ChangeInfo $changeInfo -Applied } # Source: src/public/Update-SemanticReleaseVersion.ps1 function Update-SemanticReleaseVersion { [CmdletBinding(SupportsShouldProcess = $true)] param( [Parameter(Mandatory)][string]$Path, [string]$ProjectRoot = (Get-Location).Path, [AllowEmptyString()][string]$Key, [switch]$Preview ) $context = Get-SemanticReleaseVersionUpdateContext -Path $Path -ProjectRoot $ProjectRoot -Key $Key -PreviewRelease:$Preview if (-not $context.HasCommits) { Write-Warning (Get-SemanticReleaseNoCommitMessage -Context $context) return Get-SemanticReleaseVersionUpdateResult -Context $context } if (-not $PSCmdlet.ShouldProcess($context.Target, $context.Action)) { return Get-SemanticReleaseVersionUpdateResult -Context $context } return Invoke-SemanticReleaseVersionUpdate -Context $context } # Source: src/private/release/Get-SemanticReleaseCommitRangeBasis.ps1 function Get-SemanticReleaseCommitRangeBasis { [CmdletBinding()] param( [Parameter(Mandatory)][string]$ProjectRoot ) if (-not (Test-SemanticReleaseGitAvailable)) { return Get-SemanticReleaseCommitRangeBasisResult -ProjectRoot $ProjectRoot -IsRepository:$false } $repositoryResult = Invoke-SemanticReleaseGitCommand -ProjectRoot $ProjectRoot -Arguments @('rev-parse', '--is-inside-work-tree') $repositoryState = Get-SemanticReleaseGitCommandOutputText -Result $repositoryResult if ($repositoryResult.ExitCode -ne 0 -or $repositoryState -ne 'true') { return Get-SemanticReleaseCommitRangeBasisResult -ProjectRoot $ProjectRoot -IsRepository:$false } $lastTagResult = Invoke-SemanticReleaseGitCommand -ProjectRoot $ProjectRoot -Arguments @( 'describe', '--tags', '--abbrev=0', '--match', 'v[0-9]*.[0-9]*.[0-9]*', '--match', '[0-9]*.[0-9]*.[0-9]*' ) $lastTag = Get-SemanticReleaseGitCommandOutputText -Result $lastTagResult if ($lastTagResult.ExitCode -eq 0 -and -not [string]::IsNullOrWhiteSpace($lastTag)) { return Get-SemanticReleaseCommitRangeBasisResult -ProjectRoot $ProjectRoot -IsRepository -LastTag $lastTag } return Get-SemanticReleaseCommitRangeBasisResult -ProjectRoot $ProjectRoot -IsRepository } function Get-SemanticReleaseCommitRangeBasisResult { [CmdletBinding()] param( [Parameter(Mandatory)][string]$ProjectRoot, [switch]$IsRepository, [string]$LastTag ) $hasVersionTag = -not [string]::IsNullOrWhiteSpace($LastTag) $revisionRange = $null if ($hasVersionTag) { $revisionRange = "$LastTag..HEAD" } return [pscustomobject]@{ ProjectRoot = $ProjectRoot IsRepository = [bool]$IsRepository HasVersionTag = $hasVersionTag LastTag = $LastTag RevisionRange = $revisionRange } } # Source: src/private/release/Get-SemanticReleaseJsonVersionWriteResult.ps1 function Get-SemanticReleaseJsonVersionWriteResult { [CmdletBinding()] param( [Parameter(Mandatory)][hashtable]$ChangeInfo, [switch]$Applied ) return [pscustomobject]@{ Path = [string]$ChangeInfo.Path Target = [System.IO.Path]::GetFileName([string]$ChangeInfo.Path) Key = [string]$ChangeInfo.Key PreviousVersion = [string]$ChangeInfo.PreviousVersion NewVersion = [string]$ChangeInfo.NewVersion Applied = [bool]$Applied } } # Source: src/private/release/Get-SemanticReleaseNoCommitMessage.ps1 function Get-SemanticReleaseNoCommitMessage { [OutputType([string])] [CmdletBinding()] param( [Parameter(Mandatory)][pscustomobject]$Context ) $unchanged = "$($Context.Target) stays at $($Context.PreviousVersion)." if ([string]::IsNullOrWhiteSpace($Context.LastTag)) { return "No commits found in the repository. $unchanged Create an initial commit first." } return "No commits found since version tag '$($Context.LastTag)'. $unchanged" } # Source: src/private/release/Get-SemanticReleasePropertiesVersionWriteResult.ps1 function Get-SemanticReleasePropertiesVersionWriteResult { [CmdletBinding()] param( [Parameter(Mandatory)][hashtable]$ChangeInfo, [switch]$Applied ) return [pscustomobject]@{ Path = [string]$ChangeInfo.Path Target = [System.IO.Path]::GetFileName([string]$ChangeInfo.Path) Key = [string]$ChangeInfo.Key PreviousVersion = [string]$ChangeInfo.PreviousVersion NewVersion = [string]$ChangeInfo.NewVersion Applied = [bool]$Applied } } # Source: src/private/release/Get-SemanticReleaseVersionPlan.ps1 function Get-SemanticReleaseVersionPlan { [CmdletBinding()] param( [Parameter(Mandatory)][semver]$CurrentVersion, [ValidateSet('Major', 'Minor', 'Patch')] [string]$Label = 'Patch', [switch]$PreviewRelease ) $effectiveLabel = Resolve-SemanticReleaseEffectiveLabel -CurrentVersion $CurrentVersion -Label $Label -PreviewRelease:$PreviewRelease $versionPart = Get-SemanticReleaseVersionPartForPlan -CurrentVersion $CurrentVersion -Label $effectiveLabel -PreviewRelease:$PreviewRelease $releaseType = Resolve-SemanticReleasePreReleaseLabel -CurrentVersion $CurrentVersion -PreviewRelease:$PreviewRelease $newVersion = [semver]::new($versionPart.Major, $versionPart.Minor, $versionPart.Patch, $releaseType, $null) return [pscustomobject]@{ CurrentVersion = $CurrentVersion Label = $Label EffectiveLabel = $effectiveLabel PreviewRelease = [bool]$PreviewRelease NewVersion = $newVersion } } function Get-SemanticReleaseVersionPartForPlan { [CmdletBinding()] param( [Parameter(Mandatory)][semver]$CurrentVersion, [Parameter(Mandatory)][string]$Label, [switch]$PreviewRelease ) if (Test-SemanticReleaseShouldPreserveInitialStableReleaseTarget -CurrentVersion $CurrentVersion -PreviewRelease:$PreviewRelease) { return [pscustomobject]@{ Major = $CurrentVersion.Major Minor = $CurrentVersion.Minor Patch = $CurrentVersion.Patch } } if ($PreviewRelease) { if (-not [string]::IsNullOrWhiteSpace($CurrentVersion.PreReleaseLabel)) { return [pscustomobject]@{ Major = $CurrentVersion.Major Minor = $CurrentVersion.Minor Patch = $CurrentVersion.Patch } } return Resolve-SemanticReleaseVersionPart -CurrentVersion $CurrentVersion -Label Patch } return Resolve-SemanticReleaseVersionPart -CurrentVersion $CurrentVersion -Label $Label } function Test-SemanticReleaseShouldPreserveInitialStableReleaseTarget { [OutputType([bool])] [CmdletBinding()] param( [Parameter(Mandatory)][semver]$CurrentVersion, [switch]$PreviewRelease ) if ($PreviewRelease -or -not [string]::IsNullOrWhiteSpace($CurrentVersion.PreReleaseLabel)) { return $false } return $CurrentVersion.Major -eq 1 -and $CurrentVersion.Minor -eq 0 -and $CurrentVersion.Patch -eq 0 } function Resolve-SemanticReleaseEffectiveLabel { [OutputType([string])] [CmdletBinding()] param( [Parameter(Mandatory)][semver]$CurrentVersion, [Parameter(Mandatory)][string]$Label, [switch]$PreviewRelease ) if (-not (Test-SemanticReleaseUsesMajorZeroFallback -CurrentVersion $CurrentVersion -Label $Label -PreviewRelease:$PreviewRelease)) { return $Label } Write-Warning (Get-SemanticReleaseInitialDevelopmentMessage) return 'Minor' } function Test-SemanticReleaseUsesMajorZeroFallback { [OutputType([bool])] [CmdletBinding()] param( [Parameter(Mandatory)][semver]$CurrentVersion, [Parameter(Mandatory)][string]$Label, [switch]$PreviewRelease ) if ($PreviewRelease) { return $false } if ($Label -ne 'Major') { return $false } return $CurrentVersion.Major -eq 0 } function Get-SemanticReleaseInitialDevelopmentMessage { [OutputType([string])] [CmdletBinding()] param() return 'Major version zero (0.y.z) is for initial development, so SemanticRelease plans this breaking change as the next minor version instead of 1.0.0. Set 1.0.0 manually once the software is stable; after that, automatic major-version bumps work normally.' } # Source: src/private/release/Get-SemanticReleaseVersionUpdateContext.ps1 function Get-SemanticReleaseVersionUpdateContext { [CmdletBinding()] param( [Parameter(Mandatory)][string]$Path, [Parameter(Mandatory)][string]$ProjectRoot, [AllowEmptyString()][string]$Key, [switch]$PreviewRelease ) $fileContext = Get-SemanticReleaseVersionFileContext -Path $Path -Key $Key $commitRangeBasis = Get-SemanticReleaseCommitRangeBasis -ProjectRoot $ProjectRoot $commitMessages = @(Read-SemanticReleaseCommitMessage -ProjectRoot $ProjectRoot -CommitRangeBasis $commitRangeBasis) $hasCommits = $commitMessages.Count -gt 0 -or -not $commitRangeBasis.IsRepository $versionChange = Get-SemanticReleaseVersionUpdateChange -PreviousVersion $fileContext.PreviousVersion -CommitMessages $commitMessages -HasCommits:$hasCommits -PreviewRelease:$PreviewRelease return [pscustomobject]@{ Format = $fileContext.Format Path = $fileContext.Path Target = [System.IO.Path]::GetFileName($fileContext.Path) Key = $fileContext.Key PreviousVersion = $fileContext.PreviousVersion NewVersion = $versionChange.NewVersion Label = $versionChange.Label EffectiveLabel = $versionChange.EffectiveLabel PreviewRelease = [bool]$PreviewRelease CommitMessages = $commitMessages CommitCount = $commitMessages.Count HasCommits = $hasCommits LastTag = $commitRangeBasis.LastTag Action = "Update $($fileContext.Key) to $($versionChange.NewVersion)" } } function Get-SemanticReleaseVersionUpdateChange { [OutputType([hashtable])] [CmdletBinding()] param( [Parameter(Mandatory)][string]$PreviousVersion, [AllowEmptyCollection()][string[]]$CommitMessages = @(), [switch]$HasCommits, [switch]$PreviewRelease ) if (-not $HasCommits) { return @{ NewVersion = $PreviousVersion; Label = $null; EffectiveLabel = $null } } $label = Resolve-SemanticReleaseVersionLabel -Message $CommitMessages $versionPlan = Get-SemanticReleaseVersionPlan -CurrentVersion ([semver]$PreviousVersion) -Label $label -PreviewRelease:$PreviewRelease return @{ NewVersion = $versionPlan.NewVersion.ToString() Label = $label EffectiveLabel = $versionPlan.EffectiveLabel } } function Get-SemanticReleaseVersionFileContext { [OutputType([hashtable])] [CmdletBinding()] param( [Parameter(Mandatory)][string]$Path, [AllowEmptyString()][string]$Key ) $extension = [System.IO.Path]::GetExtension($Path).ToLowerInvariant() switch ($extension) { '.json' { $resolvedKey = Get-SemanticReleaseVersionTargetKey -Key $Key -DefaultKey 'Version' $jsonData = Read-SemanticReleaseJsonData -JsonPath $Path if (-not $jsonData.ContainsKey($resolvedKey)) { throw "JSON key '$resolvedKey' was not found in $Path" } return @{ Format = 'Json' Path = $Path Key = $resolvedKey PreviousVersion = [string]$jsonData[$resolvedKey] } } '.properties' { $resolvedKey = Get-SemanticReleaseVersionTargetKey -Key $Key -DefaultKey 'version' $propertiesData = Read-SemanticReleasePropertiesData -PropertiesPath $Path -Key $resolvedKey return @{ Format = 'Properties' Path = $Path Key = $resolvedKey PreviousVersion = [string]$propertiesData.PreviousVersion } } default { throw "Unsupported version file extension '$extension'. Supported extensions are .json and .properties." } } } function Get-SemanticReleaseVersionTargetKey { [OutputType([string])] [CmdletBinding()] param( [AllowEmptyString()][string]$Key, [Parameter(Mandatory)][string]$DefaultKey ) if ([string]::IsNullOrWhiteSpace($Key)) { return $DefaultKey } return $Key } # Source: src/private/release/Get-SemanticReleaseVersionUpdateResult.ps1 function Get-SemanticReleaseVersionUpdateResult { [CmdletBinding()] param( [Parameter(Mandatory)][pscustomobject]$Context, [switch]$Applied ) return [pscustomobject]@{ Path = $Context.Path Target = $Context.Target Key = $Context.Key PreviousVersion = $Context.PreviousVersion NewVersion = $Context.NewVersion Applied = [bool]$Applied Label = $Context.Label EffectiveLabel = $Context.EffectiveLabel PreviewRelease = [bool]$Context.PreviewRelease CommitMessages = @($Context.CommitMessages) CommitCount = $Context.CommitCount } } # Source: src/private/release/Invoke-SemanticReleaseVersionUpdate.ps1 function Invoke-SemanticReleaseVersionUpdate { [CmdletBinding()] param( [Parameter(Mandatory)][pscustomobject]$Context ) $writerResult = Invoke-SemanticReleaseVersionWriter -Context $Context return Get-SemanticReleaseVersionUpdateResult -Context $Context -Applied:$writerResult.Applied } function Invoke-SemanticReleaseVersionWriter { [CmdletBinding()] param( [Parameter(Mandatory)][pscustomobject]$Context ) switch ($Context.Format) { 'Json' { return Set-SemanticReleaseJsonVersion -Path $Context.Path -Key $Context.Key -Label $Context.EffectiveLabel -Preview:$Context.PreviewRelease -Confirm:$false } 'Properties' { return Set-SemanticReleasePropertiesVersion -Path $Context.Path -Key $Context.Key -Label $Context.EffectiveLabel -Preview:$Context.PreviewRelease -Confirm:$false } default { throw "Unsupported version update format '$($Context.Format)'." } } } # Source: src/private/release/Read-SemanticReleaseCommitMessage.ps1 function Read-SemanticReleaseCommitMessage { [OutputType([object[]])] [CmdletBinding()] param( [Parameter(Mandatory)][string]$ProjectRoot, [pscustomobject]$CommitRangeBasis ) $commitRangeBasis = $CommitRangeBasis if ($null -eq $commitRangeBasis) { $commitRangeBasis = Get-SemanticReleaseCommitRangeBasis -ProjectRoot $ProjectRoot } if (-not $commitRangeBasis.IsRepository) { return @() } $format = '%s%n%b%n--END-COMMIT--' $logResult = Get-SemanticReleaseCommitLogResult -ProjectRoot $ProjectRoot -Format $format -CommitRangeBasis $commitRangeBasis return @(ConvertFrom-SemanticReleaseCommitLogResult -Result $logResult) } function Get-SemanticReleaseCommitLogResult { [CmdletBinding()] param( [Parameter(Mandatory)][string]$ProjectRoot, [Parameter(Mandatory)][string]$Format, [Parameter(Mandatory)][pscustomobject]$CommitRangeBasis ) if ($CommitRangeBasis.HasVersionTag) { return Invoke-SemanticReleaseGitCommand -ProjectRoot $ProjectRoot -Arguments @('log', $CommitRangeBasis.RevisionRange, "--format=$Format") } return Invoke-SemanticReleaseGitCommand -ProjectRoot $ProjectRoot -Arguments @('log', "--format=$Format") } function ConvertFrom-SemanticReleaseCommitLogResult { [OutputType([object[]])] [CmdletBinding()] param( [Parameter(Mandatory)][pscustomobject]$Result ) if ($Result.ExitCode -ne 0 -or @($Result.Output).Count -eq 0) { return @() } $text = @($Result.Output) -join [Environment]::NewLine $commitList = $text -split '(?m)^--END-COMMIT--\r?$' return @($commitList | ForEach-Object { $_.Trim() } | Where-Object { -not [string]::IsNullOrWhiteSpace($_) }) } # Source: src/private/release/Resolve-SemanticReleasePreReleaseLabel.ps1 function Resolve-SemanticReleasePreReleaseLabel { [CmdletBinding()] param( [AllowNull()][semver]$CurrentVersion, [switch]$PreviewRelease ) if ($PreviewRelease) { return Resolve-SemanticReleasePreviewLabel -CurrentVersion $CurrentVersion } return $null } function Resolve-SemanticReleasePreviewLabel { [OutputType([string])] [CmdletBinding()] param( [AllowNull()][semver]$CurrentVersion ) if ($null -eq $CurrentVersion -or [string]::IsNullOrWhiteSpace($CurrentVersion.PreReleaseLabel)) { return 'preview' } return Get-SemanticReleaseNextPreReleaseLabel -PreReleaseLabel $CurrentVersion.PreReleaseLabel } function Get-SemanticReleaseNextPreReleaseLabel { [OutputType([string])] [CmdletBinding()] param( [Parameter(Mandatory)][string]$PreReleaseLabel ) $match = [regex]::Match($PreReleaseLabel, '^(?<Stem>.*?)(?<Number>\d+)?$') $stem = $match.Groups['Stem'].Value $preReleaseNumber = $match.Groups['Number'].Value if ([string]::IsNullOrWhiteSpace($preReleaseNumber)) { return "$stem$( Get-SemanticReleaseInitialPreReleaseNumber )" } return "$stem$( Get-SemanticReleaseIncrementedPreReleaseNumber -PreReleaseNumber $preReleaseNumber )" } function Get-SemanticReleaseInitialPreReleaseNumber { [OutputType([string])] [CmdletBinding()] param() return '01' } function Get-SemanticReleaseIncrementedPreReleaseNumber { [CmdletBinding()] param( [Parameter(Mandatory)][string]$PreReleaseNumber ) $nextNumber = [int]$PreReleaseNumber + 1 if ($PreReleaseNumber.Length -eq 1) { return $nextNumber } return $nextNumber.ToString("D$( $PreReleaseNumber.Length )") } # Source: src/private/release/Resolve-SemanticReleaseVersionLabel.ps1 function Resolve-SemanticReleaseVersionLabel { [OutputType([string])] [CmdletBinding()] param( [AllowEmptyCollection()][string[]]$Message = @() ) if ($Message | Where-Object { Test-SemanticReleaseBreakingChangeMessage -Message $_ }) { return 'Major' } if ($Message | Where-Object { $_ -match '(?im)^\s*feat(\(.+\))?:' }) { return 'Minor' } if ($Message | Where-Object { $_ -match '(?im)^\s*fix(\(.+\))?:' }) { return 'Patch' } return 'Patch' } function Test-SemanticReleaseBreakingChangeMessage { [OutputType([bool])] [CmdletBinding()] param( [AllowEmptyString()][string]$Message ) # Conventional Commits footer tokens are case-sensitive and must start a line. if ($Message -cmatch '(?m)^BREAKING[ -]CHANGE:') { return $true } return $Message -match '(?im)^\s*[a-z]+(\(.+\))?!:' } # Source: src/private/release/Resolve-SemanticReleaseVersionPart.ps1 function Resolve-SemanticReleaseVersionPart { [CmdletBinding()] param( [Parameter(Mandatory)][semver]$CurrentVersion, [ValidateSet('Major', 'Minor', 'Patch')] [string]$Label = 'Patch' ) if (Test-SemanticReleaseShouldFinalizePrereleaseTarget -CurrentVersion $CurrentVersion -Label $Label) { return Get-SemanticReleaseVersionPartObject -CurrentVersion $CurrentVersion } switch ($Label) { 'Major' { return [pscustomobject]@{ Major = $CurrentVersion.Major + 1 Minor = 0 Patch = 0 } } 'Minor' { return [pscustomobject]@{ Major = $CurrentVersion.Major Minor = $CurrentVersion.Minor + 1 Patch = 0 } } default { return [pscustomobject]@{ Major = $CurrentVersion.Major Minor = $CurrentVersion.Minor Patch = $CurrentVersion.Patch + 1 } } } } function Test-SemanticReleaseShouldFinalizePrereleaseTarget { [OutputType([bool])] [CmdletBinding()] param( [Parameter(Mandatory)][semver]$CurrentVersion, [Parameter(Mandatory)][string]$Label ) if ([string]::IsNullOrWhiteSpace($CurrentVersion.PreReleaseLabel)) { return $false } return (Get-SemanticReleaseTargetLabelForPrerelease -CurrentVersion $CurrentVersion) -eq $Label } function Get-SemanticReleaseTargetLabelForPrerelease { [OutputType([string])] [CmdletBinding()] param( [Parameter(Mandatory)][semver]$CurrentVersion ) if ($CurrentVersion.Patch -gt 0) { return 'Patch' } if ($CurrentVersion.Minor -gt 0) { return 'Minor' } return 'Major' } function Get-SemanticReleaseVersionPartObject { [CmdletBinding()] param( [Parameter(Mandatory)][semver]$CurrentVersion ) return [pscustomobject]@{ Major = $CurrentVersion.Major Minor = $CurrentVersion.Minor Patch = $CurrentVersion.Patch } } # Source: src/private/shared/Get-SemanticReleaseGitCommandOutputText.ps1 function Get-SemanticReleaseGitCommandOutputText { [CmdletBinding()] param( [Parameter(Mandatory)][pscustomobject]$Result ) return (@($Result.Output) -join [Environment]::NewLine).Trim() } # Source: src/private/shared/Invoke-SemanticReleaseGitCommand.ps1 function Invoke-SemanticReleaseGitCommand { [CmdletBinding()] param( [Parameter(Mandatory)][string]$ProjectRoot, [Parameter(Mandatory)][string[]]$Arguments ) $output = & git -C $ProjectRoot @Arguments 2> $null return [pscustomobject]@{ ExitCode = $LASTEXITCODE Output = @($output) } } # Source: src/private/shared/Read-SemanticReleaseJsonData.ps1 function Read-SemanticReleaseJsonData { [CmdletBinding()] param( [Parameter(Mandatory)][string]$JsonPath ) $jsonContent = Get-Content -LiteralPath $JsonPath -Raw if ([string]::IsNullOrWhiteSpace($jsonContent)) { throw "JSON file is empty: $JsonPath" } try { $jsonData = $jsonContent | ConvertFrom-Json -AsHashtable } catch { throw "JSON file is not valid JSON: $JsonPath. $( $_.Exception.Message )" } if ($jsonData -isnot [hashtable]) { throw "JSON file must contain a top-level JSON object: $JsonPath" } return $jsonData } # Source: src/private/shared/Read-SemanticReleasePropertiesData.ps1 function Read-SemanticReleasePropertiesData { [OutputType([hashtable])] [CmdletBinding()] param( [Parameter(Mandatory)][string]$PropertiesPath, [Parameter(Mandatory)][string]$Key ) $lines = [string[]](Get-Content -LiteralPath $PropertiesPath) if ($lines.Count -eq 0) { throw "Properties file is empty: $PropertiesPath" } $propertyData = Find-SemanticReleasePropertyData -Lines $lines -Key $Key if ($null -eq $propertyData) { throw "Properties key '$Key' was not found in $PropertiesPath" } return @{ Path = $PropertiesPath Key = $Key PreviousVersion = $propertyData.PreviousVersion LineIndex = $propertyData.LineIndex Prefix = $propertyData.Prefix Suffix = $propertyData.Suffix Lines = $lines } } function Find-SemanticReleasePropertyData { [OutputType([hashtable])] [CmdletBinding()] param( [Parameter(Mandatory)][string[]]$Lines, [Parameter(Mandatory)][string]$Key ) $pattern = '^(?<prefix>\s*{0}\s*[:=]\s*)(?<value>.*?)(?<suffix>\s*)$' -f [regex]::Escape($Key) for ($index = 0; $index -lt $Lines.Count; $index++) { $line = $Lines[$index] if ($line -match '^\s*[#!]') { continue } $match = [regex]::Match($line, $pattern) if (-not $match.Success) { continue } return @{ PreviousVersion = $match.Groups['value'].Value.Trim() LineIndex = $index Prefix = $match.Groups['prefix'].Value Suffix = $match.Groups['suffix'].Value } } } # Source: src/private/shared/Test-SemanticReleaseGitAvailable.ps1 function Test-SemanticReleaseGitAvailable { [CmdletBinding()] param() return $null -ne (Get-Command -Name 'git' -ErrorAction SilentlyContinue) } # Source: src/private/shared/Write-SemanticReleaseJsonData.ps1 function Write-SemanticReleaseJsonData { [CmdletBinding()] param( [Parameter(Mandatory)][string]$JsonPath, [Parameter(Mandatory)][hashtable]$Data ) $jsonContent = $Data | ConvertTo-Json -Depth 20 Set-Content -LiteralPath $JsonPath -Value $jsonContent -Encoding utf8 } # Source: src/private/shared/Write-SemanticReleasePropertiesData.ps1 function Write-SemanticReleasePropertiesData { [CmdletBinding()] param( [Parameter(Mandatory)][hashtable]$Data, [Parameter(Mandatory)][string]$NewVersion ) $updatedLines = [string[]]$Data.Lines.Clone() $updatedLines[$Data.LineIndex] = '{0}{1}{2}' -f $Data.Prefix, $NewVersion, $Data.Suffix Set-Content -LiteralPath $Data.Path -Value $updatedLines -Encoding utf8 } |