/
Projects.cs
140 lines (116 loc) · 4.62 KB
/
Projects.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
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.MSBuild;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LinqAF.Generator
{
class Projects:
IDisposable
{
public struct DocumentPair
{
public string Name { get; set; }
public Document Document { get; set; }
}
public Project Template { get; private set; }
public Project Output { get; private set; }
public IEnumerable<Document> EnumerableDocuments
{
get
{
foreach(var pair in EnumerableDocumentsWithNames)
{
yield return pair.Document;
}
}
}
public IEnumerable<DocumentPair> EnumerableDocumentsWithNames
{
get
{
foreach (var kv in EnumerableRoots)
{
var name = kv.Key;
var id = kv.Value;
var doc = Output.GetDocument(id);
yield return new DocumentPair { Name = name, Document = doc };
}
}
}
IReadOnlyDictionary<string, DocumentId> EnumerableRoots { get; set; }
IReadOnlyDictionary<DocumentId, string> EnumerableRootsLookup { get; set; }
public Workspace Workspace { get; private set; }
Solution Solution { get; set; }
public void Save()
{
var templateChanges = Solution.GetProject(Template.Id).GetChanges(Template);
var anythingChangedInTemplate =
templateChanges.GetAddedAdditionalDocuments().Any() ||
templateChanges.GetAddedAnalyzerReferences().Any() ||
templateChanges.GetAddedDocuments().Any() ||
templateChanges.GetAddedMetadataReferences().Any() ||
templateChanges.GetAddedProjectReferences().Any() ||
templateChanges.GetChangedAdditionalDocuments().Any() ||
templateChanges.GetChangedDocuments().Any() ||
templateChanges.GetRemovedAdditionalDocuments().Any() ||
templateChanges.GetRemovedAnalyzerReferences().Any() ||
templateChanges.GetRemovedDocuments().Any() ||
templateChanges.GetRemovedMetadataReferences().Any() ||
templateChanges.GetRemovedProjectReferences().Any();
if (anythingChangedInTemplate)
{
throw new Exception();
}
if (!Workspace.TryApplyChanges(Solution))
{
throw new Exception();
}
}
public void SetEnumerableRoots(IDictionary<string, DocumentId> roots)
{
if (EnumerableRoots != null) throw new InvalidOperationException();
EnumerableRoots = new ReadOnlyDictionary<string, DocumentId>(roots);
EnumerableRootsLookup = new ReadOnlyDictionary<DocumentId, string>(roots.ToDictionary(kv => kv.Value, kv => kv.Key));
}
public void ModifyOutput(Func<Project, Project> modifier)
{
var newOutput = modifier(Output);
if(newOutput.Id != Output.Id)
{
throw new Exception();
}
var newSolution = newOutput.Solution;
Output = newOutput;
Solution = newSolution;
}
public static Projects Load(string solutionPath, string templateProjectName, string outputProjectName)
{
var workspace = MSBuildWorkspace.Create();
var solution = workspace.OpenSolutionAsync(solutionPath).Result;
var templateProject = solution.Projects.Single(p => p.Name.Equals(templateProjectName, StringComparison.OrdinalIgnoreCase));
var outputProject = solution.Projects.Single(p => p.Name.Equals(outputProjectName, StringComparison.OrdinalIgnoreCase));
while(outputProject.Documents.Count() > 0)
{
var toRemove = outputProject.Documents.First();
outputProject = outputProject.RemoveDocument(toRemove.Id);
}
return
new Projects
{
Workspace = workspace,
Solution = solution,
Template = templateProject,
Output = outputProject,
};
}
public void Dispose()
{
Workspace.Dispose();
Workspace = null;
}
}
}