-
Notifications
You must be signed in to change notification settings - Fork 38
/
tokenize-ps3.ps1
158 lines (137 loc) · 7.36 KB
/
tokenize-ps3.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
[CmdletBinding()]
param()
Trace-VstsEnteringInvocation $MyInvocation
try {
[string]$SourcePath = Get-VstsInput -Name SourcePath
[string]$DestinationPath = Get-VstsInput -Name DestinationPath
[string]$ConfigurationJsonFile = Get-VstsInput -Name ConfigurationJsonFile
[string]$ReplaceUndefinedValuesWithEmpty = Get-VstsInput -Name ReplaceUndefinedValuesWithEmpty -AsBool
. $PSScriptRoot\Helpers.ps1
Write-Verbose "Entering script tokenize.ps1"
Write-Verbose "SourcePath = $SourcePath"
Write-Verbose "DestinationPath = $DestinationPath"
Write-Verbose "ConfigurationJsonFile = $ConfigurationJsonFile"
Write-Verbose "ReplaceUndefinedValuesWithEmpty = $ReplaceUndefinedValuesWithEmpty"
$currentPath=Split-Path ((Get-Variable MyInvocation -Scope 0).Value).MyCommand.Path
Import-Module "$currentPath\ps_modules\VstsTaskSdk"
$allVars = ArrayToHash (Get-VstsTaskVariableInfo)
#ConfigurationJsonFile has multiple environment sections.
$environmentName = "default"
$environmentNames = @($environmentName)
if (Test-Path -Path env:RELEASE_ENVIRONMENTNAME) {
$environmentName = (get-item env:RELEASE_ENVIRONMENTNAME).value
if ($environmentNames -notcontains $environmentName) {
$environmentNames += $environmentName
}
}
Write-Host "Environment: $environmentName"
# Validate that $SourcePath is a valid path
Write-Verbose "Validate that SourcePath is a valid path: $SourcePath"
if (!(Test-Path -Path $SourcePath)) {
throw "$SourcePath is not a valid path. Please provide a valid path"
}
# Set $DestinationPath as $SourcePath if it is not passed as input. So, SourceFile gets transformed as DestinationFile
if ($DestinationPath -eq "") {
Write-Verbose "No DestinationPath passed. Use '$SourcePath' as DestinationPath"
$DestinationPath = $SourcePath
}
# Is SourceFile an XML document
$SourceIsXml=Test-ValidXmlFile $SourcePath
# Is there a valid Configuration Json input provided for modifying configuration
if ($ConfigurationJsonFile -ne "") {
Write-Verbose "Using configuration from '$ConfigurationJsonFile'"
$Configuration = Get-JsonFromFile $ConfigurationJsonFile
$environmentNames = $environmentNames | where { $Configuration.$_ }
}
# Create a copy of the source file and manipulate it
$encoding = Get-FileEncoding $SourcePath
Write-Verbose "Detected Encoding: $encoding"
$tempFile = $DestinationPath + '.tmp'
Copy-Item -Force $SourcePath $tempFile -Verbose
<#
Step 1:- if the SourceIsXml and a valid configuration file is provided then
Run through all the XPaths in the Json Configuration and update the XML file
#>
if (($SourceIsXml) -and ($Configuration)) {
Write-Verbose "'$SourcePath' is a XML file. Apply all configurations from '$ConfigurationJsonFile'"
ForEach ($environmentName in $environmentNames) {
$keys = $Configuration.$environmentName.ConfigChanges
$xmlraw = [xml](Get-Content $tempFile -Encoding $encoding)
ForEach ($key in $keys) {
# Check for a namespaced element
if ($key.NamespaceUrl -And $key.NamespacePrefix) {
$ns = New-Object System.Xml.XmlNamespaceManager($xmlraw.NameTable)
$ns.AddNamespace($key.NamespacePrefix, $key.NamespaceUrl)
$node = $xmlraw.SelectSingleNode($key.KeyName, $ns)
} else {
$node = $xmlraw.SelectSingleNode($key.KeyName)
}
if ($node) {
try {
Write-Host "Updating $($key.Attribute) of $($key.KeyName): $($key.Value)"
$node.($key.Attribute) = $key.Value
}
catch {
Write-Error "Failure while updating $($key.Attribute) of $($key.KeyName): $($key.Value)"
}
} else {
Write-Verbose "'$($key.KeyName)' not found in source"
}
}
$xmlraw.Save($tempFile)
}
}
<#
Step 2:- For each token in the source configuration that matches with the regular expression __<tokenname>__
i. If there is a custom variable at build or release definition then replace the token with the value of the same..
ii. If the variable is available in the configuration section of json document then replace the token with the vaule from json document
iii.Or else it ignores the token
#>
$regex = '__[A-Za-z0-9._-]*__'
$matches = select-string -Path $tempFile -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value }
ForEach ($match in $matches) {
Write-Host "Updating token '$match'"
$matchedItem = $match
$matchedItem = $matchedItem.Trim('_')
$variableValue = $match
try {
if ($allVars.ContainsKey($matchedItem)) {
$variableValue = $allVars[$matchedItem].Value
Write-Verbose "Found custom variable '$matchedItem' in build or release definition with value '$variableValue'"
} else {
# Select the variable value defined in the current environment. Fall back to the value defined in the default environment if the current environment doesn't define it.
$environmentVariableValue = $environmentNames | foreach { $Configuration.$_.CustomVariables.$matchedItem } | where { $_ } | select -Last 1
if ($environmentVariableValue) {
$variableValue = $environmentVariableValue
Write-Verbose "Found variable '$matchedItem' in configuration with value '$variableValue'"
} else {
# Handling back-compat - earlier we allowed replaced . (dot) with _ and we expected users to have _ while defining key in the CustomVariables section in json
Write-Verbose "This is deprecated"
$matchedItem = $matchedItem -replace '\.','_'
if ($Configuration.$environmentName.CustomVariables.$matchedItem) {
$variableValue = $Configuration.$environmentName.CustomVariables.$matchedItem
Write-Verbose "Found variable '$matchedItem' in configuration with value '$variableValue"
} else {
Write-Host "No value found for token '$match'"
if ($ReplaceUndefinedValuesWithEmpty -eq $true) {
Write-Host "Setting '$match' to an empty value."
# Explicitely set token to empty value if neither environment variable was set nor the value be found in the configuration.
$variableValue = [string]::Empty
}
}
}
}
} catch {
Write-Host "Error searching for variable for token '$match'"
}
(Get-Content $tempFile -Encoding $encoding) |
Foreach-Object {
$_ -replace $match, $variableValue
} |
Set-Content $tempFile -Encoding $encoding -Force
}
Copy-Item -Force $tempFile $DestinationPath
Remove-Item -Force $tempFile
} finally {
Trace-VstsLeavingInvocation $MyInvocation
}