From c15cde411dc74126437d397ad0e3101e27a3cb70 Mon Sep 17 00:00:00 2001 From: "Craig E. Shea" Date: Wed, 9 Aug 2023 11:52:50 -0400 Subject: [PATCH 1/2] feat(Set-BuildEnvironment): All environment variables are created with UPPER-CASED names Fixes #138 --- BuildHelpers/Public/Set-BuildEnvironment.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BuildHelpers/Public/Set-BuildEnvironment.ps1 b/BuildHelpers/Public/Set-BuildEnvironment.ps1 index a94c39c..0ea68ea 100644 --- a/BuildHelpers/Public/Set-BuildEnvironment.ps1 +++ b/BuildHelpers/Public/Set-BuildEnvironment.ps1 @@ -118,7 +118,7 @@ function Set-BuildEnvironment { $BuildHelpersVariables = Get-BuildEnvironment @GBEParams foreach ($VarName in $BuildHelpersVariables.Keys) { if($null -ne $BuildHelpersVariables[$VarName]) { - $prefixedVar = "$VariableNamePrefix$VarName" + $prefixedVar = "$VariableNamePrefix$VarName".ToUpperInvariant() Write-Verbose "storing [$prefixedVar] with value '$($BuildHelpersVariables[$VarName])'." $Output = New-Item -Path Env:\ -Name $prefixedVar -Value $BuildHelpersVariables[$VarName] -Force:$Force From c99e86701f2ffa7f59eed18aea5c293c915f59b3 Mon Sep 17 00:00:00 2001 From: fourpastmidnight Date: Fri, 11 Aug 2023 10:09:42 -0400 Subject: [PATCH 2/2] feat(Set-BuildEnvironment): Allow custom variables to be defined as part of the build environment --- BuildHelpers/Public/Set-BuildEnvironment.ps1 | 41 ++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/BuildHelpers/Public/Set-BuildEnvironment.ps1 b/BuildHelpers/Public/Set-BuildEnvironment.ps1 index 0ea68ea..0899534 100644 --- a/BuildHelpers/Public/Set-BuildEnvironment.ps1 +++ b/BuildHelpers/Public/Set-BuildEnvironment.ps1 @@ -38,6 +38,22 @@ function Set-BuildEnvironment { -BuildOutput 'C:\Build' -BuildOutput 'C:\Builds\$ProjectName' + .PARAMETER CustomVariables + Specify a hashtable containing one or more additional, custom variables that are to be created when + setting up the build environment. The hashtable key should be the name of the variable to be created. + It is not necessary to add the variable prefix--that will be added according to the value of the + VariableNamePrefix parameter. The value may include variables produced in this same call. Only include + the variable, not ENV or the prefix. Use a literal $. + + Examples: + + -CustomVariables @{ MyCustomVariable = '$ProjectPath\CustomFolder' } + -CustomVariables @{ Variable1 = 'foo'; Variable2 = 'bar' } + -CustomVariables @{ + Variable1 = 'foo' + Variable2 = 'bar' + } + .PARAMETER Passthru If specified, include output of the build variables we create @@ -93,6 +109,9 @@ function Set-BuildEnvironment { [string]$BuildOutput = '$ProjectPath\BuildOutput', + [hashtable] + $CustomVariables = @{}, + [switch] $Force, @@ -131,6 +150,28 @@ function Set-BuildEnvironment { } } } + foreach ($VarName in $CustomVariables.Keys) { + $PrefixedCustomVarName = "${VariableNamePrefix}${VarName}".ToUpperInvariant() + $PrefixedCustomVarValue = $CustomVariables[$VarName] + + $CustomVarsNames = $CustomVariables.Keys | Where-Object { $_ -ine $VarName } + foreach ($CustomVarName in $CustomVarsNames) { + $PrefixedCustomVarValue = $PrefixedCustomVarValue -replace "\`$$CustomVarName", $CustomVariables[$CustomVarName] + } + + foreach($BhVarName in $BuildHelpersVariables.keys){ + $PrefixedCustomVarValue = $PrefixedCustomvarValue -replace "\`$$BhVarName", $BuildHelpersVariables[$BhVarName] + } + + Write-Verbose "Storing [$PrefixedCustomVarName] with value '$PrefixedCustomVarValue'." + $Output = New-Item -Path Env:\ -Name $PrefixedCustomVarName -Value $PrefixedCustomVarValue -Force:$Force + if ("Azure Pipelines" -eq $BuildHelpersVariables['BuildSystem']) { + Set-AzurePipelinesVariable -Name $PrefixedCustomVarName -Value $PrefixedCustomVarValue + } + if ($PassThru) { + $Output + } + } if($VariableNamePrefix -eq 'BH' -and $BuildHelpersVariables.ModulePath) { # Handle existing scripts that reference BHPSModulePath