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 24, 2016
1 parent 696a902 commit f1e2491
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
32 changes: 13 additions & 19 deletions src/OmniSharp.Script/FileParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,22 @@ namespace OmniSharp.Script
{
public class FileParser
{
private readonly string _workingDirectory;
private readonly FileParserResult _result;

public FileParser(string workingDirectory)
public static FileParserResult ProcessFile(string path)
{
_workingDirectory = workingDirectory;
_result = new FileParserResult();
var result = new FileParserResult();
ParseFile(path, result);
return result;
}

public FileParserResult ProcessFile(string path)
{
ParseFile(path);
return _result;
}

private void ParseFile(string path)
private static void ParseFile(string path, FileParserResult result)
{
var fullPath = Path.GetFullPath(path);
if (_result.LoadedScripts.Contains(fullPath))
if (result.LoadedScripts.Contains(fullPath))
{
return;
}

_result.LoadedScripts.Add(fullPath);
result.LoadedScripts.Add(fullPath);

var scriptCode = File.ReadAllText(fullPath);

Expand All @@ -42,23 +34,25 @@ private void ParseFile(string path)
var namespaces = syntaxTree.GetCompilationUnitRoot().Usings.Select(x => x.Name.ToString());
foreach (var ns in namespaces)
{
_result.Namespaces.Add(ns.Trim());
result.Namespaces.Add(ns.Trim());
}

var refs = syntaxTree.GetCompilationUnitRoot().GetReferenceDirectives().Select(x => x.File.ToString());
foreach (var reference in refs)
{
_result.References.Add(reference.Replace("\"", string.Empty));
result.References.Add(reference.Replace("\"", string.Empty));
}

var loads = syntaxTree.GetCompilationUnitRoot().GetLoadDirectives().Select(x => x.File.ToString());
foreach (var load in loads)
{
var filePath = load.Replace("\"", string.Empty);
var loadFullPath = Path.IsPathRooted(filePath) ? filePath : Path.Combine(_workingDirectory, filePath);
var currentWorkingDirectory = Path.GetDirectoryName(fullPath);

var loadFullPath = Path.IsPathRooted(filePath) ? filePath : Path.Combine(currentWorkingDirectory, filePath);
if (!string.IsNullOrWhiteSpace(loadFullPath))
{
ParseFile(loadFullPath);
ParseFile(loadFullPath, result);
}
}
}
Expand Down
35 changes: 20 additions & 15 deletions src/OmniSharp.Script/ScriptProjectSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
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;
using OmniSharp.Models.v1;
using OmniSharp.Roslyn.Models;
using OmniSharp.Services;

namespace OmniSharp.Script
Expand Down Expand Up @@ -58,10 +57,11 @@ public void Initalize(IConfiguration configuration)
Context.RootPath = Env.Path;
Logger.LogInformation($"Found {allCsxFiles.Length} CSX files.");

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"));
// 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 = File.Exists(Path.Combine(Env.Path, "project.json")) ? ProjectContext.CreateContextForEachTarget(Env.Path) : null;

// if we have no context, then we also have no dependencies
// we can assume desktop framework
Expand All @@ -70,6 +70,9 @@ 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));
Context.CommonReferences.Add(MetadataReference.CreateFromFile(typeof(Enumerable).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,21 +92,24 @@ 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
//#if NET46
foreach (var inheritedCompileLib in inheritedCompileLibraries.SelectMany(x => x.ResolveReferencePaths()))
{
Logger.LogDebug("Adding implicit reference: " + inheritedCompileLib);
Context.CommonReferences.Add(MetadataReference.CreateFromFile(inheritedCompileLib));
}
//#endif


// Process each .CSX file
foreach (var csxPath in allCsxFiles)
Expand Down Expand Up @@ -142,8 +148,7 @@ private ProjectInfo CreateCsxProject(string csxPath)
Logger.LogInformation($"Processing script {csxPath}...");
Context.CsxFilesBeingProcessed.Add(csxPath);

var fileParser = new FileParser(Context.RootPath);
var processResult = fileParser.ProcessFile(csxPath);
var processResult = FileParser.ProcessFile(csxPath);

// CSX file usings
Context.CsxUsings[csxPath] = processResult.Namespaces.Union(Context.CommonUsings).ToList();
Expand Down

0 comments on commit f1e2491

Please sign in to comment.