1+ # This Powershell script checks Powershell compatibility of any custom scripts
2+ # in the repository. It assumes configuration for this is present in the
3+ # README.md.
4+ #
5+ # The configuration should look like this:
6+ #
7+ # ### PSScriptAnalyzer Configuration
8+ # ``` yaml
9+ # targetVersions:
10+ # - "5.1"
11+ # - "7.0"
12+ # targetProfiles:
13+ # - "win-8_x64_10.0.17763.0_5.1.17763.316_x64_4.0.30319.42000_framework"
14+ # - "win-8_x64_10.0.14393.0_7.0.0_x64_3.1.2_core"
15+ # ```
16+
17+ function Read-ComplianceConfiguration {
18+ $path = " README.md"
19+ $configMarker = " ### PSScriptAnalyzer Configuration"
20+ $backTicksYaml = ' ``` yaml'
21+ $backTicks = ' ```'
22+
23+ $readme = Get-Content $path
24+
25+ # Extract out the YAML which sits between backticks markers after the
26+ # PSScriptAnalyzer configuration marker.
27+ $yaml = " "
28+ $inConfig = $false
29+ for ($i = 0 ; $i -lt $readme.Length ; $i ++ ) {
30+ if ($readme [$i ] -eq $configMarker ) {
31+ $inConfig = $true
32+ }
33+ if ($inConfig -and ($readme [$i ] -eq $backTicksYaml )) {
34+ $i ++
35+ while ($i -lt $readme.Length -and $readme [$i ] -ne $backticks ) {
36+ $yaml += " `n " + $readme [$i ]
37+ $i ++
38+ }
39+ break
40+ }
41+ }
42+
43+ if (" " -eq $yaml ) {
44+ Write-Error " No PSScriptAnalyzer Configuration found in README.md"
45+ exit 2
46+ }
47+
48+ # Parse the YAML.
49+ $yamlObj = ConvertFrom-YAML $yaml
50+
51+ # Confirm that there is at least one TargetVersion and that there are the
52+ # same number of TargetVersions as TargetProfiles.
53+ if ($yamlObj.targetVersions.Count -eq 0 ) {
54+ Write-Error " No TargetVersions found in PSScriptAnalyzer Configuration."
55+ exit 4
56+ }
57+ if ($yamlObj.targetVersions.Count -ne $yamlObj.targetProfiles.Count ) {
58+ Write-Error " Number of TargetVersions ($ ( $yamlObj.targetVersions.Count ) ) and TargetProfiles ($ ( $yamlObj.targetprofiles.Count ) ) do not match."
59+ exit 6
60+ }
61+
62+ return $yamlObj
63+ }
64+
65+ function Confirm-Compliance {
66+ param (
67+ [string []]$TargetVersions ,
68+ [string []]$TargetProfiles
69+ )
70+ Write-Output - ForegroundColor Green ' Linting and checking Powershell back-compatibility...'
71+ Install-Module PSScriptAnalyzer - Scope CurrentUser - Force
72+ $settings = @ {
73+ # Ref: https://devblogs.microsoft.com/powershell/using-psscriptanalyzer-to-check-powershell-version-compatibility/
74+ Rules = @ {
75+ PSUseCompatibleSyntax = @ {
76+ # This turns the rule on (setting it to false will turn it off)
77+ Enable = $true
78+
79+ # List the targeted versions of PowerShell here
80+ TargetVersions = $TargetVersions
81+ }
82+ PSUseCompatibleCommands = @ {
83+ # Turns the rule on
84+ Enable = $true
85+
86+ # Lists the PowerShell platforms we want to check compatibility with
87+ # Ref: https://learn.microsoft.com/en-gb/powershell/utility-modules/psscriptanalyzer/rules/usecompatiblecommands?view=ps-modules
88+ TargetProfiles = $TargetProfiles
89+ }
90+ }
91+ }
92+
93+ # Recursively find all *.ps1 files and run Invoke-ScriptAnalyzer against them.
94+ Get-ChildItem - Path . - Recurse - Include ' *.ps1' | Invoke-ScriptAnalyzer - Settings $settings
95+ if ($LastExitCode -ne 0 ) {
96+ Write-Error ' ScriptAnalyzer found (possibly back-compatibility) issues.'
97+ exit 5
98+ }
99+ }
100+
101+ function Confirm-CustomScript {
102+ Write-Debug " Confirm custom scripts are present."
103+
104+ # Get all *.ps1 files in the current directory
105+ $scripts = Get-ChildItem - Path ./ custom - Recurse - Include ' *.ps1'
106+ if ($scripts.Length -eq 0 ) {
107+ Write-Error " No custom scripts found in the repository."
108+ exit 3
109+ }
110+ }
111+
112+ function Confirm-InAutorestEnv {
113+ Write-Debug " Confirm in an autorest directory."
114+
115+ # Get current working directory then confirm it ends in ".autorest"
116+ $cwd = Get-Location
117+ if ($cwd -notlike " *.autorest" ) {
118+ Write-Error " This script should be run from an autorest directory."
119+ exit 1
120+ }
121+
122+ }
123+
124+ # Confirm that the required modules are installed.
125+ if (-not (Get-InstalledModule - Name " powershell-yaml" )) {
126+ Write-Error " powershell-yaml module is required. Please install it."
127+ exit 7
128+ }
129+ if (-not (Get-InstalledModule - Name PSScriptAnalyzer)) {
130+ Write-Error " PSScriptAnalyzer module is required. Please install it."
131+ exit 7
132+ }
133+
134+ # Run the script.
135+ Confirm-InAutorestEnv
136+ Confirm-CustomScript
137+ $configuration = Read-ComplianceConfiguration
138+ Confirm-Compliance - TargetVersions $configuration.targetVersions - TargetProfiles $configuration.targetProfiles
0 commit comments