forked from nightroman/PowerShellTraps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.build.ps1
147 lines (123 loc) · 3.74 KB
/
1.build.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
<#
.Synopsis
Build script, https://github.com/nightroman/Invoke-Build
#>
param(
[string[]]$Tasks
)
Set-StrictMode -Version Latest
$Major = $PSVersionTable.PSVersion.Major
# bootstrap
if ($MyInvocation.ScriptName -notlike '*Invoke-Build.ps1') {
$InvokeBuildVersion = '5.9.6'
$ErrorActionPreference = 'Stop'
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Definition
$ib = "$PSScriptRoot/packages/InvokeBuild/$InvokeBuildVersion/Invoke-Build.ps1"
if (!(Test-Path -LiteralPath $ib)) {
Write-Host 'Save-Module InvokeBuild'
Save-Module InvokeBuild -Path "$PSScriptRoot/packages" -RequiredVersion $InvokeBuildVersion -Force
}
if (!(Test-Path -LiteralPath "$PSScriptRoot/packages/Invoke-PowerShell.ps1")) {
Write-Host 'Save-Script Invoke-PowerShell'
Save-Script Invoke-PowerShell -Path "$PSScriptRoot/packages" -Force
}
& $ib -Task $Tasks -File $MyInvocation.MyCommand.Path @PSBoundParameters
return
}
# saved script alias
if (Test-Path "$BuildRoot/packages/Invoke-PowerShell.ps1") {
Set-Alias Invoke-PowerShell "$BuildRoot/packages/Invoke-PowerShell.ps1"
}
# custom task header
Set-BuildHeader {
Write-Build 11 "Task $($args[0]) *".PadRight(79, '*')
}
# Synopsis: Invoke tests.
task test {
# show PowerShell version
"PowerShell version: $($PSVersionTable.PSVersion)"
# undo pwsh ConciseView
$global:ErrorView = 'NormalView'
# do tests
Invoke-Build **
}
# Synopsis: Open a random folder in Visual Studio Code
task peek {
code (Get-ChildItem . -Recurse -Directory | Get-Random).FullName
}
# Synopsis: Generate the index in README.md
task index {
function Get-List($Path, $Indent) {
$tab = ' ' * $Indent
foreach($_ in Get-ChildItem -Path $Path -Name -Directory) {
if ($_ -in @('.github', 'Workaround')) {
continue
}
'{0}- [{1}]({2})' -f $tab, $_, "$Path/$_"
Get-List "$Path/$_" ($Indent + 1)
}
}
$(
foreach($_ in Get-Content README.md) {
$_
if ($_ -eq '<!--Generated-->') {break}
}
Get-List . 0
''
'---'
) | Set-Content README.md
}
# Synopsis: Tests markdown links
# Uses pandoc for markdown.
task link {
# forbidden absolute links
$ForbiddenLink = 'https://github.com/nightroman/PowerShellTraps/(tree|blob)/'
# all relative markdown paths
$files = Get-ChildItem . -Filter *.md -Recurse -Name
"$($files.Count) markdown files"
$ns = @{x = 'http://www.w3.org/1999/xhtml'}
foreach($file in $files) {
# convert to XML
$xml = [xml](pandoc.exe --standalone --quiet --no-highlight --from=gfm --to=html $file)
if ($global:LASTEXITCODE) {throw "Pandoc failed : $file"}
# markdown folder full path
$folder = Join-Path $BuildRoot (Split-Path $file)
# test links
foreach($node in Select-Xml //x:a $xml -Namespace $ns | Select-Object -ExpandProperty Node) {
$link = $node.href
# complex link
if ($node.ChildNodes.Count -ne 1 -or $node.FirstChild.NodeType -ne 'Text') {
Write-Warning "Complex link : $file : $($node.OuterXml)"
continue
}
# outer link
if ($link -cmatch '^http') {
if ($link -cmatch $ForbiddenLink) {
Write-Warning "Link : $file : $link"
}
continue
}
# link with "\"
if ($link.Contains('\')) {
Write-Warning "Link with '\': $file : $link"
continue
}
# missing link
$target = Join-Path $folder $link
if (!(Test-Path -LiteralPath $target)) {
Write-Warning "Missing link : $file : $link -> $target"
continue
}
# mismatch link
$ext = [System.IO.Path]::GetExtension($link)
if ($ext -and $ext -ne '.md') {
$text = $node.'#text'
if (!$link.EndsWith($text)) {
Write-Warning "Link and text mismatch: $file : $link ~ $text"
}
}
}
}
}
# Synopsis: Test.
task . test