-
Notifications
You must be signed in to change notification settings - Fork 415
Expand file tree
/
Copy pathRuleExecutor.cs
More file actions
150 lines (117 loc) · 4.61 KB
/
Copy pathRuleExecutor.cs
File metadata and controls
150 lines (117 loc) · 4.61 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
146
147
148
149
150
using Microsoft.PowerShell.ScriptAnalyzer.Rules;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation.Language;
namespace Microsoft.PowerShell.ScriptAnalyzer
{
public interface IRuleExecutor
{
void AddRule(ScriptRule rule);
IReadOnlyCollection<ScriptDiagnostic> CollectDiagnostics();
}
internal class SequentialRuleExecutor : IRuleExecutor
{
private readonly Ast _scriptAst;
private readonly IReadOnlyList<Token> _scriptTokens;
private readonly string _scriptPath;
private readonly List<ScriptDiagnostic> _diagnostics;
public SequentialRuleExecutor(Ast ast, IReadOnlyList<Token> tokens, string scriptPath)
{
_scriptAst = ast;
_scriptTokens = tokens;
_scriptPath = scriptPath;
_diagnostics = new List<ScriptDiagnostic>();
}
public void AddRule(ScriptRule rule)
{
_diagnostics.AddRange(rule.AnalyzeScript(_scriptAst, _scriptTokens, _scriptPath));
}
public IReadOnlyCollection<ScriptDiagnostic> CollectDiagnostics()
{
return _diagnostics;
}
}
internal class ParallelLinqRuleExecutor : IRuleExecutor
{
private readonly Ast _scriptAst;
private readonly IReadOnlyList<Token> _scriptTokens;
private readonly string _scriptPath;
private readonly List<ScriptRule> _parallelRules;
private readonly List<ScriptRule> _sequentialRules;
public ParallelLinqRuleExecutor(Ast scriptAst, IReadOnlyList<Token> scriptTokens, string scriptPath)
{
_scriptAst = scriptAst;
_scriptTokens = scriptTokens;
_scriptPath = scriptPath;
_parallelRules = new List<ScriptRule>();
_sequentialRules = new List<ScriptRule>();
}
public void AddRule(ScriptRule rule)
{
if (rule.RuleInfo.IsThreadsafe)
{
_parallelRules.Add(rule);
return;
}
_sequentialRules.Add(rule);
}
public IReadOnlyCollection<ScriptDiagnostic> CollectDiagnostics()
{
List<ScriptDiagnostic> diagnostics = _parallelRules.AsParallel()
.SelectMany(rule => rule.AnalyzeScript(_scriptAst, _scriptTokens, _scriptPath))
.ToList();
foreach (ScriptRule sequentialRule in _sequentialRules)
{
diagnostics.AddRange(sequentialRule.AnalyzeScript(_scriptAst, _scriptTokens, _scriptPath));
}
return diagnostics;
}
}
/*
internal class DataflowRuleExecutor : IRuleExecutor
{
private static readonly ExecutionDataflowBlockOptions s_parallelExecutionOptions = new ExecutionDataflowBlockOptions()
{
MaxDegreeOfParallelism = DataflowBlockOptions.Unbounded,
EnsureOrdered = false,
};
private readonly Ast _scriptAst;
private readonly IReadOnlyList<Token> _scriptTokens;
private readonly string _scriptPath;
private readonly TransformManyBlock<ScriptRule, ScriptDiagnostic> _parallelRulePipeline;
private readonly List<ScriptRule> _sequentialRules;
public DataflowRuleExecutor(Ast scriptAst, IReadOnlyList<Token> scriptTokens, string scriptPath)
{
_scriptAst = scriptAst;
_scriptTokens = scriptTokens;
_scriptPath = scriptPath;
_parallelRulePipeline = new TransformManyBlock<ScriptRule, ScriptDiagnostic>(RunScriptRule, s_parallelExecutionOptions);
_sequentialRules = new List<ScriptRule>();
}
public void AddRule(ScriptRule rule)
{
if (rule.RuleInfo.IsThreadsafe)
{
_parallelRulePipeline.Post(rule);
return;
}
_sequentialRules.Add(rule);
}
public IReadOnlyCollection<ScriptDiagnostic> CollectDiagnostics()
{
_parallelRulePipeline.Complete();
_parallelRulePipeline.TryReceiveAll(out IList<ScriptDiagnostic> parallelDiagnostics);
var diagnostics = new List<ScriptDiagnostic>(parallelDiagnostics);
foreach (ScriptRule rule in _sequentialRules)
{
diagnostics.AddRange(rule.AnalyzeScript(_scriptAst, _scriptTokens, _scriptPath));
}
return diagnostics;
}
private IEnumerable<ScriptDiagnostic> RunScriptRule(ScriptRule rule)
{
return rule.AnalyzeScript(_scriptAst, _scriptTokens, _scriptPath);
}
}
*/
}