Public/Set-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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
function Set-BuildEnvironment { <# .SYNOPSIS Normalize build system and project details into environment variables .FUNCTIONALITY CI/CD .DESCRIPTION Normalize build system and project details into environment variables Creates the following environment variables: $ENV:<VariableNamePrefix>ProjectPath via Get-BuildVariables $ENV:<VariableNamePrefix>BranchName via Get-BuildVariables $ENV:<VariableNamePrefix>CommitMessage via Get-BuildVariables $ENV:<VariableNamePrefix>BuildNumber via Get-BuildVariables $ENV:<VariableNamePrefix>ProjectName via Get-ProjectName $ENV:<VariableNamePrefix>PSModuleManifest via Get-PSModuleManifest $ENV:<VariableNamePrefix>ModulePath via Split-Path on PSModuleManifest $ENV:<VariableNamePrefix>BuildOutput via BuildOutput parameter $ENV:BHPSModulePath Legacy, via Split-Path on PSModuleManifest If you don't specify a prefix or use BH, we create BHPSModulePath (This will be removed July 1st) .PARAMETER Path Path to project root. Defaults to the current working path .PARAMETER VariableNamePrefix Allow to set a custom Prefix to the Environment variable created. The default is BH such as $Env:BHProjectPath .PARAMETER BuildOutput Specify a path to use for build output. Defaults to '$ProjectPath\BuildOutput' You may use build variables produced in this same call. Only include the variable, not ENV or the prefix. Use a literal $. Examples: -BuildOutput '$ProjectPath\BuildOutput' -BuildOutput 'C:\Build' -BuildOutput 'C:\Builds\$ProjectName' .PARAMETER Passthru If specified, include output of the build variables we create .PARAMETER GitPath Path to git. Defaults to git (i.e. git is in $ENV:PATH) .PARAMETER Force Overrides the Environment Variables even if they exist already .NOTES We assume you are in the project root, for several of the fallback options .EXAMPLE Set-BuildEnvironment Get-Item ENV:BH* .EXAMPLE Set-BuildEnvironment -VariableNamePrefix '' -Force Get-Item ENV:* .EXAMPLE Set-BuildEnvironment -Path C:\sc\PSDepend -BuildOutput 'C:\Builds\$ProjectName' # Set BuildEnvironment pointing at C:\sc\PSDepend # Assuming ProjectName evaluates to PSDepend, BuildOutput variable will be set to C:\Builds\PSDepend .LINK https://github.com/RamblingCookieMonster/BuildHelpers .LINK Get-BuildVariables .LINK Get-BuildEnvironment .LINK Get-ProjectName .LINK about_BuildHelpers #> [cmdletbinding()] param( [validatescript({ Test-Path $_ -PathType Container })] $Path = $PWD.Path, [ValidatePattern('\w*')] [String] $VariableNamePrefix = 'BH', [string]$BuildOutput = '$ProjectPath\BuildOutput', [switch] $Force, [switch]$Passthru, [validatescript({ if(-not (Get-Command $_ -ErrorAction SilentlyContinue)) { throw "Could not find command at GitPath [$_]" } $true })] [string]$GitPath ) $GBEParams = @{ Path = $Path As = 'hashtable' BuildOutput = $BuildOutput } if($PSBoundParameters.ContainsKey('GitPath')) { $GBEParams.add('GitPath', $GitPath) } $BuildHelpersVariables = Get-BuildEnvironment @GBEParams foreach ($VarName in $BuildHelpersVariables.Keys) { if($null -ne $BuildHelpersVariables[$VarName]) { $Output = New-Item -Path Env:\ -Name ('{0}{1}' -f $VariableNamePrefix,$VarName) -Value $BuildHelpersVariables[$VarName] -Force:$Force if($Passthru) { $Output } } } if($VariableNamePrefix -eq 'BH' -and $BuildHelpersVariables.ModulePath) { # Handle existing scripts that reference BHPSModulePath $Output = New-Item -Path Env:\ -Name BHPSModulePath -Value $BuildHelpersVariables.ModulePath -Force:$Force if($Passthru) { $Output } } } |