forked from adityapatwardhan/ps-codacy
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall.ps1
More file actions
145 lines (137 loc) · 6.5 KB
/
install.ps1
File metadata and controls
145 lines (137 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env pwsh
$visibilityRules = 'psavoidusingcomputernamehardcoded' ;
$cryptoRules = 'psavoidusingconverttosecurestringwithplaintext', 'psavoidusingplaintextforpassword', 'psusepscredentialtype' ;
$commandInjectionRules = 'psavoidusinginvokeexpression' ;
$authRules = 'psavoidusingusernameandpasswordparams' ;
$enabledRules = @(
"psalignassignmentstatement",
"psavoidassignmenttoautomaticvariable",
"psavoiddefaultvalueformandatoryparameter",
"psavoiddefaultvalueswitchparameter",
"psavoidglobalaliases",
"psavoidglobalfunctions",
"psavoidglobalvars",
"psavoidinvokingemptymembers",
"psavoidshouldcontinuewithoutforce",
"psavoidusingcomputernamehardcoded",
"psavoidusingconverttosecurestringwithplaintext",
"psavoidusingdeprecatedmanifestfields",
"psavoidusingemptycatchblock",
"psavoidusinginvokeexpression",
"psavoidusingplaintextforpassword",
"psavoidusingpositionalparameters",
"psavoidusingusernameandpasswordparams",
"psavoidusingwmicmdlet",
"psavoidusingwritehost",
"psdscdscexamplespresent",
"psdscdsctestspresent",
"psdscreturncorrecttypesfordscfunctions",
"psdscstandarddscfunctionsinresource",
"psdscuseidenticalmandatoryparametersfordsc",
"psdscuseidenticalparametersfordsc",
"psmisleadingbacktick",
"psmissingmodulemanifestfield",
"psplaceclosebrace",
"psplaceopenbrace",
"pspossibleincorrectcomparisonwithnull",
"pspossibleincorrectusageofassignmentoperator",
"pspossibleincorrectusageofredirectionoperator",
"psreservedcmdletchar",
"psreservedparams",
"psshouldprocess",
"psuseapprovedverbs",
"psusebomforunicodeencodedfile",
"psusecmdletcorrectly",
"psusecompatiblecmdlets",
"psuseliteralinitializerforhashtable",
"psuseoutputtypecorrectly",
"psusepscredentialtype",
"psuseshouldprocessforstatechangingfunctions",
"psusesupportsshouldprocess",
"psusetoexportfieldsinmanifest"
)
$count = 0;
function getTitleFromRuleFile {
$patternNameCamelCased = $args[0] ;
#--- get title from finding a rule file that has a name that matches part of a pattern name
foreach($file in Get-ChildItem PSScriptAnalyzer/Rules/*) {
$fileNameWithoutExtension = $file.name.split('.cs')[0]
if($patternNameCamelCased.contains($fileNameWithoutExtension)){
$ruleFileWithShortPatternName = 'PSScriptAnalyzer/Rules/' + $file.name;
}
} ;
$commentPattern = '[\/]\{3\} [a-zA-Z0-9]*[: ] [.?]*' ;
$titleFromShortNameGrepResult = if($ruleFileWithShortPatternName){ cat $ruleFileWithShortPatternName | grep "$commentPattern" };
$titleFromShortNameMatch = if($titleFromShortNameGrepResult) { $titleFromShortNameGrepResult.split(':')[1].SubString(1).split('.')[0] } ;
#--- get title from finding a rule file that has a name that matches full pattern name
$ruleFileWithFullPatternName = 'PSScriptAnalyzer/Rules/' + $patternNameCamelCased + '.cs' ;
# the next instruction will output 'No such file or directory' from cat when this file does not exist
$titleFromFullNameGrepResult = cat $ruleFileWithFullPatternName | grep "$commentPattern" ;
$titleFromFullNameMatch = if($titleFromFullNameGrepResult) { $titleFromFullNameGrepResult.split(':')[1].SubString(1).split('.')[0] } ;
if($titleFromShortNameMatch) { $titleFromShortNameMatch } else { $titleFromFullNameMatch }
}
#resolve title by falling back from values retrieved from multiple sources
function getTitle {
$patternNameCamelCased = $args[0] ;
$titleFromRulesFile = getTitleFromRuleFile $patternNameCamelCased;
if($titleFromRulesFile) {
$titleFromRulesFile ;
} else {
$patternNameCamelCased;
};
}
# get pattern category
function getCategory {
$level = $args[0]
$patternId = $args[1]
$subcategory = getSecuritySubcategory $patternId
if($subcategory -ne '') {
@{ category='Security'; subcategory=$subcategory } ;
}elseif($level -eq 'Info') {
@{ category='CodeStyle' } ;
} else {
@{ category='ErrorProne' } ;
};
}
# get security subcategory for pattern id
function getSecuritySubcategory {
$patternId = $args[0]
switch ($patternId) {
{$visibilityRules -contains $_} { 'Visibility' }
{$cryptoRules -contains $_} {'Cryptography'}
{$commandInjectionRules -contains $_} {'CommandInjection'}
{$authRules -contains $_} {'Auth'}
default {''}
}
}
$null = New-Item -Type Directory docs -Force ;
$patterns = Get-ScriptAnalyzerRule | Where-Object { $_.RuleName -ne 'PSUseDeclaredVarsMoreThanAssignments' } ;
$codacyPatterns = @() ;
$codacyDescriptions = @();
New-Item -Type Directory docs/description -Force | Out-Null ;
foreach($pat in $patterns) {
$patternId = $pat.RuleName.ToLower() ;
$patternNameLowerCased = $patternId.SubString(2) ;
# could not use pat.RuleName for filename because of a mismatch in the uppercase 'W' in AvoidUsingUserNameAndPassWordParams
$patternNameCamelCased = (ls PSScriptAnalyzer/docs/Rules | grep -io $patternNameLowerCased).split("\n")[0] ;
$originalPatternFileName = $patternNameCamelCased + '.md' ;
$patternFileName = $patternId + '.md' ;
cp PSScriptAnalyzer/docs/Rules/$originalPatternFileName docs/description/$patternFileName ;
$title = getTitle $patternNameCamelCased ;
if($title -eq $patternNameCamelCased) { Write-Output "$patternNameCamelCased"; $count = $count+1;}
$description = $pat.Description ;
$level = if($pat.Severity -eq 'Information') { 'Info' } else { $pat.Severity.ToString() } ;
$category = getCategory $level $patternId;
$enabledByDefault = $enabledRules -contains $patternId
if($category.ContainsKey('subcategory')) {
$codacyPatterns += [ordered] @{ patternId = $patternId; level = $level; category = $category.category; subcategory = $category.subcategory; enabled = $enabledByDefault } ;
} else {
$codacyPatterns += [ordered] @{ patternId = $patternId; level = $level; category = $category.category; enabled = $enabledByDefault } ;
}
$codacyDescriptions += [ordered] @{ patternId = $patternId; title = $title; description = $description } ;
}
Write-Output "UNMATCHED PATTERNS: $count";
$toolVersion = cat psscriptanalyzer.version | tr -d '\n';
$patternFormat = [ordered] @{ name = 'psscriptanalyzer'; version = $toolVersion; patterns = $codacyPatterns} ;
$patternFormat | ConvertTo-Json -Depth 5 | Out-File docs/patterns.json -Force -Encoding ascii;
$codacyDescriptions | ConvertTo-Json -Depth 5 | Out-File docs/description/description.json -Force -Encoding ascii;