Skip to content

Commit e06774d

Browse files
committed
Publishing tests are passing
1 parent 826892f commit e06774d

File tree

6 files changed

+31
-33
lines changed

6 files changed

+31
-33
lines changed

src/Dotnet.Script.Core/ScriptEmitter.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public ScriptEmitter(ScriptConsole scriptConsole, ScriptCompiler scriptCompiler)
1818
_scriptCompiler = scriptCompiler;
1919
}
2020

21-
public virtual ScriptEmitResult Emit<TReturn, THost>(ScriptContext context)
21+
public virtual ScriptEmitResult Emit<TReturn, THost>(ScriptContext context, string assemblyName)
2222
{
2323
var compilationContext = _scriptCompiler.CreateCompilationContext<TReturn, THost>(context);
2424
foreach (var warning in compilationContext.Warnings)
@@ -37,13 +37,17 @@ public virtual ScriptEmitResult Emit<TReturn, THost>(ScriptContext context)
3737
}
3838

3939
var compilation = compilationContext.Script.GetCompilation();
40+
compilation = compilation.WithAssemblyName(assemblyName);
4041

4142
var peStream = new MemoryStream();
4243
EmitOptions emitOptions = null;
44+
4345
if (context.OptimizationLevel == Microsoft.CodeAnalysis.OptimizationLevel.Debug)
4446
{
4547
emitOptions = new EmitOptions()
4648
.WithDebugInformationFormat(DebugInformationFormat.Embedded);
49+
50+
4751
}
4852

4953
var result = compilation.Emit(peStream, options: emitOptions);

src/Dotnet.Script.Core/ScriptPublisher.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Dotnet.Script.Core
1313
{
1414
public class ScriptPublisher
1515
{
16-
private const string ScriptingVersion = "2.8.2";
16+
private const string ScriptingVersion = "3.7.0";
1717

1818
private readonly ScriptProjectProvider _scriptProjectProvider;
1919
private readonly ScriptEmitter _scriptEmitter;
@@ -70,18 +70,11 @@ public void CreateExecutable<TReturn, THost>(ScriptContext context, LogFactory l
7070
throw new ArgumentNullException(nameof(runtimeIdentifier));
7171
}
7272

73-
string targetFrameworkFolder = _scriptEnvironment.TargetFramework;
74-
if (string.Equals(targetFrameworkFolder, "netcoreapp5.0", StringComparison.InvariantCultureIgnoreCase))
75-
{
76-
targetFrameworkFolder = "net5.0";
77-
}
78-
79-
8073
executableFileName = executableFileName ?? Path.GetFileNameWithoutExtension(context.FilePath);
8174
const string AssemblyName = "scriptAssembly";
8275

83-
var tempProjectPath = ScriptProjectProvider.GetPathToProjectFile(Path.GetDirectoryName(context.FilePath), targetFrameworkFolder);
84-
var renamedProjectPath = ScriptProjectProvider.GetPathToProjectFile(Path.GetDirectoryName(context.FilePath), targetFrameworkFolder, executableFileName);
76+
var tempProjectPath = ScriptProjectProvider.GetPathToProjectFile(Path.GetDirectoryName(context.FilePath), _scriptEnvironment.TargetFramework);
77+
var renamedProjectPath = ScriptProjectProvider.GetPathToProjectFile(Path.GetDirectoryName(context.FilePath), _scriptEnvironment.TargetFramework, executableFileName);
8578
var tempProjectDirectory = Path.GetDirectoryName(tempProjectPath);
8679

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

9689
var commandRunner = new CommandRunner(logFactory);
9790
// todo: may want to add ability to return dotnet.exe errors
98-
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");
91+
var publishSingleFileArgument = ScriptEnvironment.Default.NetCoreVersion.Major >= 3 ? "/p:PublishSingleFile=true" : string.Empty;
92+
var includeNativeLibrariesForSelfExtract = ScriptEnvironment.Default.NetCoreVersion.Major >= 5 ? "/p:IncludeNativeLibrariesForSelfExtract=true" : string.Empty;
93+
94+
var exitcode = commandRunner.Execute("dotnet", $"publish \"{renamedProjectPath}\" -c Release -r {runtimeIdentifier} -o \"{context.WorkingDirectory}\" {publishSingleFileArgument} {includeNativeLibrariesForSelfExtract} /p:DebugType=Embedded");
9995

10096
if (exitcode != 0)
10197
{
@@ -107,7 +103,7 @@ public void CreateExecutable<TReturn, THost>(ScriptContext context, LogFactory l
107103

108104
private string CreateScriptAssembly<TReturn, THost>(ScriptContext context, string outputDirectory, string assemblyFileName)
109105
{
110-
var emitResult = _scriptEmitter.Emit<TReturn, THost>(context);
106+
var emitResult = _scriptEmitter.Emit<TReturn, THost>(context, assemblyFileName);
111107
var assemblyPath = Path.Combine(outputDirectory, $"{assemblyFileName}.dll");
112108
using (var peFileStream = new FileStream(assemblyPath, FileMode.Create))
113109
using (emitResult.PeStream)

src/Dotnet.Script.Core/Templates/program.publish.template

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using Microsoft.CodeAnalysis.CSharp.Scripting.Hosting;
33
using Microsoft.CodeAnalysis.Scripting.Hosting;
4+
using System.Runtime.Loader;
45
using System.Threading.Tasks;
56
using static System.Console;
67
using System.Reflection;
@@ -18,15 +19,14 @@ namespace dotnetPublishCode
1819
foreach (var arg in args)
1920
globals.Args.Add(arg);
2021

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

2727
var invokeTask = factoryMethod.Invoke(null, new object[] { new object[] { globals, null } }) as Task<int>;
2828
var invokeResult = await invokeTask;
29-
if (invokeResult != 0)
29+
if (invokeResult != 0)
3030
{
3131
WritePrettyError($"Error result: '{invokeResult}'");
3232
return 0x1;

src/Dotnet.Script.DependencyModel/Environment/ScriptEnvironment.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ public DotnetVersion(string version, string tfm)
156156
Major = int.Parse(versionMatch.Groups[1].Value);
157157
if (versionMatch.Success && versionMatch.Groups[2].Success)
158158
Minor = int.Parse(versionMatch.Groups[2].Value);
159+
if (Major >= 5)
160+
{
161+
Tfm = $"net{Major}.{Minor}";
162+
}
159163
}
160164

161165
public string Version { get; }

src/Dotnet.Script.Tests/ScriptPublisherTests.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,19 @@ public void SimplePublishTest()
3232
var mainPath = Path.Combine(workspaceFolder.Path, "main.csx");
3333
File.WriteAllText(mainPath, code);
3434

35-
//var publishResult = ScriptTestRunner.Default.Execute($"publish {mainPath}", workspaceFolder.Path);
36-
var publishResult = ScriptTestRunner.Default.ExecuteInProcess($"publish {mainPath}");
37-
// Assert.Equal(0, publishResult.exitCode);
35+
var publishResult = ScriptTestRunner.Default.Execute($"publish {mainPath}", workspaceFolder.Path);
36+
Assert.Equal(0, publishResult.exitCode);
3837

39-
// var exePath = Path.Combine(workspaceFolder.Path, "publish", _scriptEnvironment.RuntimeIdentifier, "main");
40-
// var executableRunResult = _commandRunner.Execute(exePath);
38+
var exePath = Path.Combine(workspaceFolder.Path, "publish", _scriptEnvironment.RuntimeIdentifier, "main");
39+
var executableRunResult = _commandRunner.Execute(exePath);
4140

42-
// Assert.Equal(0, executableRunResult);
41+
Assert.Equal(0, executableRunResult);
4342

44-
// var publishedFiles = Directory.EnumerateFiles(Path.Combine(workspaceFolder.Path, "publish", _scriptEnvironment.RuntimeIdentifier));
45-
// if (_scriptEnvironment.NetCoreVersion.Major >= 3)
46-
// Assert.True(publishedFiles.Count() == 1, "There should be only a single published file");
47-
// else
48-
// Assert.True(publishedFiles.Count() > 1, "There should be multiple published files");
43+
var publishedFiles = Directory.EnumerateFiles(Path.Combine(workspaceFolder.Path, "publish", _scriptEnvironment.RuntimeIdentifier));
44+
if (_scriptEnvironment.NetCoreVersion.Major >= 3)
45+
Assert.True(publishedFiles.Count() == 1, "There should be only a single published file");
46+
else
47+
Assert.True(publishedFiles.Count() > 1, "There should be multiple published files");
4948
}
5049
}
5150

src/Dotnet.Script.Tests/ScriptTestRunner.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,8 @@ private string GetDotnetScriptArguments(string arguments)
8080
#else
8181
configuration = "Release";
8282
#endif
83-
string targetFrameworkFolder = _scriptEnvironment.TargetFramework;
84-
if (string.Equals(targetFrameworkFolder, "netcoreapp5.0", StringComparison.InvariantCultureIgnoreCase))
85-
{
86-
targetFrameworkFolder = "net5.0";
87-
}
8883

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

9186
return allArgs;
9287
}

0 commit comments

Comments
 (0)