forked from microsoft/AL-Go-Actions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyamlclass.ps1
188 lines (176 loc) · 6.99 KB
/
yamlclass.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#requires -Version 5.0
# This class holds the content of a Yaml file
# The content is stored in an array of strings, where each string is a line of the Yaml file
# The class provides methods to find and replace lines in the Yaml file
class Yaml {
[string[]] $content
# Constructor based on an array of strings
Yaml([string[]] $content) {
$this.content = $content
}
# Static load function to load a Yaml file into a Yaml class
static [Yaml] Load([string] $filename) {
$fileContent = Get-Content -Path $filename -Encoding UTF8
return [Yaml]::new($fileContent)
}
# Save the Yaml file with LF line endings using UTF8 encoding
Save([string] $filename) {
$this.content | Set-ContentLF -Path $filename
}
# Find the lines for the specified Yaml path, given by $line
# If $line contains multiple segments separated by '/', then the segments are searched for recursively
# The Yaml path is case insensitive
# The Search mechanism finds only lines with the right indentation. When searching for a specific job, use jobs:/(job name)/
# $start and $count are set to the start and count of the lines found
# Returns $true if the line are found, otherwise $false
# If $line ends with '/', then the lines for the section are returned only
# If $line doesn't end with '/', then the line + the lines for the section are returned (and the lines for the section are indented)
[bool] Find([string] $line, [ref] $start, [ref] $count) {
if ($line.Contains('/')) {
$idx = $line.IndexOf('/')
$find = $line.Split('/')[0]
$rest = $line.Substring($idx+1)
$s1 = 0
$c1 = 0
if ($rest -eq '') {
[Yaml] $yaml = $this.Get($find, [ref] $s1, [ref] $c1)
if ($yaml) {
$start.value = $s1+1
$count.value = $c1-1
return $true
}
}
else {
[Yaml] $yaml = $this.Get("$find/", [ref] $s1, [ref] $c1)
if ($yaml) {
$s2 = 0
$c2 = 0
if ($yaml.Find($rest, [ref] $s2, [ref] $c2)) {
$start.value = $s1+$s2
$count.value = $c2
return $true
}
}
}
return $false
}
else {
$start.value = -1
$count.value = 0
for($i=0; $i -lt $this.content.Count; $i++) {
$s = "$($this.content[$i]) "
if ($s -like "$($line)*") {
if ($s.TrimEnd() -eq $line) {
$start.value = $i
}
else {
$start.value = $i
$count.value = 1
return $true
}
}
elseif ($start.value -ne -1 -and $s -notlike " *") {
if ($this.content[$i-1].Trim() -eq '') {
$count.value = ($i-$start.value-1)
}
else {
$count.value = ($i-$start.value)
}
return $true
}
}
if ($start.value -ne -1) {
$count.value = $this.content.Count-$start.value
return $true
}
else {
return $false
}
}
}
# Locate the lines for the specified Yaml path, given by $line and return lines as a Yaml object
# $start and $count are set to the start and count of the lines found
# Indentation of the first line returned is 0
# Indentation of the other lines returned is the original indentation - the original indentation of the first line
# Returns $null if the lines are not found
# See Find for more details
[Yaml] Get([string] $line, [ref] $start, [ref] $count) {
$s = 0
$c = 0
if ($this.Find($line, [ref] $s, [ref] $c)) {
$charCount = ($line.ToCharArray() | Where-Object {$_ -eq '/'} | Measure-Object).Count
[string[]] $result = @($this.content | Select-Object -Skip $s -First $c | ForEach-Object {
"$_$(" "*$charCount)".Substring(2*$charCount).TrimEnd()
} )
$start.value = $s
$count.value = $c
return [Yaml]::new($result)
}
else {
return $null
}
}
# Locate the lines for the specified Yaml path, given by $line and return lines as a Yaml object
# Same function as Get, but $start and $count are not set
[Yaml] Get([string] $line) {
[int]$start = 0
[int]$count = 0
return $this.Get($line, [ref] $start, [ref] $count)
}
# Replace the lines for the specified Yaml path, given by $line with the lines in $content
# If $line ends with '/', then the lines for the section are replaced only
# If $line doesn't end with '/', then the line + the lines for the section are replaced
# See Find for more details
[void] Replace([string] $line, [string[]] $content) {
[int]$start = 0
[int]$count = 0
if ($this.Find($line, [ref] $start, [ref] $count)) {
$charCount = ($line.ToCharArray() | Where-Object {$_ -eq '/'} | Measure-Object).Count
if ($charCount) {
$yamlContent = $content | ForEach-Object { "$(" "*$charCount)$_".TrimEnd() }
}
else {
$yamlContent = $content
}
$this.Remove($start, $count)
$this.Insert($start, $yamlContent)
}
else {
Write-Host -ForegroundColor Red "cannot locate $line"
}
}
# Replace all occurrences of $from with $to throughout the Yaml content
[void] ReplaceAll([string] $from, [string] $to) {
$this.content = $this.content | ForEach-Object { $_.replace($from, $to) }
}
# Remove lines in Yaml content
[void] Remove([int] $start, [int] $count) {
if ($count -eq 0) {
return
}
if ($start -eq 0) {
$this.content = $this.content[$count..($this.content.Count-1)]
}
elseif($start + $count -ge $this.content.Count) {
$this.content = $this.content[0..($start-1)]
}
else {
$this.content = $this.content[0..($start-1)] + $this.content[($start+$count)..($this.content.Count-1)]
}
}
# Insert lines in Yaml content
[void] Insert([int] $index, [string[]] $yamlContent) {
if (!$yamlContent) {
return
}
if ($index -eq 0) {
$this.content = $yamlContent + $this.content
}
elseif ($index -eq $this.content.Count) {
$this.content = $this.content + $yamlContent
}
else {
$this.content = $this.content[0..($index-1)] + $yamlContent + $this.content[$index..($this.content.Count-1)]
}
}
}