forked from microsoft/PSRule
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to improve maintainability (microsoft#1881)
- Loading branch information
1 parent
60f7f7d
commit f9f67f5
Showing
30 changed files
with
312 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...PSRule.Types/Definitions/SeverityLevel.cs → ....Types/Definitions/Rules/SeverityLevel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace PSRule.Data; | ||
|
||
/// <summary> | ||
/// A collection of <see cref="InputFileInfo"/>. | ||
/// </summary> | ||
public interface IInputFileInfoCollection : IEnumerable<InputFileInfo> | ||
{ | ||
/// <summary> | ||
/// Filters the collection to only include <see cref="InputFileInfo"/> with a specific file extension. | ||
/// </summary> | ||
/// <param name="extension">A file extension to filter the collection to.</param> | ||
/// <returns>A filtered collection.</returns> | ||
IInputFileInfoCollection WithExtension(string extension); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace PSRule.Data; | ||
|
||
/// <summary> | ||
/// A collection of issues reported by a downstream tool. | ||
/// </summary> | ||
internal sealed class TargetIssueCollection : ITargetIssueCollection | ||
{ | ||
private List<TargetIssueInfo> _Items; | ||
|
||
internal TargetIssueCollection() { } | ||
|
||
/// <inheritdoc/> | ||
public bool Any(string type = null) | ||
{ | ||
return Get(type).Length > 0; | ||
} | ||
|
||
/// <inheritdoc/> | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0046:Convert to conditional expression", Justification = "Avoid nested conditional expressions that increase complexity.")] | ||
public TargetIssueInfo[] Get(string type = null) | ||
{ | ||
if (_Items == null) | ||
return Array.Empty<TargetIssueInfo>(); | ||
|
||
return type == null ? _Items.ToArray() : _Items.Where(i => StringComparer.OrdinalIgnoreCase.Equals(i.Type, type)).ToArray(); | ||
} | ||
|
||
/// <summary> | ||
/// Add one or more issues into the collection. | ||
/// </summary> | ||
/// <param name="issueInfo">An array of <see cref="TargetIssueInfo"/> instance to add to the collection.</param> | ||
internal void AddRange(TargetIssueInfo[] issueInfo) | ||
{ | ||
for (var i = 0; issueInfo != null && i < issueInfo.Length; i++) | ||
Add(issueInfo[i]); | ||
} | ||
|
||
private void Add(TargetIssueInfo issueInfo) | ||
{ | ||
if (issueInfo == null || string.IsNullOrEmpty(issueInfo.Type)) | ||
return; | ||
|
||
_Items ??= new List<TargetIssueInfo>(); | ||
_Items.Add(issueInfo); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Management.Automation; | ||
using PSRule.Resources; | ||
|
||
namespace PSRule.Definitions.Baselines; | ||
|
||
internal sealed class BaselineFilter : IResourceFilter | ||
{ | ||
private readonly HashSet<string> _Include; | ||
private readonly WildcardPattern _WildcardMatch; | ||
|
||
public BaselineFilter(string[] include) | ||
{ | ||
_Include = include == null || include.Length == 0 ? null : new HashSet<string>(include, StringComparer.OrdinalIgnoreCase); | ||
_WildcardMatch = null; | ||
if (include != null && include.Length > 0 && WildcardPattern.ContainsWildcardCharacters(include[0])) | ||
{ | ||
if (include.Length > 1) | ||
throw new NotSupportedException(PSRuleResources.MatchSingleName); | ||
|
||
_WildcardMatch = new WildcardPattern(include[0]); | ||
} | ||
} | ||
|
||
ResourceKind IResourceFilter.Kind => ResourceKind.Baseline; | ||
|
||
public bool Match(IResource resource) | ||
{ | ||
return _Include == null || _Include.Contains(resource.Name) || MatchWildcard(resource.Name); | ||
} | ||
|
||
private bool MatchWildcard(string name) | ||
{ | ||
return _WildcardMatch != null && _WildcardMatch.IsMatch(name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using PSRule.Pipeline; | ||
|
||
namespace PSRule.Definitions.Baselines; | ||
|
||
internal sealed class BaselineRef : ResourceRef | ||
{ | ||
public readonly ScopeType Type; | ||
|
||
public BaselineRef(string id, ScopeType scopeType) | ||
: base(id, ResourceKind.Baseline) | ||
{ | ||
Type = scopeType; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using PSRule.Configuration; | ||
|
||
namespace PSRule.Definitions.Baselines; | ||
|
||
/// <summary> | ||
/// A specification for a V1 baseline resource. | ||
/// </summary> | ||
public sealed class BaselineSpec : Spec, IBaselineV1Spec | ||
{ | ||
/// <inheritdoc/> | ||
public BindingOption Binding { get; set; } | ||
|
||
/// <inheritdoc/> | ||
public ConfigurationOption Configuration { get; set; } | ||
|
||
/// <inheritdoc/> | ||
public ConventionOption Convention { get; set; } | ||
|
||
/// <inheritdoc/> | ||
public RuleOption Rule { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using PSRule.Configuration; | ||
|
||
namespace PSRule.Definitions.Baselines; | ||
|
||
/// <summary> | ||
/// A specification for a V1 baseline resource. | ||
/// </summary> | ||
internal interface IBaselineV1Spec | ||
{ | ||
/// <summary> | ||
/// Options that affect property binding. | ||
/// </summary> | ||
BindingOption Binding { get; set; } | ||
|
||
/// <summary> | ||
/// Allows configuration key/ values to be specified that can be used within rule definitions. | ||
/// </summary> | ||
ConfigurationOption Configuration { get; set; } | ||
|
||
/// <summary> | ||
/// Options that configure conventions. | ||
/// </summary> | ||
ConventionOption Convention { get; set; } | ||
|
||
/// <summary> | ||
/// Options for that affect which rules are executed by including and filtering discovered rules. | ||
/// </summary> | ||
RuleOption Rule { get; set; } | ||
} |
Oops, something went wrong.