This repository was archived by the owner on Mar 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathWorkspace.cs
More file actions
331 lines (280 loc) · 12.5 KB
/
Copy pathWorkspace.cs
File metadata and controls
331 lines (280 loc) · 12.5 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Xml;
using Microsoft;
using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation;
using Microsoft.Language.Xml;
namespace ProjectFileTools.MSBuild
{
/// <summary>
/// Contains an MSBuild project and logic to extract information from it
/// </summary>
public class Workspace : IWorkspace, IDisposableObservable
{
private ProjectCollection _collection;
private readonly HashSet<string> _containedFiles;
private bool _needsReload;
private Project _project;
private List<FileSystemWatcher> _watchers;
private readonly PropertyInfo _projectItemElementXmlElementProperty = typeof(ProjectElement).GetProperty("XmlElement", BindingFlags.NonPublic | BindingFlags.Instance);
internal Workspace(string filePath)
{
_collection = new ProjectCollection();
_containedFiles = new HashSet<string>(StringComparer.Ordinal);
_watchers = new List<FileSystemWatcher>();
_needsReload = false;
try
{
_project = _collection.LoadProject(filePath);
UpdateContainedFiles();
}
// TODO: Propagate error to the errors list
catch
{
_project = null;
}
}
public bool IsDisposed { get; private set; }
public void Dispose()
{
IsDisposed = true;
List<FileSystemWatcher> watchers = Interlocked.Exchange(ref _watchers, null);
if (watchers != null)
{
foreach (FileSystemWatcher watcher in watchers)
{
watcher.Changed -= MarkReload;
watcher.Deleted -= MarkReload;
watcher.Renamed -= MarkReload;
watcher.Dispose();
}
}
}
public bool EvaluateCondition(string text)
{
return _project?.CreateProjectInstance().EvaluateCondition(text) ?? false;
}
public string GetEvaluatedPropertyValue(string text)
{
return _project?.ExpandString(text) ?? string.Empty;
}
public List<Definition> GetItemProvenance(string fileSpec)
{
List<Definition> results = new List<Definition>();
if (_project == null)
{
return results;
}
List<string> items = GetPathsFromFileSpec(fileSpec);
XmlDocument doc = new XmlDocument();
foreach (string item in items)
{
foreach (ProjectItem include in _project.GetItemsByEvaluatedInclude(item))
{
foreach (ProvenanceResult record in _project.GetItemProvenance(include))
{
XmlElement element = (XmlElement)_projectItemElementXmlElementProperty.GetValue(record.ItemElement);
XmlElement clone = doc.CreateElement(element.LocalName);
foreach (XmlAttribute node in element.Attributes.OfType<XmlAttribute>().ToList())
{
clone.SetAttribute(node.LocalName, node.Value);
}
string lineText = clone.OuterXml;
results.Add(new Definition(record.ItemElement.ContainingProject.FullPath, include.EvaluatedInclude, record.Operation.ToString(), lineText, record.ItemElement.Location.Line, record.ItemElement.Location.Column));
}
}
}
return results;
}
public List<Definition> GetItems(string fileSpec)
{
if (_project == null)
{
return new List<Definition>();
}
List<string> items = GetPathsFromFileSpec(fileSpec);
List<Definition> results = new List<Definition>();
string projectName = Path.GetFileNameWithoutExtension(_project.FullPath);
foreach (string item in items)
{
bool isIncluded = _project.GetItemsByEvaluatedInclude(item).Any();
string message = isIncluded ? "Included" : "Not Included";
results.Add(new Definition(item, projectName, message, "(Glob Match)"));
}
return results;
}
/// <summary>
/// Returns the URL of the file that contains the definition of the item at the current position
/// </summary>
/// <param name="filePath">Current file</param>
/// <param name="sourceText">Text in the current file</param>
/// <param name="position">Position of item that is to be resolved</param>
/// <returns></returns>
public List<Definition> ResolveDefinition(string filePath, string sourceText, int position)
{
Verify.NotDisposed(this);
List<Definition> definitions = new List<Definition>();
if (_project != null)
{
XmlDocumentSyntax root = Parser.ParseText(sourceText);
SyntaxNode syntaxNode = root.FindNode(position);
// Resolves Definition for properties e.g. $(foo)
if (syntaxNode.Kind == SyntaxKind.XmlTextLiteralToken && Utilities.IsProperty(sourceText.Substring(syntaxNode.Span.Start, syntaxNode.FullWidth), position - syntaxNode.Span.Start, out string propertyName))
{
foreach (ProjectProperty property in _project.Properties)
{
if (property.Name == propertyName)
{
ProjectProperty currentProperty = property;
while (currentProperty.Predecessor != null)
{
if (currentProperty.Xml?.Location != null)
{
ElementLocation location = currentProperty.Xml.Location;
definitions.Add(new Definition(location.File, Path.GetFileNameWithoutExtension(_project.Xml.Location.File), currentProperty.Name + " Definitions", currentProperty.EvaluatedValue, location.Line, location.Column));
}
currentProperty = currentProperty.Predecessor;
}
if (currentProperty.Xml?.Location != null)
{
ElementLocation lastLocation = currentProperty.Xml.Location;
definitions.Add(new Definition(lastLocation.File, Path.GetFileNameWithoutExtension(_project.Xml.Location.File), currentProperty.Name + " Definitions", currentProperty.EvaluatedValue, lastLocation.Line, lastLocation.Column));
}
break;
}
}
}
// Resolves Definition for regular imports
else if (syntaxNode.ParentElement != null && syntaxNode.ParentElement.Name.Equals(SyntaxNames.Import))
{
while (syntaxNode.Parent.ParentElement == syntaxNode.ParentElement)
{
syntaxNode = syntaxNode.Parent;
}
int nodeStart = syntaxNode.Parent.Span.Start;
int col = nodeStart - Utilities.GetStartOfLine(sourceText, nodeStart) + 1;
int line = Utilities.GetLine(sourceText, nodeStart) + 1;
foreach (ResolvedImport import in _project.Imports)
{
ElementLocation location = import.ImportingElement.Location;
if (location.File == filePath && col == location.Column && line == location.Line)
{
definitions.Add(new Definition(import.ImportedProject.FullPath, Path.GetFileNameWithoutExtension(_project.Xml.Location.File), "Imported Files", Path.GetFileName(import.ImportedProject.FullPath)));
}
}
}
// Resolves Definition for the project's sdk
else if (syntaxNode.ParentElement != null && syntaxNode.ParentElement.Name.Equals(SyntaxNames.Project))
{
bool foundSdk = false;
for (int i = 0; i < 3; i++)
{
if (sourceText.Substring(syntaxNode.Start, 3).Equals(SyntaxNames.Sdk))
{
foundSdk = true;
break;
}
syntaxNode = syntaxNode.Parent;
}
if (foundSdk)
{
foreach (ResolvedImport import in _project.Imports)
{
ElementLocation location = import.ImportingElement.Location;
if (location.File == filePath && 0 == location.Column && 0 == location.Line)
{
definitions.Add(new Definition(import.ImportedProject.FullPath, Path.GetFileNameWithoutExtension(_project.Xml.Location.File), "Sdk Imports", Path.GetFileName(import.ImportedProject.FullPath)));
}
}
}
}
}
return definitions;
}
internal bool ContainsProject(string filePath)
{
Verify.NotDisposed(this);
ReloadIfNecessary();
return _containedFiles.Contains(filePath);
}
private List<string> GetPathsFromFileSpec(string fileSpec)
{
if (_project == null)
{
return new List<string>();
}
IList<ProjectItem> items = _project.AddItem("___TEMPORARY___", fileSpec);
_project.RemoveItems(items);
List<string> files = new List<string>();
foreach (ProjectItem item in items)
{
files.Add(item.EvaluatedInclude);
}
return files;
}
private void MarkReload(object sender, FileSystemEventArgs e)
{
_needsReload = true;
}
private void ReloadIfNecessary()
{
if (_needsReload && _project != null)
{
string tempPath = _project.FullPath;
try
{
ProjectCollection tempCollection = new ProjectCollection();
Project tempProject = tempCollection.LoadProject(tempPath);
_collection.UnloadAllProjects();
_collection = tempCollection;
_project = tempProject;
UpdateContainedFiles();
}
// TODO: Propagate error to the errors list
catch
{
}
_needsReload = false;
}
}
private void UpdateContainedFiles()
{
_containedFiles.Clear();
foreach (FileSystemWatcher watcher in _watchers)
{
watcher.Changed -= MarkReload;
watcher.Deleted -= MarkReload;
watcher.Renamed -= MarkReload;
watcher.Dispose();
}
_watchers.Clear();
if (_project != null)
{
foreach (ResolvedImport import in _project.Imports)
{
_containedFiles.Add(import.ImportedProject.FullPath);
}
_containedFiles.Add(_project.FullPath);
foreach (string path in _containedFiles)
{
FileSystemWatcher watcher = new FileSystemWatcher
{
Path = Path.GetDirectoryName(path),
Filter = Path.GetFileName(path),
NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName,
EnableRaisingEvents = true
};
watcher.Changed += MarkReload;
watcher.Deleted += MarkReload;
watcher.Renamed += MarkReload;
_watchers.Add(watcher);
}
}
}
}
}