Skip to content
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

(#32) Add PSScriptAnalyzer #73

Merged
merged 1 commit into from
Jan 4, 2024

Conversation

TheCakeIsNaOH
Copy link
Member

@TheCakeIsNaOH TheCakeIsNaOH commented Aug 16, 2022

Description Of Changes

This adds a task to run PSScriptAnalyzer. It utilizes the Cake.Powershell
module to run a script that will get ps1/psm1 files that are not excluded
and run them through the specified settings file.

Either a default formatting only run can be done, or custom run(s) can be
specified. A custom base analysis path, setting file and set of folder
exclusions can be specified for each run. For example, a custom run can
be created to check that the Chocolatey powershell helpers use only the
PSv2 cmdlets.

The default run is for formatting, it checks files against the build in
formatting related rules. This run can be expanded in the future, once
enough projects are up to a baseline of good scripting practices.

Motivation and Context

See #32

Testing

  1. Clone this repository locally and checkout this PR branch
  2. Copy the contents of the Chocolatey.Cake.Recipe/Content folder to the tools/Chocolatye.Cake.Recipe.0.24.0 folder in the chocolatey/choco repository
  3. At the command line for that repository, run the following build.bat. The build should complete successfully, and a new task Run-PSScriptAnalyzer should be completed, which adds two new files into the code_drop\TestResults\PSScriptAnalyzer folder
  4. At the command line for that repository, run the following build.bat --shouldRunAnalyze=false. The build should succeed, but all the analyze steps should be skipped, and no files added into the code_drop\TestResults\PSScriptAnalyzer folder
    1. At the command line for that repository, run the following build.bat --shouldRunPSScriptAnalyzer=false. The build should succeed, but all the analyze steps should be skipped, and no files added into the code_drop\TestResults\PSScriptAnalyzer folder, but other test results, for example, InspectCode, should be generated.

Change Types Made

  • Bug fix (non-breaking change)
  • Feature / Enhancement (non-breaking change)
  • Breaking change (fix or feature that could cause existing functionality to change)
  • PowerShell code changes.

Related Issue

Fixes #32

Change Checklist

  • Requires a change to the documentation
  • Documentation has been updated
  • Tests to cover my changes, have been added
  • All new and existing tests passed.
  • PowerShell v2 compatibility checked.

@TheCakeIsNaOH TheCakeIsNaOH force-pushed the add-psscriptanalyzer branch 3 times, most recently from fb2fa47 to ecb082d Compare August 16, 2022 19:46
@gep13 gep13 force-pushed the add-psscriptanalyzer branch 4 times, most recently from 6d5e01a to b07b890 Compare August 17, 2023 14:56
@gep13 gep13 requested a review from vexx32 August 17, 2023 14:57
@gep13 gep13 marked this pull request as ready for review August 17, 2023 14:57
@gep13 gep13 force-pushed the add-psscriptanalyzer branch from b07b890 to 7d499c2 Compare August 17, 2023 14:59
@gep13
Copy link
Member

gep13 commented Aug 17, 2023

NOTE: As a result of running the build using the changes in this PR, two new PowerShell Modules will be installed, PSScriptAnalyzer, and ConvertToSARIF.

@gep13
Copy link
Member

gep13 commented Aug 17, 2023

@TheCakeIsNaOH would be great to get your thoughts on this PR as well. I have made some changes on top of the work that you originally did here.

Mainly around the installation of the PS Modules, and also switching to use Start-Job as I found that any call to Invoke-ScriptAnalyzer caused the terminal to hang when invoked through Cake, and you had to CTRL+C out of it.

Copy link
Member Author

@TheCakeIsNaOH TheCakeIsNaOH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall the concept looks good, a couple of comments/questions.

Chocolatey.Cake.Recipe/Content/install-module.ps1 Outdated Show resolved Hide resolved
@gep13 gep13 force-pushed the add-psscriptanalyzer branch from 7d499c2 to 4662ea8 Compare August 21, 2023 07:03
vexx32
vexx32 previously approved these changes Sep 27, 2023
Copy link
Member

@vexx32 vexx32 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing any additional concerns here beyond what @TheCakeIsNaOH has already raised tbh, this looks pretty good otherwise. :3

This adds a task to run PSScriptAnalyzer. It utilizes the Cake.Powershell
module to run a script that will get ps1/psm1 files that are not excluded
and run them through the specified settings file.

Either a default formatting only run can be done, or custom run(s) can be
specified. A custom base analysis path, setting file and set of folder
exclusions can be specified for each run. For example, a custom run can
be created to check that the Chocolatey powershell helpers use only the
PSv2 cmdlets.

The default run is for formatting, it checks files against the build in
formatting related rules. This run can be expanded in the future, once
enough projects are up to a baseline of good scripting practices.
Copy link
Member

@gep13 gep13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@gep13 gep13 merged commit da4c186 into chocolatey:develop Jan 4, 2024
@gep13
Copy link
Member

gep13 commented Jan 4, 2024

@TheCakeIsNaOH thanks again for the start on this work! Really appreciate it!

@TheCakeIsNaOH TheCakeIsNaOH deleted the add-psscriptanalyzer branch January 4, 2024 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for running PSScriptAnalyzer
3 participants