-
Notifications
You must be signed in to change notification settings - Fork 16
/
validate.ps1
55 lines (48 loc) · 1.49 KB
/
validate.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
$postsFolder = Join-Path -Path $PSScriptRoot -ChildPath '_posts'
$posts = Get-ChildItem -Path $postsFolder -Filter '*.md'
$validationIssues = $false
function Get-FileEncoding {
param (
[Parameter(Mandatory)]
[string] $File
)
$streamReader = [System.IO.StreamReader]::new($File)
[void] $streamReader.Peek()
$streamReader.CurrentEncoding.WebName
$streamReader.Close()
$streamReader.Dispose()
}
function Test-Bom {
param (
[Parameter(Mandatory)]
[string] $File
)
$bom = [System.Byte[]]::CreateInstance([System.Byte], 3)
$reader = [System.IO.File]::OpenRead($File)
[void] $reader.Read($bom, 0, 3)
$reader.Close()
$bom[0] -eq 0xEF -and $bom[1] -eq 0xBB -and $bom[2] -eq 0xBF
}
foreach ($p in $posts) {
$postContent = Get-Content -Path $p.FullName -Raw
if ($postContent -match '\t') {
Write-Warning -Message "$($p.Name) contains tabs. Use spaces instead"
$validationIssues = $true
}
if ($postContent -match '\r\n') {
Write-Warning -Message "$($p.Name) uses crlf for end of line. Use lf instead"
$validationIssues = $true
}
$postEncoding = Get-FileEncoding -File $p.FullName
if ($postEncoding -ne 'utf-8') {
Write-Warning -Message "$($p.Name) uses $postEncoding encoding. Use utf8 instead"
$validationIssues = $true
}
if (Test-Bom -File $p.FullName) {
Write-Warning -Message "$($p.Name) uses $postEncoding with a BOM. Use no BOM instead"
$validationIssues = $true
}
}
if ($validationIssues) {
throw 'Validation issues where found'
}