-
Notifications
You must be signed in to change notification settings - Fork 0
/
Invoke-DpaFormatter.ps1
88 lines (78 loc) · 3.32 KB
/
Invoke-DpaFormatter.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
function Invoke-DpaFormatter {
<#
.SYNOPSIS
Helps formatting function files to dbatools' standards
.DESCRIPTION
Uses PSSA's Invoke-Formatter to format the target files and saves it without the BOM.
.PARAMETER Path
The path to the ps1 file that needs to be formatted
.PARAMETER EnableException
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
.NOTES
Tags: Formatting
Author: Simone Bizzotto
Website: https://dbatools.io
Copyright: (c) 2018 by dbatools, licensed under MIT
License: MIT https://opensource.org/licenses/MIT
.LINK
https://dbatools.io/Invoke-DbatoolsFormatter
.EXAMPLE
PS C:\> Invoke-DbatoolsFormatter -Path C:\dbatools\functions\Get-DbaDatabase.ps1
Reformats C:\dbatools\functions\Get-DbaDatabase.ps1 to dbatools' standards
#>
[CmdletBinding()]
param (
[parameter(Mandatory, ValueFromPipeline)]
[object[]]$Path,
[switch]$EnableException
)
begin {
$HasInvokeFormatter = $null -ne (Get-Command Invoke-Formatter -ErrorAction SilentlyContinue).Version
if (!($HasInvokeFormatter)) {
Stop-Function -Message "You need a recent version of PSScriptAnalyzer installed"
}
$CBHRex = [regex]'(?smi)\s+<#[^#]*#>'
$CBHStartRex = [regex]'(?<spaces>[ ]+)<#'
$CBHEndRex = [regex]'(?<spaces>[ ]*)#>'
}
process {
foreach ($p in $Path) {
try {
$realPath = (Resolve-Path -Path $p -ErrorAction Stop).Path
} catch {
Stop-Function -Message "Cannot find or resolve $p" -Continue
}
$content = Get-Content -Path $realPath -Raw -Encoding UTF8
#strip ending empty lines
$content = $content -replace "(?s)`r`n\s*$"
try {
$content = Invoke-Formatter -ScriptDefinition $content -Settings CodeFormattingOTBS -ErrorAction Stop
} catch {
Write-Message -Level Warning "Unable to format $p"
}
#match the ending indentation of CBH with the starting one, see #4373
$CBH = $CBHRex.Match($content).Value
if ($CBH) {
#get starting spaces
$startSpaces = $CBHStartRex.Match($CBH).Groups['spaces']
if ($startSpaces) {
#get end
$newCBH = $CBHEndRex.Replace($CBH, "$startSpaces#>")
if ($newCBH) {
#replace the CBH
$content = $content.Replace($CBH, $newCBH)
}
}
}
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
$realContent = @()
#trim whitespace lines
foreach ($line in $content.Split("`n")) {
$realContent += $line.TrimEnd()
}
[System.IO.File]::WriteAllText($realPath, ($realContent -Join "`r`n"), $Utf8NoBomEncoding)
}
}
}