Skip to content

Commit

Permalink
update to allow overriding settings via env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
sayedihashimi committed Jul 15, 2015
1 parent 71eb78f commit b00cc3c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/psbuild.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,42 @@ $global:PSBuildSettings = New-Object PSObject -Property @{
EnableAddingHashToLogDir = $true
}

function InternalOverrideSettingsFromEnv{
[cmdletbinding()]
param(
[Parameter(Position=0)]
[object[]]$settings = ($global:PSBuildSettings),

[Parameter(Position=1)]
[string]$prefix = 'PSBuild'
)
process{
foreach($settingsObj in $settings){
if($settingsObj -eq $null){
continue
}

$settingNames = $null
if($settingsObj -is [hashtable]){
$settingNames = $settingsObj.Keys
}
else{
$settingNames = ($settingsObj | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name)

}

foreach($name in ($settingNames.Clone())){
$fullname = ('{0}{1}' -f $prefix,$name)
if(Test-Path "env:$fullname"){
'Updating setting [{0}] to [{1}]' -f ($settingsObj.$name),((get-childitem "env:$fullname").Value) | Write-Verbose
$settingsObj.$name = ((get-childitem "env:$fullname").Value)
}
}
}
}
}
InternalOverrideSettingsFromEnv -settings $global:PSBuildSettings -prefix PSBuild

<#
.SYNOPSIS
This returns the path to the tools folder. The tools folder is where you can find
Expand Down
22 changes: 22 additions & 0 deletions tests/psbuild.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,26 @@ Describe 'file-replacer tests'{
get-command -Module file-replacer | Should not be $null
(get-command -Module file-replacer).Name.Contains('Replace-TextInFolder') | Should be $true
}
}

Describe 'settings tests'{
It 'can override a setting with an env var'{
$env:PSBuildEnableBuildLogging = $false
$env:PSBuildBuildMessageEnabled = $false
$env:PSBuildDefaultClp = 'custom value'

Remove-Module -Name psbuild -Force | Out-Null
Remove-Item -Path variable:PSBuildSettings | Out-Null
# import the module
. $importPsbuild

try{
[Convert]::ToBoolean($global:PSBuildSettings.EnableBuildLogging) | Should be $false
[Convert]::ToBoolean($global:PSBuildSettings.BuildMessageEnabled) | Should be $false
$global:PSBuildSettings.DefaultClp | Should be 'custom value'
}
finally{
Remove-Item -Path env:PSBuildEnableBuildLogging,env:PSBuildBuildMessageEnabled,env:PSBuildDefaultClp
}
}
}

0 comments on commit b00cc3c

Please sign in to comment.