-
Notifications
You must be signed in to change notification settings - Fork 418
/
Copy pathScriptProjectSystem.cs
260 lines (225 loc) · 10.3 KB
/
ScriptProjectSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using System;
using System.Collections.Generic;
using System.Composition;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.Logging;
using OmniSharp.Models.WorkspaceInformation;
using OmniSharp.Services;
using Dotnet.Script.DependencyModel.Compilation;
using LogLevel = Dotnet.Script.DependencyModel.Logging.LogLevel;
namespace OmniSharp.Script
{
[Export(typeof(IProjectSystem)), Shared]
public class ScriptProjectSystem : IProjectSystem
{
private const string CsxExtension = ".csx";
private readonly MetadataFileReferenceCache _metadataFileReferenceCache;
// used for tracking purposes only
private readonly HashSet<string> _assemblyReferences = new HashSet<string>();
private readonly Dictionary<string, ProjectInfo> _projects;
private readonly OmniSharpWorkspace _workspace;
private readonly IOmniSharpEnvironment _env;
private readonly ILogger _logger;
private readonly CompilationDependencyResolver _compilationDependencyResolver;
[ImportingConstructor]
public ScriptProjectSystem(OmniSharpWorkspace workspace, IOmniSharpEnvironment env, ILoggerFactory loggerFactory,
MetadataFileReferenceCache metadataFileReferenceCache)
{
_metadataFileReferenceCache = metadataFileReferenceCache;
_workspace = workspace;
_env = env;
_logger = loggerFactory.CreateLogger<ScriptProjectSystem>();
_projects = new Dictionary<string, ProjectInfo>();
_compilationDependencyResolver = new CompilationDependencyResolver(type =>
{
// Prefix with "OmniSharp" so that we make it through the log filter.
var categoryName = $"OmniSharp.Script.{type.FullName}";
var dependencyResolverLogger = loggerFactory.CreateLogger(categoryName);
return ((level, message) =>
{
if (level == LogLevel.Debug)
{
dependencyResolverLogger.LogDebug(message);
}
if (level == LogLevel.Info)
{
dependencyResolverLogger.LogInformation(message);
}
});
});
}
public string Key => "Script";
public string Language => LanguageNames.CSharp;
public IEnumerable<string> Extensions { get; } = new[] { CsxExtension };
public void Initalize(IConfiguration configuration)
{
var scriptHelper = new ScriptHelper(configuration);
_logger.LogInformation($"Detecting CSX files in '{_env.TargetDirectory}'.");
// Nothing to do if there are no CSX files
var allCsxFiles = Directory.GetFiles(_env.TargetDirectory, "*.csx", SearchOption.AllDirectories);
if (allCsxFiles.Length == 0)
{
_logger.LogInformation("Could not find any CSX files");
return;
}
_logger.LogInformation($"Found {allCsxFiles.Length} CSX files.");
// explicitly inherit scripting library references to all global script object (CommandLineScriptGlobals) to be recognized
var inheritedCompileLibraries = DependencyContext.Default.CompileLibraries.Where(x =>
x.Name.ToLowerInvariant().StartsWith("microsoft.codeanalysis")).ToList();
// explicitly include System.ValueTuple
inheritedCompileLibraries.AddRange(DependencyContext.Default.CompileLibraries.Where(x =>
x.Name.ToLowerInvariant().StartsWith("system.valuetuple")));
if (!bool.TryParse(configuration["enableScriptNuGetReferences"], out var enableScriptNuGetReferences))
{
enableScriptNuGetReferences = false;
}
var commonReferences = new HashSet<MetadataReference>();
var compilationDependencies = TryGetCompilationDependencies(enableScriptNuGetReferences);
// if we have no compilation dependencies
// we will assume desktop framework
// and add default CLR references
// same applies for having a context that is not a .NET Core app
if (!compilationDependencies.Any())
{
_logger.LogInformation("Unable to find dependency context for CSX files. Will default to non-context usage (Destkop CLR scripts).");
AddDefaultClrMetadataReferences(commonReferences);
}
else
{
foreach (var compilationAssembly in compilationDependencies)
{
_logger.LogDebug("Discovered script compilation assembly reference: " + compilationAssembly);
AddMetadataReference(commonReferences, compilationAssembly);
}
}
// inject all inherited assemblies
foreach (var inheritedCompileLib in inheritedCompileLibraries.SelectMany(x => x.ResolveReferencePaths()))
{
_logger.LogDebug("Adding implicit reference: " + inheritedCompileLib);
AddMetadataReference(commonReferences, inheritedCompileLib);
}
// Each .CSX file becomes an entry point for it's own project
// Every #loaded file will be part of the project too
foreach (var csxPath in allCsxFiles)
{
try
{
var csxFileName = Path.GetFileName(csxPath);
var project = scriptHelper.CreateProject(csxFileName, commonReferences);
// add CSX project to workspace
_workspace.AddProject(project);
_workspace.AddDocument(project.Id, csxPath, SourceCodeKind.Script);
_projects[csxPath] = project;
_logger.LogInformation($"Added CSX project '{csxPath}' to the workspace.");
}
catch (Exception ex)
{
_logger.LogError(ex, $"{csxPath} will be ignored due to an following error");
}
}
}
private string[] TryGetCompilationDependencies(bool enableScriptNuGetReferences)
{
try
{
return _compilationDependencyResolver.GetDependencies(_env.TargetDirectory, enableScriptNuGetReferences).ToArray();
}
catch (Exception e)
{
_logger.LogError("Failed to resolve compilation dependencies", e);
return Array.Empty<string>();
}
}
private void AddDefaultClrMetadataReferences(HashSet<MetadataReference> commonReferences)
{
var assemblies = new[]
{
typeof(object).GetTypeInfo().Assembly,
typeof(Enumerable).GetTypeInfo().Assembly,
typeof(Stack<>).GetTypeInfo().Assembly,
typeof(Lazy<,>).GetTypeInfo().Assembly,
FromName("System.Runtime"),
FromName("mscorlib")
};
var references = assemblies
.Where(a => a != null)
.Select(a => a.Location)
.Distinct()
.Select(l => _metadataFileReferenceCache.GetMetadataReference(l));
foreach (var reference in references)
{
commonReferences.Add(reference);
}
Assembly FromName(string assemblyName)
{
try
{
return Assembly.Load(new AssemblyName(assemblyName));
}
catch
{
return null;
}
}
}
private void AddMetadataReference(ISet<MetadataReference> referenceCollection, string fileReference)
{
if (!File.Exists(fileReference))
{
_logger.LogWarning($"Couldn't add reference to '{fileReference}' because the file was not found.");
return;
}
var metadataReference = _metadataFileReferenceCache.GetMetadataReference(fileReference);
if (metadataReference == null)
{
_logger.LogWarning($"Couldn't add reference to '{fileReference}' because the loaded metadata reference was null.");
return;
}
referenceCollection.Add(metadataReference);
_assemblyReferences.Add(fileReference);
_logger.LogDebug($"Added reference to '{fileReference}'");
}
private ProjectInfo GetProjectFileInfo(string path)
{
if (!_projects.TryGetValue(path, out ProjectInfo projectFileInfo))
{
return null;
}
return projectFileInfo;
}
Task<object> IProjectSystem.GetProjectModelAsync(string filePath)
{
// only react to .CSX file paths
if (!filePath.EndsWith(CsxExtension, StringComparison.OrdinalIgnoreCase))
{
return Task.FromResult<object>(null);
}
var document = _workspace.GetDocument(filePath);
var projectFilePath = document != null
? document.Project.FilePath
: filePath;
var projectInfo = GetProjectFileInfo(projectFilePath);
if (projectInfo == null)
{
_logger.LogDebug($"Could not locate project for '{projectFilePath}'");
return Task.FromResult<object>(null);
}
return Task.FromResult<object>(new ScriptContextModel(filePath, projectInfo, _assemblyReferences));
}
Task<object> IProjectSystem.GetWorkspaceModelAsync(WorkspaceInformationRequest request)
{
var scriptContextModels = new List<ScriptContextModel>();
foreach (var project in _projects)
{
scriptContextModels.Add(new ScriptContextModel(project.Key, project.Value, _assemblyReferences));
}
return Task.FromResult<object>(new ScriptContextModelCollection(scriptContextModels));
}
}
}