-
Notifications
You must be signed in to change notification settings - Fork 24
feat: Alias and Breakingchange through PSA #100
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
Alex-Sol
wants to merge
33
commits into
Azure:main
Choose a base branch
from
Alex-Sol:lizeng/PSACustomRules
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 14 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
c6674ba
feat: Alias and Breakingchange
ff97be8
Merge branch 'main' into lizeng/PSACustomRules
isra-fel f43c3ec
feat: merge code of Alias and Breakingchange
6f26420
Merge branches 'lizeng/PSACustomRules' and 'lizeng/PSACustomRules' of…
924a7b0
style: delete annotations
64d1f9b
feat: get alias json file from modules
2473b42
style: delete annotation
5be1410
style: convert the path from absolute to relative
a1e17bc
feat: check for existence of PSA module
c699b37
feat: add change event
b5760ab
style: fix some annotations and log message
86786c0
feat:para alias of cmdlet
801a616
fix: change the path of tempfile when changing
d39d90b
fix: generation of alias cmdlets
2a22cc6
docs: how to generate alias spec and breaking change spec
5764847
fix: resolve the PR
a0e8207
feat: add debounce
72cff02
fix: copy custom rules to powershell exec path
515c5ed
fix: resolve review2
ad46e7d
fix: yml and lint
d8a1c14
fix: resolve review3
477c121
fix: remove the log of timer
f633147
feat: description to type of diagnostic
4b63e5c
prettier
isra-fel a59f57f
refactor debounce code
isra-fel c84dd8b
fix: copy-item
73d7870
fix: formatPlansToDiag
e4f0d73
fix: logic of breakingchange parameter implicit
69ccac8
docs: change version to 0.3.0
04c3425
feat: more detailed message about breaking change
cd89c0d
feat: filter the para belongs to powershell
da7e4b0
feat: get message from GetBreakingChangeTextFromAttribute method
600b47c
docs: add some notes for developers
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
68,398 changes: 68,398 additions & 0 deletions
68,398
vscode-extension/PSA_custom_Rules/Alias/AliasSpec.json
Large diffs are not rendered by default.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
vscode-extension/PSA_custom_Rules/Alias/Get-AliasSpec.psm1
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,16 @@ | ||
| #load the alias from the json file | ||
|
|
||
| function Get-AliasSpec { | ||
| param ( | ||
| [Parameter( | ||
| Mandatory = $true, | ||
| HelpMessage = "Specify the path to the file that contains alias mapping.")] | ||
| [System.String] | ||
| [ValidateNotNullOrEmpty()] | ||
| $AliasPath | ||
| ) | ||
|
|
||
| $aliasTocmdlets = Get-Content $AliasPath | ConvertFrom-Json | ||
|
|
||
| return $aliasTocmdlets | ||
| } | ||
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,92 @@ | ||
| #Requires -Version 3.0 | ||
|
|
||
| <# | ||
| .SYNOPSI | ||
| Avoid alias in powershell script. | ||
| .DESCRIPTION | ||
| Find all aliases that appear in the powershell script. And give the suggestion to change them to formal name. | ||
| .EXAMPLE | ||
| Measure-AvoidAlias -ScriptBlockAst $ScriptBlockAst | ||
| .INPUTS | ||
| [System.Management.Automation.Language.ScriptBlockAst] | ||
| .OUTPUTS | ||
| [OutputType([Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]])] | ||
| .NOTES | ||
| None | ||
| #> | ||
| function Measure-AvoidAlias { | ||
| [CmdletBinding()] | ||
| [OutputType([Microsoft.Windows.Powershell.ScriptAnalyzer.Generic.DiagnosticRecord[]])] | ||
| Param | ||
| ( | ||
| [Parameter(Mandatory = $true)] | ||
| [ValidateNotNullOrEmpty()] | ||
| [System.Management.Automation.Language.ScriptBlockAst] | ||
| $testAst | ||
|
Alex-Sol marked this conversation as resolved.
Outdated
|
||
| ) | ||
|
|
||
| Process { | ||
| $results = @() | ||
| # import functions | ||
| $findCmdFunctionFile = ".\PSA_custom_Rules\Find-CmdletsInFile.psm1" | ||
| Import-Module $findCmdFunctionFile | ||
| $getAliasSpecFunctionFile = ".\PSA_custom_Rules\Alias\Get-AliasSpec.psm1" | ||
| Import-Module $getAliasSpecFunctionFile | ||
|
|
||
| #get the alias mapping data | ||
| $aliasSpecFile = ".\PSA_custom_Rules\Alias\AliasSpec.json" | ||
| $AliasSpec = Get-AliasSpec -AliasPath $aliasSpecFile | ||
|
|
||
| # get the commandAst in the file | ||
| $foundCmdlets = Find-CmdletsInFile -rootAstNode $testAst | ||
|
Alex-Sol marked this conversation as resolved.
Outdated
|
||
|
|
||
| #list of CorrectionExtents | ||
| $l = (new-object System.Collections.ObjectModel.Collection["Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent"]) | ||
|
Alex-Sol marked this conversation as resolved.
Outdated
|
||
|
|
||
| foreach ($cmdletReference in $foundCmdlets) { | ||
| if ($AliasSpec.cmdlet.psobject.properties.match($cmdletReference.CommandName).Count) { | ||
|
Alex-Sol marked this conversation as resolved.
Outdated
|
||
| [int]$startLineNumber = $cmdletReference.StartLine | ||
| [int]$endLineNumber = $cmdletReference.EndLine | ||
| [int]$startColumnNumber = $cmdletReference.StartColumn | ||
| [int]$endColumnNumber = $cmdletReference.EndPosition | ||
| [string]$correction = $AliasSpec.cmdlet.($cmdletReference.CommandName) | ||
| [string]$filePath = $cmdletReference.FullPath | ||
| [string]$optionalDescription = 'The alias can be changed to be formal name.' | ||
|
Alex-Sol marked this conversation as resolved.
Outdated
|
||
|
|
||
| $c = (new-object Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent $startLineNumber, $endLineNumber, $startColumnNumber, $endColumnNumber, $correction, $filePath, $optionalDescription) | ||
| $l.Add($c) | ||
|
|
||
| } | ||
|
|
||
| if ($AliasSpec.para_cmdlet.psobject.properties.match($cmdletReference.CommandName).Count){ | ||
|
Alex-Sol marked this conversation as resolved.
Outdated
|
||
| foreach ($para in $cmdletReference.parameters){ | ||
| if ($AliasSpec.para_cmdlet.($cmdletReference.CommandName).psobject.properties.match($para.Name).Count){ | ||
| [int]$startLineNumber = $para.StartLine | ||
| [int]$endLineNumber = $para.EndLine | ||
| [int]$startColumnNumber = $para.StartColumn | ||
| [int]$endColumnNumber = $para.EndPosition | ||
| [string]$correction = $AliasSpec.para_cmdlet.($cmdletReference.CommandName).($para.Name) | ||
| [string]$filePath = $para.FullPath | ||
| [string]$optionalDescription = 'The alias can be changed to be formal name.' | ||
|
|
||
| $c = (new-object Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent $startLineNumber, $endLineNumber, $startColumnNumber, $endColumnNumber, $correction, $filePath, $optionalDescription) | ||
| $l.Add($c) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| $extent = $null | ||
|
|
||
| #returned anaylse results | ||
| $dr = New-Object ` | ||
|
Alex-Sol marked this conversation as resolved.
Outdated
|
||
| -Typename "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord" ` | ||
| -ArgumentList "This is help", $extent, $PSCmdlet.MyInvocation.InvocationName, Warning, "MyRuleSuppressionID", $l | ||
|
Alex-Sol marked this conversation as resolved.
Outdated
|
||
| $dr.SuggestedCorrections = $l | ||
| $results += $dr | ||
| return $results | ||
| } | ||
| } | ||
| Export-ModuleMember -Function Measure* | ||
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,80 @@ | ||
| #generate the json file includes alias information | ||
|
|
||
| $az_modules = gmo az.* -ListAvailable | Where-object {$_.Name -ne "Az.Tools.Migration"} | ||
|
Alex-Sol marked this conversation as resolved.
Outdated
|
||
| for ([int]$i = 0; $i -lt $az_modules.Count; $i++){ | ||
| import-module $az_modules[$i].name | ||
| } | ||
|
|
||
| $aliasResults = @{} | ||
|
|
||
| $matchPattern = "(\b[a-zA-z]+-?[a-zA-z]+\b)" | ||
| $matchPatternFormalName = "(\b[a-zA-z]+-?[Az][a-zA-z]+\b)" | ||
|
Alex-Sol marked this conversation as resolved.
Outdated
|
||
| $cmdletRegex = New-Object System.Text.RegularExpressions.Regex($matchPattern) | ||
| $cmdletRegexFormal = New-Object System.Text.RegularExpressions.Regex($matchPatternFormalName) | ||
| $aliasCmdlets = get-alias | where-object {$cmdletRegex.IsMatch($_.Name) -and $cmdletRegexFormal.IsMatch($_.ReferencedCommand.Name)} | ||
|
|
||
| for ([int]$i = 0; $i -lt $aliasCmdlets.Count; $i++){ | ||
| $aliasCmdlet = $aliasCmdlets[$i] | ||
| $aliasResults[$aliasCmdlet.Name] = $aliasCmdlet.ReferencedCommand.Name | ||
| } | ||
|
|
||
| # $json = $aliasResults | ConvertTo-Json | ||
|
Alex-Sol marked this conversation as resolved.
Outdated
|
||
| # $json > aliasTocmdlet.json | ||
|
|
||
|
|
||
|
|
||
| class getBreakingchangeResult_paraCmdlet{ | ||
|
Alex-Sol marked this conversation as resolved.
Outdated
|
||
| [System.String] $Name | ||
| [System.String] $TypeBreakingChange | ||
| } | ||
|
|
||
| $results = @{ | ||
| } | ||
|
|
||
| $results["updateTime"] = Get-Date | ||
| $results["cmdlet"] = $aliasResults | ||
| $results["para_cmdlet"] = @{} | ||
|
|
||
| $results["updateTime"] = $results["updateTime"].ToString() | ||
|
Alex-Sol marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
|
|
||
| for ([int]$i = 0; $i -lt $az_modules.Count; $i++){ | ||
|
|
||
| # import-module $az_modules[$i].name | ||
| $module = get-module $az_modules[$i].name | ||
|
|
||
| $exportedCmdlets = $module.ExportedCmdlets | ||
|
|
||
| foreach ($key in $exportedCmdlets.Keys){ | ||
| $Cmdlet = $exportedCmdlets[$key] | ||
|
|
||
| #attributes of parameters in cmdlet | ||
| $results["para_cmdlet"][$Cmdlet.Name] = @{} | ||
|
|
||
| foreach ($parameter_key in $Cmdlet.Parameters.keys){ | ||
| $parameter = $Cmdlet.Parameters[$parameter_key] | ||
|
|
||
| for ([int]$j = 0; $j -lt $parameter.Aliases.Count; $j++){ | ||
| $paraAlias = $parameter.Aliases[$j] | ||
| $paraFormal = $parameter_key | ||
| $results["para_cmdlet"][$Cmdlet.Name][$paraAlias] = $paraFormal | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
| } | ||
|
Alex-Sol marked this conversation as resolved.
|
||
| $json = $results | ConvertTo-Json -depth 10 | ||
| $json > AliasSpec.json | ||
|
|
||
|
|
||
Oops, something went wrong.
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.