Repository navigation
Expand file tree
/
Copy pathInvokeScriptAnalyzerCommand.cs
More file actions
106 lines (88 loc) · 3.28 KB
/
Copy pathInvokeScriptAnalyzerCommand.cs
File metadata and controls
106 lines (88 loc) · 3.28 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
using Microsoft.PowerShell.ScriptAnalyzer.Builder;
using Microsoft.PowerShell.ScriptAnalyzer.Configuration;
using System;
using System.Collections.Concurrent;
using System.Management.Automation;
#if !CORECLR
using Microsoft.PowerShell.ScriptAnalyzer.Internal;
#endif
namespace Microsoft.PowerShell.ScriptAnalyzer.Commands
{
[Cmdlet(VerbsLifecycle.Invoke, "ScriptAnalyzer2")]
public class InvokeScriptAnalyzerCommand : Cmdlet
{
private static readonly ConcurrentDictionary<ParameterSetting, ScriptAnalyzer> s_configuredScriptAnalyzers = new ConcurrentDictionary<ParameterSetting, ScriptAnalyzer>();
private ScriptAnalyzer _scriptAnalyzer;
[ValidateNotNullOrEmpty]
[Parameter(Position = 0, Mandatory = true, ParameterSetName = "FilePath")]
public string[] Path { get; set; }
[ValidateNotNullOrEmpty]
[Parameter(Position = 0, Mandatory = true, ParameterSetName = "Input")]
public string[] ScriptDefinition { get; set; }
[Parameter]
public string ConfigurationPath { get; set; }
[Parameter]
public string[] ExcludeRules { get; set; }
protected override void BeginProcessing()
{
_scriptAnalyzer = GetScriptAnalyzer();
}
protected override void ProcessRecord()
{
if (Path != null)
{
foreach (string path in Path)
{
foreach (ScriptDiagnostic diagnostic in _scriptAnalyzer.AnalyzeScriptPath(path))
{
WriteObject(diagnostic);
}
}
return;
}
if (ScriptDefinition != null)
{
foreach (string input in ScriptDefinition)
{
foreach (ScriptDiagnostic diagnostic in _scriptAnalyzer.AnalyzeScriptInput(input))
{
WriteObject(diagnostic);
}
}
}
}
private ScriptAnalyzer GetScriptAnalyzer()
{
var parameters = new ParameterSetting(this);
return s_configuredScriptAnalyzers.GetOrAdd(parameters, CreateScriptAnalyzerWithParameters);
}
private ScriptAnalyzer CreateScriptAnalyzerWithParameters(ParameterSetting parameters)
{
var configBuilder = new ScriptAnalyzerConfigurationBuilder()
.WithBuiltinRuleSet(BuiltinRulePreference.Default);
if (parameters.ConfigurationPath != null)
{
configBuilder.AddConfigurationFile(parameters.ConfigurationPath);
}
return configBuilder.Build().CreateScriptAnalyzer();
}
private struct ParameterSetting
{
public ParameterSetting(InvokeScriptAnalyzerCommand command)
{
ConfigurationPath = command.ConfigurationPath;
}
public string ConfigurationPath { get; }
public override int GetHashCode()
{
#if CORECLR
return HashCode.Combine(ConfigurationPath);
#else
return HashCodeCombinator.Create()
.Add(ConfigurationPath)
.GetHashCode();
#endif
}
}
}
}