From 9613a4c126cd81cb74e32c01b00d0aae95498c9e Mon Sep 17 00:00:00 2001 From: Tareq Imbasher Date: Thu, 23 Nov 2023 08:03:36 +0200 Subject: [PATCH] Adds support for .NET 8 --- docs/SPA App.md | 4 ++-- docs/Supporting a new .NET Version.md | 12 ++++++++++++ .../app-dependencies-check-dialog.html | 2 +- .../CSharp/CSharpCodeCompiler.cs | 12 +----------- src/Core/NetPad.Domain/Common/GlobalConsts.cs | 3 +++ .../DotNet/DotNetFrameworkVersionUtil.cs | 14 +++++++++++++- src/Core/NetPad.Domain/NetPad.Domain.csproj | 2 +- .../CSharp/CSharpCodeCompilerTests.cs | 12 +++++++++++- 8 files changed, 44 insertions(+), 17 deletions(-) create mode 100644 docs/Supporting a new .NET Version.md diff --git a/docs/SPA App.md b/docs/SPA App.md index e3b21bdb..0557a454 100644 --- a/docs/SPA App.md +++ b/docs/SPA App.md @@ -4,8 +4,8 @@ * `core`: folders contained here should be thought of as separate modules. * `@common`: contains common utils and non-app specific code. - * `@domain`: the app-specific domain layer. - * `@application`: the application layer. + * `@domain`: contains api client code and services that are not concerned with UI or the structure of the app. Should only rely on `@common`. + * `@application`: contains components and application services. * `@plugins`: app plugins. * `styles`: application-wide styling. * `windows`: each window should have a separate folder within this folder. diff --git a/docs/Supporting a new .NET Version.md b/docs/Supporting a new .NET Version.md new file mode 100644 index 00000000..d43f3066 --- /dev/null +++ b/docs/Supporting a new .NET Version.md @@ -0,0 +1,12 @@ +# Adding Support for a new .NET Version + +To add support for a new version of .NET to NetPad use the following steps: + +1. Update `DotNetFrameworkVersionUtil` and add proper entries for new .NET version. +2. Update `GlobalConsts.EntityFrameworkLibVersion` method for new .NET version. +3. If needed, update `Microsoft.CodeAnalysis.CSharp` package in `NetPad.Domain.csproj`. +4. `CSharpCodeCompilerTests`: + - Add a test case to `Compiler_Uses_Correct_CSharp_LanguageVersion()`. + - Add a new unit test `Can_Compile_CSharpX_Features` (ex: `X` = 12 for C# 12) to test compiling new C# language version. + +Done. \ No newline at end of file diff --git a/src/Apps/NetPad.Apps.App/App/src/core/@application/dialogs/app-dependencies-check-dialog/app-dependencies-check-dialog.html b/src/Apps/NetPad.Apps.App/App/src/core/@application/dialogs/app-dependencies-check-dialog/app-dependencies-check-dialog.html index 4b9fcf17..f9515906 100644 --- a/src/Apps/NetPad.Apps.App/App/src/core/@application/dialogs/app-dependencies-check-dialog/app-dependencies-check-dialog.html +++ b/src/Apps/NetPad.Apps.App/App/src/core/@application/dialogs/app-dependencies-check-dialog/app-dependencies-check-dialog.html @@ -32,7 +32,7 @@
No supported .NET SDK could be found.
- Make sure the .NET SDK version 6 and/or 7 is installed and the dotnet + Make sure the .NET SDK version 6 or greater is installed and the dotnet executable is in your PATH. All installed SDKs:
diff --git a/src/Core/NetPad.Compilation/CSharp/CSharpCodeCompiler.cs b/src/Core/NetPad.Compilation/CSharp/CSharpCodeCompiler.cs index ef94bdca..31ec2188 100644 --- a/src/Core/NetPad.Compilation/CSharp/CSharpCodeCompiler.cs +++ b/src/Core/NetPad.Compilation/CSharp/CSharpCodeCompiler.cs @@ -112,17 +112,7 @@ public CSharpParseOptions GetParseOptions(DotNetFrameworkVersion targetFramework { // TODO investigate using SourceKind.Script return CSharpParseOptions.Default - .WithLanguageVersion(GetCSharpLanguageVersion(targetFrameworkVersion)) + .WithLanguageVersion(DotNetFrameworkVersionUtil.GetLatestSupportedCSharpLanguageVersion(targetFrameworkVersion)) .WithKind(SourceCodeKind.Regular); } - - private LanguageVersion GetCSharpLanguageVersion(DotNetFrameworkVersion dotNetFrameworkVersion) - { - return dotNetFrameworkVersion switch - { - DotNetFrameworkVersion.DotNet6 => LanguageVersion.CSharp10, - DotNetFrameworkVersion.DotNet7 => LanguageVersion.CSharp11, - _ => throw new ArgumentOutOfRangeException(nameof(dotNetFrameworkVersion), dotNetFrameworkVersion, "Unhandled .NET framework version") - }; - } } diff --git a/src/Core/NetPad.Domain/Common/GlobalConsts.cs b/src/Core/NetPad.Domain/Common/GlobalConsts.cs index ac8ebd18..a85d6a27 100644 --- a/src/Core/NetPad.Domain/Common/GlobalConsts.cs +++ b/src/Core/NetPad.Domain/Common/GlobalConsts.cs @@ -19,6 +19,7 @@ public static string EntityFrameworkLibVersion(DotNetFrameworkVersion dotNetFram { DotNetFrameworkVersion.DotNet6 => "6.0.19", DotNetFrameworkVersion.DotNet7 => "7.0.8", + DotNetFrameworkVersion.DotNet8 => "8.0.0", _ => throw new ArgumentOutOfRangeException(nameof(dotNetFrameworkVersion), dotNetFrameworkVersion, $"Unsupported framework version: {dotNetFrameworkVersion}") }; @@ -30,6 +31,7 @@ public static string EntityFrameworkLibVersion(DotNetFrameworkVersion dotNetFram { DotNetFrameworkVersion.DotNet6 => "6.0.8", DotNetFrameworkVersion.DotNet7 => "7.0.4", + DotNetFrameworkVersion.DotNet8 => "8.0.0", _ => throw new ArgumentOutOfRangeException(nameof(dotNetFrameworkVersion), dotNetFrameworkVersion, $"Unsupported framework version: {dotNetFrameworkVersion}") }; @@ -41,6 +43,7 @@ public static string EntityFrameworkLibVersion(DotNetFrameworkVersion dotNetFram { DotNetFrameworkVersion.DotNet6 => "6.0.21", DotNetFrameworkVersion.DotNet7 => "7.0.10", + DotNetFrameworkVersion.DotNet8 => "8.0.0", _ => throw new ArgumentOutOfRangeException(nameof(dotNetFrameworkVersion), dotNetFrameworkVersion, $"Unsupported framework version: {dotNetFrameworkVersion}") }; diff --git a/src/Core/NetPad.Domain/DotNet/DotNetFrameworkVersionUtil.cs b/src/Core/NetPad.Domain/DotNet/DotNetFrameworkVersionUtil.cs index 9d7f5ba9..dc9adbd4 100644 --- a/src/Core/NetPad.Domain/DotNet/DotNetFrameworkVersionUtil.cs +++ b/src/Core/NetPad.Domain/DotNet/DotNetFrameworkVersionUtil.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; +using Microsoft.CodeAnalysis.CSharp; namespace NetPad.DotNet; @@ -43,7 +44,7 @@ public static bool TryGetDotNetFrameworkVersion(string targetFrameworkMoniker, [ public static bool IsSdkVersionSupported(SemanticVersion sdkVersion) { - return sdkVersion.Major is 6 or 7; + return sdkVersion.Major is >= 6 and <= 8; } public static bool IsSupported(this DotNetSdkVersion sdkVersion) @@ -81,4 +82,15 @@ public static int GetMajorVersion(this DotNetFrameworkVersion frameworkVersion) return _map.First(x => x.Value == frameworkVersion).Key; } + + public static LanguageVersion GetLatestSupportedCSharpLanguageVersion(DotNetFrameworkVersion dotNetFrameworkVersion) + { + return dotNetFrameworkVersion switch + { + DotNetFrameworkVersion.DotNet6 => LanguageVersion.CSharp10, + DotNetFrameworkVersion.DotNet7 => LanguageVersion.CSharp11, + DotNetFrameworkVersion.DotNet8 => LanguageVersion.CSharp12, + _ => throw new ArgumentOutOfRangeException(nameof(dotNetFrameworkVersion), dotNetFrameworkVersion, "Unhandled .NET framework version") + }; + } } diff --git a/src/Core/NetPad.Domain/NetPad.Domain.csproj b/src/Core/NetPad.Domain/NetPad.Domain.csproj index 048ca328..fa581df8 100644 --- a/src/Core/NetPad.Domain/NetPad.Domain.csproj +++ b/src/Core/NetPad.Domain/NetPad.Domain.csproj @@ -8,7 +8,7 @@ - + diff --git a/src/Tests/Core/NetPad.Compilation.Tests/CSharp/CSharpCodeCompilerTests.cs b/src/Tests/Core/NetPad.Compilation.Tests/CSharp/CSharpCodeCompilerTests.cs index 3df916e7..a876f89d 100644 --- a/src/Tests/Core/NetPad.Compilation.Tests/CSharp/CSharpCodeCompilerTests.cs +++ b/src/Tests/Core/NetPad.Compilation.Tests/CSharp/CSharpCodeCompilerTests.cs @@ -85,7 +85,7 @@ public void Can_Compile_Program_With_DotNet_Usings(string @namespace, string cod [InlineData(DotNetFrameworkVersion.DotNet5, null)] [InlineData(DotNetFrameworkVersion.DotNet6, LanguageVersion.CSharp10)] [InlineData(DotNetFrameworkVersion.DotNet7, LanguageVersion.CSharp11)] - [InlineData(DotNetFrameworkVersion.DotNet8, null)] + [InlineData(DotNetFrameworkVersion.DotNet8, LanguageVersion.CSharp12)] public void Compiler_Uses_Correct_CSharp_LanguageVersion(DotNetFrameworkVersion targetFrameworkVersion, LanguageVersion? expectedLangVersion) { var compiler = CreateCSharpCodeCompiler(); @@ -141,6 +141,16 @@ public void Can_Compile_CSharp11_Features() Assert.True(result.Success, result.Diagnostics.JoinToString(Environment.NewLine)); } + [Fact] + public void Can_Compile_CSharp12_Features() + { + var code = GetProgram("var IncrementBy = (int source, int increment = 1) => source + increment;"); + + var result = CreateCSharpCodeCompiler().Compile(new CompilationInput(code, DotNetFrameworkVersion.DotNet8)); + + Assert.True(result.Success, result.Diagnostics.JoinToString(Environment.NewLine)); + } + private CSharpCodeCompiler CreateCSharpCodeCompiler() { return new CSharpCodeCompiler(new DotNetInfo(new Settings()));