-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Initial Powershell release compliance tool. #26540
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
Open
papadeltasierra
wants to merge
13
commits into
Azure:main
Choose a base branch
from
xw-zhang24:pauldsmith/psscriptanalyzer-main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
aa5bd93
Initial Powershell release compliance tool.
3acf909
LASTERROREXIT does not catch Invoke-Analyzer errors.
d721b42
Updated Powershell definitions to latest versions.
021daf4
Correct backticks ref
b39f4bc
Correct source to show real powershell versions.
e56ec8e
Correct backticks.
9a28413
Initial Powershell release compliance tool.
c2d8ba2
LASTERROREXIT does not catch Invoke-Analyzer errors.
1af7f82
Updated Powershell definitions to latest versions.
fcdf90f
Correct backticks ref
97e644d
Correct source to show real powershell versions.
e0ace04
Correct backticks.
b8731e6
Merge branch 'pauldsmith/psscriptanalyzer-main' of https://github.com…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| # This Powershell script checks Powershell compatibility of any custom scripts | ||
| # in the repository. It assumes configuration for this is present in the | ||
| # README.md. | ||
| # | ||
| # The configuration should look like this: | ||
| # | ||
| # ### PSScriptAnalyzer Configuration | ||
| # ``` yaml | ||
| # targetVersions: | ||
| # - "5.1" | ||
| # - "7.0" | ||
| # targetProfiles: | ||
| # - "win-8_x64_10.0.17763.0_5.1.17763.316_x64_4.0.30319.42000_framework" | ||
| # - "win-8_x64_10.0.14393.0_7.0.0_x64_3.1.2_core" | ||
| # ``` | ||
|
|
||
| function Read-ComplianceConfiguration { | ||
| $path = "README.md" | ||
| $configMarker = "### PSScriptAnalyzer Configuration" | ||
| $backTicksYaml = '``` yaml' | ||
| $backTicks = '```' | ||
|
|
||
| $readme = Get-Content $path | ||
|
|
||
| # Extract out the YAML which sits between backticks markers after the | ||
| # PSScriptAnalyzer configuration marker. | ||
| $yaml = "" | ||
| $inConfig = $false | ||
| for ($i = 0; $i -lt $readme.Length; $i++) { | ||
| if ($readme[$i] -eq $configMarker) { | ||
| $inConfig = $true | ||
| } | ||
| if ($inConfig -and ($readme[$i] -eq $backTicksYaml)) { | ||
| $i++ | ||
| while ($i -lt $readme.Length -and $readme[$i] -ne $backticks) { | ||
| $yaml += "`n" + $readme[$i] | ||
| $i++ | ||
| } | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if ("" -eq $yaml) { | ||
| Write-Error "No PSScriptAnalyzer Configuration found in README.md" | ||
papadeltasierra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| exit 2 | ||
| } | ||
|
|
||
| # Parse the YAML. | ||
| $yamlObj = ConvertFrom-YAML $yaml | ||
|
|
||
| # Confirm that there is at least one TargetVersion and that there are the | ||
| # same number of TargetVersions as TargetProfiles. | ||
| if ($yamlObj.targetVersions.Count -eq 0) { | ||
| Write-Error "No TargetVersions found in PSScriptAnalyzer Configuration." | ||
| exit 4 | ||
| } | ||
| if ($yamlObj.targetVersions.Count -ne $yamlObj.targetProfiles.Count) { | ||
| Write-Error "Number of TargetVersions ($($yamlObj.targetVersions.Count)) and TargetProfiles ($($yamlObj.targetprofiles.Count)) do not match." | ||
| exit 6 | ||
| } | ||
|
|
||
| return $yamlObj | ||
| } | ||
|
|
||
| function Confirm-Compliance { | ||
| param( | ||
| [string[]]$TargetVersions, | ||
| [string[]]$TargetProfiles | ||
| ) | ||
| Write-Output -ForegroundColor Green 'Linting and checking Powershell back-compatibility...' | ||
| Install-Module PSScriptAnalyzer -Scope CurrentUser -Force | ||
| $settings = @{ | ||
| # Ref: https://devblogs.microsoft.com/powershell/using-psscriptanalyzer-to-check-powershell-version-compatibility/ | ||
| Rules = @{ | ||
| PSUseCompatibleSyntax = @{ | ||
| # This turns the rule on (setting it to false will turn it off) | ||
| Enable = $true | ||
|
|
||
| # List the targeted versions of PowerShell here | ||
| TargetVersions = $TargetVersions | ||
| } | ||
| PSUseCompatibleCommands = @{ | ||
| # Turns the rule on | ||
| Enable = $true | ||
|
|
||
| # Lists the PowerShell platforms we want to check compatibility with | ||
| # Ref: https://learn.microsoft.com/en-gb/powershell/utility-modules/psscriptanalyzer/rules/usecompatiblecommands?view=ps-modules | ||
| TargetProfiles = $TargetProfiles | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # Recursively find all *.ps1 files and run Invoke-ScriptAnalyzer against them. | ||
| $problems = $(Get-ChildItem -Path ./custom -Recurse -Include '*.ps1' | Invoke-ScriptAnalyzer -Settings $settings) | ||
| if ("" -ne $problems) { | ||
| Write-Output $problems | ||
| Write-Error 'ScriptAnalyzer found (possibly back-compatibility) issues.' | ||
| exit 5 | ||
| } | ||
| } | ||
|
|
||
| function Confirm-CustomScript { | ||
| Write-Debug "Confirm custom scripts are present." | ||
|
|
||
| # Get all *.ps1 files in the current directory | ||
| $scripts = Get-ChildItem -Path ./custom -Recurse -Include '*.ps1' | ||
| if ($scripts.Length -eq 0) { | ||
| Write-Error "No custom scripts found in the repository." | ||
| exit 3 | ||
| } | ||
| } | ||
|
|
||
| function Confirm-InAutorestEnv { | ||
| Write-Debug "Confirm in an autorest directory." | ||
|
|
||
| # Get current working directory then confirm it ends in ".autorest" | ||
| $cwd = Get-Location | ||
| if ($cwd -notlike "*.autorest") { | ||
| Write-Error "This script should be run from an autorest directory." | ||
| exit 1 | ||
| } | ||
|
|
||
| } | ||
|
|
||
| # Confirm that the required modules are installed. | ||
| if (-not(Get-InstalledModule -Name "powershell-yaml")) { | ||
| Write-Error "powershell-yaml module is required. Please install it." | ||
| exit 7 | ||
| } | ||
| if (-not(Get-InstalledModule -Name PSScriptAnalyzer)) { | ||
| Write-Error "PSScriptAnalyzer module is required. Please install it." | ||
| exit 7 | ||
| } | ||
|
|
||
| # Run the script. | ||
| Confirm-InAutorestEnv | ||
| Confirm-CustomScript | ||
| $configuration = Read-ComplianceConfiguration | ||
| Confirm-Compliance -TargetVersions $configuration.targetVersions -TargetProfiles $configuration.targetProfiles | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.