Public/Get-BuildEnvironment.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
function Get-BuildEnvironment { <# .SYNOPSIS Get normalized build system and project details .FUNCTIONALITY CI/CD .DESCRIPTION Get normalized build system and project details Returns the following details: ProjectPath via Get-BuildVariables BranchName via Get-BuildVariables CommitMessage via Get-BuildVariables BuildNumber via Get-BuildVariables ProjectName via Get-ProjectName PSModuleManifest via Get-PSModuleManifest ModulePath via Split-Path on PSModuleManifest BuildOutput via BuildOutput parameter .PARAMETER Path Path to project root. Defaults to the current working path .PARAMETER BuildOutput Specify a path to use for build output. Defaults to '$ProjectPath\BuildOutput' You may use build variables produced in this same call. Refer to them as variables, with a literal (escaped) $ Examples: -BuildOutput '$ProjectPath\BuildOutput' -BuildOutput 'C:\Build' -BuildOutput 'C:\Builds\$ProjectName' .PARAMETER GitPath Path to git. Defaults to git (i.e. git is in $ENV:PATH) .NOTES We assume you are in the project root, for several of the fallback options .EXAMPLE Get-BuildEnvironment .EXAMPLE Get-BuildEnvironment -Path C:\sc\PSDepend -BuildOutput 'C:\Builds\$ProjectName' # Get BuildEnvironment pointing at C:\sc\PSDepend # Assuming ProjectName evaluates to PSDepend, BuildOutput will be set to C:\Builds\PSDepend .LINK https://github.com/RamblingCookieMonster/BuildHelpers .LINK Get-BuildVariables .LINK Set-BuildEnvironment .LINK Get-ProjectName .LINK about_BuildHelpers #> [cmdletbinding()] param( [validatescript({ Test-Path $_ -PathType Container })] $Path = $PWD.Path, [string]$BuildOutput = '$ProjectPath\BuildOutput', [validatescript({ if(-not (Get-Command $_ -ErrorAction SilentlyContinue)) { throw "Could not find command at GitPath [$_]" } $true })] [string]$GitPath, [validateset('object', 'hashtable')] [string]$As = 'object' ) $GBVParams = @{Path = $Path} if($PSBoundParameters.ContainsKey('GitPath')) { $GBVParams.add('GitPath', $GitPath) } ${Build.Vars} = Get-BuildVariables @GBVParams ${Build.ProjectName} = Get-ProjectName -Path $Path ${Build.ManifestPath} = Get-PSModuleManifest -Path $Path if( ${Build.ManifestPath} ) { ${Build.ModulePath} = Split-Path -Path ${Build.ManifestPath} -Parent } else { ${Build.ModulePath} = $null } $BuildHelpersVariables = [ordered]@{ BuildSystem = ${Build.Vars}.BuildSystem ProjectPath = ${Build.Vars}.ProjectPath BranchName = ${Build.Vars}.BranchName CommitMessage = ${Build.Vars}.CommitMessage BuildNumber = ${Build.Vars}.BuildNumber ProjectName = ${Build.ProjectName} PSModuleManifest = ${Build.ManifestPath} ModulePath = ${Build.ModulePath} } foreach($VarName in $BuildHelpersVariables.keys){ $BuildOutput = $BuildOutput -replace "\`$$VarName", $BuildHelpersVariables[$VarName] } $BuildOutput = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($BuildOutput) $BuildHelpersVariables.add('BuildOutput', $BuildOutput) if($As -eq 'object') { return [pscustomobject]$BuildHelpersVariables } if($As -eq 'hashtable') { return $BuildHelpersVariables } } |