Skip to content

Commit d82c704

Browse files
authored
Refactoring conventions (microsoft#2679)
1 parent 5c1fdb9 commit d82c704

24 files changed

+886
-675
lines changed

.vscode/settings.json

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
"cSpell.words": [
9191
"APPSERVICEMININSTANCECOUNT",
9292
"Arity",
93+
"Authenticode",
9394
"CLIXML",
9495
"cmdlet",
9596
"cmdlets",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using PSRule.Runtime;
5+
6+
namespace PSRule.Definitions.Baselines;
7+
8+
#nullable enable
9+
10+
/// <summary>
11+
/// Extensions methods for baselines.
12+
/// </summary>
13+
internal static class BaselineExtensions
14+
{
15+
/// <summary>
16+
/// Convert any baseline language blocks into <see cref="Baseline"/> resources.
17+
/// </summary>
18+
public static Baseline[] ToBaselineV1(this IEnumerable<ILanguageBlock> blocks, RunspaceContext context)
19+
{
20+
if (blocks == null) return [];
21+
22+
// Index baselines by BaselineId
23+
var results = new Dictionary<string, Baseline>(StringComparer.OrdinalIgnoreCase);
24+
25+
foreach (var block in blocks.OfType<Baseline>().ToArray())
26+
{
27+
context.EnterLanguageScope(block.Source);
28+
try
29+
{
30+
// Ignore baselines that don't match
31+
if (!Match(context, block))
32+
continue;
33+
34+
if (!results.ContainsKey(block.BaselineId))
35+
results[block.BaselineId] = block;
36+
37+
}
38+
finally
39+
{
40+
context.ExitLanguageScope(block.Source);
41+
}
42+
}
43+
return [.. results.Values];
44+
}
45+
46+
private static bool Match(RunspaceContext context, Baseline resource)
47+
{
48+
try
49+
{
50+
context.EnterLanguageScope(resource.Source);
51+
var filter = context.LanguageScope!.GetFilter(ResourceKind.Baseline);
52+
return filter == null || filter.Match(resource);
53+
}
54+
finally
55+
{
56+
context.ExitLanguageScope(resource.Source);
57+
}
58+
}
59+
}
60+
61+
#nullable restore
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using PSRule.Runtime;
5-
64
namespace PSRule.Definitions.Conventions;
75

86
/// <summary>
97
/// Orders conventions by the order they are specified.
108
/// </summary>
119
internal sealed class ConventionComparer : IComparer<IConventionV1>
1210
{
13-
private readonly RunspaceContext _Context;
11+
private readonly Func<IConventionV1, int> _GetOrder;
1412

15-
internal ConventionComparer(RunspaceContext context)
13+
internal ConventionComparer(Func<IConventionV1, int> getOrder)
1614
{
17-
_Context = context;
15+
_GetOrder = getOrder;
1816
}
1917

2018
public int Compare(IConventionV1 x, IConventionV1 y)
2119
{
22-
return _Context.Pipeline.GetConventionOrder(x) - _Context.Pipeline.GetConventionOrder(y);
20+
return _GetOrder(x) - _GetOrder(y);
2321
}
2422
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using PSRule.Runtime;
5+
6+
namespace PSRule.Definitions.Conventions;
7+
8+
#nullable enable
9+
10+
/// <summary>
11+
/// Extensions for conventions.
12+
/// </summary>
13+
internal static class ConventionExtensions
14+
{
15+
/// <summary>
16+
/// Convert any convention language blocks into <see cref="IConventionV1"/> resources.
17+
/// </summary>
18+
public static IConventionV1[] ToConventionsV1(this IEnumerable<ILanguageBlock> blocks, RunspaceContext context)
19+
{
20+
if (blocks == null) return [];
21+
22+
// Index by Id.
23+
var index = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
24+
var results = new List<IConventionV1>();
25+
26+
foreach (var block in blocks.OfType<ScriptBlockConvention>().ToArray())
27+
{
28+
context.EnterLanguageScope(block.Source);
29+
try
30+
{
31+
// Ignore blocks that don't match.
32+
if (!Match(context, block))
33+
continue;
34+
35+
if (!index.Contains(block.Id.Value))
36+
results.Add(block);
37+
38+
}
39+
finally
40+
{
41+
context.ExitLanguageScope(block.Source);
42+
}
43+
}
44+
return [.. results];
45+
}
46+
47+
private static bool Match(RunspaceContext context, ScriptBlockConvention block)
48+
{
49+
try
50+
{
51+
context.EnterLanguageScope(block.Source);
52+
var filter = context.LanguageScope.GetFilter(ResourceKind.Convention);
53+
return filter == null || filter.Match(block);
54+
}
55+
finally
56+
{
57+
context.ExitLanguageScope(block.Source);
58+
}
59+
}
60+
}
61+
62+
#nullable restore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using PSRule.Runtime;
5+
6+
namespace PSRule.Definitions.ModuleConfigs;
7+
8+
#nullable enable
9+
10+
/// <summary>
11+
/// Extensions methods for module configurations.
12+
/// </summary>
13+
internal static class ModuleConfigExtensions
14+
{
15+
/// <summary>
16+
/// Convert any selector language blocks into <see cref="ModuleConfigV1"/> resources.
17+
/// </summary>
18+
public static ModuleConfigV1[] ToModuleConfigV1(this IEnumerable<ILanguageBlock> blocks, RunspaceContext context)
19+
{
20+
if (blocks == null) return [];
21+
22+
// Index configurations by Name.
23+
var results = new Dictionary<string, ModuleConfigV1>(StringComparer.OrdinalIgnoreCase);
24+
foreach (var block in blocks.OfType<ModuleConfigV1>().ToArray())
25+
{
26+
if (!results.ContainsKey(block.Name))
27+
results[block.Name] = block;
28+
}
29+
return [.. results.Values];
30+
}
31+
}
32+
33+
#nullable restore

0 commit comments

Comments
 (0)