Skip to content

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
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c6674ba
feat: Alias and Breakingchange
Sep 1, 2021
ff97be8
Merge branch 'main' into lizeng/PSACustomRules
isra-fel Sep 1, 2021
f43c3ec
feat: merge code of Alias and Breakingchange
Sep 1, 2021
6f26420
Merge branches 'lizeng/PSACustomRules' and 'lizeng/PSACustomRules' of…
Sep 1, 2021
924a7b0
style: delete annotations
Sep 2, 2021
64d1f9b
feat: get alias json file from modules
Sep 4, 2021
2473b42
style: delete annotation
Sep 4, 2021
5be1410
style: convert the path from absolute to relative
Sep 4, 2021
a1e17bc
feat: check for existence of PSA module
Sep 5, 2021
c699b37
feat: add change event
Sep 5, 2021
b5760ab
style: fix some annotations and log message
Sep 6, 2021
86786c0
feat:para alias of cmdlet
Sep 6, 2021
801a616
fix: change the path of tempfile when changing
Sep 6, 2021
d39d90b
fix: generation of alias cmdlets
Sep 6, 2021
2a22cc6
docs: how to generate alias spec and breaking change spec
Sep 9, 2021
5764847
fix: resolve the PR
Sep 9, 2021
a0e8207
feat: add debounce
Sep 9, 2021
72cff02
fix: copy custom rules to powershell exec path
Sep 9, 2021
515c5ed
fix: resolve review2
Sep 9, 2021
ad46e7d
fix: yml and lint
Sep 9, 2021
d8a1c14
fix: resolve review3
Sep 9, 2021
477c121
fix: remove the log of timer
Sep 9, 2021
f633147
feat: description to type of diagnostic
Sep 9, 2021
4b63e5c
prettier
isra-fel Sep 10, 2021
a59f57f
refactor debounce code
isra-fel Sep 10, 2021
c84dd8b
fix: copy-item
Sep 11, 2021
73d7870
fix: formatPlansToDiag
Sep 12, 2021
e4f0d73
fix: logic of breakingchange parameter implicit
Sep 12, 2021
69ccac8
docs: change version to 0.3.0
Sep 14, 2021
04c3425
feat: more detailed message about breaking change
Sep 14, 2021
cd89c0d
feat: filter the para belongs to powershell
Sep 17, 2021
da7e4b0
feat: get message from GetBreakingChangeTextFromAttribute method
Sep 22, 2021
600b47c
docs: add some notes for developers
Sep 22, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68,398 changes: 68,398 additions & 0 deletions vscode-extension/PSA_custom_Rules/Alias/AliasSpec.json

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions vscode-extension/PSA_custom_Rules/Alias/Get-AliasSpec.psm1
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
}
92 changes: 92 additions & 0 deletions vscode-extension/PSA_custom_Rules/Alias/avoidAlias.psm1
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
)

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

#list of CorrectionExtents
$l = (new-object System.Collections.ObjectModel.Collection["Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent"])

foreach ($cmdletReference in $foundCmdlets) {
if ($AliasSpec.cmdlet.psobject.properties.match($cmdletReference.CommandName).Count) {
[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.'

$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){
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 `
-Typename "Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord" `
-ArgumentList "This is help", $extent, $PSCmdlet.MyInvocation.InvocationName, Warning, "MyRuleSuppressionID", $l
$dr.SuggestedCorrections = $l
$results += $dr
return $results
}
}
Export-ModuleMember -Function Measure*
80 changes: 80 additions & 0 deletions vscode-extension/PSA_custom_Rules/Alias/geneAlias.ps1
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"}
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)"
$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
# $json > aliasTocmdlet.json



class getBreakingchangeResult_paraCmdlet{
[System.String] $Name
[System.String] $TypeBreakingChange
}

$results = @{
}

$results["updateTime"] = Get-Date
$results["cmdlet"] = $aliasResults
$results["para_cmdlet"] = @{}

$results["updateTime"] = $results["updateTime"].ToString()



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
}




}




}



}
$json = $results | ConvertTo-Json -depth 10
$json > AliasSpec.json


Loading