-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
235 lines (192 loc) · 8.36 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Xml;
namespace X2ProjectGenerator
{
internal static class Program
{
public static void Main(string[] args)
{
HashSet<string> argsSet = new HashSet<string>(args);
bool verifyOnly = argsSet.Remove("--verify-only");
bool excludeContents = argsSet.Remove("--exclude-contents");
if (argsSet.Count == 0) {
throw new Exception("Missing project directory");
} else if (argsSet.Count > 1) {
throw new Exception("Too many arguments");
}
string projectPath = argsSet.First();
Console.WriteLine("Project directory is " + projectPath);
string projectFilePath = GetX2ProjectFilePath(projectPath);
Console.WriteLine("Project file is " + projectFilePath);
Console.WriteLine();
XmlDocument projectFile = LoadProjectFile(projectFilePath);
IEnumerable<string> filesQuery = Directory.EnumerateFiles(projectPath, "*", SearchOption.AllDirectories)
.Where(path => path != projectFilePath)
.Select(path => RemovePrefix(path, projectPath))
.Select(path => path.Replace('/', '\\'))
.Select(path => path.TrimStart('\\'));
if (excludeContents) {
filesQuery = filesQuery.Where(path => {
string[] components = path.Split(new[] {"\\"}, 2, StringSplitOptions.RemoveEmptyEntries);
return components.Count() < 2 || !components[0].StartsWith("Content");
});
}
string[] files = filesQuery.ToArray();
IEnumerable<string> folderPaths = files
.SelectMany(path =>
{
string[] folders = path.Split('\\');
folders = folders.Take(folders.Length - 1).ToArray();
List<string> result = new List<string>();
for (var i = 0; i < folders.Length; i++)
{
string folderPath = "";
for (int j = 0; j <= i; j++)
{
folderPath += folders[j] + "\\";
}
folderPath = folderPath.TrimEnd('\\');
result.Add(folderPath);
}
return result;
})
.Distinct()
.ToArray();
if (verifyOnly) {
if (!CheckProjectFile(projectFile, files, folderPaths)) {
throw new Exception("Project file missing folders or files.");
}
} else {
UpdateProjectFile(projectFile, files, folderPaths);
projectFile.Save(projectFilePath);
}
}
private static string RemovePrefix(string test, string prefix)
{
if (test.StartsWith(prefix)) {
return test.Substring(prefix.Length);
} else {
throw new Exception("String " + test + "does not start with " + prefix);
}
}
private static string GetX2ProjectFilePath(string projectPath)
{
string[] files = Directory.EnumerateFiles(projectPath, "*.x2proj", SearchOption.TopDirectoryOnly).ToArray();
if (files.Length == 0)
{
throw new Exception("Failed to find .x2proj file");
}
if (files.Length > 1)
{
throw new Exception("Found more than one .x2proj file");
}
return files[0];
}
private static XmlDocument LoadProjectFile(string filePath)
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(filePath);
return xmlDocument;
}
private static bool CheckProjectFile(
XmlDocument projectFile,
IEnumerable<string> filePaths,
IEnumerable<string> folderPaths
)
{
XmlNode projectNode = projectFile.ChildNodes.Cast<XmlNode>().First(node => node.Name == "Project");
IEnumerable<XmlNode> itemNodes = projectNode.ChildNodes
.Cast<XmlNode>()
.Where(node => node.Name == "ItemGroup")
.ToList();
List<XmlNode> nodes = itemNodes
.SelectMany(node => node.ChildNodes.Cast<XmlNode>())
.ToList();
IEnumerable<string> existingFiles = GetExistingPaths(nodes, "Content");
IEnumerable<string> existingFolders = GetExistingPaths(nodes, "Folder");
IEnumerable<string> projOnlyFiles = existingFiles.Except(filePaths);
IEnumerable<string> projOnlyFolders = existingFolders.Except(folderPaths);
IEnumerable<string> diskOnlyFiles = filePaths.Except(existingFiles);
IEnumerable<string> diskOnlyFolders = folderPaths.Except(existingFolders);
bool ok = true;
if (diskOnlyFolders.Count() > 0) {
ok = false;
Console.WriteLine("Missing folders: " + string.Join(", ", diskOnlyFolders));
}
if (diskOnlyFiles.Count() > 0) {
ok = false;
Console.WriteLine("Missing files: " + string.Join(", ", diskOnlyFiles));
}
return ok;
}
private static void UpdateProjectFile(
XmlDocument projectFile,
IEnumerable<string> filePaths,
IEnumerable<string> folderPaths
)
{
XmlNode projectNode = projectFile.ChildNodes.Cast<XmlNode>().First(node => node.Name == "Project");
IEnumerable<XmlNode> itemNodes = projectNode.ChildNodes
.Cast<XmlNode>()
.Where(node => node.Name == "ItemGroup")
.ToList();
List<XmlNode> nodes = itemNodes
.SelectMany(node => node.ChildNodes.Cast<XmlNode>())
.ToList();
UpdateItems(projectNode, nodes, "Folder", folderPaths);
UpdateItems(projectNode, nodes, "Content", filePaths);
// Remove current ItemGroups
foreach (XmlNode node in itemNodes)
{
projectNode.RemoveChild(node);
}
// Create 1 new big container
XmlNode itemGroupNode = projectFile.CreateElement("ItemGroup", projectNode.NamespaceURI);
projectNode.AppendChild(itemGroupNode);
foreach (XmlNode node in nodes.OrderByDescending(node => node.Name))
{
itemGroupNode.AppendChild(node);
}
}
private static void UpdateItems(
XmlNode projectNode, List<XmlNode> nodes,
string nodeName, IEnumerable<string> paths
)
{
IEnumerable<XmlNode> newNodes = paths
.Except(GetExistingPaths(nodes, nodeName))
.Select(path => CreateItemNode(projectNode, nodeName, path));
nodes.AddRange(newNodes);
}
private static IEnumerable<string> GetExistingPaths(IEnumerable<XmlNode> nodes, string nodeName)
{
return nodes
.Where(node => node.Name == nodeName)
.Select(node =>
{
Debug.Assert(node.Attributes != null, "node.Attributes != null");
string val = node.Attributes["Include"].Value;
if (nodeName == "Folder") {
val = val.TrimEnd('\\');
}
return val;
})
.ToArray();
}
private static XmlNode CreateItemNode(XmlNode projectNode, string nodeName, string path)
{
XmlDocument projectFile = projectNode.OwnerDocument;
Debug.Assert(projectFile != null, "projectFile != null");
XmlNode node = projectFile.CreateElement(nodeName, projectNode.NamespaceURI);
XmlAttribute includeAttribute = projectFile.CreateAttribute("Include");
includeAttribute.Value = path;
Debug.Assert(node.Attributes != null, "node.Attributes != null");
node.Attributes.Append(includeAttribute);
return node;
}
}
}