forked from CubeCoders/AMPTemplates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMassEdit.ps1
69 lines (59 loc) · 2.15 KB
/
MassEdit.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
function ProcessKeyValuePair {
param (
[string]$key,
[string]$value,
[string]$originalFileName,
[ref]$configVersionFound
)
if ($key -eq "App.Ports") {
if ($value -match "^\[") {
$jsonFile = $originalFileName.Replace(".kvp", "ports.json")
$parsedValue = $value | convertfrom-json
$parsedValue = $parsedValue | convertto-json -depth 100
$parsedValue | set-content $jsonFile
$find = $key + "=" + $value
$replace = $key + '=@IncludeJson[' + $jsonFile + ']'
(Get-Content $file).replace($find, $replace) | Set-Content $file
}
} elseif ($key -eq "App.UpdateSources") {
if ($value -match "^\[") {
$jsonFile = $originalFileName.Replace(".kvp", "updates.json")
$parsedValue = $value | convertfrom-json
$parsedValue = $parsedValue | convertto-json -depth 100
$parsedValue | set-content $jsonFile
$find = $key + "=" + $value
$replace = $key + '=@IncludeJson[' + $jsonFile + ']'
(Get-Content $file).replace($find, $replace) | Set-Content $file
}
} elseif ($key -eq "Meta.ConfigVersion") {
$configVersionFound.Value = $true
}
return $value
}
$kvpFiles = Get-ChildItem -Path . -Filter *.kvp
foreach ($file in $kvpFiles) {
$fileName = $file.Name
$lines = Get-Content $file.FullName
$configVersionFound = $false
foreach ($line in $lines) {
$line = $line.Trim()
if (-not [string]::IsNullOrWhiteSpace($line)) {
$keyValue = $line.Split("=", 2)
$key = $keyValue[0]
$value = $keyValue[1]
$processedValue = ProcessKeyValuePair $key $value $fileName -configVersionFound ([ref]$configVersionFound)
"$key=$processedValue"
}
}
if (-not $configVersionFound) {
(Get-Content $file.FullName) | ForEach-Object {
if ($_ -match "Meta.NoCommercialUsage") {
$_
"Meta.ConfigVersion=1.1"
}
else {
$_
}
} | Set-Content $file.FullName
}
}