From b53a48f6eddcdde777b9ce365c0ebf8dac24b1ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borja=20Dom=C3=ADnguez?= Date: Fri, 23 Dec 2022 01:17:16 +0100 Subject: [PATCH 1/5] Add PowerShell formatter linter --- .../powershell.megalinter-descriptor.yml | 28 +++++++++++++++++-- megalinter/linters/PowershellLinter.py | 15 ++++++++-- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/megalinter/descriptors/powershell.megalinter-descriptor.yml b/megalinter/descriptors/powershell.megalinter-descriptor.yml index 2a413139e4b..f429abf132b 100644 --- a/megalinter/descriptors/powershell.megalinter-descriptor.yml +++ b/megalinter/descriptors/powershell.megalinter-descriptor.yml @@ -10,7 +10,7 @@ file_extensions: - ".pssc" - ".psrc" - ".cdxml" -# Reference: https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux?view=powershell-7 +# Reference: https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-linux # Slightly modified to always retrieve latest stable Powershell version # If changing PWSH_VERSION='latest' to a specific version, use format PWSH_VERSION='tags/v7.0.2' install: @@ -36,8 +36,8 @@ linters: linter_url: https://github.com/PowerShell/PSScriptAnalyzer linter_repo: https://github.com/PowerShell/PSScriptAnalyzer linter_image_url: https://github.com/PowerShell/PSScriptAnalyzer/raw/master/logo.png - linter_rules_configuration_url: https://github.com/PowerShell/PSScriptAnalyzer#explicit - linter_rules_inline_disable_url: https://github.com/PowerShell/PSScriptAnalyzer#suppressing-rules + linter_rules_configuration_url: https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/using-scriptanalyzer?view=ps-modules#explicit + linter_rules_inline_disable_url: https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/using-scriptanalyzer?view=ps-modules#suppressing-rules config_file_name: .powershell-psscriptanalyzer.psd1 version_extract_regex: "(\\d+) *(\\d+) *(\\d+)" examples: @@ -52,3 +52,25 @@ linters: vscode: - name: VSCode PowerShell extension url: https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell + # Internal powershell linter + - class: PowershellLinter + linter_name: powershell_formatter + linter_url: https://github.com/PowerShell/PSScriptAnalyzer + linter_repo: https://github.com/PowerShell/PSScriptAnalyzer + linter_image_url: https://github.com/PowerShell/PSScriptAnalyzer/raw/master/logo.png + linter_rules_configuration_url: https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/using-scriptanalyzer?view=ps-modules#explicit + linter_rules_inline_disable_url: https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/using-scriptanalyzer?view=ps-modules#suppressing-rules + config_file_name: .powershell-psscriptanalyzer.psd1 + version_extract_regex: "(\\d+) *(\\d+) *(\\d+)" + examples: + - 'pwsh -NoProfile -NoLogo -Command "Invoke-Formatter -ScriptDefinition write-host"' + - 'pwsh -NoProfile -NoLogo -Command "Invoke-Formatter -Settings .powershell-psscriptanalyzer.psd1 -ScriptDefinition write-host"' + # If changing PWSH_VERSION='latest' to a specific version, use format PWSH_VERSION='tags/v7.0.2' + install: + dockerfile: + - ARG PSSA_VERSION='latest' + - RUN pwsh -c 'Install-Module -Name PSScriptAnalyzer -RequiredVersion ${PSSA_VERSION} -Scope AllUsers -Force' + ide: + vscode: + - name: VSCode PowerShell extension + url: https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell diff --git a/megalinter/linters/PowershellLinter.py b/megalinter/linters/PowershellLinter.py index 55f609d8f5d..4d916e9747c 100644 --- a/megalinter/linters/PowershellLinter.py +++ b/megalinter/linters/PowershellLinter.py @@ -17,10 +17,21 @@ def __init__(self, params=None, linter_config=None): # Build the CLI command to call to lint a file with a powershell script def build_lint_command(self, file=None): - pwsh_script = ["Invoke-ScriptAnalyzer -EnableExit"] + if self.linter_name == "powershell": + pwsh_script = ["Invoke-ScriptAnalyzer -EnableExit"] + elif self.linter_name == "powershell_formatter": + pwsh_script = ["Invoke-Formatter"] + if self.config_file is not None: pwsh_script[0] += " -Settings " + self.config_file - pwsh_script[0] += f" -Path '{file}'" + + if self.linter_name == "powershell": + pwsh_script[0] += f" -Path '{file}'" + elif self.linter_name == "powershell_formatter": + pwsh_script[0] += f" -ScriptDefinition (Get-Content '{file}') > '{file}'" + + if self.linter_name == "powershell" and self.apply_fixes is True and self.cli_lint_fix_arg_name is not None: + pwsh_script[0] += f" {self.cli_lint_fix_arg_name}" cmd = [ self.cli_executable, "-NoProfile", From 3d415f698e7f86be959eac49e25ecf09c0b9adde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borja=20Dom=C3=ADnguez?= Date: Fri, 23 Dec 2022 08:33:24 +0100 Subject: [PATCH 2/5] Use -Raw parameter --- megalinter/linters/PowershellLinter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/megalinter/linters/PowershellLinter.py b/megalinter/linters/PowershellLinter.py index 4d916e9747c..290281e20d6 100644 --- a/megalinter/linters/PowershellLinter.py +++ b/megalinter/linters/PowershellLinter.py @@ -28,7 +28,7 @@ def build_lint_command(self, file=None): if self.linter_name == "powershell": pwsh_script[0] += f" -Path '{file}'" elif self.linter_name == "powershell_formatter": - pwsh_script[0] += f" -ScriptDefinition (Get-Content '{file}') > '{file}'" + pwsh_script[0] += f" -ScriptDefinition (Get-Content -Path '{file}' -Raw) > '{file}'" if self.linter_name == "powershell" and self.apply_fixes is True and self.cli_lint_fix_arg_name is not None: pwsh_script[0] += f" {self.cli_lint_fix_arg_name}" From bf1514d0f5c2db766fede47abe9ba674fd27791c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Borja=20Dom=C3=ADnguez?= Date: Sun, 25 Dec 2022 23:45:09 +0100 Subject: [PATCH 3/5] Run build.sh --- .automation/generated/linter-helps.json | 117 ++++++++++++++++++ .../generated/linter-links-previews.json | 5 + .automation/generated/linter-versions.json | 1 + .../powershell_formatter/powershell_bad_1.ps1 | 14 +++ .../powershell_bad_1.psd1 | 3 + .../powershell_bad_1.psm1 | 1 + .../powershell_good_1.ps1 | 1 + .../powershell_good_1.psd1 | 3 + .../powershell_good_1.psm1 | 1 + Dockerfile | 4 + flavors/dotnet/Dockerfile | 4 + flavors/dotnet/flavor.json | 1 + megalinter/descriptors/all_flavors.json | 1 + .../powershell.megalinter-descriptor.yml | 4 +- .../megalinter-configuration.jsonschema.json | 1 + .../powershell_powershell_formatter_test.py | 14 +++ 16 files changed, 173 insertions(+), 2 deletions(-) create mode 100644 .automation/test/powershell_formatter/powershell_bad_1.ps1 create mode 100644 .automation/test/powershell_formatter/powershell_bad_1.psd1 create mode 100644 .automation/test/powershell_formatter/powershell_bad_1.psm1 create mode 100644 .automation/test/powershell_formatter/powershell_good_1.ps1 create mode 100644 .automation/test/powershell_formatter/powershell_good_1.psd1 create mode 100644 .automation/test/powershell_formatter/powershell_good_1.psm1 create mode 100644 megalinter/tests/test_megalinter/linters/powershell_powershell_formatter_test.py diff --git a/.automation/generated/linter-helps.json b/.automation/generated/linter-helps.json index 640a20b2cca..2d63f3b8ad9 100644 --- a/.automation/generated/linter-helps.json +++ b/.automation/generated/linter-helps.json @@ -4084,6 +4084,123 @@ " prepend the command parameters with a hyphen (-), not a forward slash (/).", "" ], + "powershell_formatter": [ + "", + "PowerShell[.exe] [-PSConsoleFile | -Version ]", + " [-NoLogo] [-NoExit] [-Sta] [-Mta] [-NoProfile] [-NonInteractive]", + " [-InputFormat {Text | XML}] [-OutputFormat {Text | XML}]", + " [-WindowStyle