-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathkeyvalues-parser.ps1
More file actions
109 lines (95 loc) · 2.82 KB
/
keyvalues-parser.ps1
File metadata and controls
109 lines (95 loc) · 2.82 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
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
function ConvertFrom-KeyValues {
param(
[string]$text
)
$lines = $text -split "`r?`n"
$stack = @()
$current = @{}
$pendingKey = $null
foreach ($line in $lines) {
$line = $line.Trim()
if ($line -eq "" -or $line.StartsWith("//")) { continue }
# "Key" "Value"
if ($line -match '^\s*"([^"]+)"\s+"([^"]+)"\s*$') {
$k = $matches[1]
$v = $matches[2]
if ($current.ContainsKey($k)) {
# If key already exists, convert to array
if ($current[$k] -isnot [array]) {
$current[$k] = @($current[$k])
}
$current[$k] += $v
}
else {
$current[$k] = $v
}
continue
}
# "Key"
if ($line -match '^\s*"([^"]+)"\s*$') {
$pendingKey = $matches[1]
continue
}
# {
if ($line -eq "{") {
$stack += @($current)
$newDict = @{}
if ($pendingKey) {
$current[$pendingKey] = $newDict
$current = $newDict
$pendingKey = $null
}
continue
}
# }
if ($line -eq "}") {
$current = $stack[-1]
$stack = $stack[0..($stack.Count - 2)]
continue
}
Write-Error "KeyValue parsing failed: $line"
exit 1
}
return $current
}
function Test-UpdaterFile {
param(
[string]$KeyValueFile
)
if (-not $KeyValueFile -or -not (Test-Path $KeyValueFile)) {
Write-Error "KeyValue file not specified or does not exist: $KeyValueFile"
exit 1
}
$text = Get-Content $KeyValueFile -Raw
$parsed = ConvertFrom-KeyValues -text $text
$missingSections = @()
if (-not $parsed.ContainsKey("Updater")) {
$missingSections += "Updater"
}
else {
$updater = $parsed["Updater"]
if (-not $updater.ContainsKey("Information")) {
$missingSections += "Updater.Information"
}
else {
$info = $updater["Information"]
if (-not $info.ContainsKey("Version") -or -not $info["Version"].ContainsKey("Latest")) {
$missingSections += "Updater.Information.Version.Latest"
}
if (-not $info.ContainsKey("Notes")) {
$missingSections += "Updater.Information.Notes"
}
}
if (-not $updater.ContainsKey("Files") -or -not $updater["Files"].ContainsKey("Plugin")) {
$missingSections += "Updater.Files.Plugin"
}
}
if ($missingSections.Count -eq 0) {
return $true
}
else {
Write-Host "Missing sections:"
$missingSections | ForEach-Object { Write-Host "- $_" }
return $false
}
}