-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoccheck.ps1
91 lines (75 loc) · 2.91 KB
/
toccheck.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
<#
.SYNOPSIS
Validate a given table of contents file for broken links and the repo for unused files.
.DESCRIPTION
Parse a given TOC.md and note all of the referenced links. Search
from the directory relative to the TOC.md and validate that all of
the referenced files exist. Also, report any *.md files not referenced
by the TOC.md. Output is written to log.txt.
.PARAMETER tocFile
The path to the TOC.md that you want to evaluate.
.NOTES
Author: Keith Robertson <keithro@gmail.com>
#>
Param(
[Parameter(Mandatory = $True, HelpMessage = "Please the path to the TOC.md")]
[string]$tocFile
)
if ( -Not (Test-Path $tocFile) ) {
[Console]::ForegroundColor = 'red'
[Console]::Error.WriteLine("ERROR: $tocFile does not exist. Please supply a valid path.")
[Console]::ResetColor()
exit 1
}
$tocDir = (get-item $tocFile).Directory
#
# Find all of the *.md files relative to the TOC.md
#
[System.Collections.ArrayList]$mdfiles = Get-ChildItem -Path $tocDir -Filter *.md -Recurse -File -Name
Write-Host("INFO: There are " + $mdfiles.Count + " *.md files in " + $tocDir)
#
# Time to start looping through the the TOC MD file.
#
[System.Collections.ArrayList]$missingFiles = @()
$linecount = 1
foreach ($line in [System.IO.File]::ReadLines((get-item $tocFile).FullName)) {
# Write-Host "LINE: $linecount $line"
$linecount += 1
$groups = [regex]::Match($line, '^.+\[(.+)\].*?\((.+)\).*?').captures.groups
if ( $groups -ne $null -and $groups.count -eq 3 ) {
$mdfile = $groups[2].value.split('?', [System.StringSplitOptions]::RemoveEmptyEntries)[0]
if ( -Not (Test-Path $mdfile) ) {
$var = $missingFiles.Add($groups[2].value)
[Console]::ForegroundColor = 'red'
[Console]::Error.WriteLine("WARN: Found " + $groups[2].value + " on line $linecount. It does not exist.")
[Console]::ResetColor()
}
$mdfiles.Remove($mdfile.Replace('/', '\'))
}
}
Write-Host("INFO: There are " + $mdfiles.Count + " unreferenced *.md files in " + $tocDir)
$logfile = @"
There are no missing or unreferenced files.
===========================================
"@
if ( ( $missingFiles.Count -gt 0 ) -or ( $mdfiles.Count -gt 0 )) {
$logfile = ""
if ( $missingFiles.Count -gt 0 ) {
$logfile = @"
The following files are referenced in $tocFile, but do not exist in $tocDir.
==================================================================================================
"@
$logfile += $missingFiles -join "`r`n"
$logfile += "`r`n`r`n"
}
if ( $mdfiles.Count -gt 0 ) {
$logfile += @"
The following files are in $tocDir, but are not referenced in $tocFile.
==================================================================================================
"@
$logfile += $mdfiles -join "`r`n"
$logfile += "`r`n`r`n"
}
}
Write-Host "INFO: log written to log.txt"
$logfile | Out-File 'log.txt'