Skip to content

Commit

Permalink
Refactoring (microsoft#1852)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Jun 8, 2024
1 parent 3b6be55 commit cdc08bb
Show file tree
Hide file tree
Showing 86 changed files with 2,352 additions and 2,026 deletions.
1 change: 0 additions & 1 deletion src/PSRule.CommandLine/ClientContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.CommandLine.Invocation;
using PSRule.Configuration;
using PSRule.Options;

namespace PSRule.CommandLine;

Expand Down
9 changes: 4 additions & 5 deletions src/PSRule.Types/Emitters/InternalFileStream.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace PSRule.Emitters
namespace PSRule.Emitters;

internal class InternalFileStream
{
internal class InternalFileStream
public InternalFileStream()
{
public InternalFileStream()
{
}
}
}
68 changes: 68 additions & 0 deletions src/PSRule/Badges/Badge.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace PSRule.Badges;

/// <summary>
/// An instance of a badge created by the Badge API.
/// </summary>
internal sealed class Badge : IBadge
{
private readonly string _LeftText;
private readonly string _RightText;
private readonly double _LeftWidth;
private readonly double _RightWidth;
private readonly int _MidPadding;
private readonly int _BorderPadding;
private readonly string _Fill;

internal Badge(string left, string right, string fill)
{
_LeftWidth = BadgeResources.Measure(left);
_RightWidth = BadgeResources.Measure(right);

_LeftText = left;
_RightText = right;
_MidPadding = 3;
_BorderPadding = 7;
_Fill = fill;
}

/// <inheritdoc/>
public override string ToString()
{
return ToSvg();
}

/// <inheritdoc/>
public string ToSvg()
{
var w = (int)Math.Round(_LeftWidth + _RightWidth + 2 * _BorderPadding + 2 * _MidPadding);
var x = (int)Math.Round(_LeftWidth + _BorderPadding + _MidPadding);

var builder = new SvgBuilder(
width: w,
height: 20,
textScale: 10,
midPoint: x,
rounding: 2,
borderPadding: _BorderPadding,
midPadding: _MidPadding);
builder.Begin(string.Concat(_LeftText, ": ", _RightText));
builder.Backfill(_Fill);
builder.TextBlock(_LeftText, _RightText, 110);
builder.End();
return builder.ToString();
}

/// <inheritdoc/>
public void ToFile(string path)
{
path = Environment.GetRootedPath(path);
var parentPath = Directory.GetParent(path);
if (!parentPath.Exists)
Directory.CreateDirectory(path: parentPath.FullName);

File.WriteAllText(path, contents: ToSvg());
}
}
130 changes: 0 additions & 130 deletions src/PSRule/Badges/BadgeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,136 +7,6 @@

namespace PSRule.Badges;

/// <summary>
/// The type of badge.
/// </summary>
public enum BadgeType
{
/// <summary>
/// A badge that reports an unknown state.
/// </summary>
Unknown = 0,

/// <summary>
/// A badge reporting a successful state.
/// </summary>
Success = 1,

/// <summary>
/// A bagde reporting a failed state.
/// </summary>
Failure = 2
}

/// <summary>
/// An instance of a badge created by the badge API.
/// </summary>
public interface IBadge
{
/// <summary>
/// Get the badge as SVG text content.
/// </summary>
string ToSvg();

/// <summary>
/// Write the SVG badge content directly to disk.
/// </summary>
void ToFile(string path);
}

/// <summary>
/// A builder for the badge API.
/// </summary>
public interface IBadgeBuilder
{
/// <summary>
/// Create a badge for the worst case of an analyzed object.
/// </summary>
/// <param name="result">A single result. The worst case for all records of an object is used for the badge.</param>
/// <returns>An instance of a badge.</returns>
IBadge Create(InvokeResult result);

/// <summary>
/// Create a badge for the worst case of all analyzed objects.
/// </summary>
/// <param name="result">A enumeration of results. The worst case from all results is used for the badge.</param>
/// <returns>An instance of a badge.</returns>
IBadge Create(IEnumerable<InvokeResult> result);

/// <summary>
/// Create a custom badge.
/// </summary>
/// <param name="title">The left badge text.</param>
/// <param name="type">Determines if the result is Unknown, Success, or Failure.</param>
/// <param name="label">The right badge text.</param>
/// <returns>An instance of a badge.</returns>
IBadge Create(string title, BadgeType type, string label);
}

/// <summary>
/// An instance of a badge created by the Badge API.
/// </summary>
internal sealed class Badge : IBadge
{
private readonly string _LeftText;
private readonly string _RightText;
private readonly double _LeftWidth;
private readonly double _RightWidth;
private readonly int _MidPadding;
private readonly int _BorderPadding;
private readonly string _Fill;

internal Badge(string left, string right, string fill)
{
_LeftWidth = BadgeResources.Measure(left);
_RightWidth = BadgeResources.Measure(right);

_LeftText = left;
_RightText = right;
_MidPadding = 3;
_BorderPadding = 7;
_Fill = fill;
}

/// <inheritdoc/>
public override string ToString()
{
return ToSvg();
}

/// <inheritdoc/>
public string ToSvg()
{
var w = (int)Math.Round(_LeftWidth + _RightWidth + 2 * _BorderPadding + 2 * _MidPadding);
var x = (int)Math.Round(_LeftWidth + _BorderPadding + _MidPadding);

var builder = new SvgBuilder(
width: w,
height: 20,
textScale: 10,
midPoint: x,
rounding: 2,
borderPadding: _BorderPadding,
midPadding: _MidPadding);
builder.Begin(string.Concat(_LeftText, ": ", _RightText));
builder.Backfill(_Fill);
builder.TextBlock(_LeftText, _RightText, 110);
builder.End();
return builder.ToString();
}

/// <inheritdoc/>
public void ToFile(string path)
{
path = Environment.GetRootedPath(path);
var parentPath = Directory.GetParent(path);
if (!parentPath.Exists)
Directory.CreateDirectory(path: parentPath.FullName);

File.WriteAllText(path, contents: ToSvg());
}
}

/// <summary>
/// A badge builder that implements the Badge API within PSRule.
/// </summary>
Expand Down
25 changes: 25 additions & 0 deletions src/PSRule/Badges/BadgeType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace PSRule.Badges;

/// <summary>
/// The type of badge.
/// </summary>
public enum BadgeType
{
/// <summary>
/// A badge that reports an unknown state.
/// </summary>
Unknown = 0,

/// <summary>
/// A badge reporting a successful state.
/// </summary>
Success = 1,

/// <summary>
/// A badge reporting a failed state.
/// </summary>
Failure = 2
}
20 changes: 20 additions & 0 deletions src/PSRule/Badges/IBadge.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace PSRule.Badges;

/// <summary>
/// An instance of a badge created by the badge API.
/// </summary>
public interface IBadge
{
/// <summary>
/// Get the badge as SVG text content.
/// </summary>
string ToSvg();

/// <summary>
/// Write the SVG badge content directly to disk.
/// </summary>
void ToFile(string path);
}
35 changes: 35 additions & 0 deletions src/PSRule/Badges/IBadgeBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using PSRule.Pipeline;

namespace PSRule.Badges;

/// <summary>
/// A builder for the badge API.
/// </summary>
public interface IBadgeBuilder
{
/// <summary>
/// Create a badge for the worst case of an analyzed object.
/// </summary>
/// <param name="result">A single result. The worst case for all records of an object is used for the badge.</param>
/// <returns>An instance of a badge.</returns>
IBadge Create(InvokeResult result);

/// <summary>
/// Create a badge for the worst case of all analyzed objects.
/// </summary>
/// <param name="result">A enumeration of results. The worst case from all results is used for the badge.</param>
/// <returns>An instance of a badge.</returns>
IBadge Create(IEnumerable<InvokeResult> result);

/// <summary>
/// Create a custom badge.
/// </summary>
/// <param name="title">The left badge text.</param>
/// <param name="type">Determines if the result is Unknown, Success, or Failure.</param>
/// <param name="label">The right badge text.</param>
/// <returns>An instance of a badge.</returns>
IBadge Create(string title, BadgeType type, string label);
}
3 changes: 2 additions & 1 deletion src/PSRule/Common/ResourceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ internal static bool Match(this IResourceFilter filter, Baseline resource)

internal static bool IsLocalScope(this IResource resource)
{
return string.IsNullOrEmpty(resource.Source.Module);
return resource != null &&
(string.IsNullOrEmpty(resource.Id.Scope) || resource.Id.Scope == ".");
}

internal static IEnumerable<ResourceId> GetIds(this IResource resource)
Expand Down
10 changes: 0 additions & 10 deletions src/PSRule/Configuration/BindingOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,9 @@
// Licensed under the MIT License.

using System.ComponentModel;
using System.Diagnostics;

namespace PSRule.Configuration;

internal static class BindingOptionExtensions
{
[DebuggerStepThrough]
public static StringComparer GetComparer(this BindingOption option)
{
return option.IgnoreCase.GetValueOrDefault(BindingOption.Default.IgnoreCase.Value) ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal;
}
}

/// <summary>
/// Options that affect property binding of TargetName and TargetType.
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions src/PSRule/Configuration/BindingOptionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Diagnostics;

namespace PSRule.Configuration;

internal static class BindingOptionExtensions
{
[DebuggerStepThrough]
public static StringComparer GetComparer(this BindingOption option)
{
return option.IgnoreCase.GetValueOrDefault(BindingOption.Default.IgnoreCase.Value) ? StringComparer.OrdinalIgnoreCase : StringComparer.Ordinal;
}
}
Loading

0 comments on commit cdc08bb

Please sign in to comment.