Skip to content

Commit df141a1

Browse files
authored
ci(): Add code formatter (#24)
Add a script to format the scripts using Invoke-Formatter and Edit-DTWBeautifyScript.
1 parent 4cedc09 commit df141a1

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Downloaded PS Modules
2+
/psmodules/

Invoke-Formatter.ps1

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
param(
2+
[switch]$exitCodeOnFormat = $false,
3+
[string]$moduleRoot
4+
)
5+
6+
$ErrorActionPreference = 'Stop';
7+
8+
# Import the PowerShell modules locally
9+
if (-not ($moduleRoot)) {
10+
$moduleRoot = Join-Path $PSScriptRoot -ChildPath 'psmodules';
11+
}
12+
13+
if (-not (Test-Path -Path $moduleRoot)) {
14+
New-Item -Path $moduleRoot -ItemType 'directory' | Out-Null;
15+
}
16+
17+
function Import-ModuleLocally {
18+
param(
19+
[string]$name,
20+
[string]$version
21+
)
22+
23+
Write-Information -MessageData ('Importing {0} v{1}' -f $name,$version) -InformationAction Continue;
24+
if (Get-Module -Name $name) {
25+
Remove-Module -Name $name;
26+
}
27+
28+
$importModule = Join-Path $moduleRoot -ChildPath $name | `
29+
Join-Path -ChildPath $version | `
30+
Join-Path -ChildPath ($name + '.psd1');
31+
32+
if (-not (Test-Path -Path $importModule)) {
33+
Write-Information -MessageData ('Saving module {0}' -f $name) -InformationAction Continue;
34+
Save-Module $name -Path $moduleRoot -Repository PSGallery -RequiredVersion $version;
35+
}
36+
37+
Import-Module $importModule;
38+
}
39+
40+
Import-ModuleLocally -Name PSScriptAnalyzer -Version 1.20.0;
41+
Import-ModuleLocally -Name PowerShell-Beautifier -Version 1.2.5;
42+
43+
# Use Invoke-Formatter on all code
44+
# Using -Exclude breaks the -Filter functionality so a regex using Where-Object is used instead
45+
$psScripts = Get-ChildItem -Path $PSScriptRoot -Exclude 'psmodules' | `
46+
Get-ChildItem -Recurse | `
47+
Where-Object { $_.FullName -match '.*\.ps1$' }
48+
$formatted = $false;
49+
50+
foreach ($script in $psScripts) {
51+
$scriptPath = $script.FullName;
52+
$initialHash = (Get-FileHash -Path $scriptPath).Hash;
53+
$contents = Get-Content -Raw -Path $scriptPath;
54+
55+
# Use built-in PowerShell formatter
56+
$contents = Invoke-Formatter -ScriptDefinition $contents;
57+
Set-Content -Path $scriptPath -Value $contents.Trim();
58+
59+
# Use the Powershell beautifier
60+
# It can't take a string input so have to use the file path
61+
Edit-DTWBeautifyScript -SourcePath $scriptPath -IndentType FourSpaces;
62+
$finalHash = (Get-FileHash -Path $scriptPath).Hash;
63+
64+
if ($initialHash -ne $finalHash) {
65+
Write-Information -MessageData ('Formatted {0}' -f $scriptPath) -InformationAction Continue;
66+
$formatted = $true;
67+
}
68+
else {
69+
Write-Information -MessageData ('No changes to {0}' -f $scriptPath) -InformationAction Continue;
70+
}
71+
}
72+
73+
if ($formatted -and $exitCodeOnFormat) {
74+
exit -1;
75+
}

0 commit comments

Comments
 (0)