Skip to content

Commit

Permalink
Framework bump from netstandard2.1 to net8.0 (#15)
Browse files Browse the repository at this point in the history
* Updated target framework & refactored
  • Loading branch information
Scandal-UK authored Jan 24, 2024
1 parent db0ffbc commit cf3e82c
Show file tree
Hide file tree
Showing 47 changed files with 2,502 additions and 2,507 deletions.
2 changes: 1 addition & 1 deletion src/ConsoleTestQuickCompare/ConsoleTestQuickCompare.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
1 change: 1 addition & 0 deletions src/ConsoleTestQuickCompare/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace ConsoleTestQuickCompare;
using System.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
using QuickCompareModel;
using QuickCompareModel.Models;

/// <summary> Main program loop. </summary>
internal static class Program
Expand Down
1 change: 1 addition & 0 deletions src/ConsoleTestQuickCompare/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace ConsoleTestQuickCompare;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using QuickCompareModel;
using QuickCompareModel.Models;

/// <summary> Initialises the dependency injection container from app settings. </summary>
internal class Startup
Expand Down
149 changes: 76 additions & 73 deletions src/QuickCompareModel/DatabaseDifferences/BaseDifference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,97 +2,100 @@
// Copyright (c) Dan Ware. All rights reserved.
// </copyright>

namespace QuickCompareModel.DatabaseDifferences
namespace QuickCompareModel.DatabaseDifferences;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

/// <summary> Model to represent the most basic element and track the existence across two databases. </summary>
/// <remarks>
/// Initialises a new instance of the <see cref="BaseDifference"/> class
/// with values determining whether the item exists in each database.
/// </remarks>
/// <param name="existsInDatabase1">Value indicating whether the item exists in database 1.</param>
/// <param name="existsInDatabase2">Value indicating whether the item exists in database 2.</param>
public partial class BaseDifference(bool existsInDatabase1, bool existsInDatabase2)
{
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
/// <summary> Whitespace indentation used for output text. </summary>
protected const string TabIndent = " ";

/// <summary> Model to represent the most basic element and track the existence across two databases. </summary>
public class BaseDifference
/// <summary> Gets or sets a value indicating whether the item exists in database 1. </summary>
public bool ExistsInDatabase1 { get; set; } = existsInDatabase1;

/// <summary> Gets or sets a value indicating whether the item exists in database 2. </summary>
public bool ExistsInDatabase2 { get; set; } = existsInDatabase2;

/// <summary> Gets a value indicating whether the item exists in both databases. </summary>
public bool ExistsInBothDatabases => this.ExistsInDatabase1 && this.ExistsInDatabase2;

/// <summary> Gets a value indicating whether there are any differences. </summary>
public virtual bool IsDifferent => !this.ExistsInBothDatabases;

/// <summary> A helper method to trim whitespace and comments from text to reduce false-positive results. </summary>
/// <param name="definition">Text to modify.</param>
/// <param name="stripWhiteSpace">Value indicating where to reduce all whitespace to a single character.</param>
/// <returns>Modified text ready for comparison.</returns>
public static string CleanDefinitionText(string definition, bool stripWhiteSpace)
{
/// <summary> Whitespace indentation used for output text. </summary>
protected const string TabIndent = " ";

/// <summary>
/// Initialises a new instance of the <see cref="BaseDifference"/> class
/// with values determining whether the item exists in each database.
/// </summary>
/// <param name="existsInDatabase1">Value indicating whether the item exists in database 1.</param>
/// <param name="existsInDatabase2">Value indicating whether the item exists in database 2.</param>
public BaseDifference(bool existsInDatabase1, bool existsInDatabase2)
if (string.IsNullOrWhiteSpace(definition))
{
this.ExistsInDatabase1 = existsInDatabase1;
this.ExistsInDatabase2 = existsInDatabase2;
return string.Empty;
}

/// <summary> Gets or sets a value indicating whether the item exists in database 1. </summary>
public bool ExistsInDatabase1 { get; set; }
definition = StripMultiLineComments(definition);
definition = StripSingleLineComments(definition);

/// <summary> Gets or sets a value indicating whether the item exists in database 2. </summary>
public bool ExistsInDatabase2 { get; set; }
if (stripWhiteSpace)
{
definition = StripCommaWhitespace(definition);
definition = NormaliseCommas(definition);
definition = ReduceWhitespaceToSingleCharacter(definition);
}

/// <summary> Gets a value indicating whether the item exists in both databases. </summary>
public bool ExistsInBothDatabases => this.ExistsInDatabase1 && this.ExistsInDatabase2;
return definition.Trim();
}

/// <summary> Gets a value indicating whether there are any differences. </summary>
public virtual bool IsDifferent => !this.ExistsInBothDatabases;
/// <summary> Gets a text description of the difference or returns an empty string if no difference is detected. </summary>
/// <returns> Difference description. </returns>
public override string ToString() => this.ExistsInBothDatabases ? string.Empty : $"does not exist in database {(this.ExistsInDatabase1 ? 2 : 1)}\r\n";

/// <summary> A helper method to list difference values for subsections. </summary>
/// <typeparam name="T">Implementation of <see cref="BaseDifference"/>.</typeparam>
/// <param name="source">Difference collection.</param>
/// <param name="name">Name of subsection.</param>
/// <returns>Text output for subsection.</returns>
protected static string GetSubSectionDifferenceOutput<T>(Dictionary<string, T> source, string name)
where T : BaseDifference
{
var section = new StringBuilder();

/// <summary> A helper method to trim whitespace and comments from text to reduce false-positive results. </summary>
/// <param name="definition">Text to modify.</param>
/// <param name="stripWhiteSpace">Value indicating where to reduce all whitespace to a single character.</param>
/// <returns>Modified text ready for comparison.</returns>
public static string CleanDefinitionText(string definition, bool stripWhiteSpace)
foreach (var prop in source.Where(x => x.Value.IsDifferent))
{
if (string.IsNullOrWhiteSpace(definition))
{
return string.Empty;
}

definition = StripMultiLineComments(definition);
definition = StripSingleLineComments(definition);

if (stripWhiteSpace)
{
definition = StripCommaWhitespace(definition);
definition = NormaliseCommas(definition);
definition = ReduceWhitespaceToSingleCharacter(definition);
}

return definition.Trim();
section.Append($"{TabIndent}{name}: {prop.Key} {prop.Value}");
}

/// <summary> Gets a text description of the difference or returns an empty string if no difference is detected. </summary>
/// <returns> Difference description. </returns>
public override string ToString() => this.ExistsInBothDatabases ? string.Empty : $"does not exist in database {(this.ExistsInDatabase1 ? 2 : 1)}\r\n";

/// <summary> A helper method to list difference values for subsections. </summary>
/// <typeparam name="T">Implementation of <see cref="BaseDifference"/>.</typeparam>
/// <param name="source">Difference collection.</param>
/// <param name="name">Name of subsection.</param>
/// <returns>Text output for subsection.</returns>
protected static string GetSubSectionDifferenceOutput<T>(Dictionary<string, T> source, string name)
where T : BaseDifference
{
var section = new StringBuilder();
return section.ToString();
}

foreach (var prop in source.Where(x => x.Value.IsDifferent))
{
section.Append($"{TabIndent}{name}: {prop.Key} {prop.Value}");
}
private static string StripMultiLineComments(string input) => MultilineCommentRegex().Replace(input, string.Empty);

return section.ToString();
}
private static string StripSingleLineComments(string input) => SingleLineCommentRegex().Replace(input, string.Empty);

private static string StripMultiLineComments(string input) => Regex.Replace(input, @"/\*[^*]*\*+([^/*][^*]*\*+)*/", string.Empty);
private static string StripCommaWhitespace(string input) => CommaWitespaceRegex().Replace(input, ",");

private static string StripSingleLineComments(string input) => Regex.Replace(input, @"(--)([^\r\n]+)", string.Empty);
private static string NormaliseCommas(string input) => CommaRegex().Replace(input, ", ");

private static string StripCommaWhitespace(string input) => Regex.Replace(input, @"\s*,\s*", ",");
private static string ReduceWhitespaceToSingleCharacter(string input) => WhitespaceRegex().Replace(input, " ");

private static string NormaliseCommas(string input) => Regex.Replace(input, @"[,]", ", ");
[GeneratedRegex(@"/\*[^*]*\*+([^/*][^*]*\*+)*/")] private static partial Regex MultilineCommentRegex();

private static string ReduceWhitespaceToSingleCharacter(string input) => Regex.Replace(input, @"[\s]+", " ");
}
[GeneratedRegex(@"(--)([^\r\n]+)")] private static partial Regex SingleLineCommentRegex();

[GeneratedRegex(@"\s*,\s*")] private static partial Regex CommaWitespaceRegex();

[GeneratedRegex(@"[,]")] private static partial Regex CommaRegex();

[GeneratedRegex(@"[\s]+")] private static partial Regex WhitespaceRegex();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,63 @@
// Copyright (c) Dan Ware. All rights reserved.
// </copyright>

namespace QuickCompareModel.DatabaseDifferences
{
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace QuickCompareModel.DatabaseDifferences;

/// <summary> Model to represent a complex database element and track the differences across two databases. </summary>
public class DatabaseObjectDifference : BaseDifference
{
/// <summary>
/// Initialises a new instance of the <see cref="DatabaseObjectDifference"/> class
/// with values determining whether the item exists in each database.
/// </summary>
/// <param name="existsInDatabase1">Value indicating whether the item exists in database 1.</param>
/// <param name="existsInDatabase2">Value indicating whether the item exists in database 2.</param>
public DatabaseObjectDifference(bool existsInDatabase1, bool existsInDatabase2)
: base(existsInDatabase1, existsInDatabase2)
{
}
using System.Collections.Generic;
using System.Linq;
using System.Text;

/// <summary> Gets or sets the body text of the object in database 1. </summary>
public string ObjectDefinition1 { get; set; }
/// <summary> Model to represent a complex database element and track the differences across two databases. </summary>
/// <param name="existsInDatabase1">Value indicating whether the item exists in database 1.</param>
/// <param name="existsInDatabase2">Value indicating whether the item exists in database 2.</param>
public class DatabaseObjectDifference(bool existsInDatabase1, bool existsInDatabase2) : BaseDifference(existsInDatabase1, existsInDatabase2)
{
/// <summary> Gets or sets the body text of the object in database 1. </summary>
public string ObjectDefinition1 { get; set; }

/// <summary> Gets or sets the body text of the object in database 2. </summary>
public string ObjectDefinition2 { get; set; }
/// <summary> Gets or sets the body text of the object in database 2. </summary>
public string ObjectDefinition2 { get; set; }

/// <summary> Gets or sets a set of models to represent extended properties and track the differences across two databases. </summary>
public Dictionary<string, ExtendedPropertyDifference> ExtendedPropertyDifferences { get; set; } = new Dictionary<string, ExtendedPropertyDifference>();
/// <summary> Gets or sets a set of models to represent extended properties and track the differences across two databases. </summary>
public Dictionary<string, ExtendedPropertyDifference> ExtendedPropertyDifferences { get; set; } = [];

/// <summary> Gets or sets a set of models to represent permissions and track the differences across two databases. </summary>
public Dictionary<string, BaseDifference> PermissionDifferences { get; set; } = new Dictionary<string, BaseDifference>();
/// <summary> Gets or sets a set of models to represent permissions and track the differences across two databases. </summary>
public Dictionary<string, BaseDifference> PermissionDifferences { get; set; } = [];

/// <summary> Gets a value indicating whether the body text is different. </summary>
public bool DefinitionsAreDifferent => CleanDefinitionText(this.ObjectDefinition1, true) != CleanDefinitionText(this.ObjectDefinition2, true);
/// <summary> Gets a value indicating whether the body text is different. </summary>
public bool DefinitionsAreDifferent => CleanDefinitionText(this.ObjectDefinition1, true) != CleanDefinitionText(this.ObjectDefinition2, true);

/// <summary> Gets a value indicating whether any differences have been tracked. </summary>
public override bool IsDifferent =>
base.IsDifferent ||
this.DefinitionsAreDifferent ||
this.PermissionDifferences.Values.Any(x => x.IsDifferent) ||
this.ExtendedPropertyDifferences.Values.Any(x => x.IsDifferent);
/// <summary> Gets a value indicating whether any differences have been tracked. </summary>
public override bool IsDifferent =>
base.IsDifferent ||
this.DefinitionsAreDifferent ||
this.PermissionDifferences.Values.Any(x => x.IsDifferent) ||
this.ExtendedPropertyDifferences.Values.Any(x => x.IsDifferent);

/// <summary> Gets a text description of the difference or returns an empty string if no difference is detected. </summary>
/// <returns> Difference description. </returns>
public override string ToString()
/// <summary> Gets a text description of the difference or returns an empty string if no difference is detected. </summary>
/// <returns> Difference description. </returns>
public override string ToString()
{
if (!this.IsDifferent)
{
if (!this.IsDifferent)
{
return string.Empty;
}
return string.Empty;
}

if (base.IsDifferent)
{
return base.ToString();
}
if (base.IsDifferent)
{
return base.ToString();
}

var sb = new StringBuilder("\r\n");
var sb = new StringBuilder("\r\n");

if (this.DefinitionsAreDifferent)
{
sb.AppendLine($"{TabIndent}Definitions are different");
}
if (this.DefinitionsAreDifferent)
{
sb.AppendLine($"{TabIndent}Definitions are different");
}

sb.Append(GetSubSectionDifferenceOutput(this.ExtendedPropertyDifferences, "Extended property"));
sb.Append(GetSubSectionDifferenceOutput(this.PermissionDifferences, "Permission"));
sb.Append(GetSubSectionDifferenceOutput(this.ExtendedPropertyDifferences, "Extended property"));
sb.Append(GetSubSectionDifferenceOutput(this.PermissionDifferences, "Permission"));

return sb.ToString();
}
return sb.ToString();
}
}
Loading

0 comments on commit cf3e82c

Please sign in to comment.