chocolatey / Chocolatey.Cake.Recipe

A set of convention based Cake scripts for building Chocolatey projects
Apache License 2.0
5 stars 9 forks source link

(#32) Add PSScriptAnalyzer #73

Closed TheCakeIsNaOH closed 6 months ago

TheCakeIsNaOH commented 1 year ago

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

Related Issue

Fixes #32

Change Checklist

gep13 commented 10 months ago

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 commented 10 months ago

@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.

gep13 commented 6 months ago

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