Skip to content

Commit

Permalink
Upgrade to include .net7.0 and netcore 3.1, and cleanup (#32)
Browse files Browse the repository at this point in the history
* Upgrade to include .net7.0 and netcore 3.1, and cleanup

* Switch away from multi framework targeting
  • Loading branch information
BenjaminMichaelis authored Dec 5, 2022
1 parent 76fc267 commit c28cbe8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 44 deletions.
2 changes: 1 addition & 1 deletion ListingManager.Tests/ListingInformationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ListingInformationTests
public void Constructor_GivenValidListings_PropertiesPopulatedSuccessfully(string listing,
int chapterNumber, int listingNumber, string suffix, string description)
{
ListingInformation listingInformation = new ListingInformation(listing);
ListingInformation listingInformation = new(listing);

Assert.AreEqual(chapterNumber, listingInformation.ChapterNumber);
Assert.AreEqual(listingNumber, listingInformation.ListingNumber);
Expand Down
4 changes: 2 additions & 2 deletions ListingManager.Tests/ListingManager.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net7.0</TargetFramework>

<IsPackable>false</IsPackable>

Expand All @@ -12,7 +12,7 @@

<ItemGroup>
<PackageReference Include="IntelliTect.TestTools.Console" Version="1.0.0-CI-20181030-214503" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="Polly" Version="7.2.3" />
Expand Down
10 changes: 5 additions & 5 deletions ListingManager.Tests/ListingManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,23 @@ public void GetAllExtraListings_ExtraListingsReturned()
[Ignore]
public void UpdateChapterListingNumbers_ListingsWithinListMissing_ListingsRenumbered()
{
List<string> filesToMake = new List<string>
List<string> filesToMake = new()
{
"Listing01.01.SpecifyingLiteralValues.cs",
"Listing01.02.cs",
"Listing01.04.cs",
"Listing01.06.Something.cs"
};

List<string> expectedFiles = new List<string>
List<string> expectedFiles = new()
{
"Listing01.01.SpecifyingLiteralValues.cs",
"Listing01.02.cs",
"Listing01.03.cs",
"Listing01.04.Something.cs"
};

List<string> toWrite = new List<string>
List<string> toWrite = new()
{
"namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter18.Listing18_01",
"{",
Expand Down Expand Up @@ -559,7 +559,7 @@ public void GenerateUnitTests_TestsGenerated()
{
string chapter = "Chapter01";

List<string> filesToCreate = new List<string>
List<string> filesToCreate = new()
{
@"Listing01.01.Something.cs",
@"Listing01.02A.cs",
Expand Down Expand Up @@ -609,7 +609,7 @@ private List<FileInfo> WriteFiles(DirectoryInfo targetDirectory, IEnumerable<str
IEnumerable<string>? toWrite)
{
List<string> filesToWrite = toWrite?.ToList() ?? new List<string>();
List<FileInfo> ret = new List<FileInfo>();
List<FileInfo> ret = new();
foreach (string file in fileNames)
{
ret.Add(WriteFile(targetDirectory, file, filesToWrite));
Expand Down
4 changes: 2 additions & 2 deletions ListingManager/ListingInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ListingInformation

public ListingInformation(string listingPath)
{
Regex regex = new Regex(@"Listing(\d{2}).(\d{2})([A-Za-z]*)(\.{1}(.*))?.cs$");
Regex regex = new(@"Listing(\d{2}).(\d{2})([A-Za-z]*)(\.{1}(.*))?.cs$");

var matches = regex.Match(listingPath);

Expand All @@ -31,7 +31,7 @@ public ListingInformation(string listingPath)
}
else
{
throw new ArgumentException(nameof(listingPath));
throw new ArgumentException("Listing information not successfully able to be parsed from listing path.", nameof(listingPath));
}
}
}
Expand Down
48 changes: 22 additions & 26 deletions ListingManager/ListingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static void UpdateChapterListingNumbers(string pathToChapter,
if (curListingData is null || !chapterOnly && !byFolder && listingNumber == curListingData.ListingNumber)
{
File.Copy(curListingData?.TemporaryPath, curListingData?.Path, true);
if (testListingData.Where(x => x?.ListingNumber == curListingData.ListingNumber && x.ListingSuffix == curListingData.ListingSuffix).FirstOrDefault() is ListingInformation currentTestListingData)
if (testListingData.FirstOrDefault(x => x?.ListingNumber == curListingData.ListingNumber && x.ListingSuffix == curListingData.ListingSuffix) is ListingInformation currentTestListingData)
{
File.Copy(currentTestListingData?.TemporaryPath, currentTestListingData?.Path, true);
}
Expand All @@ -127,14 +127,14 @@ public static void UpdateChapterListingNumbers(string pathToChapter,

UpdateListingNamespace(cur, listingChapterNumber,
completeListingNumber,
curListingData.ListingDescription, curListingData, verbose, preview);
curListingData.ListingDescription, verbose, preview);

if (testListingData.Where(x => x?.ListingNumber == curListingData.ListingNumber && x.ListingSuffix == curListingData.ListingSuffix).FirstOrDefault() is ListingInformation curTestListingData)
{
Console.Write("Updating test. ");
UpdateTestListingNamespace(curTestListingData.TemporaryPath, listingChapterNumber,
completeListingNumber,
curListingData.ListingDescription, curListingData, verbose, preview);
curListingData.ListingDescription, verbose, preview);
}

}
Expand All @@ -151,7 +151,7 @@ public static void UpdateChapterListingNumbers(string pathToChapter,
public static bool IsExtraListing(string path,
string regexNamespace = @".*Listing\d{2}\.\d{2}(A|B|C|D).*\.cs$")
{
Regex fileNameRegex = new Regex(regexNamespace);
Regex fileNameRegex = new(regexNamespace);

string directoryNameFull = Path.GetDirectoryName(path) ?? string.Empty;
string directoryName = Path.GetFileName(directoryNameFull);
Expand All @@ -166,16 +166,15 @@ public static bool IsExtraListing(string path,
/// <param name="chapterNumber">The chapter the listing belongs to</param>
/// <param name="listingNumber">The updated listing number</param>
/// <param name="listingData">The name of the listing to be included in the namespace/path</param>
/// <param name="curListingData"></param>
/// <param name="verbose">When true, enables verbose console output</param>
/// <param name="preview">When true, leaves files in place and only print console output</param>
private static void UpdateTestListingNamespace(string path, int chapterNumber, string listingNumber,
string listingData, ListingInformation curListingData, bool verbose = false, bool preview = false)
string listingData, bool verbose = false, bool preview = false)
{
string paddedChapterNumber = chapterNumber.ToString("00");

string regexSingleDigitListingWithSuffix = @"\d{1}[A-Za-z]";
string paddedListingNumber = "";
string paddedListingNumber;
if (Regex.IsMatch(listingNumber, regexSingleDigitListingWithSuffix))
{ //allows for keeping the original listing number with a suffix. e.g. "01A"
paddedListingNumber = listingNumber.PadLeft(3, '0');
Expand Down Expand Up @@ -217,9 +216,8 @@ private static void UpdateTestListingNamespace(string path, int chapterNumber, s
/// <param name="listingData">The name of the listing to be included in the namespace/path</param>
/// <param name="verbose">When true, enables verbose console output</param>
/// <param name="preview">When true, leaves files in place and only print console output</param>
/// <param name="curListingData">An instance of ListingInformation for the current listing.</param>
private static void UpdateListingNamespace(string path, int chapterNumber, string listingNumber,
string listingData, ListingInformation curListingData, bool verbose = false, bool preview = false)
string listingData, bool verbose = false, bool preview = false)
{
string paddedChapterNumber = chapterNumber.ToString("00");

Expand Down Expand Up @@ -265,41 +263,39 @@ private static void UpdateNamespaceOfPath(string path, string newNamespace, stri

string targetPath = Path.Combine(Path.GetDirectoryName(path) ?? string.Empty, newFileName) ?? path;

using (TextWriter textWriter = new StreamWriter(targetPath, true))
using TextWriter textWriter = new StreamWriter(targetPath, true);
foreach (string line in allLinesInFile)
{
foreach (string line in allLinesInFile)
if (line.StartsWith("namespace"))
{
if (line.StartsWith("namespace"))
if (line.TrimEnd().EndsWith(";"))
{
if (line.TrimEnd().EndsWith(";"))
{
textWriter.WriteLine("namespace " + newNamespace + ";");
}
else
{
textWriter.WriteLine("namespace " + newNamespace);
}
textWriter.WriteLine("namespace " + newNamespace + ";");
}
else
{
textWriter.WriteLine(line);
textWriter.WriteLine("namespace " + newNamespace);
}
}
else
{
textWriter.WriteLine(line);
}
}
}

public static bool GetPathToAccompanyingUnitTest(string listingPath, out string pathToTest)
{
string testDirectory = $"{Path.GetDirectoryName(listingPath)}.Tests";

Regex regex = new Regex(@"((Listing\d{2}\.\d{2})([A-Z]?)((\.Tests)?)).*\.cs.tmp$");
Regex regex = new(@"((Listing\d{2}\.\d{2})([A-Z]?)((\.Tests)?)).*\.cs.tmp$");

Match fileNameMatch = regex.Match(listingPath);

string testFileName = fileNameMatch.Success ? regex.Match(listingPath).Groups[1].Value : "";

Regex pathToTestRegex =
new Regex(Regex.Escape($"{testDirectory}{Path.DirectorySeparatorChar}{testFileName}")
new(Regex.Escape($"{testDirectory}{Path.DirectorySeparatorChar}{testFileName}")
+ @".*\.cs");

if (Directory.Exists(testDirectory))
Expand All @@ -326,12 +322,12 @@ private static string GetTestLayout(string chapterNumber, string listingNumber)
listingNumber.PadLeft(2, '0')) + TestBodyLayout;
}

private static string TestHeaderLayout =
private static readonly string TestHeaderLayout =
@"using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter{0}.Listing{0}_{1}.Tests";

private static string TestBodyLayout =
private static readonly string TestBodyLayout =
@"
{
[TestClass]
Expand Down Expand Up @@ -393,7 +389,7 @@ private static bool GenerateUnitTest(string pathToTest)
return false;
}

Regex getListingData = new Regex(@"Listing(\d{2})\.(\d{2})");
Regex getListingData = new(@"Listing(\d{2})\.(\d{2})");

var match = getListingData.Match(pathToTest);

Expand Down
12 changes: 4 additions & 8 deletions ListingManager/ListingManager.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<PackAsTool>true</PackAsTool>
<ToolCommandName>ListingManager</ToolCommandName>
<RepositoryType>git</RepositoryType>
Expand Down Expand Up @@ -32,19 +32,15 @@
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>
<ItemGroup>
<None Include="../README.md" Pack="true" PackagePath="\"/>
<SourceRoot Include="$(MSBuildThisFileDirectory)/"/>
<None Include="../README.md" Pack="true" PackagePath="\" />
<SourceRoot Include="$(MSBuildThisFileDirectory)/" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.CommandLine.DragonFruit" Version="0.4.0-alpha.22272.1" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>

0 comments on commit c28cbe8

Please sign in to comment.