Noveris.Build.psm1
<#
#> ################ # Global settings $InformationPreference = "Continue" $ErrorActionPreference = "Stop" ################ # Script variables $semVerPattern = "^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$" $script:BuildState = [PSCustomObject]@{ Stages = @{} RunState = @{} } $script:BuildArtifacts = New-Object 'System.Collections.Generic.HashSet[string]' Function Use-BuildVar { [CmdletBinding()] param( [Parameter(mandatory=$true)] [AllowEmptyString()] [AllowNull()] [string]$Source, [Parameter(mandatory=$true)] [ValidateNotNull()] [ScriptBlock]$Check ) process { $ret = $Source | ForEach-Object -Process $Check if (!$ret) { Write-Error "Source string (${Source}) failed validation" return } $Source } } <# #> Function Set-BuildVersionInfo { [CmdletBinding()] param( [Parameter(mandatory=$true)] [ValidateNotNull()] [AllowEmptyString()] [string[]]$Sources ) process { foreach ($src in $Sources) { if ($src -eq $null -or $src -eq "") { continue } Write-Verbose "Processing version candidate: ${src}" # Strip any refs/tags/ reference at the beginning of the version source $tagBranch = "refs/tags/" if ($src.StartsWith($tagBranch)) { Write-Verbose "Version starts with refs/tags format - Removing" $src = $src.Substring($tagBranch.Length) } if ($src.StartsWith("v")) { Write-Verbose "Version starts with 'v' - Removing" $src = $src.Substring(1) } if ($src -notmatch $semVerPattern) { Write-Verbose "Version string not in correct format. skipping" continue } $Prerelease = $Matches[4] if ($Prerelease -eq $null) { $Prerelease = "" } $Buildmetadata = $Matches[5] if ($Buildmetadata -eq $null) { $Buildmetadata = "" } Write-Verbose "Version is valid" $script:BuildState.RunState["Version"] = [ordered]@{ Full = $src Major = [Convert]::ToInt32($Matches[1]) Minor = [Convert]::ToInt32($Matches[2]) Patch = [Convert]::ToInt32($Matches[3]) Prerelease = $Prerelease Buildmetadata = $Buildmetadata } return } # throw error as we didn't find a valid version source Write-Error "Could not find a valid version source" } } <# #> Function Set-BuildStage { [CmdletBinding()] param( [Parameter(mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$Stage, [Parameter(mandatory=$true)] [ValidateNotNull()] [ScriptBlock]$Script ) process { $script:BuildState.Stages[$Stage] = $Script } } <# #> Function Remove-BuildStage { [CmdletBinding()] param( [Parameter(mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$Stage ) process { $script:BuildState.Stages.Remove($Stage) } } <# #> function Invoke-BuildStages { [CmdletBinding()] param( [Parameter(mandatory=$true)] [ValidateNotNull()] [string[]]$Stages ) process { $Stages | ForEach-Object { if (!$script:BuildState.Stages.ContainsKey($_) -or $script:BuildState.Stages[$_] -eq $null) { Write-Error "Processing for stage ($_) requested, but it does not exist or is null." return } Write-Information "" Write-Information ("================ BEGIN ({0}) Stage: $_" -f [DateTime]::Now.ToString("yyyyMMdd HHmm")) ForEach-Object -Process $script:BuildState.Stages[$_] -InputObject $script:BuildState.RunState Write-Information ("================ END ({0}) Stage: $_" -f [DateTime]::Now.ToString("yyyyMMdd HHmm")) } } } <# #> Function Format-TemplateFile { [CmdletBinding()] param( [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$Template, [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$Target, [Parameter(Mandatory=$true)] [Hashtable]$Content ) process { $dirPath = ([System.IO.Path]::GetDirectoryName($Target)) if (![string]::IsNullOrEmpty($dirPath)) { New-Item -ItemType Directory -Path $dirPath -EA Ignore } Get-Content $Template -Encoding UTF8 | Format-TemplateString -Content $Content | Out-File -Encoding UTF8 $Target } } <# #> Function Format-TemplateString { [CmdletBinding()] param( [Parameter(Mandatory=$true,ValueFromPipeline)] [AllowEmptyString()] [string]$TemplateString, [Parameter(Mandatory=$true)] [Hashtable]$Content ) process { $working = $TemplateString foreach ($key in $Content.Keys) { $working = $working.Replace($key, $Content[$key]) } $working } } <# #> Function Publish-ArtifactFile { [CmdletBinding()] param( [Parameter(mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$BaseUri, [Parameter(mandatory=$false)] [ValidateNotNullOrEmpty()] [string]$Method = "Put", [Parameter(mandatory=$false)] [ValidateNotNull()] [HashTable]$Headers = @{}, [Parameter(mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$File, [Parameter(mandatory=$false)] [ValidateNotNull()] [PSCredential]$Credential ) process { $args = @{ Method = $Method Uri = ("{0}/{1}" -f $BaseUri, (Get-Item $File).Name) Headers = $Headers InFile = $File UseBasicParsing = $true #PreserveAuthorizationOnRedirect = $true } if ($PSBoundParameters.Keys -contains "Credential") { #$args["Authentication"] = "Basic" $args["Credential"] = $Credential } $args $response = Invoke-RestMethod @args } } <# #> Function Get-BuildNumber { [CmdletBinding()] param( ) process { $MinDate = New-Object DateTime -ArgumentList 1970, 1, 1 [Int64]([DateTime]::Now - $MinDate).TotalDays } } <# #> Function Invoke-BuildProcess { [CmdletBinding()] param( [Parameter(mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$FilePath, [Parameter(mandatory=$false)] [ValidateNotNullOrEmpty()] [string[]]$ArgumentList ) process { $args = @{ FilePath = $FilePath NoNewWindow = $true PassThru = $true Wait = $true } if ($PSBoundParameters.Keys -contains "ArgumentList") { $args["ArgumentList"] = $ArgumentList } Write-Information ([string]::Format("Execute Process: {0}", $FilePath)) Write-Information ([string]::Format("ArgumentList: {0}", ($ArgumentList -join " "))) $Process = Start-Process @args Write-Information ([string]::Format("Process Exit Code: {0}", $Process.ExitCode)) if ($Process.ExitCode -ne 0) { Write-Error ([string]::Format("Call to {0} returned non-zero ({1})", $FilePath, $Process.ExitCode)) } } } <# #> Function Use-BuildDirectory { [CmdletBinding()] param( [Parameter(mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$Path ) process { $handle = $null $handle = New-Item -ItemType Directory $Path -EA Ignore | Out-Null Write-Information "Using build directory: ${Path}" if (!(Test-Path $Path -PathType Container)) { Write-Error "Target does not exist or is not a directory" } try { Get-Item $Path -Force } catch { Write-Error $_ } } } <# #> Function Clear-BuildDirectory { [CmdletBinding()] param( [Parameter(mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$Path ) process { Use-BuildDirectory -Path $Path | Out-Null Write-Information "Clearing directory: ${Path}" Get-ChildItem -Path $Path | ForEach-Object { Remove-Item -Path $_.FullName -Recurse -Force } } } <# #> Function Add-BuildArtifact { [CmdletBinding()] param( [Parameter(mandatory=$true)] [ValidateNotNullOrEmpty()] [string]$FilePath ) process { $script:BuildArtifacts.Add($FilePath) | Out-Null } } <# #> Function Get-BuildArtifacts { [CmdletBinding()] param( ) process { $script:BuildArtifacts | ForEach-Object { $_ } } } |