Skip to content

Commit 01f6873

Browse files
committed
Added latest version of ScriptAnalyzer
1 parent 329802e commit 01f6873

File tree

185 files changed

+18473
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

185 files changed

+18473
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.ComponentModel.Composition;
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.Globalization;
7+
using System.Linq;
8+
using System.Management.Automation;
9+
using System.Resources;
10+
using System.Threading;
11+
using System.Reflection;
12+
13+
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Commands
14+
{
15+
/// <summary>
16+
/// GetScriptAnalyzerLoggerCommand: Cmdlet that lists the PSScriptAnalyzer logger names and descriptions.
17+
/// </summary>
18+
[Cmdlet(VerbsCommon.Get, "ScriptAnalyzerLogger", HelpUri = "http://go.microsoft.com/fwlink/?LinkId=525912")]
19+
public class GetScriptAnalyzerLoggerCommand : PSCmdlet
20+
{
21+
#region Parameters
22+
23+
/// <summary>
24+
/// Path: Path to custom logger folder or assembly files.
25+
/// </summary>
26+
[Parameter(Mandatory = false)]
27+
[ValidateNotNullOrEmpty]
28+
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
29+
public string[] Path
30+
{
31+
get { return path; }
32+
set { path = value; }
33+
}
34+
private string[] path;
35+
36+
/// <summary>
37+
/// Name: The name of a specific logger to list.
38+
/// </summary>
39+
[Parameter(Mandatory = false)]
40+
[ValidateNotNullOrEmpty]
41+
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
42+
public string[] Name
43+
{
44+
get { return name; }
45+
set { name = value; }
46+
}
47+
private string[] name;
48+
49+
#endregion Parameters
50+
51+
#region Private Members
52+
53+
Dictionary<string, List<string>> validationResults = new Dictionary<string, List<string>>();
54+
private const string baseName = "Microsoft.Windows.Powershell.ScriptAnalyzer.Strings";
55+
private CultureInfo cul = Thread.CurrentThread.CurrentCulture;
56+
private ResourceManager rm = new ResourceManager(baseName, Assembly.GetExecutingAssembly());
57+
58+
#endregion
59+
60+
#region Overrides
61+
62+
/// <summary>
63+
/// BeginProcessing : TBD
64+
/// </summary>
65+
protected override void BeginProcessing()
66+
{
67+
#region Set PSCmdlet property of Helper
68+
69+
Helper.Instance.MyCmdlet = this;
70+
71+
#endregion
72+
73+
// Verifies paths
74+
if (path != null)
75+
{
76+
validationResults = ScriptAnalyzer.Instance.CheckPath(path, this);
77+
foreach (string invalidPath in validationResults["InvalidPaths"])
78+
{
79+
WriteWarning(string.Format(cul, rm.GetString("InvalidPath", cul), invalidPath));
80+
}
81+
}
82+
else
83+
{
84+
validationResults.Add("InvalidPaths", new List<string>());
85+
validationResults.Add("ValidPaths", new List<string>());
86+
}
87+
88+
try
89+
{
90+
if (validationResults["ValidPaths"].Count == 0)
91+
{
92+
ScriptAnalyzer.Instance.Initialize();
93+
}
94+
else
95+
{
96+
ScriptAnalyzer.Instance.Initilaize(validationResults);
97+
}
98+
}
99+
catch (Exception ex)
100+
{
101+
ThrowTerminatingError(new ErrorRecord(ex, ex.HResult.ToString("X", cul),
102+
ErrorCategory.NotSpecified, this));
103+
}
104+
}
105+
106+
/// <summary>
107+
/// ProcessRecord : TBD
108+
/// </summary>
109+
protected override void ProcessRecord()
110+
{
111+
IEnumerable<ILogger> loggers = ScriptAnalyzer.Instance.Loggers;
112+
if (loggers != null)
113+
{
114+
if (name != null)
115+
{
116+
foreach (ILogger logger in loggers)
117+
{
118+
if (name.Contains(logger.GetName(), StringComparer.OrdinalIgnoreCase))
119+
{
120+
WriteObject(new LoggerInfo(logger.GetName(), logger.GetDescription()));
121+
}
122+
}
123+
}
124+
else
125+
{
126+
foreach (ILogger logger in loggers)
127+
{
128+
WriteObject(new LoggerInfo(logger.GetName(), logger.GetDescription()));
129+
}
130+
}
131+
}
132+
else
133+
{
134+
WriteObject(rm.GetString("LoggersNotFound", cul));
135+
}
136+
}
137+
138+
#endregion
139+
}
140+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.ComponentModel.Composition;
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.Globalization;
7+
using System.Linq;
8+
using System.Management.Automation;
9+
using System.Resources;
10+
using System.Threading;
11+
using System.Reflection;
12+
13+
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.Commands
14+
{
15+
/// <summary>
16+
/// GetScriptAnalyzerRuleCommand: Cmdlet to list all the analyzer rule names and descriptions.
17+
/// </summary>
18+
[Cmdlet(VerbsCommon.Get, "ScriptAnalyzerRule", HelpUri = "http://go.microsoft.com/fwlink/?LinkId=525913")]
19+
public class GetScriptAnalyzerRuleCommand : PSCmdlet
20+
{
21+
#region Parameters
22+
/// <summary>
23+
/// Path: Path to custom rules folder.
24+
/// </summary>
25+
[Parameter(Mandatory = false)]
26+
[ValidateNotNullOrEmpty]
27+
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
28+
public string[] CustomizedRulePath
29+
{
30+
get { return customizedRulePath; }
31+
set { customizedRulePath = value; }
32+
}
33+
private string[] customizedRulePath;
34+
35+
/// <summary>
36+
/// Name: The name of a specific rule to list.
37+
/// </summary>
38+
[Parameter(Mandatory = false)]
39+
[ValidateNotNullOrEmpty]
40+
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
41+
public string[] Name
42+
{
43+
get { return name; }
44+
set { name = value; }
45+
}
46+
private string[] name;
47+
#endregion Parameters
48+
49+
#region Private Members
50+
51+
Dictionary<string, List<string>> validationResults = new Dictionary<string, List<string>>();
52+
53+
#endregion
54+
55+
#region Overrides
56+
57+
/// <summary>
58+
/// BeginProcessing : TBD
59+
/// </summary>
60+
protected override void BeginProcessing()
61+
{
62+
#region Set PSCmdlet property of Helper
63+
64+
Helper.Instance.MyCmdlet = this;
65+
66+
#endregion
67+
// Verifies rule extensions
68+
if (customizedRulePath != null)
69+
{
70+
validationResults = ScriptAnalyzer.Instance.CheckRuleExtension(customizedRulePath, this);
71+
foreach (string extension in validationResults["InvalidPaths"])
72+
{
73+
WriteWarning(string.Format(CultureInfo.CurrentCulture,Strings.MissingRuleExtension, extension));
74+
}
75+
}
76+
else
77+
{
78+
validationResults.Add("InvalidPaths", new List<string>());
79+
validationResults.Add("ValidModPaths", new List<string>());
80+
validationResults.Add("ValidDllPaths", new List<string>());
81+
}
82+
83+
try
84+
{
85+
if (validationResults["ValidDllPaths"].Count == 0)
86+
{
87+
ScriptAnalyzer.Instance.Initialize();
88+
}
89+
else
90+
{
91+
ScriptAnalyzer.Instance.Initilaize(validationResults);
92+
}
93+
}
94+
catch (Exception ex)
95+
{
96+
ThrowTerminatingError(new ErrorRecord(ex, ex.HResult.ToString("X", CultureInfo.CurrentCulture),
97+
ErrorCategory.NotSpecified, this));
98+
}
99+
}
100+
101+
/// <summary>
102+
/// ProcessRecord : TBD
103+
/// </summary>
104+
protected override void ProcessRecord()
105+
{
106+
string[] modNames = null;
107+
if (validationResults["ValidModPaths"].Count > 0)
108+
{
109+
modNames = validationResults["ValidModPaths"].ToArray<string>();
110+
}
111+
112+
IEnumerable<IRule> rules = ScriptAnalyzer.Instance.GetRule(modNames, name);
113+
if (rules == null)
114+
{
115+
WriteObject(string.Format(CultureInfo.CurrentCulture, Strings.RulesNotFound));
116+
}
117+
else
118+
{
119+
foreach (IRule rule in rules)
120+
{
121+
WriteObject(new RuleInfo(rule.GetName(), rule.GetCommonName(), rule.GetDescription(), rule.GetSourceType(), rule.GetSourceName()));
122+
}
123+
}
124+
}
125+
126+
#endregion
127+
}
128+
}

0 commit comments

Comments
 (0)