Skip to content

Commit 6f86639

Browse files
committed
Copy native assets to publish output directory
1 parent cc86dd7 commit 6f86639

File tree

8 files changed

+45
-52
lines changed

8 files changed

+45
-52
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
internal static class StringExtensions
4+
{
5+
public static bool Contains(this string source, string value, StringComparison comparison)
6+
{
7+
return source?.IndexOf(value, comparison) >= 0;
8+
}
9+
}

src/Dotnet.Script.Core/ScriptCompilationContext.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Dotnet.Script.DependencyModel.Runtime;
12
using Microsoft.CodeAnalysis.Scripting;
23
using Microsoft.CodeAnalysis.Scripting.Hosting;
34
using Microsoft.CodeAnalysis.Text;
@@ -14,12 +15,15 @@ public class ScriptCompilationContext<TReturn>
1415

1516
public ScriptOptions ScriptOptions { get; }
1617

17-
public ScriptCompilationContext(Script<TReturn> script, SourceText sourceText, InteractiveAssemblyLoader loader, ScriptOptions scriptOptions)
18+
public RuntimeDependency[] RuntimeDependencies {get;}
19+
20+
public ScriptCompilationContext(Script<TReturn> script, SourceText sourceText, InteractiveAssemblyLoader loader, ScriptOptions scriptOptions, RuntimeDependency[] runtimeDependencies)
1821
{
1922
Script = script;
2023
SourceText = sourceText;
2124
ScriptOptions = scriptOptions;
2225
Loader = loader;
26+
RuntimeDependencies = runtimeDependencies;
2327
}
2428
}
2529
}

src/Dotnet.Script.Core/ScriptCompiler.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ static ScriptCompiler()
6262

6363
public RuntimeDependencyResolver RuntimeDependencyResolver { get; }
6464

65-
65+
6666

6767
public ScriptCompiler(LogFactory logFactory, RuntimeDependencyResolver runtimeDependencyResolver)
6868
{
6969
_logger = logFactory(typeof(ScriptCompiler));
7070
_scriptEnvironment = ScriptEnvironment.Default;
71-
RuntimeDependencyResolver = runtimeDependencyResolver;
71+
RuntimeDependencyResolver = runtimeDependencyResolver;
7272
}
7373

7474
public virtual ScriptOptions CreateScriptOptions(ScriptContext context, IList<RuntimeDependency> runtimeDependencies)
@@ -107,7 +107,7 @@ public virtual ScriptCompilationContext<TReturn> CreateCompilationContext<TRetur
107107
{
108108
if (context == null) throw new ArgumentNullException(nameof(context));
109109

110-
_logger.Info($"Current runtime is '{_scriptEnvironment.PlatformIdentifier}'.");
110+
_logger.Info($"Current runtime is '{_scriptEnvironment.PlatformIdentifier}'.");
111111
RuntimeDependency[] runtimeDependencies = GetRuntimeDependencies(context);
112112

113113
var encoding = context.Code.Encoding ?? Encoding.UTF8; // encoding is required when emitting debug information
@@ -135,7 +135,7 @@ public virtual ScriptCompilationContext<TReturn> CreateCompilationContext<TRetur
135135

136136
EvaluateDiagnostics(script);
137137

138-
return new ScriptCompilationContext<TReturn>(script, context.Code, loader, scriptOptions);
138+
return new ScriptCompilationContext<TReturn>(script, context.Code, loader, scriptOptions, runtimeDependencies);
139139
}
140140

141141
private RuntimeDependency[] GetRuntimeDependencies(ScriptContext context)
@@ -173,9 +173,9 @@ private ScriptOptions AddScriptReferences(ScriptOptions scriptOptions, Dictionar
173173
}
174174
else
175175
{
176-
//Add the reference from the AssemblyLoadContext if present.
176+
//Add the reference from the AssemblyLoadContext if present.
177177
scriptOptions = scriptOptions.AddReferences(loadedAssembly);
178-
_logger.Trace("Already loaded => " + loadedAssembly);
178+
_logger.Trace("Already loaded => " + loadedAssembly);
179179
}
180180
}
181181

@@ -188,7 +188,7 @@ private void EvaluateDiagnostics<TReturn>(Script<TReturn> script)
188188

189189
var suppressedDiagnostics = orderedDiagnostics.Where(d => SuppressedDiagnosticIds.Contains(d.Id));
190190
foreach (var suppressedDiagnostic in suppressedDiagnostics)
191-
{
191+
{
192192
_logger.Debug($"Suppressed diagnostic {suppressedDiagnostic.Id}: {suppressedDiagnostic.ToString()}");
193193
}
194194

src/Dotnet.Script.Core/ScriptEmitResult.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.CodeAnalysis;
1+
using Dotnet.Script.DependencyModel.Runtime;
2+
using Microsoft.CodeAnalysis;
23
using System.Collections.Generic;
34
using System.Collections.Immutable;
45
using System.IO;
@@ -10,13 +11,15 @@ public class ScriptEmitResult
1011
{
1112
private ScriptEmitResult() { }
1213

13-
public ScriptEmitResult(MemoryStream peStream, IEnumerable<MetadataReference> directiveReferences)
14+
public ScriptEmitResult(MemoryStream peStream, IEnumerable<MetadataReference> directiveReferences, RuntimeDependency[] runtimeDependencies)
1415
{
1516
PeStream = peStream;
17+
RuntimeDependencies = runtimeDependencies;
1618
DirectiveReferences = directiveReferences.ToImmutableArray();
1719
}
1820

1921
public MemoryStream PeStream { get; }
22+
public RuntimeDependency[] RuntimeDependencies { get; }
2023
public ImmutableArray<Diagnostic> Diagnostics { get; private set; } = ImmutableArray.Create<Diagnostic>();
2124
public ImmutableArray<MetadataReference> DirectiveReferences { get; } = ImmutableArray.Create<MetadataReference>();
2225
public bool Success => !Diagnostics.Any(d => d.Severity == DiagnosticSeverity.Error);

src/Dotnet.Script.Core/ScriptEmitter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public virtual ScriptEmitResult Emit<TReturn, THost>(ScriptContext context)
2121
try
2222
{
2323
var compilationContext = _scriptCompiler.CreateCompilationContext<TReturn, THost>(context);
24+
2425
var compilation = compilationContext.Script.GetCompilation();
2526

2627
var peStream = new MemoryStream();
@@ -35,7 +36,7 @@ public virtual ScriptEmitResult Emit<TReturn, THost>(ScriptContext context)
3536

3637
if (result.Success)
3738
{
38-
return new ScriptEmitResult(peStream, compilation.DirectiveReferences);
39+
return new ScriptEmitResult(peStream, compilation.DirectiveReferences, compilationContext.RuntimeDependencies);
3940
}
4041

4142
return ScriptEmitResult.Error(result.Diagnostics);

src/Dotnet.Script.Core/ScriptPublisher.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,17 @@ private string CreateScriptAssembly<TReturn, THost>(ScriptContext context, strin
127127
}
128128
}
129129

130+
foreach (var runtimeDependency in emitResult.RuntimeDependencies)
131+
{
132+
foreach (var nativeAsset in runtimeDependency.NativeAssets)
133+
{
134+
if (!runtimeDependency.Name.Contains("microsoft.netcore", StringComparison.OrdinalIgnoreCase))
135+
{
136+
File.Copy(nativeAsset,Path.Combine(outputDirectory, Path.GetFileName(nativeAsset)), true);
137+
}
138+
}
139+
}
140+
130141
return assemblyPath;
131142
}
132143
catch (CompilationErrorException ex)

src/Dotnet.Script.DependencyModel/Context/DotnetRestorer.cs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using Dotnet.Script.DependencyModel.Environment;
22
using Dotnet.Script.DependencyModel.Logging;
33
using Dotnet.Script.DependencyModel.Process;
4-
using Microsoft.Extensions.DependencyModel;
54
using System;
6-
using System.IO;
75
using System.Linq;
86

97
namespace Dotnet.Script.DependencyModel.Context
@@ -34,35 +32,6 @@ public void Restore(string pathToProjectFile, string[] packageSources)
3432
throw new Exception($"Unable to restore packages from '{pathToProjectFile}'. Make sure that all script files contains valid NuGet references");
3533
}
3634

37-
38-
var pathToPublishFolder = Path.Combine(Path.GetDirectoryName(pathToProjectFile), "publish");
39-
_logger.Debug($"Restoring {pathToProjectFile} using the dotnet cli. RuntimeIdentifier : {runtimeIdentifier}");
40-
exitcode = _commandRunner.Execute("dotnet", $"publish \"{pathToProjectFile}\" --no-restore -o {pathToPublishFolder} {packageSourcesArgument}");
41-
if (exitcode != 0)
42-
{
43-
// We must throw here, otherwise we may incorrectly run with the old 'project.assets.json'
44-
throw new Exception($"Unable to restore packages from '{pathToProjectFile}'. Make sure that all script files contains valid NuGet references");
45-
}
46-
47-
48-
var pathToDepsFile = Path.Combine(pathToPublishFolder,"script.deps.json");
49-
using (var contextReader = new DependencyContextJsonReader())
50-
{
51-
var dependencyContext = contextReader.Read(pathToDepsFile);
52-
var nativeAssets = dependencyContext.GetRuntimeNativeAssets(ScriptEnvironment.Default.RuntimeIdentifier);
53-
foreach (var nativeAsset in nativeAssets)
54-
{
55-
var fullPathToNativeAsset = Path.Combine(pathToPublishFolder,nativeAsset);
56-
if (File.Exists(fullPathToNativeAsset))
57-
{
58-
var destinationPath = Path.Combine(pathToPublishFolder, Path.GetFileName(fullPathToNativeAsset));
59-
File.Copy(fullPathToNativeAsset, destinationPath, overwrite:true);
60-
}
61-
}
62-
}
63-
64-
65-
6635
string CreatePackageSourcesArguments()
6736
{
6837
return packageSources.Length == 0

src/Dotnet.Script.Tests/ScriptExecutionTests.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
using Dotnet.Script.DependencyModel.Environment;
2-
using Dotnet.Script.Shared.Tests;
31
using System;
42
using System.IO;
3+
using Dotnet.Script.DependencyModel.Environment;
4+
using Dotnet.Script.Shared.Tests;
55
using Xunit;
66
using Xunit.Abstractions;
77

@@ -23,8 +23,8 @@ public ScriptExecutionTests(ITestOutputHelper testOutputHelper)
2323
[Fact]
2424
public void ShouldExecuteHelloWorld()
2525
{
26-
var result = ScriptTestRunner.Default.ExecuteFixtureInProcess("HelloWorld");
27-
//Assert.Contains("Hello World", result.output);
26+
var result = ScriptTestRunner.Default.ExecuteFixture("HelloWorld");
27+
Assert.Contains("Hello World", result.output);
2828
}
2929

3030
[Fact]
@@ -44,12 +44,8 @@ public void ShouldIncludeExceptionLineNumberAndFile()
4444
[Fact]
4545
public void ShouldHandlePackageWithNativeLibraries()
4646
{
47-
// We have no story for this on *nix yet
48-
//if (_scriptEnvironment.IsWindows)
49-
{
50-
var result = ScriptTestRunner.Default.ExecuteFixture("NativeLibrary");
51-
Assert.Contains("Connection successful", result.output);
52-
}
47+
var result = ScriptTestRunner.Default.ExecuteFixture("NativeLibrary");
48+
Assert.Contains("Connection successful", result.output);
5349
}
5450

5551
[Fact]

0 commit comments

Comments
 (0)