-
Notifications
You must be signed in to change notification settings - Fork 420
/
Copy pathScriptProjectSystem.cs
190 lines (161 loc) · 7.36 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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Composition;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Dotnet.Script.DependencyModel.NuGet;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Scripting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using OmniSharp.FileSystem;
using OmniSharp.FileWatching;
using OmniSharp.Mef;
using OmniSharp.Models.WorkspaceInformation;
using OmniSharp.Services;
namespace OmniSharp.Script
{
[ExportProjectSystem(ProjectSystemNames.ScriptProjectSystem), Shared]
public class ScriptProjectSystem : IProjectSystem
{
private const string CsxExtension = ".csx";
private readonly ConcurrentDictionary<string, ProjectInfo> _projects = new ConcurrentDictionary<string, ProjectInfo>();
private readonly ScriptContextProvider _scriptContextProvider;
private readonly OmniSharpWorkspace _workspace;
private readonly IOmniSharpEnvironment _env;
private readonly ILogger _logger;
private readonly IFileSystemWatcher _fileSystemWatcher;
private readonly FileSystemHelper _fileSystemHelper;
private ScriptOptions _scriptOptions;
private Lazy<ScriptContext> _scriptContext;
[ImportingConstructor]
public ScriptProjectSystem(OmniSharpWorkspace workspace, IOmniSharpEnvironment env, ILoggerFactory loggerFactory,
ScriptContextProvider scriptContextProvider, IFileSystemWatcher fileSystemWatcher, FileSystemHelper fileSystemHelper)
{
_workspace = workspace;
_env = env;
_fileSystemWatcher = fileSystemWatcher;
_fileSystemHelper = fileSystemHelper;
_logger = loggerFactory.CreateLogger<ScriptProjectSystem>();
_scriptContextProvider = scriptContextProvider;
}
public string Key { get; } = "Script";
public string Language { get; } = LanguageNames.CSharp;
public IEnumerable<string> Extensions { get; } = new[] { CsxExtension };
public bool EnabledByDefault { get; } = true;
public bool Initialized { get; private set; }
public void Initalize(IConfiguration configuration)
{
if (Initialized) return;
_scriptOptions = new ScriptOptions();
ConfigurationBinder.Bind(configuration, _scriptOptions);
_logger.LogInformation($"Detecting CSX files in '{_env.TargetDirectory}'.");
// Nothing to do if there are no CSX files
var allCsxFiles = _fileSystemHelper.GetFiles("**/*.csx").ToArray();
_scriptContext = new Lazy<ScriptContext>(() => _scriptContextProvider.CreateScriptContext(_scriptOptions, allCsxFiles, _workspace.EditorConfigEnabled));
if (allCsxFiles.Length == 0)
{
_logger.LogInformation("Did not find any CSX files");
Initialized = true;
// Watch CSX files in order to add/remove them in workspace
_fileSystemWatcher.Watch(CsxExtension, OnCsxFileChanged);
return;
}
_logger.LogInformation($"Found {allCsxFiles.Length} CSX files.");
// Each .CSX file becomes an entry point for its own project
// Every #loaded file will be part of the project too
foreach (var csxPath in allCsxFiles)
{
AddToWorkspace(csxPath);
}
// Watch CSX files in order to add/remove them in workspace
_fileSystemWatcher.Watch(CsxExtension, OnCsxFileChanged);
Initialized = true;
}
private void OnCsxFileChanged(string filePath, FileChangeType changeType)
{
if (changeType == FileChangeType.Unspecified && !File.Exists(filePath) ||
changeType == FileChangeType.Delete)
{
RemoveFromWorkspace(filePath);
}
if (changeType == FileChangeType.Unspecified && File.Exists(filePath) ||
changeType == FileChangeType.Create)
{
AddToWorkspace(filePath);
}
}
private void AddToWorkspace(string csxPath)
{
try
{
var csxFileName = Path.GetFileName(csxPath);
var project = _scriptContext.Value.ScriptProjectProvider.CreateProject(csxFileName, _scriptContext.Value.MetadataReferences, csxPath, _scriptContext.Value.GlobalsType);
if (_scriptOptions.IsNugetEnabled())
{
var scriptMap = _scriptContext.Value.CompilationDependencies.ToDictionary(rdt => rdt.Name, rdt => rdt.Scripts);
var options = project.CompilationOptions.WithSourceReferenceResolver(
new NuGetSourceReferenceResolver(ScriptSourceResolver.Default,
scriptMap));
project = project.WithCompilationOptions(options);
}
// 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 void RemoveFromWorkspace(string csxPath)
{
if (_projects.TryRemove(csxPath, out var project))
{
_workspace.RemoveProject(project.Id);
_logger.LogInformation($"Removed CSX project '{csxPath}' from the workspace.");
}
}
private ProjectInfo GetProjectFileInfo(string path)
{
if (!_projects.TryGetValue(path, out ProjectInfo projectFileInfo))
{
return null;
}
return projectFileInfo;
}
Task IProjectSystem.WaitForIdleAsync() => Task.CompletedTask;
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));
}
Task<object> IProjectSystem.GetWorkspaceModelAsync(WorkspaceInformationRequest request)
{
var scriptContextModels = new List<ScriptContextModel>();
foreach (var project in _projects)
{
scriptContextModels.Add(new ScriptContextModel(project.Key, project.Value));
}
return Task.FromResult<object>(new ScriptContextModelCollection(scriptContextModels));
}
}
}