generated from ebekker/pwsh-github-action-base
-
Notifications
You must be signed in to change notification settings - Fork 2
/
action.ps1
321 lines (244 loc) · 8.37 KB
/
action.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/env pwsh
[cmdletbinding()]
param()
if (-not (Get-Module -ListAvailable GitHubActions)) {
Install-Module GitHubActions -Force
}
Import-Module GitHubActions
$githubToken = Get-ActionInput githubToken -Required
$defaultBranch = Get-ActionInput defaultBranch
$rootDocsFolder = Get-ActionInput rootDocsFolder
$convertRootReadmeToHomePage = Get-ActionInput convertRootReadmeToHomePage
$useHeaderForWikiName = Get-ActionInput useHeaderForWikiName
$customWikiFileHeaderFormat = Get-ActionInput customWikiFileHeaderFormat
$customCommitMessageFormat = Get-ActionInput customCommitMessageFormat
$repositoryName = $env:GITHUB_REPOSITORY
$repositoryUrl = "https://github.com/$repositoryName"
$repositoryCloneUrl = "https://[email protected]/$repositoryName"
$wikiRepoDirectory = ($repositoryName -split "/")[-1] + ".wiki"
$sourceRepoDirectory = $pwd.Path
if (-not $defaultBranch)
{
$defaultBranch = git branch --show-current
}
if (-not $rootDocsFolder)
{
$rootDocsFolder = "."
$rootDocsFolderDirs = @()
}
else
{
$rootDocsFolderDirs = $rootDocsFolder -split "/"
}
$filenameToWikiNameMap = @{}
$wikiNameToFileNameMap = @{}
Function ProcessSourceDirectory()
{
[cmdletbinding()]
param([string[]]$directories=@())
foreach ($file in Get-ChildItem "*.md")
{
ProcessSourceFile $file $directories
}
foreach ($dir in Get-ChildItem -Directory)
{
Push-Location $dir.Name
ProcessSourceDirectory ($directories + @($dir.Name))
Pop-Location
}
}
Function ProcessSourceFile()
{
[cmdletbinding()]
param($file, [string[]]$directories)
Write-Verbose "Processing file $($file.FullName)"
$outputFileName = ($directories + $file.Name) -join "__"
$content = Get-Content -Path $file.FullName
$content = UpdateFileLinks $file.Name $directories $content
$override = GetOutputFileNameFromFile $content
if ($convertRootReadmeToHomePage -and ($directories.Count -eq 0) -and ($file.Name -eq "readme.md"))
{
$outputFileName = "Home.md"
}
elseif ($override)
{
Write-Verbose "Using overridden file name $($override.FileName)"
if ($wikiNameToFileNameMap[$override.FileName])
{
throw "Overridden file name $($override.FileName) is already in use by $($wikiNameToFileNameMap[$override.FileName])"
}
$wikiNameToFileNameMap[$override.FileName] = $outputFileName
$filenameToWikiNameMap[$outputFileName] = $override.FileName
$outputFileName = $override.FileName
$content = $override.NewContent
}
if ($customWikiFileHeaderFormat -and ($file.Name -ne "_sidebar.md"))
{
$content = AddCustomHeader $file.Name $directories $content
}
$outputPath = $wikiRepoPath + "/" + $outputFileName
$content | Set-Content -Path $outputPath
}
Function GetOutputFileNameFromFile()
{
[cmdletbinding()]
param($content)
if ($useHeaderForWikiName)
{
$firstLine = $content | Select-Object -First 1
$headerMatch = [regex]::match($firstLine, "^#\s+(.*)$")
if ($headerMatch.Success)
{
$filename = $headerMatch.Groups[1].Value
$filename = $filename -replace " ", "-"
$filename = $filename -replace "[^A-Za-z0-9\s.(){}_!?-]", ""
return @{
FileName = "$filename.md";
NewContent = $content[1..$content.Count];
}
}
}
}
Function UpdateFileLinks()
{
[cmdletbinding()]
param($filename, [string[]]$directories, $content)
$evaluator = {
[cmdletbinding()]
param($match)
$text = $match.Groups[1].Value
$link = $match.Groups[2].Value
if (($link -like "http*") -or ($link -like "onenote*"))
{
# Absolute link, no change
Write-Verbose "Link $link is already absolute, nothing to do"
return "[$text]($link)"
}
if ($link -like "#*")
{
# Header anchor, no change
Write-Verbose "Link $link is to an anchor within the page (generally a header), nothing to do"
return "[$text]($link)"
}
$upDirs = 0
$path = @()
$link -split "/" | % {
if ($_ -eq "..")
{
$upDirs += 1
}
else
{
$path += @($_)
}
}
$isMarkdownLink = ($link -split "#")[0] -like "*.md"
if (($upDirs -le $directories.Count) -and ($isMarkdownLink))
{
# Link to another doc which should now point to a wiki file
$relativeWikiPath = @($directories | Select-Object -First ($directories.Count - $upDirs)) + @($path)
$wikiFileName = ($relativeWikiPath -join "__") -replace ".md", ""
Write-Verbose "Link $link updated to $wikiFileName"
return "[$text]($wikiFileName)"
}
# Outside the root directory or not a doc, convert to absolute Url
$extraUpDirs = $upDirs - $directories.Count
if ($extraUpDirs -gt $rootDocsFolderDirs.Count)
{
throw "Relative link $link in $filename does not exist"
}
$relativePathFromRoot = ($rootDocsFolderDirs | Select-Object -First ($rootDocsFolderDirs.Count - $extraUpDirs)) -join "/"
if ($relativePathFromRoot) { $relativePathFromRoot += "/" }
$absoluteLink = $repositoryUrl + "/blob/$defaultBranch/" + $relativePathFromRoot + ($path -join "/")
if ($isMarkdownLink)
{
Write-Verbose "Link $link is outside the docs root, updating to absolute link $absoluteLink"
}
else
{
Write-Verbose "Link $link is not a doc, updating to absolute link $absoluteLink"
}
return "[$text]($absoluteLink)"
}
# TODO - handle nested brackets
# Matches `[text](link)`
$linkRegex = [regex]"\[([^\]]+)\]\(([^\)]+)\)"
$content | % { $linkRegex.Replace($_, $evaluator) }
}
Function AddCustomHeader()
{
[cmdletbinding()]
param($filename, [string[]]$directories, $content)
$header = $customWikiFileHeaderFormat
$relativePath = "$rootDocsFolder/$($directories -join "/")"
$sourceFileLink = "$repositoryUrl/blob/$defaultBranch/$relativePath/$($fileName)"
$header = $header -replace "{sourceFileLink}", $sourceFileLink
@($header, "`n", "`n") + $content
}
Function ProcessWikiDirectory()
{
[cmdletbinding()]
param([string[]]$directories=@())
foreach ($file in Get-ChildItem "*.md")
{
ProcessWikiFile $file $directories
}
foreach ($dir in Get-ChildItem -Directory)
{
Push-Location $dir.Name
ProcessWikiDirectory ($directories + @($dir.Name))
Pop-Location
}
}
Function ProcessWikiFile()
{
[cmdletbinding()]
param($file, [string[]]$directories)
Write-Verbose "Processing file $($file.Name)"
$content = Get-Content $file
$filenameToWikiNameMap.Keys | % {
$originalLink = $_ -replace ".md$", ""
$newLink = $filenameToWikiNameMap[$_] -replace ".md$", ""
$content = $content -replace $originalLink, $newLink
}
Set-Content -Path $file.FullName -Value $content -Force
}
Function GetWikiCommitMessage()
{
if (-not $customCommitMessageFormat)
{
return "Sync Files"
}
Push-Location $sourceRepoDirectory
$commitMessage = $customCommitMessageFormat
$message = git log -1 --pretty=%B
$commitMessage = $commitMessage -replace "{commitMessage}", $message
$shaFull = git rev-parse HEAD
$commitMessage = $commitMessage -replace "{shaFull}", $shaFull
$shaShort = git rev-parse --short HEAD
$commitMessage = $commitMessage -replace "{shaShort}", $shaShort
Pop-Location
return $commitMessage
}
git config --global user.email "[email protected]"
git config --global user.name "GitHub Action"
Push-Location ..
Write-ActionInfo "Cloning wiki repo..."
git clone "$repositoryCloneUrl.wiki.git"
$wikiRepoPath = $pwd.Path + "/" + $wikiRepoDirectory
cd $wikiRepoDirectory
git rm -rf * | Out-Null
Pop-Location
Push-Location $rootDocsFolder
Write-ActionInfo "Processing source directory..."
ProcessSourceDirectory
Pop-Location
Push-Location ..\$wikiRepoDirectory
Write-ActionInfo "Post-processing wiki files..."
ProcessWikiDirectory
$commitMessage = GetWikiCommitMessage
Write-ActionInfo "Pushing wiki"
git add .
git commit -am $commitMessage
git push
Pop-Location