forked from ModuleBuild/ModuleBuild
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet-BuildEnvironment.ps1
More file actions
75 lines (60 loc) · 2.24 KB
/
Copy pathSet-BuildEnvironment.ps1
File metadata and controls
75 lines (60 loc) · 2.24 KB
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
. .\Get-BuildEnvironment.ps1
. .\New-DynamicParameter.ps1
function Set-BuildEnvironment {
<#
.SYNOPSIS
Sets a stored setting in a buildenvironment.json file.
.DESCRIPTION
Sets the stored setting in a buildenvironment.json file.
.PARAMETER Path
Specifies the path to a buildenvironment.json file.
.LINK
https://github.com/zloeber/ModuleBuild
.EXAMPLE
TBD
#>
[CmdletBinding()]
param(
[parameter(Position = 0, ValueFromPipeline = $TRUE)]
[String]$Path
)
DynamicParam {
# Create dictionary
$DynamicParameters = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
if ((Test-Path $Path) -and ($Path -like "*.buildenvironment.json")) {
try {
$LoadedBuildEnv = Get-Content $Path | ConvertFrom-Json
$NewParams = (Get-Member -Type 'NoteProperty' -InputObject $LoadedBuildEnv).Name
$NewParams | ForEach-Object {
$NewParamSettings = @{
Name = $_
Type = $LoadedBuildEnv.$_.gettype().Name.toString()
ValueFromPipeline = $TRUE
}
# Add new dynamic parameter to dictionary
New-DynamicParameter @NewParamSettings -Dictionary $DynamicParameters
}
}
catch {
#throw "Unable to load the build file in $Path"
}
}
# Return dictionary with dynamic parameters
$DynamicParameters
}
process {
New-DynamicParameter -CreateVariables -BoundParameters $PSBoundParameters
try {
$LoadedBuildEnv = Get-BuildEnvironment $Path
Foreach ($ParamKey in ($PSBoundParameters.Keys | Where-Object {$_ -ne 'Path'})) {
$LoadedBuildEnv.$ParamKey = $PSBoundParameters[$ParamKey]
Write-Output "Updating $ParamKey to be $($PSBoundParameters[$ParamKey])"
}
$LoadedBuildEnv | ConvertTo-Json | Out-File -FilePath $Path -Encoding:utf8 -Force
Write-Output "Saved configuration file - $Path"
}
catch {
throw "Unable to load the build file in $Path"
}
}
}