Skip to content

Commit

Permalink
Merge pull request #116 from tareqimbasher/feat/net8-support
Browse files Browse the repository at this point in the history
Adds support for .NET 8
  • Loading branch information
tareqimbasher authored Nov 23, 2023
2 parents 78578ec + 9613a4c commit 8a36d02
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 17 deletions.
4 changes: 2 additions & 2 deletions docs/SPA App.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions docs/Supporting a new .NET Version.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ <h5>
<div if.bind="dotnetSdkMissing">
<span class="text-red">No supported .NET SDK could be found.</span>
<br/>
Make sure the .NET SDK version 6 and/or 7 is installed and the <code>dotnet</code>
Make sure the .NET SDK version 6 or greater is installed and the <code>dotnet</code>
executable is in your <code>PATH</code>. All installed SDKs:
</div>
<div else>
Expand Down
12 changes: 1 addition & 11 deletions src/Core/NetPad.Compilation/CSharp/CSharpCodeCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
};
}
}
3 changes: 3 additions & 0 deletions src/Core/NetPad.Domain/Common/GlobalConsts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
};
Expand All @@ -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}")
};
Expand All @@ -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}")
};
Expand Down
14 changes: 13 additions & 1 deletion src/Core/NetPad.Domain/DotNet/DotNetFrameworkVersionUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp;

namespace NetPad.DotNet;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
};
}
}
2 changes: 1 addition & 1 deletion src/Core/NetPad.Domain/NetPad.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.DataProtection.Abstractions" Version="6.0.11" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.6.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()));
Expand Down

0 comments on commit 8a36d02

Please sign in to comment.