-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathProgram.cs
More file actions
369 lines (330 loc) · 11.4 KB
/
Program.cs
File metadata and controls
369 lines (330 loc) · 11.4 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
using NDesk.Options;
namespace SyncProjects
{
// TODO: This tool might not work for Empty destination projects.
class Program
{
private const string Ns = "{http://schemas.microsoft.com/developer/msbuild/2003}";
static int Main( string[] args )
{
var file = "Sync.xml";
var sourceBasePath = "src" + Path.DirectorySeparatorChar;
var projectExtension = ".csproj";
bool help = false;
var options =
new OptionSet
{
{ "d|def=", "File path to synchronization definition. Default: Sync.xml", v => file = v },
{ "s|src=", "File path to base directory of source tree. Default: src" + Path.DirectorySeparatorChar, v => sourceBasePath = v },
{ "e|ext=", "Extension (including leading dot) of the project file. Default: .csproj", v => projectExtension = v },
{ "h|?|help", "Show this help.", _ => help = true },
};
options.Parse( args );
if ( help )
{
Console.WriteLine( "SyncProjects" );
Console.WriteLine();
Console.WriteLine( "Usage: SyncProjects.exe [<OPTIONS>]" );
Console.WriteLine();
Console.WriteLine( "Options:" );
options.WriteOptionDescriptions( Console.Out );
return 0;
}
try
{
SynchronizeProjects( file, sourceBasePath, projectExtension );
return 0;
}
catch ( Exception ex )
{
Console.Error.WriteLine( ex );
return Marshal.GetHRForException( ex );
}
}
private static readonly XElement EmptyExcludes = new XElement( "Excludes" );
private static void SynchronizeProjects( string syncFilePath, string sourceBasePath, string projectFileExtension )
{
var sync = XDocument.Load( syncFilePath );
if ( sync.Root == null || sync.Root.Name.LocalName != "ProjectSync" )
{
throw new XmlException( "Invalid sync file." );
}
foreach ( var project in
sync.Root.Elements( "Project" )
.Select( p =>
new
{
Name = p.Attribute( "Name" ),
Base = p.Attribute( "Base" ),
Includes =
p.Elements( "Include" )
.Select( ToPattern ).Where( pattern => pattern != null ).ToArray(),
Excludes =
p.Elements( "Exclude" )
.Select( ToPattern ).Where( pattern => pattern != null ).ToArray(),
Preserves =
p.Elements( "Preserve" )
.Select( ToPattern ).Where( pattern => pattern != null ).ToArray()
}
) )
{
var projectFilePath = Path.Combine( sourceBasePath, project.Name.Value, project.Name.Value + projectFileExtension );
var projectXml = XDocument.Load( projectFilePath, LoadOptions.SetBaseUri | LoadOptions.SetLineInfo );
if ( projectXml.Root == null || projectXml.Root.Name.LocalName != "Project" )
{
throw new XmlException( "Invalid project file :" + projectFilePath );
}
var baseProjectFilePath = Path.Combine( sourceBasePath, project.Base.Value, project.Base.Value + projectFileExtension );
var baseProjectXml = XDocument.Load( baseProjectFilePath, LoadOptions.SetBaseUri | LoadOptions.SetLineInfo );
if ( baseProjectXml.Root == null || baseProjectXml.Root.Name.LocalName != "Project" )
{
throw new XmlException( "Invalid project file :" + baseProjectFilePath );
}
var relativePath =
GetRelativePath(
Path.GetDirectoryName( projectFilePath ),
Path.GetDirectoryName( baseProjectFilePath )
);
var targetItemGroups =
baseProjectXml.Root
.Elements( Ns + "ItemGroup" )
.Select( ig =>
{
ig.Elements()
.Where(
e =>
e.Attribute( "Include" ) != null
&& project.Excludes.Any( excludes =>
e.Element( Ns + "Link" ) != null
? Regex.IsMatch( e.Element( Ns + "Link" ).Value, excludes )
: Regex.IsMatch( e.Attribute( "Include" ).Value, excludes )
)
).Remove();
return ig;
}
).Where( ig => ig.Elements().Any() )
.Select( e => new ItemGroup( e ) );
var baseItemGroups = baseProjectXml.Root.Elements( Ns + "ItemGroup" ).SelectMany( ig => ig.Elements() ).ToLookup( e => e.Name.LocalName );
var existingItemGroups = projectXml.Root.Elements( Ns + "ItemGroup" ).Where( e => e.Elements().Any() ).Select( e => new ItemGroup( e ) ).ToDictionary( ig => ig.Key );
foreach ( var itemGroup in targetItemGroups )
{
switch ( itemGroup.Key )
{
case "Compile":
case "Content":
case "None":
{
// If there are no <ItemGroup> for current type, then create new type and register it to the document and bookkeeping.
XElement destinationElement;
ItemGroup destinationGroup;
if ( existingItemGroups.TryGetValue( itemGroup.Key, out destinationGroup ) )
{
destinationElement = destinationGroup.Element;
}
else
{
destinationElement = new XElement( Ns + "ItemGroup" );
// insert to the destination DOM
projectXml.Root.Elements( Ns + "ItemGroup" ).Last().AddAfterSelf( destinationElement );
// bookkeeping
existingItemGroups.Add( itemGroup.Key, new ItemGroup( destinationElement, itemGroup.Key ) );
}
CopyItemGroup(
itemGroup.Key,
baseItemGroups[ itemGroup.Key ],
destinationElement,
relativePath,
project.Includes,
project.Excludes,
project.Preserves
);
break;
}
}
}
projectXml.Save( projectFilePath );
}
}
private static IEnumerable<ItemGroup> ToItemGroups( XContainer projectXml )
{
return projectXml.Elements( Ns + "ItemGroup" ).Select( e => new ItemGroup( e ) );
}
private static void CopyItemGroup( string elementName, IEnumerable<XElement> sourceItems, XElement destinationItemGroup, string relativePath, IEnumerable<string> includings, IEnumerable<string> excludings, IEnumerable<string> preservings )
{
var remaining =
destinationItemGroup.Elements( Ns + elementName )
.Where( e =>
e.Attribute( "Include" ) != null &&
preservings.Any( preserves =>
e.Element( Ns + "Link" ) != null
? Regex.IsMatch( e.Element( Ns + "Link" ).Value, preserves )
: Regex.IsMatch( e.Attribute( "Include" ).Value, preserves )
)
).Select( e => new XElement( e ) )
.ToArray();
var appendings =
sourceItems
.Where( e =>
e.Attribute( "Include" ) != null &&
includings.Any( includes =>
Regex.IsMatch( e.Attribute( "Include" ).Value, includes )
)
).Select( e => CreateCopyingElement( relativePath, e ) )
.ToArray();
destinationItemGroup.Elements( Ns + elementName ).Remove();
var adding = new Dictionary<string, XElement>();
foreach ( var item in
sourceItems.Where( e =>
e.Attribute( "Include" ) != null &&
!excludings.Any( excludes =>
e.Element( Ns + "Link" ) != null
? Regex.IsMatch( e.Element( Ns + "Link" ).Value, excludes )
: Regex.IsMatch( e.Attribute( "Include" ).Value, excludes )
) &&
!preservings.Any( preserves =>
e.Element( Ns + "Link" ) != null
? Regex.IsMatch( e.Element( Ns + "Link" ).Value, preserves )
: Regex.IsMatch( e.Attribute( "Include" ).Value, preserves )
)
).Select( copying =>
CreateCopyingElement( relativePath, copying )
).Concat( remaining ).Concat( appendings ) )
{
var itemPath = item.Attribute( "Include" ).Value;
if ( !adding.ContainsKey( itemPath ) )
{
adding.Add( itemPath, item );
}
}
// To stable order, sort with their "display path".
destinationItemGroup.Add(
adding.OrderBy( kv =>
( kv.Value.Element( "Link" ) == null ? kv.Key : kv.Value.Element( "Link" ).Value ),
StringComparer.OrdinalIgnoreCase
).Select( kv => kv.Value )
);
}
private static XElement CreateCopyingElement( string relativePath, XElement copying )
{
return
copying.Element( Ns + "Link" ) != null
? new XElement( copying )
: new XElement(
copying.Name,
new XAttribute(
"Include",
Path.Combine( relativePath, copying.Attribute( "Include" ).Value )
),
new XElement( Ns + "Link", copying.Attribute( "Include" ).Value )
);
}
#region String Utlities
private static readonly char[] DirectorySeparators = new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };
private static readonly string DirectorySeparatorPattern =
"(" + Regex.Escape( Path.AltDirectorySeparatorChar.ToString() ) + "|" + Regex.Escape( Path.DirectorySeparatorChar.ToString() ) + ")";
private static string ToPattern( XElement xml )
{
var file = xml.Attribute( "File" );
var path = xml.Attribute( "Path" );
if ( file == null && path == null )
{
return null;
}
if ( file == null )
{
return
"^" +
String.Join(
DirectorySeparatorPattern,
path.Value.Split( DirectorySeparators, StringSplitOptions.RemoveEmptyEntries ).Select( ToPathRegex )
) +
"$";
}
else
{
var filePattern = ToFileRegex( ( file.Value ) );
return "(^" + filePattern + "|" + DirectorySeparatorPattern + filePattern + ")$";
}
}
private static string ToPathRegex( string wildcard )
{
// Wildcard in path should not contain directory separator.
return Regex.Escape( wildcard ).Replace( "\\*\\*", ".*" ).Replace( "\\*", @"[^\\]*" ).Replace( "\\?", @"[^\\]" );
}
private static string ToFileRegex( string wildcard )
{
return Regex.Escape( wildcard ).Replace( "\\*", ".*" ).Replace( "\\?", "." );
}
private static string GetRelativePath( string fromPath, string toPath )
{
var fromPathSegments = fromPath.Split( DirectorySeparators, StringSplitOptions.RemoveEmptyEntries );
var toPathSegments = toPath.Split( DirectorySeparators, StringSplitOptions.RemoveEmptyEntries );
var sameTo = 0;
for ( ; sameTo < fromPathSegments.Length && sameTo < toPathSegments.Length; sameTo++ )
{
if ( !fromPathSegments[ sameTo ].Equals( toPathSegments[ sameTo ], StringComparison.OrdinalIgnoreCase ) )
{
break;
}
}
return String.Join( Path.DirectorySeparatorChar.ToString( CultureInfo.InvariantCulture ), Enumerable.Repeat( "..", fromPathSegments.Length - sameTo ).Concat( toPathSegments.Skip( sameTo ) ) );
}
#endregion
private class ItemGroup
{
public readonly XElement Element;
public readonly string Key;
public ItemGroup( XElement newItemGroup, string key )
{
this.Element = newItemGroup;
this.Key = key;
}
public ItemGroup( XElement itemGroup )
{
this.Element = itemGroup;
try
{
this.Key = itemGroup.Elements().Select( e => e.Name.LocalName ).Distinct().Single();
}
catch ( InvalidOperationException )
{
var lineInfo = ( ( itemGroup ) as IXmlLineInfo ?? NullXmlLineInfo.Instance );
throw new InvalidOperationException(
String.Format(
CultureInfo.CurrentCulture,
"ItemGroup at line {0} of xml file '{1}' contains hetro genious children [{2}].",
lineInfo.LineNumber,
itemGroup.Document.BaseUri,
String.Join( ", ", itemGroup.Elements().Select( e => e.Name.LocalName ).Distinct() )
)
);
}
}
}
private sealed class NullXmlLineInfo : IXmlLineInfo
{
public static readonly NullXmlLineInfo Instance = new NullXmlLineInfo();
public bool HasLineInfo()
{
return false;
}
public int LineNumber
{
get { return -1; }
}
public int LinePosition
{
get { return -1; }
}
}
}
}