Skip to content

Commit

Permalink
Code quality updates (#1712)
Browse files Browse the repository at this point in the history
* Code quality updates

* Refactoring
  • Loading branch information
BernieWhite authored Dec 21, 2023
1 parent b65a56a commit 107eb69
Show file tree
Hide file tree
Showing 15 changed files with 281 additions and 23 deletions.
29 changes: 29 additions & 0 deletions src/PSRule.Badges/BadgeException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Runtime.Serialization;

namespace PSRule.Badges;

/// <summary>
/// A generic exception for handling of badges.
/// </summary>
[Serializable]
internal class BadgeException : Exception
{
public BadgeException()
{
}

public BadgeException(string message) : base(message)
{
}

public BadgeException(string message, Exception innerException) : base(message, innerException)
{
}

protected BadgeException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
14 changes: 10 additions & 4 deletions src/PSRule.Badges/BadgeResources.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Reflection;
using Newtonsoft.Json;
using PSRule.Badges.Resources;

namespace PSRule.Badges;

Expand All @@ -13,8 +14,8 @@ internal static class BadgeResources
{
private const string DEFAULT_CULTURE_RESOURCE = "PSRule.Badges.Resources.en.json";

private static char[] _Char;
private static double[] _Width;
private static char[]? _Char;
private static double[]? _Width;

/// <summary>
/// Load pre-calculated widths for characters.
Expand All @@ -29,6 +30,9 @@ private static void Load()
using var reader = new StreamReader(stream);
var json = reader.ReadToEnd();
var d = JsonConvert.DeserializeObject<object[][]>(json);
if (d == null)
throw new BadgeException(Messages.LoadException);

_Char = new char[d.Length];
_Width = new double[d.Length];
for (var i = 0; i < d.Length; i++)
Expand All @@ -52,14 +56,16 @@ private static double GetWidth(char c)
/// </summary>
private static double Find(char c)
{
if (_Char == null || _Width == null) return 0d;

var index = Array.BinarySearch(_Char, c);
return index >= 0 ? _Width[index] : 0d;
}

public static double Measure(string s)
{
var length = 0d;
for (var i = 0; i < s.Length; i++)
for (var i = 0; s != null && i < s.Length; i++)
length += GetWidth(s[i]);

return length;
Expand Down
16 changes: 16 additions & 0 deletions src/PSRule.Badges/PSRule.Badges.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<PackageId>Microsoft.PSRule.Badges</PackageId>
<OutputType>Library</OutputType>
<ProjectGuid>{309bed8b-4e60-4c42-a2b4-37a2e7ebef3f}</ProjectGuid>
<Nullable>enable</Nullable>
<PackageReadmeFile>README.md</PackageReadmeFile>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
</PropertyGroup>
Expand All @@ -22,4 +23,19 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup>
<Compile Update="Resources\Messages.Designer.cs">
<DependentUpon>Messages.resx</DependentUpon>
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
</Compile>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Update="Resources\Messages.resx">
<LastGenOutput>Messages.Designer.cs</LastGenOutput>
<Generator>ResXFileCodeGenerator</Generator>
</EmbeddedResource>
</ItemGroup>

</Project>
72 changes: 72 additions & 0 deletions src/PSRule.Badges/Resources/Messages.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 123 additions & 0 deletions src/PSRule.Badges/Resources/Messages.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="LoadException" xml:space="preserve">
<value>Failed to load badge resources.</value>
</data>
</root>
2 changes: 1 addition & 1 deletion src/PSRule/Pipeline/GetBaselinePipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal sealed class GetBaselinePipeline : RulePipeline
internal GetBaselinePipeline(
PipelineContext pipeline,
Source[] source,
PipelineReader reader,
PipelineInputStream reader,
IPipelineWriter writer,
IResourceFilter filter
)
Expand Down
2 changes: 1 addition & 1 deletion src/PSRule/Pipeline/GetRuleHelpPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ protected override PipelineWriter PrepareWriter()

internal sealed class GetRuleHelpPipeline : RulePipeline, IPipeline
{
internal GetRuleHelpPipeline(PipelineContext pipeline, Source[] source, PipelineReader reader, IPipelineWriter writer)
internal GetRuleHelpPipeline(PipelineContext pipeline, Source[] source, PipelineInputStream reader, IPipelineWriter writer)
: base(pipeline, source, reader, writer)
{
// Do nothing
Expand Down
2 changes: 1 addition & 1 deletion src/PSRule/Pipeline/GetRulePipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal sealed class GetRulePipeline : RulePipeline, IPipeline
internal GetRulePipeline(
PipelineContext pipeline,
Source[] source,
PipelineReader reader,
PipelineInputStream reader,
IPipelineWriter writer,
bool includeDependencies
)
Expand Down
6 changes: 3 additions & 3 deletions src/PSRule/Pipeline/GetTargetPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public override IPipeline Build(IPipelineWriter writer = null)
}

/// <inheritdoc/>
protected override PipelineReader PrepareReader()
protected override PipelineInputStream PrepareReader()
{
if (!string.IsNullOrEmpty(Option.Input.ObjectPath))
{
Expand Down Expand Up @@ -124,7 +124,7 @@ protected override PipelineReader PrepareReader()
return PipelineReceiverActions.DetectInputFormat(sourceObject, next);
});
}
return new PipelineReader(VisitTargetObject, _InputPath, GetInputObjectSourceFilter());
return new PipelineInputStream(VisitTargetObject, _InputPath, GetInputObjectSourceFilter());
}
}

Expand All @@ -133,7 +133,7 @@ protected override PipelineReader PrepareReader()
/// </summary>
internal sealed class GetTargetPipeline : RulePipeline
{
internal GetTargetPipeline(PipelineContext context, PipelineReader reader, IPipelineWriter writer)
internal GetTargetPipeline(PipelineContext context, PipelineInputStream reader, IPipelineWriter writer)
: base(context, null, reader, writer) { }

public override void Process(PSObject sourceObject)
Expand Down
4 changes: 2 additions & 2 deletions src/PSRule/Pipeline/InvokePipelineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ private static bool IsBlocked(string path)
}
}

protected override PipelineReader PrepareReader()
protected override PipelineInputStream PrepareReader()
{
if (!string.IsNullOrEmpty(Option.Input.ObjectPath))
{
Expand Down Expand Up @@ -201,7 +201,7 @@ protected override PipelineReader PrepareReader()
return PipelineReceiverActions.DetectInputFormat(sourceObject, next);
});
}
return new PipelineReader(VisitTargetObject, _InputPath, GetInputObjectSourceFilter());
return new PipelineInputStream(VisitTargetObject, _InputPath, GetInputObjectSourceFilter());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/PSRule/Pipeline/PipelineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,9 @@ protected string ResolveBaselineGroup(string name)
return baselines[0];
}

protected virtual PipelineReader PrepareReader()
protected virtual PipelineInputStream PrepareReader()
{
return new PipelineReader(null, null, GetInputObjectSourceFilter());
return new PipelineInputStream(null, null, GetInputObjectSourceFilter());
}

protected virtual PipelineWriter PrepareWriter()
Expand Down
Loading

0 comments on commit 107eb69

Please sign in to comment.