Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
487 lines (435 loc) · 19.3 KB
/
Copy pathProgram.cs
File metadata and controls
487 lines (435 loc) · 19.3 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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using CSharpMerge.Utilities;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace CSharpMerge;
partial class Program
{
private static readonly Regex _versionRegex = VersionRegex();
static void Main(string[] args)
{
if (Debugger.IsAttached)
{
SafeMain(args);
return;
}
try
{
SafeMain(args);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
static void SafeMain(string[] args)
{
Console.WriteLine("CSharpMerge - Copyright (C) 2017-" + DateTime.Now.Year + " Simon Mourier. All rights reserved.");
Console.WriteLine();
if (CommandLine.HelpRequested || args.Length < 2)
{
Help();
return;
}
var inputDirectoryPath = CommandLine.GetArgument<string>(0);
var outputFilePath = CommandLine.GetArgument<string>(1);
if (inputDirectoryPath == null || outputFilePath == null)
{
Help();
return;
}
inputDirectoryPath = Path.GetFullPath(inputDirectoryPath);
if (!Directory.Exists(inputDirectoryPath))
{
Console.WriteLine(inputDirectoryPath + " directory does not exists.");
return;
}
outputFilePath = Path.GetFullPath(outputFilePath);
var nosonar = CommandLine.GetArgument("nosonar", true);
var internalize = CommandLine.GetArgument("internalize", false);
var symbols = Conversions.SplitToList<string>(CommandLine.GetNullifiedArgument("symbols"), ';');
var excludedFiles = Conversions.SplitToList<string>(CommandLine.GetNullifiedArgument("exclude"), ';');
var excludedNamespaces = Conversions.SplitToList<string>(CommandLine.GetNullifiedArgument("excludeNs"), ';');
var commentsFiles = Conversions.SplitToList<string>(CommandLine.GetNullifiedArgument("comments", @"..\LICENSE"), ';');
var nullable = CommandLine.GetNullifiedArgument("nullable");
var encoding = Encoding.UTF8;
var enc = CommandLine.GetNullifiedArgument("encoding");
if (enc != null)
{
if (int.TryParse(enc, NumberStyles.Integer, CultureInfo.CurrentCulture, out int cp))
{
encoding = Encoding.GetEncoding(cp);
}
else
{
encoding = Encoding.GetEncoding(enc);
}
}
Console.WriteLine("Input : " + inputDirectoryPath);
Console.WriteLine("Output : " + outputFilePath);
Console.WriteLine("Internalize : " + internalize);
Console.WriteLine("No Sonar : " + nosonar);
Console.WriteLine("Encoding : " + encoding.WebName);
Console.WriteLine("Excluded : " + string.Join(", ", excludedFiles));
Console.WriteLine("Comments : " + string.Join(", ", commentsFiles));
Console.WriteLine("Symbols : " + string.Join(", ", symbols));
Console.WriteLine("ExcludedNs : " + string.Join(", ", excludedNamespaces));
Console.WriteLine();
Merge(
inputDirectoryPath,
outputFilePath,
encoding,
excludedFiles,
commentsFiles,
symbols,
excludedNamespaces,
internalize,
nosonar,
nullable);
}
static void Help()
{
Console.WriteLine(Assembly.GetEntryAssembly().GetName().Name.ToUpperInvariant() + " <input directory path> <output file path>");
Console.WriteLine();
Console.WriteLine("Description:");
Console.WriteLine(" This tool is used to merge .CS files from a directory into a single .CS file.");
Console.WriteLine();
Console.WriteLine("Options:");
Console.WriteLine(" /symbols Add a list of semicolon-separated preprocessor symbols.");
Console.WriteLine(" /incai Includes files whose name ends with 'AssemblyInfo.cs', 'AssemblyVersionInfo.cs'");
Console.WriteLine(" or 'AssemblyAttributes.cs'. Default is false.");
Console.WriteLine(" /incav Includes assembly version information from files whose name ends with 'AssemblyInfo.cs',");
Console.WriteLine(" 'AssemblyVersionInfo.cs' or 'AssemblyAttributes.cs'. Default is reverse of /incai.");
Console.WriteLine(" /incgs Includes files whose name ends with 'GlobalSuppressions.cs'. Default is false.");
Console.WriteLine(" /toponly Includes only the current directory in a search operation. Default is false (recursive).");
Console.WriteLine(" /encoding:<enc> Defines the encoding to use for the output file path. Default is '" + Encoding.UTF8.WebName + "'.");
Console.WriteLine(" /exclude:<files> Defines a list of semicolon-separated file paths or names. Default is none.");
Console.WriteLine(" /comments:<files> Defines a list of semicolon-separated file paths or names used as comments. Default is ..\\LICENSE.");
Console.WriteLine(" /nullable:<text> Adds nullable directive. Default is no directive.");
Console.WriteLine(" /internalize Internalize all classes.");
Console.WriteLine(" /nosonar Add NOSONAR.");
Console.WriteLine(" /excludens:<ns> Defines a list of semicolon-separated namespaces to exclude from usings. Default is none.");
Console.WriteLine();
Console.WriteLine("Examples:");
Console.WriteLine();
Console.WriteLine(" " + Assembly.GetEntryAssembly().GetName().Name.ToUpperInvariant() + " c:\\mypath\\myproject myproject_merged.cs /encoding:" + Encoding.Default.WebName);
Console.WriteLine();
Console.WriteLine(" Merges recursively all .cs files, except AssemblyInfo.cs, from the myproject path");
Console.WriteLine(" into a single myproject_merged.cs file using '" + Encoding.Default.WebName + "' encoding.");
Console.WriteLine();
}
static Encoding DetectEncoding(string filePath)
{
using var reader = new StreamReader(filePath, Encoding.Default, true);
reader.Peek();
return reader.CurrentEncoding;
}
static string NormalizeLineEndings(string text) => NormalizeLineEndings(text, Environment.NewLine);
static string NormalizeLineEndings(string text, string newLine)
{
if (string.IsNullOrEmpty(text))
return text;
var sb = new StringBuilder(text);
sb.Replace("\r", string.Empty).Replace("\n", newLine);
return sb.ToString();
}
private sealed class Ns(string text, UsingDirectiveSyntax first)
{
public string Text { get; } = text;
public List<UsingDirectiveSyntax> Usings { get; } = [first];
public bool HasPublicTypes { get; set; }
public override string ToString() => Text;
}
static void Merge(
string inputDirectoryPath,
string outputFilePath,
Encoding encoding,
IList<string> excludedFiles,
IList<string> commentsFiles,
IList<string> symbols,
IList<string> excludedNamespaces,
bool internalize,
bool nosonar,
string nullable)
{
var incai = CommandLine.GetArgument("incai", false);
var incav = CommandLine.GetArgument("incav", !incai);
var incgs = CommandLine.GetArgument("incgs", false);
var topdir = CommandLine.GetArgument("toponly", false);
var option = SearchOption.TopDirectoryOnly;
if (!topdir)
{
option |= SearchOption.AllDirectories;
}
var usings = new Dictionary<string, List<Ns>>(StringComparer.Ordinal);
var comments = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var commentTexts = new List<string>();
var codes = new Dictionary<string, List<string>>();
var publicTypes = new Dictionary<string, BaseTypeDeclarationSyntax>();
foreach (var file in Directory.GetFiles(inputDirectoryPath, "*.*", option))
{
var name = Path.GetFileName(file);
if (commentsFiles.Contains(name, StringComparer.OrdinalIgnoreCase) || commentsFiles.Contains(file, StringComparer.OrdinalIgnoreCase))
{
Console.WriteLine("Comment " + file);
comments.Add(file);
continue;
}
if (string.Compare(Path.GetExtension(file), ".cs", StringComparison.OrdinalIgnoreCase) != 0)
continue;
if (name.Contains("TemporaryGeneratedFile", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Skip " + file);
continue;
}
if (!incai && (name.EndsWith("AssemblyInfo.cs", StringComparison.OrdinalIgnoreCase) ||
name.EndsWith("AssemblyVersionInfo.cs", StringComparison.OrdinalIgnoreCase) ||
name.EndsWith("AssemblyAttributes.cs", StringComparison.OrdinalIgnoreCase)))
{
Console.WriteLine("Skip " + file);
if (incav)
{
var allText = File.ReadAllText(file);
foreach (var match in _versionRegex.Matches(allText).OfType<Match>())
{
var matchName = match.Groups["name"].Value;
var matchVersion = match.Groups["version"].Value;
commentTexts.Add($"Assembly{matchName}Version: {matchVersion}");
}
}
continue;
}
if (!incgs && name.EndsWith("GlobalSuppressions.cs", StringComparison.OrdinalIgnoreCase))
{
Console.WriteLine("Skip " + file);
continue;
}
if (excludedFiles.Contains(name, StringComparer.OrdinalIgnoreCase) || excludedFiles.Contains(file, StringComparer.OrdinalIgnoreCase))
{
Console.WriteLine("Skip " + file);
continue;
}
var enc = DetectEncoding(file);
var text = File.ReadAllText(file, enc);
Console.WriteLine(file + ", encoding: " + enc.WebName);
var options = CSharpParseOptions.Default
.WithLanguageVersion(LanguageVersion.Default)
.WithPreprocessorSymbols(symbols);
var tree = CSharpSyntaxTree.ParseText(text, options);
var root = (CompilationUnitSyntax)tree.GetRoot();
foreach (var rootMember in root.Members)
{
if (rootMember is NamespaceDeclarationSyntax ns)
{
foreach (var member in ns.Members)
{
if (member is BaseTypeDeclarationSyntax type)
{
if (member.Modifiers.Any(m => m.ValueText == "public"))
{
publicTypes[ns.Name + "." + type.Identifier.Text] = type;
}
}
}
}
else if (rootMember is FileScopedNamespaceDeclarationSyntax fns)
{
foreach (var member in fns.Members)
{
if (member is BaseTypeDeclarationSyntax type)
{
if (member.Modifiers.Any(m => m.ValueText == "public"))
{
publicTypes[fns.Name + "." + type.Identifier.Text] = type;
}
}
}
}
}
if (internalize && publicTypes.Count > 0)
{
var dic = new Dictionary<BaseTypeDeclarationSyntax, BaseTypeDeclarationSyntax>();
foreach (var kv in publicTypes)
{
var publicType = kv.Value;
var publicToken = publicType.Modifiers.First(m => m.ValueText == "public");
var modifiers = publicType.Modifiers.Remove(publicToken).Add(SyntaxFactory.Identifier("internal "));
var internalType = publicType.WithModifiers(modifiers);
dic[publicType] = internalType;
}
root = root.ReplaceNodes(publicTypes.Values, (publ, inte) => dic[publ]);
}
foreach (var us in root.Usings)
{
var key = us.ToString();
if (!usings.TryGetValue(key, out var list))
{
list = [new Ns(key, us)];
usings.Add(key, list);
continue;
}
list.Add(new Ns(key, us));
}
foreach (var rootMember in root.Members)
{
// only one file-scoped namespace decl
if (rootMember is FileScopedNamespaceDeclarationSyntax fns)
{
addNamespace(fns.Name.ToString(), fns.Members);
continue;
}
if (rootMember is NamespaceDeclarationSyntax ns)
{
addNamespace(ns.Name.ToString(), ns.Members);
continue;
}
void addNamespace(string nsName, SyntaxList<MemberDeclarationSyntax> members)
{
var sb = new StringBuilder();
for (var i = 0; i < members.Count; i++)
{
var member = members[i];
var annotations = member.WithAdditionalAnnotations();
sb.AppendLine(annotations.ToString());
if (i != members.Count - 1)
{
sb.AppendLine();
}
}
if (!codes.TryGetValue(nsName, out var codeList))
{
codeList = [];
codes.Add(nsName, codeList);
}
else
{
codeList.Add(Environment.NewLine);
}
codeList.Add(sb.ToString());
}
}
}
// scan for comments in all places
foreach (var commentFile in commentsFiles)
{
var path = Path.GetFullPath(Path.Combine(inputDirectoryPath, commentFile));
if (File.Exists(path))
{
Console.WriteLine("Comment " + path);
comments.Add(path);
}
}
// sort out global vs non global ns...
var array = usings.ToArray();
foreach (var kv in array)
{
if (!kv.Key.StartsWith("global "))
{
var ns = kv.Key.Split(["using "], StringSplitOptions.None)[1];
if (excludedNamespaces.Contains(ns, StringComparer.OrdinalIgnoreCase))
{
usings.Remove(kv.Key);
continue;
}
var globalUsing = "global using global::" + ns;
if (usings.ContainsKey(globalUsing))
{
usings.Remove(kv.Key);
}
}
else
{
var ns = kv.Key.Split(["using "], StringSplitOptions.None)[1].Replace("global::", string.Empty).Replace(";", string.Empty);
Console.WriteLine("GLU: " + ns);
if (excludedNamespaces.Contains(ns, StringComparer.OrdinalIgnoreCase))
{
var globalUsing = "global using global::" + ns;
usings.Remove(kv.Key);
usings.Remove(globalUsing);
continue;
}
}
}
var uss = new List<KeyValuePair<string, List<Ns>>>();
foreach (var kv in usings)
{
if (publicTypes.Where(t => t.Key.StartsWith(kv.Key)).Any())
{
kv.Value.ForEach(v => v.HasPublicTypes = true);
}
uss.Add(kv);
}
// bit of a hack... seems to work for me so far :-)
using var writer = new StreamWriter(outputFilePath, false, encoding);
if (nosonar)
{
writer.WriteLine("//NOSONAR");
}
if (nullable != null)
{
writer.Write("#nullable ");
writer.WriteLine(nullable);
}
foreach (var file in comments)
{
var enc = DetectEncoding(file);
var comment = File.ReadAllText(file, enc);
writer.WriteLine("/*");
writer.Write(NormalizeLineEndings(comment));
writer.WriteLine("*/");
}
if (commentTexts.Count > 0)
{
writer.WriteLine("/*");
foreach (var commentText in commentTexts)
{
writer.WriteLine(commentText);
}
writer.WriteLine("*/");
}
foreach (var us in uss.OrderBy(u => u.Key))
{
Console.WriteLine("Using: " + us.Key);
writer.WriteLine(NormalizeLineEndings(us.Key));
}
writer.WriteLine();
writer.WriteLine("#pragma warning disable IDE0079 // Remove unnecessary suppression");
writer.WriteLine("#pragma warning disable IDE0130 // Namespace does not match folder structure");
writer.WriteLine();
foreach (var group in codes.GroupBy(k => k.Key).OrderBy(g => g.Key))
{
foreach (var kv in group)
{
writer.WriteLine("namespace " + kv.Key);
writer.WriteLine("{");
foreach (var lines in kv.Value)
{
using var reader = new StringReader(lines);
do
{
var line = reader.ReadLine();
if (line == null)
break;
writer.WriteLine("\t" + NormalizeLineEndings(line));
}
while (true);
}
writer.WriteLine("}");
writer.WriteLine();
}
}
writer.WriteLine("#pragma warning restore IDE0130 // Namespace does not match folder structure");
writer.WriteLine("#pragma warning restore IDE0079 // Remove unnecessary suppression");
}
[GeneratedRegex(@"\s*\[\s*assembly:\s*Assembly\s*(?<name>.*)Version?\s*\(""(?<version>.*?)""\)\]")]
private static partial Regex VersionRegex();
}