Skip to content

Commit

Permalink
Class clean up and docs #1186 (#1327)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored Oct 26, 2022
1 parent 644b014 commit 935d485
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG-v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ What's changed since v2.5.3:
[#1283](https://github.com/microsoft/PSRule/pull/1283)
- Bump PSScriptAnalyzer to v1.21.0.
[#1318](https://github.com/microsoft/PSRule/pull/1318)
- Class clean up and documentation by @BernieWhite.
[#1186](https://github.com/microsoft/PSRule/issues/1186)

## v2.5.3

Expand Down
59 changes: 59 additions & 0 deletions src/PSRule.Types/Data/SemanticVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@

namespace PSRule.Data
{
/// <summary>
/// A semantic version constraint.
/// </summary>
public interface IVersionConstraint
{
/// <summary>
/// Determines if the semantic version meets the requirments of the constraint.
/// </summary>
bool Equals(SemanticVersion.Version version);
}

Expand All @@ -36,6 +42,9 @@ public static class SemanticVersion
private const string FLAG_PRERELEASE = "@prerelease ";
private const string FLAG_PRE = "@pre ";

/// <summary>
/// A comparison operation for a version constraint.
/// </summary>
[Flags]
internal enum ComparisonOperator
{
Expand Down Expand Up @@ -76,10 +85,14 @@ internal enum ConstraintModifier
Prerelease = 1
}

/// <summary>
/// A semantic version constraint.
/// </summary>
public sealed class VersionConstraint : IVersionConstraint
{
private List<ConstraintExpression> _Constraints;

/// <inheritdoc/>
public bool Equals(Version version)
{
if (_Constraints == null || _Constraints.Count == 0)
Expand Down Expand Up @@ -311,12 +324,34 @@ private static bool IsStable(PR prid)
}
}

/// <summary>
/// A semantic version.
/// </summary>
public sealed class Version : IComparable<Version>, IEquatable<Version>
{
/// <summary>
/// The major part of the version.
/// </summary>
public readonly int Major;

/// <summary>
/// The minor part of the version.
/// </summary>
public readonly int Minor;

/// <summary>
/// The patch part of the version.
/// </summary>
public readonly int Patch;

/// <summary>
/// The pre-release part of the version.
/// </summary>
public readonly PR Prerelease;

/// <summary>
/// The build part of the version.
/// </summary>
public readonly string Build;

internal Version(int major, int minor, int patch, PR prerelease, string build)
Expand All @@ -328,6 +363,9 @@ internal Version(int major, int minor, int patch, PR prerelease, string build)
Build = build;
}

/// <summary>
/// Try to parse a semantic version from a string.
/// </summary>
public static bool TryParse(string value, out Version version)
{
return TryParseVersion(value, out version);
Expand Down Expand Up @@ -360,19 +398,28 @@ public override int GetHashCode()
}
}

/// <summary>
/// Compare the version against another version.
/// </summary>
public bool Equals(Version other)
{
return other != null &&
Equals(other.Major, other.Minor, other.Patch);
}

/// <summary>
/// Compare the version against another version based on major.minor.patch.
/// </summary>
public bool Equals(int major, int minor, int patch)
{
return major == Major &&
minor == Minor &&
patch == Patch;
}

/// <summary>
/// Compare the version against another version.
/// </summary>
public int CompareTo(Version other)
{
if (other == null)
Expand All @@ -391,6 +438,9 @@ public int CompareTo(Version other)
}
}

/// <summary>
/// A semantic version pre-release identifier.
/// </summary>
[DebuggerDisplay("{Value}")]
public sealed class PR
{
Expand All @@ -411,10 +461,19 @@ internal PR(string value)
_Identifiers = string.IsNullOrEmpty(value) ? null : value.Split(SEPARATORS, StringSplitOptions.RemoveEmptyEntries);
}

/// <summary>
/// The string value of a pre-release identifier.
/// </summary>
public string Value { get; }

/// <summary>
/// Is the pre-release identifier empty, indicating a stable release.
/// </summary>
public bool Stable => _Identifiers == null;

/// <summary>
/// Compare the pre-release identifer to another pre-release identifier.
/// </summary>
public int CompareTo(PR pr)
{
if (pr == null || pr.Stable)
Expand Down
12 changes: 12 additions & 0 deletions src/PSRule/Definitions/IDependencyTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,24 @@

namespace PSRule.Definitions
{
/// <summary>
/// An object that relies on a dependency chain.
/// </summary>
public interface IDependencyTarget
{
/// <summary>
/// The unique identifier of the resource.
/// </summary>
ResourceId Id { get; }

/// <summary>
/// A unique reference for the resource.
/// </summary>
ResourceId? Ref { get; }

/// <summary>
/// Additional aliases for the resource.
/// </summary>
ResourceId[] Alias { get; }

/// <summary>
Expand Down
1 change: 0 additions & 1 deletion src/PSRule/Pipeline/Output/NUnit3OutputWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
Expand Down
Loading

0 comments on commit 935d485

Please sign in to comment.