8000 Support for .Net 5 by seesharper · Pull Request #589 · dotnet-script/dotnet-script · GitHub
[go: up one dir, main page]

Skip to content

Support for .Net 5 #589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Nov 10, 2020
14 changes: 13 additions & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ jobs:
- bash: "curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin -version 2.1.402"
displayName: "Install 2.1.402"

- bash: "curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin -version 5.0.100"
displayName: " 5.0.100"

- bash: |
10000 export PATH=/home/vsts/.dotnet:$PATH
curl -L https://github.com/filipw/dotnet-script/releases/download/0.28.0/dotnet-script.0.28.0.zip > dotnet-script.zip
Expand All @@ -44,9 +47,12 @@ jobs:
steps:
- bash: |
curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin -version 3.1.102

displayName: "Install 3.0.100"

- bash: |
curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin -version 5.0.100
displayName: "Install 5.0.100"

- bash: |
curl -L https://github.com/filipw/dotnet-script/releases/download/0.28.0/dotnet-script.0.28.0.zip > dotnet-script.zip
unzip -o dotnet-script.zip -d ./
Expand All @@ -73,6 +79,12 @@ jobs:

displayName: "Install 2.1.402 SDK"

- powershell: |
iwr https://raw.githubusercontent.com/dotnet/cli/release/2.1.3xx/scripts/obtain/dotnet-install.ps1 -outfile dotnet-install.ps1
.\dotnet-install.ps1 -Version 5.0.100

displayName: "Install 5.0.100"

# NuGet Tool Installer
# Acquires a specific version of NuGet from the internet or the tools cache and adds it to the PATH. Use this task to change the version of NuGet used in the NuGet tasks.
- task: NuGetToolInstaller@0
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "3.1.102",
"version": "5.0.100",
"rollForward": "latestFeature"
}
}
5 changes: 3 additions & 2 deletions src/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"coverage-gutters.lcovname": "coverage.info"
}
"coverage-gutters.lcovname": "coverage.info",
"dotnetCoreExplorer.searchpatterns": "**/bin/Debug/net5.0/Dotnet.Script.Tests.dll"
}
2 changes: 1 addition & 1 deletion src/Dotnet.Script.Core/Dotnet.Script.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>A cross platform library allowing you to run C# (CSX) scripts with support for debugging and inline NuGet packages. Based on Roslyn.</Description>
<VersionPrefix>0.53.0</VersionPrefix>
<VersionPrefix>1.0.0</VersionPrefix>
<Authors>filipw</Authors>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<AssemblyName>Dotnet.Script.Core</AssemblyName>
Expand Down
6 changes: 5 additions & 1 deletion src/Dotnet.Script.Core/ScriptEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ScriptEmitter(ScriptConsole scriptConsole, ScriptCompiler scriptCompiler)
_scriptCompiler = scriptCompiler;
}

public virtual ScriptEmitResult Emit<TReturn, THost>(ScriptContext context)
public virtual ScriptEmitResult Emit<TReturn, THost>(ScriptContext context, string assemblyName)
{
var compilationContext = _scriptCompiler.CreateCompilationContext<TReturn, THost>(context);
foreach (var warning in compilationContext.Warnings)
E7F5 Expand All @@ -37,13 +37,17 @@ public virtual ScriptEmitResult Emit<TReturn, THost>(ScriptContext context)
}

var compilation = compilationContext.Script.GetCompilation();
compilation = compilation.WithAssemblyName(assemblyName);

var peStream = new MemoryStream();
EmitOptions emitOptions = null;

if (context.OptimizationLevel == Microsoft.CodeAnalysis.OptimizationLevel.Debug)
{
emitOptions = new EmitOptions()
.WithDebugInformationFormat(DebugInformationFormat.Embedded);


}

var result = compilation.Emit(peStream, options: emitOptions);
Expand Down
13 changes: 8 additions & 5 deletions src/Dotnet.Script.Core/ScriptPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Dotnet.Script.Core
{
public class ScriptPublisher
{
private const string ScriptingVersion = "2.8.2";
private const string ScriptingVersion = "3.7.0";

private readonly ScriptProjectProvider _scriptProjectProvider;
private readonly ScriptEmitter _scriptEmitter;
Expand Down Expand Up @@ -73,8 +73,8 @@ public void CreateExecutable<TReturn, THost>(ScriptContext context, LogFactory l
executableFileName = executableFileName ?? Path.GetFileNameWithoutExtension(context.FilePath);
const string AssemblyName = "scriptAssembly";

var tempProjectPath = ScriptProjectProvider.GetPathToProjectFile(Path.GetDirectoryName(context.FilePath), ScriptEnvironment.Default.TargetFramework);
var renamedProjectPath = ScriptProjectProvider.GetPathToProjectFile(Path.GetDirectoryName(context.FilePath), ScriptEnvironment.Default.TargetFramework, executableFileName);
var tempProjectPath = ScriptProjectProvider.GetPathToProjectFile(Path.GetDirectoryName(context.FilePath), _scriptEnvironment.TargetFramework);
var renamedProjectPath = ScriptProjectProvider.GetPathToProjectFile(Path.GetDirectoryName(context.FilePath), _scriptEnvironment.TargetFramework, executableFileName);
var tempProjectDirectory = Path.GetDirectoryName(tempProjectPath);

var scriptAssemblyPath = CreateScriptAssembly<TReturn, THost>(context, tempProjectDirectory, AssemblyName);
Expand All @@ -88,7 +88,10 @@ public void CreateExecutable<TReturn, THost>(ScriptContext context, LogFactory l

var commandRunner = new CommandRunner(logFactory);
// todo: may want to add ability to return dotnet.exe errors
var exitcode = commandRunner.Execute("dotnet", $"publish \"{renamedProjectPath}\" -c Release -r {runtimeIdentifier} -o \"{context.WorkingDirectory}\" {(ScriptEnvironment.Default.NetCoreVersion.Major >= 3 ? "/p:PublishSingleFile=true" : string.Empty)} /p:DebugType=Embedded");
var publishSingleFileArgument = ScriptEnvironment.Default.NetCoreVersion.Major >= 3 ? "/p:PublishSingleFile=true" : string.Empty;
var includeNativeLibrariesForSelfExtract = ScriptEnvironment.Default.NetCoreVersion.Major >= 5 ? "/p:IncludeNativeLibrariesForSelfExtract=true" : string.Empty;

var exitcode = commandRunner.Execute("dotnet", $"publish \"{renamedProjectPath}\" -c Release -r {runtimeIdentifier} -o \"{context.WorkingDirectory}\" {publishSingleFileArgument} {includeNativeLibrariesForSelfExtract} /p:DebugType=Embedded");

if (exitcode != 0)
{
Expand All @@ -100,7 +103,7 @@ public void CreateExecutable<TReturn, THost>(ScriptContext context, LogFactory l

private string CreateScriptAssembly<TReturn, THost>(ScriptContext context, string outputDirectory, string assemblyFileName)
{
var emitResult = _scriptEmitter.Emit<TReturn, THost>(context);
var emitResult = _scriptEmitter.Emit<TReturn, THost>(context, assemblyFileName);
var assemblyPath = Path.Combine(outputDirectory, $"{assemblyFileName}.dll");
using (var peFileStream = new FileStream(assemblyPath, FileMode.Create))
using (emitResult.PeStream)
Expand Down
6 changes: 3 additions & 3 deletions src/Dotnet.Script.Core/Templates/program.publish.template
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Microsoft.CodeAnalysis.CSharp.Scripting.Hosting;
using Microsoft.CodeAnalysis.Scripting.Hosting;
using System.Runtime.Loader;
using System.Threading.Tasks;
using static System.Console;
using System.Reflection;
Expand All @@ -18,15 +19,14 @@ namespace dotnetPublishCode
foreach (var arg in args)
globals.Args.Add(arg);

var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var assembly = Assembly.LoadFrom(Path.Combine(path, "scriptAssembly.dll"));
var assembly = AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName("scriptAssembly"));
var type = assembly.GetType("Submission#0");
var factoryMethod = type.GetMethod("<Factory>");
if (factoryMethod == null) throw new Exception("couldn't find factory method to initiate script");

var invokeTask = factoryMethod.Invoke(null, new object[] { new object[] { globals, null } }) as Task<int>;
var invokeResult = await invokeTask;
if (invokeResult != 0)
if (invokeResult != 0)
{
WritePrettyError($"Error result: '{invokeResult}'");
return 0x1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<RepositoryUrl>https://github.com/filipw/dotnet-script.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>script;csx;csharp;roslyn;nuget</PackageTags>
<Version>0.53.0</Version>
<Version>1.0.0</Version>
<Description>A MetadataReferenceResolver that allows inline nuget references to be specified in script(csx) files.</Description>
<Authors>dotnet-script</Authors>
<Company>dotnet-script</Company>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<RepositoryUrl>https://github.com/filipw/dotnet-script.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>script;csx;csharp;roslyn;omnisharp</PackageTags>
<Version>0.53.0</Version>
<Version>1.0.0</Version>
<LangVersion>latest</LangVersion>
</PropertyGroup>

Expand Down
15 changes: 15 additions & 0 deletions src/Dotnet.Script.DependencyModel/Environment/ScriptEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ private static string GetPlatformIdentifier()

private static DotnetVersion GetNetCoreAppVersion()
{
GetNetCoreVersion();
// https://github.com/dotnet/BenchmarkDotNet/blob/94863ab4d024eca04d061423e5aad498feff386b/src/BenchmarkDotNet/Portability/RuntimeInformation.cs#L156
var codeBase = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly.CodeBase;
var pattern = @"^.*Microsoft\.NETCore\.App\/(\d+\.\d+)(.*?)\/";
Expand All @@ -88,6 +89,16 @@ private static DotnetVersion GetNetCoreAppVersion()
return new DotnetVersion(version, $"netcoreapp{tfm}");
}

public static string GetNetCoreVersion()
{
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];
return null;
}

private static string GetInstallLocation()
{
return Path.GetDirectoryName(new Uri(typeof(ScriptEnvironment).GetTypeInfo().Assembly.CodeBase).LocalPath);
Expand Down Expand Up @@ -145,6 +156,10 @@ public DotnetVersion(string version, string tfm)
Major = int.Parse(versionMatch.Groups[1].Value);
if (versionMatch.Success && versionMatch.Groups[2].Success)
Minor = int.Parse(versionMatch.Groups[2].Value);
if (Major >= 5)
{
Tfm = $"net{Major}.{Minor}";
}
}

public string Version { get; }
Expand Down
2 changes: 1 addition & 1 deletion src/Dotnet.Script.Tests/Dotnet.Script.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks>net5.0</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
Expand Down
34 changes: 20 additions & 14 deletions src/Dotnet.Script.Tests/ScriptPackagesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ public ScriptPackagesTests(ITestOutputHelper testOutputHelper)
[Fact]
public void ShouldHandleScriptPackageWithMainCsx()
{
var result = Execute("WithMainCsx/WithMainCsx.csx");
Assert.StartsWith("Hello from netstandard2.0", result);
var (output, exitcode) = ScriptTestRunner.Default.ExecuteWithScriptPackage("WithMainCsx", "--no-cache");
Assert.Equal(0, exitcode);
Assert.StartsWith("Hello from netstandard2.0", output);
}

[Fact]
//[Fact]
public void ShouldThrowMeaningfulExceptionWhenScriptPackageIsMissing()
{
using (var scriptFolder = new DisposableFolder())
Expand Down Expand Up @@ -60,37 +61,42 @@ public void ShouldThrowMeaningfulExceptionWhenScriptPackageIsMissing()
[Fact]
public void ShouldHandleScriptWithAnyTargetFramework()
{
var result = Execute("WithAnyTargetFramework/WithAnyTargetFramework.csx");
Assert.StartsWith("Hello from any target framework", result);
var (output, exitcode) = ScriptTestRunner.Default.ExecuteWithScriptPackage("WithAnyTargetFramework", "--no-cache");
Assert.Equal(0, exitcode);
Assert.StartsWith("Hello from any target framework", output);
}

[Fact]
public void ShouldHandleScriptPackageWithNoEntryPointFile()
{
var result = Execute("WithNoEntryPointFile/WithNoEntryPointFile.csx");
Assert.Contains("Hello from Foo.csx", result);
Assert.Contains("Hello from Bar.csx", result);
var (output, exitcode) = ScriptTestRunner.Default.ExecuteWithScriptPackage("WithNoEntryPointFile", "--no-cache");
Assert.Equal(0, exitcode);
Assert.Contains("Hello from Foo.csx", output);
Assert.Contains("Hello from Bar.csx", output);
}

[Fact]
public void ShouldHandleScriptPackageWithScriptPackageDependency()
{
var result = Execute("WithScriptPackageDependency/WithScriptPackageDependency.csx");
Assert.StartsWith("Hello from netstandard2.0", result);
var (output, exitcode) = ScriptTestRunner.Default.ExecuteWithScriptPackage("WithScriptPackageDependency", "--no-cache");
Assert.Equal(0, exitcode);
Assert.StartsWith("Hello from netstandard2.0", output);
}

[Fact]
public void ShouldThrowExceptionWhenReferencingUnknownPackage()
{
var result = Execute("WithInvalidPackageReference/WithInvalidPackageReference.csx");
Assert.StartsWith("Unable to restore packages from", result);
var (output, exitcode) = ScriptTestRunner.Default.ExecuteWithScriptPackage("WithInvalidPackageReference", "--no-cache");
Assert.NotEqual(0, exitcode);
Assert.StartsWith("Unable to restore packages from", output);
}

[Fact]
public void ShouldHandleScriptPackageWithSubFolder()
{
var result = Execute("WithSubFolder/WithSubFolder.csx");
Assert.StartsWith("Hello from Bar.csx", result);
var (output, exitcode) = ScriptTestRunner.Default.ExecuteWithScriptPackage("WithSubFolder", "--no-cache");
Assert.Equal(0, exitcode);
Assert.StartsWith("Hello from Bar.csx", output);
}

[Fact]
Expand Down
8 changes: 8 additions & 0 deletions src/Dotnet.Script.Tests/ScriptTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public int ExecuteInProcess(string arguments = null)
return result;
}

public (string output, int exitcode) ExecuteWithScriptPackage(string fixture, string arguments = null, string workingDirectory = null)
{
var pathToScriptPackageFixtures = TestPathUtils.GetPathToTestFixtureFolder("ScriptPackage");
var pathToFixture = Path.Combine(pathToScriptPackageFixtures, fixture, $"{fixture}.csx");
return ProcessHelper.RunAndCaptureOutput("dotnet", GetDotnetScriptArguments($"{pathToFixture} {arguments}"), workingDirectory);
}

public int ExecuteFixtureInProcess(string fixture, string arguments = null)
{
var pathToFixture = TestPathUtils.GetPathToTestFixture(fixture);
Expand Down Expand Up @@ -80,6 +87,7 @@ private string GetDotnetScriptArguments(string arguments)
#else
configuration = "Release";
#endif

var allArgs = $"exec {Path.Combine(Directory.GetCurrentDirectory(), "..", "..", "..", "..", "Dotnet.Script", "bin", configuration, _scriptEnvironment.TargetFramework, "dotnet-script.dll")} {arguments}";

return allArgs;
Expand Down
4 changes: 2 additions & 2 deletions src/Dotnet.Script/Dotnet.Script.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>Dotnet CLI tool allowing you to run C# (CSX) scripts.</Description>
<VersionPrefix>0.53.0</VersionPrefix>
<VersionPrefix>1.0.0</VersionPrefix>
<Authors>filipw</Authors>
<PackageId>Dotnet.Script</PackageId>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks>net5.0;netcoreapp2.1;netcoreapp3.1</TargetFrameworks>
<DebugType>portable</DebugType>
<AssemblyName>dotnet-script</AssemblyName>
<OutputType>Exe</OutputType>
Expand Down
0