-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectAnalyzer.cs
96 lines (84 loc) · 2.74 KB
/
ProjectAnalyzer.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
using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace MsBuildDebugger
{
public class ProjectAnalyzer
{
private readonly ProjectCollection collection;
private readonly ProjectInstance instance;
public ProjectAnalyzer(string projectFile)
{
collection = ProjectCollection.GlobalProjectCollection;
var project = collection.LoadProject(projectFile);
instance = BuildManager.DefaultBuildManager.GetProjectInstanceForBuild(project);
TargetTree = new TargetTree(this);
TargetTree.SetDefaultTargets(GetDefaultTargets());
}
public TargetTree TargetTree { get; }
public string ProjectFileName()
{
return Path.GetFileName(instance.FullPath);
}
public ProjectTargetInstance GetTarget(string name)
{
return instance.Targets[name];
}
public ProjectTargetInstance[] GetTargets(string query)
{
var targets = instance.Targets;
var result = targets.Where(pair =>
{
return Regex.IsMatch(pair.Key, query);
}).Select(item => item.Value).ToArray();
return result;
}
public string[] GetDefaultTargets()
{
return instance.DefaultTargets.ToArray();
}
public ProjectPropertyInstance[] GetProperties(string query)
{
var props = instance.Properties;
var result = props.Where(prop =>
{
return Regex.IsMatch(prop.Name, query);
}).ToArray();
return result;
}
public string GetPropertyValue(string name)
{
return instance.GetPropertyValue(name);
}
public ProjectItemInstance[] GetItems(string query)
{
var items = instance.Items;
var result = items.Where(item =>
{
return Regex.IsMatch(item.ItemType, query);
}).ToArray();
return result;
}
public ProjectItemInstance GetItem(string itemType)
{
foreach(var item in instance.Items) {
if (itemType.Equals(item.ItemType, StringComparison.OrdinalIgnoreCase))
{
return item;
}
}
return null;
}
public ProjectTargetInstance[] GetStackTrace(string startTarget)
{
var trace = new List<ProjectTargetInstance>();
trace.Add(GetTarget(startTarget));
return trace.ToArray();
}
}
}