Skip to content

Commit

Permalink
some extra scripting reference logic
Browse files Browse the repository at this point in the history
  • Loading branch information
filipw committed Nov 23, 2016
1 parent 696a902 commit 022fb27
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/OmniSharp.Script/ScriptProjectSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
using Microsoft.CodeAnalysis.Scripting.Hosting;
using Microsoft.CodeAnalysis.Text;
using Microsoft.DotNet.ProjectModel;
using Microsoft.DotNet.ProjectModel.Compilation;
using Microsoft.DotNet.ProjectModel.Graph;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -58,10 +56,12 @@ public void Initalize(IConfiguration configuration)
Context.RootPath = Env.Path;
Logger.LogInformation($"Found {allCsxFiles.Length} CSX files.");


// explicitly inherit scripting library references to all global script object (InteractiveScriptGlobals) to be recognized
var inheritedCompileLibraries = DependencyContext.Default.CompileLibraries.Where(x =>
x.Name.ToLowerInvariant().StartsWith("microsoft.codeanalysis")).ToList();

var runtimeContexts = ProjectContext.CreateContextForEachTarget(Env.Path);
var inheritedCompileLibraries = DependencyContext.Default.CompileLibraries.Where(x =>
x.Name.ToLowerInvariant().StartsWith("microsoft.codeanalysis") ||
x.Name.ToLowerInvariant().StartsWith("system.runtime"));

// if we have no context, then we also have no dependencies
// we can assume desktop framework
Expand All @@ -70,6 +70,8 @@ public void Initalize(IConfiguration configuration)
{
Logger.LogInformation("Unable to find project context for CSX files. Will default to non-context usage.");
Context.CommonReferences.Add(MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location));
inheritedCompileLibraries.AddRange(DependencyContext.Default.CompileLibraries.Where(x =>
x.Name.ToLowerInvariant().StartsWith("system.runtime")));
}
// otherwise we will grab dependencies for the script from the runtime context
else
Expand All @@ -89,13 +91,13 @@ public void Initalize(IConfiguration configuration)
Context.CommonReferences.Add(MetadataReference.CreateFromFile(compilationAssembly.ResolvedPath));
}

// if we are on .NET Core, we may need to avoid injecting System.Runtime
#if !NET46
var needsSystemRuntimeReference = Context.CommonReferences.Any(x => !x.Display.ToLowerInvariant().EndsWith("system.runtime.dll"));
inheritedCompileLibraries = needsSystemRuntimeReference ?
DependencyContext.Default.CompileLibraries.Where(x => x.Name.ToLowerInvariant().StartsWith("microsoft.codeanalysis") || x.Name.ToLowerInvariant().StartsWith("system.runtime")) :
DependencyContext.Default.CompileLibraries.Where(x => x.Name.ToLowerInvariant().StartsWith("microsoft.codeanalysis"));
#endif
// for non .NET Core, include System.Runtime
if (runtimeContext.TargetFramework.Framework != ".NETCoreApp")
{
inheritedCompileLibraries.AddRange(DependencyContext.Default.CompileLibraries.Where(x =>
x.Name.ToLowerInvariant().StartsWith("system.runtime")));
}

}

// inject all inherited assemblies
Expand Down

0 comments on commit 022fb27

Please sign in to comment.