From 4095f5b3eea28482c00eaf6f7be8d6748fa0778e Mon Sep 17 00:00:00 2001 From: Craig Treasure Date: Tue, 14 Nov 2023 11:46:32 -0800 Subject: [PATCH] Upgrade to .NET 8 (#214) This change upgrades the project to use the .NET 8 SDK and adds the `net8.0` target to all projects. I've also removed support for .NET 7 and updated the version to 2.0. It also addresses the new analyzer warnings introduced as well as conforms to new C# 12 feature recommendations imposed by the new analyzers. Using the new artifacts support for output. --- .editorconfig | 3 +++ .github/actions/install-tools/action.yml | 2 +- .github/workflows/workflow_build.yml | 6 ++--- .github/workflows/workflow_release.yml | 4 ++-- Directory.Build.props | 6 ++--- eng/DotNetAnalyzers.props | 2 +- eng/DotNetDefaults.props | 10 ++++++++- global.json | 5 +---- src/Directory.Packages.props | 2 +- src/SlnUp.Core/SlnUp.Core.csproj | 2 +- src/SlnUp.Core/VersionManager.g.cs | 5 +++-- src/SlnUp.Json/SlnUp.Json.csproj | 2 +- src/SlnUp/CLI/ArgumentParser.cs | 8 +++++++ src/SlnUp/SlnUp.csproj | 2 +- src/SlnUp/SolutionFile.cs | 2 +- .../Extensions/SetExtensions.cs | 12 ---------- .../VisualStudio.VersionScraper.csproj | 2 +- .../VisualStudioVersionDocScraper.cs | 14 ++++++------ .../Writers/CSharp/CSharpVersionWriter.cs | 3 +-- .../SlnUp.Core.Tests/SlnUp.Core.Tests.csproj | 2 +- .../SlnUp.Json.Tests/SlnUp.Json.Tests.csproj | 2 +- ... => VisualStudioVersionJsonHelperTests.cs} | 0 .../SlnUp.TestLibrary.csproj | 2 +- .../CLI/ProgramOptionsBinderTests.cs | 6 ++--- tests/SlnUp.Tests/CLI/ProgramOptionsTests.cs | 12 +++++----- tests/SlnUp.Tests/ProgramTests.cs | 22 ++++++++++--------- tests/SlnUp.Tests/SlnUp.Tests.csproj | 2 +- tests/SlnUp.Tests/SolutionFileBuilder.cs | 2 +- version.json | 2 +- 29 files changed, 74 insertions(+), 70 deletions(-) delete mode 100644 src/VisualStudio.VersionScraper/Extensions/SetExtensions.cs rename tests/SlnUp.Json.Tests/{VisualStudioVersionTests.cs => VisualStudioVersionJsonHelperTests.cs} (100%) diff --git a/.editorconfig b/.editorconfig index 1079929..bea1611 100644 --- a/.editorconfig +++ b/.editorconfig @@ -203,6 +203,9 @@ csharp_style_expression_bodied_operators = when_on_single_line # IDE0090: Use 'new(...)' csharp_style_implicit_object_creation_when_type_is_apparent = true +# IDE0290: Use Primary Constructors +csharp_style_prefer_primary_constructors = false + csharp_indent_labels = one_less_than_current csharp_space_around_binary_operators = before_and_after csharp_using_directive_placement = inside_namespace diff --git a/.github/actions/install-tools/action.yml b/.github/actions/install-tools/action.yml index cbacc76..398712c 100644 --- a/.github/actions/install-tools/action.yml +++ b/.github/actions/install-tools/action.yml @@ -8,7 +8,7 @@ runs: with: dotnet-version: | 6.x - 7.x + 8.x - name: Install .NET tools shell: pwsh diff --git a/.github/workflows/workflow_build.yml b/.github/workflows/workflow_build.yml index 7eb4478..2bc099e 100644 --- a/.github/workflows/workflow_build.yml +++ b/.github/workflows/workflow_build.yml @@ -55,7 +55,7 @@ jobs: uses: codecov/codecov-action@v3 with: name: codecov - directory: __test-results + directory: __artifacts/test-results fail_ci_if_error: true verbose: true @@ -63,10 +63,10 @@ jobs: uses: actions/upload-artifact@v3 with: name: output_${{ inputs.platform }} - path: __output + path: __artifacts/bin - name: Upload package artifact uses: actions/upload-artifact@v3 with: name: packages_${{ inputs.platform }} - path: __packages + path: __artifacts/package diff --git a/.github/workflows/workflow_release.yml b/.github/workflows/workflow_release.yml index e6f63de..8b46564 100644 --- a/.github/workflows/workflow_release.yml +++ b/.github/workflows/workflow_release.yml @@ -25,7 +25,7 @@ jobs: uses: actions/download-artifact@v3 with: name: packages_ubuntu - path: __packages + path: __artifacts/package - name: Push ${{ inputs.package-version }} to NuGet.org - run: nuget push __packages/NuGet/Release/SlnUp.*.nupkg -ApiKey ${{ secrets.NUGET_API_KEY }} -Source https://api.nuget.org/v3/index.json + run: nuget push __artifacts/package/release/SlnUp.*.nupkg -ApiKey ${{ secrets.NUGET_API_KEY }} -Source https://api.nuget.org/v3/index.json diff --git a/Directory.Build.props b/Directory.Build.props index f4b7dac..46d9629 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -2,11 +2,9 @@ $(MSBuildThisFileDirectory) - $(RepoRootPath) + true + $(RepoRootPath)__artifacts - - - diff --git a/eng/DotNetAnalyzers.props b/eng/DotNetAnalyzers.props index 8495bd2..10191c6 100644 --- a/eng/DotNetAnalyzers.props +++ b/eng/DotNetAnalyzers.props @@ -7,7 +7,7 @@ true AllEnabledByDefault true - 7.0 + 8.0 diff --git a/eng/DotNetDefaults.props b/eng/DotNetDefaults.props index 96330a4..472e22b 100644 --- a/eng/DotNetDefaults.props +++ b/eng/DotNetDefaults.props @@ -10,7 +10,7 @@ - 11.0 + latest + $(ArtifactsPath)/test-results/ + $(BaseTestResultsOutputPath)$(MSBuildProjectName)/ + $(BaseProjectTestResultsOutputPath) + $(BaseProjectTestResultsOutputPath) + + diff --git a/global.json b/global.json index 1d2535c..b7e3357 100644 --- a/global.json +++ b/global.json @@ -1,10 +1,7 @@ { "sdk": { - "version": "7.0.100", + "version": "8.0.100", "allowPrerelease": false, "rollForward": "latestFeature" - }, - "msbuild-sdks": { - "Treasure.Build.CentralBuildOutput" : "3.0.0" } } diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props index ac9c653..3065539 100644 --- a/src/Directory.Packages.props +++ b/src/Directory.Packages.props @@ -16,7 +16,7 @@ - + diff --git a/src/SlnUp.Core/SlnUp.Core.csproj b/src/SlnUp.Core/SlnUp.Core.csproj index 5331e25..9492432 100644 --- a/src/SlnUp.Core/SlnUp.Core.csproj +++ b/src/SlnUp.Core/SlnUp.Core.csproj @@ -1,7 +1,7 @@ - net6.0;net7.0 + net6.0;net8.0 diff --git a/src/SlnUp.Core/VersionManager.g.cs b/src/SlnUp.Core/VersionManager.g.cs index 5e44da9..68e90c5 100644 --- a/src/SlnUp.Core/VersionManager.g.cs +++ b/src/SlnUp.Core/VersionManager.g.cs @@ -1,6 +1,6 @@ //------------------------------------------------------------------------------ // -// This code was generated by a tool on 11/11/2023. +// This code was generated by a tool on 11/14/2023. // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -9,7 +9,7 @@ namespace SlnUp.Core; -[System.CodeDom.Compiler.GeneratedCodeAttribute("VisualStudio.VersionScraper", "1.4.0.0")] +[System.CodeDom.Compiler.GeneratedCodeAttribute("VisualStudio.VersionScraper", "2.0.0.0")] public partial class VersionManager { /// @@ -30,6 +30,7 @@ public static IReadOnlyList GetDefaultVersions() { return new [] { + new VisualStudioVersion(VisualStudioProduct.VisualStudio2022, Version.Parse("17.8.0"), Version.Parse("17.8.34309.116"), "LTSC 17.8", false), new VisualStudioVersion(VisualStudioProduct.VisualStudio2022, Version.Parse("17.7.6"), Version.Parse("17.7.34221.43"), "Current", false), new VisualStudioVersion(VisualStudioProduct.VisualStudio2022, Version.Parse("17.7.5"), Version.Parse("17.7.34202.233"), "Current", false), new VisualStudioVersion(VisualStudioProduct.VisualStudio2022, Version.Parse("17.7.4"), Version.Parse("17.7.34031.279"), "Current", false), diff --git a/src/SlnUp.Json/SlnUp.Json.csproj b/src/SlnUp.Json/SlnUp.Json.csproj index 122ec42..fbb8518 100644 --- a/src/SlnUp.Json/SlnUp.Json.csproj +++ b/src/SlnUp.Json/SlnUp.Json.csproj @@ -1,7 +1,7 @@ - net6.0;net7.0 + net6.0;net8.0 diff --git a/src/SlnUp/CLI/ArgumentParser.cs b/src/SlnUp/CLI/ArgumentParser.cs index 6e4b3d4..59221b4 100644 --- a/src/SlnUp/CLI/ArgumentParser.cs +++ b/src/SlnUp/CLI/ArgumentParser.cs @@ -7,6 +7,10 @@ internal static class ArgumentParser { private const string CannotParseArgumentOption = "Cannot parse argument '{0}' for option '{1}' as expected type '{2}'."; +#if NET8_0_OR_GREATER + private static readonly System.Text.CompositeFormat CannotParseArgumentOptionFormat = System.Text.CompositeFormat.Parse(CannotParseArgumentOption); +#endif + public static Version? ParseVersion(ArgumentResult result) { string tokenValue = result.Tokens.Single().Value; @@ -17,7 +21,11 @@ internal static class ArgumentParser result.ErrorMessage = string.Format( CultureInfo.InvariantCulture, +#if NET8_0_OR_GREATER + CannotParseArgumentOptionFormat, +#else CannotParseArgumentOption, +#endif tokenValue, result.Argument.Name, result.Argument.ValueType); diff --git a/src/SlnUp/SlnUp.csproj b/src/SlnUp/SlnUp.csproj index 09844ea..97fc0bd 100644 --- a/src/SlnUp/SlnUp.csproj +++ b/src/SlnUp/SlnUp.csproj @@ -2,7 +2,7 @@ Exe - net6.0;net7.0 + net6.0;net8.0 true slnup diff --git a/src/SlnUp/SolutionFile.cs b/src/SlnUp/SolutionFile.cs index a28570f..63abc99 100644 --- a/src/SlnUp/SolutionFile.cs +++ b/src/SlnUp/SolutionFile.cs @@ -97,7 +97,7 @@ internal void UpdateFileHeader(SolutionFileHeader fileHeader) }; } - List lines = this.fileSystem.File.ReadAllLines(this.filePath).ToList(); + List lines = [.. this.fileSystem.File.ReadAllLines(this.filePath)]; if (this.FileHeader.LastVisualStudioMajorVersion is null) { diff --git a/src/VisualStudio.VersionScraper/Extensions/SetExtensions.cs b/src/VisualStudio.VersionScraper/Extensions/SetExtensions.cs deleted file mode 100644 index f87a9d2..0000000 --- a/src/VisualStudio.VersionScraper/Extensions/SetExtensions.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace VisualStudio.VersionScraper.Extensions; - -internal static class SetExtensions -{ - public static void AddRange(this ISet collection, IEnumerable values) - { - foreach (T value in values) - { - collection.Add(value); - } - } -} diff --git a/src/VisualStudio.VersionScraper/VisualStudio.VersionScraper.csproj b/src/VisualStudio.VersionScraper/VisualStudio.VersionScraper.csproj index 4246afc..efa3e0c 100644 --- a/src/VisualStudio.VersionScraper/VisualStudio.VersionScraper.csproj +++ b/src/VisualStudio.VersionScraper/VisualStudio.VersionScraper.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net8.0 diff --git a/src/VisualStudio.VersionScraper/VisualStudioVersionDocScraper.cs b/src/VisualStudio.VersionScraper/VisualStudioVersionDocScraper.cs index 21c88c3..0852efe 100644 --- a/src/VisualStudio.VersionScraper/VisualStudioVersionDocScraper.cs +++ b/src/VisualStudio.VersionScraper/VisualStudioVersionDocScraper.cs @@ -9,8 +9,6 @@ using SlnUp.Core; using SlnUp.Core.Extensions; -using VisualStudio.VersionScraper.Extensions; - internal sealed class VisualStudioVersionDocScraper { private const string buildNumberColumnName = "Build Number"; @@ -48,10 +46,12 @@ public VisualStudioVersionDocScraper(bool useCache = true) /// . public IEnumerable ScrapeVisualStudioVersions() { - HashSet versions = new(); - versions.AddRange(this.ScrapeVisualStudioVersions(vsCurrentVersionsDocUrl, "VSCurrentVersionCache")); - versions.AddRange(this.ScrapeVisualStudioVersions(vs2019VersionsDocUrl, "VS2019VersionCache")); - versions.AddRange(this.ScrapeVisualStudioVersions(vs2017VersionsDocUrl, "VS2017VersionCache")); + HashSet versions = + [ + .. this.ScrapeVisualStudioVersions(vsCurrentVersionsDocUrl, "VSCurrentVersionCache"), + .. this.ScrapeVisualStudioVersions(vs2019VersionsDocUrl, "VS2019VersionCache"), + .. this.ScrapeVisualStudioVersions(vs2017VersionsDocUrl, "VS2017VersionCache"), + ]; return versions; } @@ -167,7 +167,7 @@ private static bool TryGetTableData( return false; } - List data = new(); + List data = []; foreach (HtmlNode row in rows) { diff --git a/src/VisualStudio.VersionScraper/Writers/CSharp/CSharpVersionWriter.cs b/src/VisualStudio.VersionScraper/Writers/CSharp/CSharpVersionWriter.cs index 8a21cff..64d79b5 100644 --- a/src/VisualStudio.VersionScraper/Writers/CSharp/CSharpVersionWriter.cs +++ b/src/VisualStudio.VersionScraper/Writers/CSharp/CSharpVersionWriter.cs @@ -1,6 +1,5 @@ namespace VisualStudio.VersionScraper.Writers.CSharp; -using System.Diagnostics.CodeAnalysis; using System.IO.Abstractions; using SlnUp.Core; @@ -16,6 +15,7 @@ internal sealed class CSharpVersionWriter private const string NamespaceName = "SlnUp.Core"; private readonly IFileSystem fileSystem; + public CSharpVersionWriter(IFileSystem fileSystem) => this.fileSystem = Argument.NotNull(fileSystem); public void WriteClassToFile(IEnumerable versions, string filePath) @@ -41,7 +41,6 @@ private static void WriteClass(CodeWriter writer, IEnumerable versions) { writer.WriteLines(@" diff --git a/tests/SlnUp.Core.Tests/SlnUp.Core.Tests.csproj b/tests/SlnUp.Core.Tests/SlnUp.Core.Tests.csproj index 39c1c62..860c3db 100644 --- a/tests/SlnUp.Core.Tests/SlnUp.Core.Tests.csproj +++ b/tests/SlnUp.Core.Tests/SlnUp.Core.Tests.csproj @@ -1,7 +1,7 @@ - net6.0;net7.0 + net6.0;net8.0 diff --git a/tests/SlnUp.Json.Tests/SlnUp.Json.Tests.csproj b/tests/SlnUp.Json.Tests/SlnUp.Json.Tests.csproj index 5cff69f..ddbd1b6 100644 --- a/tests/SlnUp.Json.Tests/SlnUp.Json.Tests.csproj +++ b/tests/SlnUp.Json.Tests/SlnUp.Json.Tests.csproj @@ -1,7 +1,7 @@ - net6.0;net7.0 + net6.0;net8.0 diff --git a/tests/SlnUp.Json.Tests/VisualStudioVersionTests.cs b/tests/SlnUp.Json.Tests/VisualStudioVersionJsonHelperTests.cs similarity index 100% rename from tests/SlnUp.Json.Tests/VisualStudioVersionTests.cs rename to tests/SlnUp.Json.Tests/VisualStudioVersionJsonHelperTests.cs diff --git a/tests/SlnUp.TestLibrary/SlnUp.TestLibrary.csproj b/tests/SlnUp.TestLibrary/SlnUp.TestLibrary.csproj index 82f1a95..d5f7f17 100644 --- a/tests/SlnUp.TestLibrary/SlnUp.TestLibrary.csproj +++ b/tests/SlnUp.TestLibrary/SlnUp.TestLibrary.csproj @@ -1,7 +1,7 @@ - net6.0;net7.0 + net6.0;net8.0 false diff --git a/tests/SlnUp.Tests/CLI/ProgramOptionsBinderTests.cs b/tests/SlnUp.Tests/CLI/ProgramOptionsBinderTests.cs index 9a7f6ec..21815ac 100644 --- a/tests/SlnUp.Tests/CLI/ProgramOptionsBinderTests.cs +++ b/tests/SlnUp.Tests/CLI/ProgramOptionsBinderTests.cs @@ -42,12 +42,12 @@ public void GetBoundValue(string? version, string? expectedPath, string? expecte Option pathOption = new("--path"); Argument versionArgument = new("version", getDefaultValue: () => ProgramOptions.DefaultVersionArgument); Option buildVersionOption = new("--build-version", parseArgument: ArgumentParser.ParseVersion); - RootCommand command = new() - { + RootCommand command = + [ pathOption, versionArgument, buildVersionOption, - }; + ]; ProgramOptionsBinder binder = new(pathOption, versionArgument, buildVersionOption); StringBuilder argsBuilder = new(version); diff --git a/tests/SlnUp.Tests/CLI/ProgramOptionsTests.cs b/tests/SlnUp.Tests/CLI/ProgramOptionsTests.cs index f4808a0..9171874 100644 --- a/tests/SlnUp.Tests/CLI/ProgramOptionsTests.cs +++ b/tests/SlnUp.Tests/CLI/ProgramOptionsTests.cs @@ -18,7 +18,7 @@ public class ProgramOptionsTests public void Configure(string version) { // Arrange - string[] args = new[] { version }; + string[] args = [version]; // Act ProgramOptions? result = this.ConfigureAndInvoke(args, out int exitCode); @@ -36,7 +36,7 @@ public void Configure(string version) public void Configure_NoParameters() { // Arrange - string[] args = Array.Empty(); + string[] args = []; // Act ProgramOptions? result = this.ConfigureAndInvoke(args, out int exitCode); @@ -92,12 +92,12 @@ public void Configure_WithHelp(params string[] args) public void Configure_WithInvalidBuildVersion() { // Arrange - string[] args = new[] - { + string[] args = + [ "2022", "--build-version", "invalid-version" - }; + ]; // Act ProgramOptions? result = this.ConfigureAndInvoke(args, out int exitCode); @@ -137,7 +137,7 @@ public void Configure_WithPath(params string[] args) public void Configure_WithVersion() { // Arrange - string[] args = new[] { "--version" }; + string[] args = ["--version"]; // Act ProgramOptions? result = this.ConfigureAndInvoke(args, out int exitCode); diff --git a/tests/SlnUp.Tests/ProgramTests.cs b/tests/SlnUp.Tests/ProgramTests.cs index 7012165..b009772 100644 --- a/tests/SlnUp.Tests/ProgramTests.cs +++ b/tests/SlnUp.Tests/ProgramTests.cs @@ -14,7 +14,7 @@ public void Main_NoLocalFile() { // Arrange using ScopedDirectory directory = TemporaryDirectory.CreateRandom(); - string[] args = new[] { "2022" }; + string[] args = ["2022"]; using IDisposable _ = directory.SetAsScopedWorkingDirectory(); // Act @@ -31,11 +31,12 @@ public void Main_WithFilePath() using ScopedFile file = TemporaryFile.CreateRandomWithExtension("sln"); SolutionFileBuilder solutionFileBuilder = new(Version.Parse("16.0.30114.105")); SolutionFile solutionFile = solutionFileBuilder.BuildToSolutionFile(file.FileSystem, file.Path); - string[] args = new[] - { + string[] args = + [ "2022", - "--path", file.Path, - }; + "--path", + file.Path, + ]; // Act int exitCode = Program.Main(args); @@ -51,11 +52,12 @@ public void Main_WithInvalidFile() { // Arrange using ScopedFile file = TemporaryFile.CreateRandomWithExtension("sln"); - string[] args = new[] - { + string[] args = + [ "2022", - "--path", file.Path, - }; + "--path", + file.Path, + ]; // Act int exitCode = Program.Main(args); @@ -72,7 +74,7 @@ public void Main_WithLocalFile() string solutionFilePath = directory.GetRandomFilePathWithExtension("sln"); SolutionFileBuilder solutionFileBuilder = new(Version.Parse("16.0.30114.105")); SolutionFile solutionFile = solutionFileBuilder.BuildToSolutionFile(directory.FileSystem, solutionFilePath); - string[] args = new[] { "2022" }; + string[] args = ["2022"]; using IDisposable _ = directory.SetAsScopedWorkingDirectory(); // Act diff --git a/tests/SlnUp.Tests/SlnUp.Tests.csproj b/tests/SlnUp.Tests/SlnUp.Tests.csproj index afadd5e..afe3a2b 100644 --- a/tests/SlnUp.Tests/SlnUp.Tests.csproj +++ b/tests/SlnUp.Tests/SlnUp.Tests.csproj @@ -1,7 +1,7 @@ - net6.0;net7.0 + net6.0;net8.0 diff --git a/tests/SlnUp.Tests/SolutionFileBuilder.cs b/tests/SlnUp.Tests/SolutionFileBuilder.cs index 5107b85..57100f6 100644 --- a/tests/SlnUp.Tests/SolutionFileBuilder.cs +++ b/tests/SlnUp.Tests/SolutionFileBuilder.cs @@ -38,7 +38,7 @@ public SolutionFileBuilder() /// /// Initializes a new instance of the class from a full Visual Studio version - /// and optional file format version and mimimum Visual Studio version. + /// and optional file format version and minimum Visual Studio version. /// /// The full Visual Studio version. /// The file format version. diff --git a/version.json b/version.json index 0a9d112..21e8829 100644 --- a/version.json +++ b/version.json @@ -1,6 +1,6 @@ { "$schema": "https://raw.githubusercontent.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", - "version": "1.4.4-beta.{height}", + "version": "2.0", "buildNumberOffset": -1, "nugetPackageVersion": { "semVer": 2