Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting of custom build variables #142

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion BuildHelpers/Public/Set-BuildEnvironment.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -93,6 +109,9 @@ function Set-BuildEnvironment {

[string]$BuildOutput = '$ProjectPath\BuildOutput',

[hashtable]
$CustomVariables = @{},

[switch]
$Force,

Expand All @@ -118,7 +137,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
Expand All @@ -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
Expand Down