Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context refactoring #2524

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/PSRule.Types/Definitions/ResourceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal static class ResourceHelper

private const char SCOPE_SEPARATOR = '\\';

internal const string STANDALONE_SCOPENAME = ".";
internal const string STANDALONE_SCOPE_NAME = ".";

internal static string GetIdString(string scope, string name)
{
Expand Down Expand Up @@ -63,7 +63,7 @@ internal static void ParseIdString(string id, out string? scope, out string? nam

internal static ResourceId GetRuleId(string? defaultScope, string name, ResourceIdKind kind)
{
defaultScope ??= STANDALONE_SCOPENAME;
defaultScope ??= STANDALONE_SCOPE_NAME;
return name.IndexOf(SCOPE_SEPARATOR) > 0 ? ResourceId.Parse(name, kind) : new ResourceId(defaultScope, name, kind);
}

Expand All @@ -87,6 +87,6 @@ internal static SeverityLevel GetLevel(SeverityLevel? level)

internal static string NormalizeScope(string? scope)
{
return scope == null || string.IsNullOrEmpty(scope) ? STANDALONE_SCOPENAME : scope;
return scope == null || string.IsNullOrEmpty(scope) ? STANDALONE_SCOPE_NAME : scope;
}
}
2 changes: 1 addition & 1 deletion src/PSRule.Types/Definitions/ResourceId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private static bool TryParse(string id, ResourceIdKind kind, out ResourceId? val
if (string.IsNullOrEmpty(id) || !TryParse(id, out var scope, out var name) || name == null)
return false;

scope ??= ResourceHelper.STANDALONE_SCOPENAME;
scope ??= ResourceHelper.STANDALONE_SCOPE_NAME;
value = new ResourceId(id, scope, name, kind);
return true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/PSRule/Commands/NewRuleDefinitionCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace PSRule.Commands;

/// <summary>
/// A Rule language block.
/// When processed, creates a rule language element that can be used later during rule execution.
/// </summary>
[Cmdlet(VerbsCommon.New, RuleLanguageNouns.RuleDefinition)]
internal sealed class NewRuleDefinitionCommand : LanguageBlock
Expand Down Expand Up @@ -89,7 +90,7 @@ internal sealed class NewRuleDefinitionCommand : LanguageBlock
public string[] Alias { get; set; }

/// <summary>
/// An optional reference identifer for the resource.
/// An optional reference identifier for the resource.
/// </summary>
[Parameter(Mandatory = false)]
[ValidateLength(3, 128)]
Expand Down
14 changes: 7 additions & 7 deletions src/PSRule/Definitions/ResourceValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

using System.Management.Automation;
using System.Text.RegularExpressions;
using PSRule.Pipeline;
using PSRule.Resources;
using PSRule.Runtime;

namespace PSRule.Definitions;

Expand All @@ -22,11 +22,11 @@ internal sealed class ResourceValidator : IResourceValidator

private static readonly Regex ValidName = new("^[^<>:/\\\\|?*\"'`+@._\\-\x00-\x1F][^<>:/\\\\|?*\"'`+@\x00-\x1F]{1,126}[^<>:/\\\\|?*\"'`+@._\\-\x00-\x1F]$", RegexOptions.Compiled, TimeSpan.FromSeconds(5));

private readonly RunspaceContext _Context;
private readonly IPipelineWriter _Writer;

public ResourceValidator(RunspaceContext context)
public ResourceValidator(IPipelineWriter writer)
{
_Context = context;
_Writer = writer;
}

internal static bool IsNameValid(string name)
Expand Down Expand Up @@ -74,7 +74,7 @@ private static string ReportExtent(ISourceExtent extent)

private void ReportError(string errorId, string message, params object[] args)
{
if (_Context == null)
if (_Writer == null)
return;

ReportError(new Pipeline.ParseException(
Expand All @@ -85,10 +85,10 @@ private void ReportError(string errorId, string message, params object[] args)

private void ReportError(Pipeline.ParseException exception)
{
if (_Context == null)
if (_Writer == null)
return;

_Context.WriteError(new ErrorRecord(
_Writer.WriteError(new ErrorRecord(
exception: exception,
errorId: exception.ErrorId,
errorCategory: ErrorCategory.InvalidOperation,
Expand Down
4 changes: 2 additions & 2 deletions src/PSRule/Host/HostHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ private static ILanguageBlock[] GetPSLanguageBlocks(RunspaceContext context, Sou
private static ILanguageBlock[] GetYamlLanguageBlocks(Source[] sources, RunspaceContext context)
{
var result = new Collection<ILanguageBlock>();
var visitor = new ResourceValidator(context);
var visitor = new ResourceValidator(context.Writer);
var d = new DeserializerBuilder()
.IgnoreUnmatchedProperties()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
Expand Down Expand Up @@ -313,7 +313,7 @@ private static ILanguageBlock[] GetYamlLanguageBlocks(Source[] sources, Runspace
private static ILanguageBlock[] GetJsonLanguageBlocks(Source[] sources, RunspaceContext context)
{
var result = new Collection<ILanguageBlock>();
var visitor = new ResourceValidator(context);
var visitor = new ResourceValidator(context.Writer);
var deserializer = new JsonSerializer
{
DateTimeZoneHandling = DateTimeZoneHandling.Utc
Expand Down
33 changes: 33 additions & 0 deletions src/PSRule/Pipeline/IPipelineReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using PSRule.Data;

namespace PSRule.Pipeline;

internal interface IPipelineReader
{
int Count { get; }

bool IsEmpty { get; }

/// <summary>
/// Add a new object into the stream.
/// </summary>
/// <param name="sourceObject">An object to process.</param>
/// <param name="targetType">A pre-bound type.</param>
/// <param name="skipExpansion">Determines if expansion is skipped.</param>
void Enqueue(object sourceObject, string? targetType = null, bool skipExpansion = false);

Check warning on line 20 in src/PSRule/Pipeline/IPipelineReader.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 20 in src/PSRule/Pipeline/IPipelineReader.cs

View workflow job for this annotation

GitHub Actions / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

bool TryDequeue(out ITargetObject sourceObject);

void Open();

/// <summary>
/// Add a path to the list of inputs.
/// </summary>
/// <param name="path">The path of files to add.</param>
void Add(string path);
}

#nullable restore
2 changes: 1 addition & 1 deletion src/PSRule/Pipeline/OptionContextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ internal void ModuleConfig(string module, ModuleConfigV1Spec spec)

private static bool ShouldCombine(string languageScope, OptionScope optionScope)
{
return optionScope.LanguageScope == ResourceHelper.STANDALONE_SCOPENAME || optionScope.LanguageScope == languageScope || optionScope.Type == ScopeType.Explicit;
return optionScope.LanguageScope == ResourceHelper.STANDALONE_SCOPE_NAME || optionScope.LanguageScope == languageScope || optionScope.Type == ScopeType.Explicit;
}

/// <summary>
Expand Down
21 changes: 0 additions & 21 deletions src/PSRule/Pipeline/PipelineContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,27 +114,6 @@ public string[] SourceContentCache
}
}

internal enum ResourceIssueType
{
Unknown
}

internal sealed class ResourceIssue
{
public ResourceIssue(ResourceKind kind, string id, ResourceIssueType issue)
{
Kind = kind;
Id = id;
Issue = issue;
}

public ResourceKind Kind { get; }

public string Id { get; }

public ResourceIssueType Issue { get; }
}

internal Runspace GetRunspace()
{
if (_Runspace == null)
Expand Down
22 changes: 22 additions & 0 deletions src/PSRule/Pipeline/ResourceIssue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using PSRule.Definitions;

namespace PSRule.Pipeline;

internal sealed class ResourceIssue
{
public ResourceIssue(ResourceKind resourceKind, ResourceId resourceId, ResourceIssueType issue)
{
ResourceKind = resourceKind;
ResourceId = resourceId;
Issue = issue;
}

public ResourceKind ResourceKind { get; }

public ResourceId ResourceId { get; }

public ResourceIssueType Issue { get; }
}
11 changes: 11 additions & 0 deletions src/PSRule/Pipeline/ResourceIssueType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace PSRule.Pipeline;

internal enum ResourceIssueType
{
Unknown,

SuppressionGroupExpired
}
8 changes: 4 additions & 4 deletions src/PSRule/Pipeline/TargetBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public TargetBinderBuilder(BindTargetMethod bindTargetName, BindTargetMethod bin
_BindTargetName = bindTargetName;
_BindTargetType = bindTargetType;
_BindField = bindField;
_BindingContext = new List<ITargetBindingContext>();
_BindingContext = [];
if (typeFilter != null && typeFilter.Length > 0)
_TypeFilter = new HashSet<string>(typeFilter, StringComparer.OrdinalIgnoreCase);
}
Expand All @@ -82,7 +82,7 @@ public TargetBinderBuilder(BindTargetMethod bindTargetName, BindTargetMethod bin
/// </summary>
public ITargetBinder Build()
{
return new TargetBinder(_BindingContext.ToArray());
return new TargetBinder([.. _BindingContext]);
}

/// <summary>
Expand Down Expand Up @@ -114,8 +114,8 @@ internal sealed class TargetBinder : ITargetBinder

internal TargetBinder(ITargetBindingContext[] bindingContext)
{
_BindingContext = new Dictionary<string, ITargetBindingContext>();
_BindingResult = new Dictionary<string, ITargetBindingResult>();
_BindingContext = [];
_BindingResult = [];
for (var i = 0; bindingContext != null && i < bindingContext.Length; i++)
_BindingContext.Add(bindingContext[i].LanguageScope ?? STANDALONE_SCOPE, bindingContext[i]);
}
Expand Down
29 changes: 23 additions & 6 deletions src/PSRule/Runtime/ILanguageScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

namespace PSRule.Runtime;

#nullable enable

/// <summary>
/// A named scope for language elements.
/// Any elements in a language scope are not visible to language elements in another scope.
/// </summary>
internal interface ILanguageScope : IDisposable
{
Expand All @@ -29,14 +32,17 @@ internal interface ILanguageScope : IDisposable
/// <summary>
/// Try to get a specific configuration value by name.
/// </summary>
bool TryConfigurationValue(string key, out object value);
bool TryConfigurationValue(string key, out object? value);

/// <summary>
/// Add a filter to the language scope.
/// </summary>
void WithFilter(IResourceFilter resourceFilter);

/// <summary>
/// Get a filter for a specific resource kind.
/// </summary>
IResourceFilter GetFilter(ResourceKind kind);
IResourceFilter? GetFilter(ResourceKind kind);

/// <summary>
/// Add a service to the scope.
Expand All @@ -46,11 +52,22 @@ internal interface ILanguageScope : IDisposable
/// <summary>
/// Get a previously added service.
/// </summary>
object GetService(string name);
object? GetService(string name);

bool TryGetType(object o, out string type, out string path);
/// <summary>
/// Try to bind the type of the object.
/// </summary>
bool TryGetType(object o, out string? type, out string? path);

bool TryGetName(object o, out string name, out string path);
/// <summary>
/// Try to bind the name of the object.
/// </summary>
bool TryGetName(object o, out string? name, out string? path);

bool TryGetScope(object o, out string[] scope);
/// <summary>
/// Try to bind the scope of the object.
/// </summary>
bool TryGetScope(object o, out string[]? scope);
}

#nullable restore
Loading