-
-
Notifications
You must be signed in to change notification settings - Fork 767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding powershell linter hook - best practice #2645
Comments
you shouldn't need a full path to
I think what you actually want is to use |
If I don't pass the full path for pwsh, I get
ok for pass_filenames true but how?
on dotnet, not sure how, https://github.com/PowerShell/PSScriptAnalyzer is not a dotnet app. |
that's because you've incorrectly set |
I tested locally with following pre-commit-config
which should be valid unless language can't be overriden but behavior below shown it applies full output
|
yeah that output would say it's not a |
yeah I think the way to make this work nicely with pre-commit would be to build into a path and use |
Sadly install part does not seem to work directly from my testing
also same reported here And match the absence of a prefix option for Install-Module Seems the only option would be download and copy files manually # https://www.powershellgallery.com/packages/PSScriptAnalyzer/1.21.0
# https://www.comparitech.com/net-admin/install-powershell-modules/
Invoke-WebRequest -Uri https://psg-prod-eastus.azureedge.net/packages/psscriptanalyzer.1.21.0.nupkg -Outfile /tmp/psscriptanalyzer.zip
New-Item -Path $home/.cache/pre-commit/psmodules/PSScriptAnalyzer/psscriptanalyzer.1.21.0 -ItemType Directory
Expand-Archive -Path /tmp/psscriptanalyzer.zip -DestinationPath $home/.cache/pre-commit/psmodules/PSScriptAnalyzer/1.21.0
# usage
$p = [Environment]::GetEnvironmentVariable("PSModulePath")
$p2 = "$home/.cache/pre-commit/psmodules:" + $p
[Environment]::SetEnvironmentVariable("PSModulePath", $p2)
# confirm module present
Get-Module -ListAvailable
# use it
Invoke-ScriptAnalyzer -Settings PSGallery -Recurse -ReportSummary -EnableExit -Severity Warning -Path . -ExcludeRule PSAvoidGlobalVars, PSAvoidUsingPlainTextForPassword, PSUseShouldProcessForStateChangingFunctions Where should this configuration go? Also the current need for a full pwsh path is annoying, especially if running pre-commit both client and server side where path may be different. |
yeah the default |
I have been trying to accomplish something similar. Took some lessons from this thread. I have an adhoc implementation in my dotfiles repo: Basically, the wrapper/hook does the following:
This seems reasonable to expect of a linter. I understand that this may be out of scope for a generalized tool as the analyzer seems to be, but does everyone really hand-roll this much in PS when they try to use this in real life? Anyway, I realize that what I should actually be doing is taking a list of files provided by pre-commit, but I am pretty new to PowerShell, so I focused on getting the proof of concept part of making it useful. Seems the PowerShell people have had a ticket open regarding more control over what files it includes/excludes for a while now? Maybe I'm not getting something and this is much simpler to accomplish? I think it should be possible to do this the "correct" way, but I have a few concerns:
I'm new to this. I might still tough up and try to make this a generalized hook in a new repo. Anyone inspired to collaborate? |
search you tried in the issue tracker
powershell, return code, exit code
describe your issue
I'm working on a repository containing powershell scripts.
I have a pre-commit already enabled for other checks and I want to add powershell linting with https://github.com/PowerShell/PSScriptAnalyzer.
I added a hook config as
PowerShell/PSScriptAnalyzer@master...juju4:PSScriptAnalyzer:devel-precommit
In the target repo, I added following config
3 minor issues as using entry to deal with them for now.
/usr/local/bin/pwsh -Command "Invoke-ScriptAnalyzer -Settings PSGallery -Recurse -ReportSummary -EnableExit -Path $@"
did not work in my tests.Else happy to take more comments
Thanks a lot for your work!
pre-commit --version
2.20.0
.pre-commit-config.yaml
~/.cache/pre-commit/pre-commit.log (if present)
No response
The text was updated successfully, but these errors were encountered: