Skip to content

Commit

Permalink
Code quality updates (#1784)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Mar 30, 2024
1 parent 7203d44 commit bb67aab
Show file tree
Hide file tree
Showing 28 changed files with 590 additions and 463 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup .NET
uses: actions/setup-dotnet@v4
Expand Down
7 changes: 7 additions & 0 deletions PSRule.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PSRule.Tool.Tests", "tests\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PSRule.CommandLine", "src\PSRule.CommandLine\PSRule.CommandLine.csproj", "{9A556814-8E9D-4C76-8F6D-1AF2DA23A9E0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PSRule.CommandLine.Tests", "tests\PSRule.CommandLine.Tests\PSRule.CommandLine.Tests.csproj", "{C25E2FC1-E306-4D99-925C-15E5DD51F6A2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -82,13 +84,18 @@ Global
{9A556814-8E9D-4C76-8F6D-1AF2DA23A9E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9A556814-8E9D-4C76-8F6D-1AF2DA23A9E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9A556814-8E9D-4C76-8F6D-1AF2DA23A9E0}.Release|Any CPU.Build.0 = Release|Any CPU
{C25E2FC1-E306-4D99-925C-15E5DD51F6A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C25E2FC1-E306-4D99-925C-15E5DD51F6A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C25E2FC1-E306-4D99-925C-15E5DD51F6A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C25E2FC1-E306-4D99-925C-15E5DD51F6A2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{D3488CE2-779F-4474-B38A-F894A4B689F7} = {E0EA0CBA-96C5-4447-8B69-BC13EF0D7A4A}
{DA46C891-08F1-4D01-9F98-1F8BB10CAFEC} = {E0EA0CBA-96C5-4447-8B69-BC13EF0D7A4A}
{C25E2FC1-E306-4D99-925C-15E5DD51F6A2} = {E0EA0CBA-96C5-4447-8B69-BC13EF0D7A4A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {533491EB-BAE9-472E-B57F-A675ECD335B5}
Expand Down
10 changes: 6 additions & 4 deletions src/PSRule/Common/GitHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ private static string GetDiffArgs(string target, string source, string filter, s
return $"diff --diff-filter={filter} --ignore-submodules=all --name-only --no-renames {target}";
}

private static bool TryReadHead(string path, out string value)
internal static bool TryReadHead(string path, out string value)
{
value = null;
return TryGitFile(path, GIT_HEAD, out var filePath) && TryCommit(filePath, out value, out _);
}

private static bool TryReadCommit(string path, out string value)
internal static bool TryReadCommit(string path, out string value)
{
value = null;
if (!TryGitFile(path, GIT_HEAD, out var filePath))
Expand All @@ -182,15 +182,17 @@ private static bool TryReadCommit(string path, out string value)

private static bool TryGitFile(string path, string file, out string filePath)
{
path ??= Environment.GetRootedBasePath(GIT_DEFAULT_PATH);
filePath = Path.Combine(path, file);
filePath = Path.Combine(Environment.GetRootedBasePath(path ?? GIT_DEFAULT_PATH), file);
return File.Exists(filePath);
}

private static bool TryCommit(string path, out string value, out bool isRef)
{
value = null;
isRef = false;
if (!File.Exists(path))
return false;

var lines = File.ReadAllLines(path);
if (lines == null || lines.Length == 0)
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/PSRule/Configuration/PSRuleOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public static PSRuleOption FromDefault()
}

/// <summary>
/// Merge two option instances by repacing any unset properties from <paramref name="o1"/> with <paramref name="o2"/> values.
/// Merge two option instances by replacing any unset properties from <paramref name="o1"/> with <paramref name="o2"/> values.
/// Values from <paramref name="o1"/> that are set are not overridden.
/// </summary>
/// <returns>A new <see cref="PSRuleOption"/> instance combining options from both instances.</returns>
Expand Down
2 changes: 1 addition & 1 deletion src/PSRule/PSRule.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Resources\DocumentStrings.es-us.resx">
<EmbeddedResource Update="Resources\DocumentStrings.es-US.resx">
<Generator>ResXFileCodeGenerator</Generator>
</EmbeddedResource>
<EmbeddedResource Update="Resources\DocumentStrings.es.resx">
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/PSRule/Pipeline/CommandLineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private static void LoadModules(SourcePipelineBuilder builder, string[] module,
builder.ModuleByName(module[i], version);
}

for (var i = 0; option.Include.Module != null && i < option.Include.Module.Length; i++)
for (var i = 0; option?.Include?.Module != null && i < option.Include.Module.Length; i++)
{
var version = file != null && file.Modules.TryGetValue(option.Include.Module[i], out var entry) ? entry.Version.ToString() : null;
builder.ModuleByName(option.Include.Module[i], version);
Expand Down
21 changes: 0 additions & 21 deletions src/PSRule/Pipeline/GetRuleHelpPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,6 @@

namespace PSRule.Pipeline;

/// <summary>
/// A helper to build a pipeline for getting help from rules.
/// </summary>
public interface IHelpPipelineBuilder : IPipelineBuilder
{
/// <summary>
/// Get the full help output for a rule.
/// </summary>
void Full();

/// <summary>
/// Open or show online help for a rule if it exists.
/// </summary>
void Online();

/// <summary>
/// Filter by name.
/// </summary>
void Name(string[] name);
}

internal sealed class GetRuleHelpPipelineBuilder : PipelineBuilderBase, IHelpPipelineBuilder
{
private bool _Full;
Expand Down
11 changes: 0 additions & 11 deletions src/PSRule/Pipeline/GetRulePipelineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@

namespace PSRule.Pipeline;

/// <summary>
/// A helper to build a get pipeline.
/// </summary>
public interface IGetPipelineBuilder : IPipelineBuilder
{
/// <summary>
/// Determines if the returned rules also include rule dependencies.
/// </summary>
void IncludeDependencies();
}

/// <summary>
/// A helper to construct a get pipeline.
/// </summary>
Expand Down
124 changes: 0 additions & 124 deletions src/PSRule/Pipeline/GetTargetPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,133 +2,9 @@
// Licensed under the MIT License.

using System.Management.Automation;
using PSRule.Configuration;
using PSRule.Options;

namespace PSRule.Pipeline;

/// <summary>
/// A helper to build a pipeline to return target objects.
/// </summary>
public interface IGetTargetPipelineBuilder : IPipelineBuilder
{
/// <summary>
/// Specifies a path for reading input objects from disk.
/// </summary>
void InputPath(string[] path);
}

/// <summary>
/// A helper to construct the pipeline for Assert-PSRule.
/// </summary>
internal sealed class GetTargetPipelineBuilder : PipelineBuilderBase, IGetTargetPipelineBuilder
{
private InputPathBuilder _InputPath;

internal GetTargetPipelineBuilder(Source[] source, IHostContext hostContext)
: base(source, hostContext)
{
_InputPath = null;
}

/// <inheritdoc/>
public override IPipelineBuilder Configure(PSRuleOption option)
{
if (option == null)
return this;

base.Configure(option);

Option.Output = new OutputOption();
Option.Output.Culture = GetCulture(option.Output.Culture);

ConfigureBinding(option);
Option.Requires = new RequiresOption(option.Requires);

return this;
}

/// <inheritdoc/>
public void InputPath(string[] path)
{
if (path == null || path.Length == 0)
return;

PathFilter required = null;
if (TryChangedFiles(out var files))
{
required = PathFilter.Create(Environment.GetWorkingPath(), path);
path = files;
}

var builder = new InputPathBuilder(GetOutput(), Environment.GetWorkingPath(), "*", GetInputFilter(), required);
builder.Add(path);
_InputPath = builder;
}

/// <inheritdoc/>
public override IPipeline Build(IPipelineWriter writer = null)
{
return new GetTargetPipeline(PrepareContext(null, null, null), PrepareReader(), writer ?? PrepareWriter());
}

/// <inheritdoc/>
protected override PipelineInputStream PrepareReader()
{
if (!string.IsNullOrEmpty(Option.Input.ObjectPath))
{
AddVisitTargetObjectAction((sourceObject, next) =>
{
return PipelineReceiverActions.ReadObjectPath(sourceObject, next, Option.Input.ObjectPath, true);
});
}

if (Option.Input.Format == InputFormat.Yaml)
{
AddVisitTargetObjectAction((sourceObject, next) =>
{
return PipelineReceiverActions.ConvertFromYaml(sourceObject, next);
});
}
else if (Option.Input.Format == InputFormat.Json)
{
AddVisitTargetObjectAction((sourceObject, next) =>
{
return PipelineReceiverActions.ConvertFromJson(sourceObject, next);
});
}
else if (Option.Input.Format == InputFormat.Markdown)
{
AddVisitTargetObjectAction((sourceObject, next) =>
{
return PipelineReceiverActions.ConvertFromMarkdown(sourceObject, next);
});
}
else if (Option.Input.Format == InputFormat.PowerShellData)
{
AddVisitTargetObjectAction((sourceObject, next) =>
{
return PipelineReceiverActions.ConvertFromPowerShellData(sourceObject, next);
});
}
else if (Option.Input.Format == InputFormat.File)
{
AddVisitTargetObjectAction((sourceObject, next) =>
{
return PipelineReceiverActions.ConvertFromGitHead(sourceObject, next);
});
}
else if (Option.Input.Format == InputFormat.Detect && _InputPath != null)
{
AddVisitTargetObjectAction((sourceObject, next) =>
{
return PipelineReceiverActions.DetectInputFormat(sourceObject, next);
});
}
return new PipelineInputStream(VisitTargetObject, _InputPath, GetInputObjectSourceFilter());
}
}

/// <summary>
/// A pipeline that gets target objects through the pipeline.
/// </summary>
Expand Down
118 changes: 118 additions & 0 deletions src/PSRule/Pipeline/GetTargetPipelineBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using PSRule.Configuration;
using PSRule.Options;

namespace PSRule.Pipeline;

/// <summary>
/// A helper to construct the pipeline for Assert-PSRule.
/// </summary>
internal sealed class GetTargetPipelineBuilder : PipelineBuilderBase, IGetTargetPipelineBuilder
{
private InputPathBuilder _InputPath;

internal GetTargetPipelineBuilder(Source[] source, IHostContext hostContext)
: base(source, hostContext)
{
_InputPath = null;
}

/// <inheritdoc/>
public override IPipelineBuilder Configure(PSRuleOption option)
{
if (option == null)
return this;

base.Configure(option);

Option.Output = new OutputOption();
Option.Output.Culture = GetCulture(option.Output.Culture);

ConfigureBinding(option);
Option.Requires = new RequiresOption(option.Requires);

return this;
}

/// <inheritdoc/>
public void InputPath(string[] path)
{
if (path == null || path.Length == 0)
return;

PathFilter required = null;
if (TryChangedFiles(out var files))
{
required = PathFilter.Create(Environment.GetWorkingPath(), path);
path = files;
}

var builder = new InputPathBuilder(GetOutput(), Environment.GetWorkingPath(), "*", GetInputFilter(), required);
builder.Add(path);
_InputPath = builder;
}

/// <inheritdoc/>
public override IPipeline Build(IPipelineWriter writer = null)
{
return new GetTargetPipeline(PrepareContext(null, null, null), PrepareReader(), writer ?? PrepareWriter());
}

/// <inheritdoc/>
protected override PipelineInputStream PrepareReader()
{
if (!string.IsNullOrEmpty(Option.Input.ObjectPath))
{
AddVisitTargetObjectAction((sourceObject, next) =>
{
return PipelineReceiverActions.ReadObjectPath(sourceObject, next, Option.Input.ObjectPath, true);
});
}

if (Option.Input.Format == InputFormat.Yaml)
{
AddVisitTargetObjectAction((sourceObject, next) =>
{
return PipelineReceiverActions.ConvertFromYaml(sourceObject, next);
});
}
else if (Option.Input.Format == InputFormat.Json)
{
AddVisitTargetObjectAction((sourceObject, next) =>
{
return PipelineReceiverActions.ConvertFromJson(sourceObject, next);
});
}
else if (Option.Input.Format == InputFormat.Markdown)
{
AddVisitTargetObjectAction((sourceObject, next) =>
{
return PipelineReceiverActions.ConvertFromMarkdown(sourceObject, next);
});
}
else if (Option.Input.Format == InputFormat.PowerShellData)
{
AddVisitTargetObjectAction((sourceObject, next) =>
{
return PipelineReceiverActions.ConvertFromPowerShellData(sourceObject, next);
});
}
else if (Option.Input.Format == InputFormat.File)
{
AddVisitTargetObjectAction((sourceObject, next) =>
{
return PipelineReceiverActions.ConvertFromGitHead(sourceObject, next);
});
}
else if (Option.Input.Format == InputFormat.Detect && _InputPath != null)
{
AddVisitTargetObjectAction((sourceObject, next) =>
{
return PipelineReceiverActions.DetectInputFormat(sourceObject, next);
});
}
return new PipelineInputStream(VisitTargetObject, _InputPath, GetInputObjectSourceFilter());
}
}
Loading

0 comments on commit bb67aab

Please sign in to comment.