Skip to content

Commit 91ada06

Browse files
authored
ci(): Add linter check (#27)
Run the linter over the powershell code.
1 parent e3c9118 commit 91ada06

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

.drone.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ steps:
2121
status:
2222
- failure
2323

24+
- name: linter
25+
image: mcr.microsoft.com/powershell:alpine-3.14
26+
commands:
27+
- pwsh ./Invoke-Linter.ps1 -exitCodeOnError
28+
environment:
29+
# Disable debugging and profiling
30+
COMPlus_EnableDiagnostics: 0
31+
2432
trigger:
2533
ref:
2634
- refs/heads/master

Invoke-Linter.ps1

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
param(
2+
[switch]$exitCodeOnError = $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+
42+
# Use Invoke-Formatter on all code
43+
# Using -Exclude breaks the -Filter functionality so a regex using Where-Object is used instead
44+
$psScripts = Get-ChildItem -Path $PSScriptRoot -Exclude 'psmodules' | `
45+
Get-ChildItem -Recurse | `
46+
Where-Object { $_.FullName -match '.*\.ps1$' }
47+
$issues = 0;
48+
$files = 0;
49+
50+
foreach ($script in $psScripts) {
51+
$files++;
52+
$scriptPath = $script.FullName;
53+
54+
$records = Invoke-ScriptAnalyzer -Path $scriptPath `
55+
-Exclude PSUseSingularNouns,PSAvoidUsingInvokeExpression,PSUseShouldProcessForStateChangingFunctions `
56+
57+
Write-Information -MessageData ('Analyzing {0}' -f $scriptPath) -InformationAction Continue;
58+
59+
if ($records) {
60+
foreach ($record in $records) {
61+
$issues++;
62+
Write-Information -MessageData ('{0}:{1}: {2} {3}' -f $record.ScriptName,$record.Line,$record.RuleName,$record.Message) -InformationAction Continue;
63+
}
64+
}
65+
}
66+
67+
Write-Information -MessageData ('Found {0} issues across {1} files' -f $issues,$files) -InformationAction Continue;
68+
69+
if ($issues -gt 0 -and $exitCodeOnError) {
70+
exit -1;
71+
}

0 commit comments

Comments
 (0)