-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileMaker.cs
More file actions
80 lines (67 loc) · 1.98 KB
/
FileMaker.cs
File metadata and controls
80 lines (67 loc) · 1.98 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
using System.Collections.Generic;
using System.Linq;
public class FileMaker
{
private readonly GeneratorConfig.ConfigSection config;
private readonly string filename;
private readonly string @namespace;
private readonly List<ClassMaker> classMakers = new List<ClassMaker>();
private readonly List<string> usings = new List<string>();
public FileMaker(GeneratorConfig.ConfigSection config, string filename, string @namespace)
{
this.config = config;
this.filename = filename;
this.@namespace = @namespace;
}
public ClassMaker AddClass(string className)
{
var cm = new ClassMaker(className);
classMakers.Add(cm);
return cm;
}
public ClassMaker AddInterface(string className)
{
var cm = new ClassMaker(className, true);
classMakers.Add(cm);
return cm;
}
public void AddUsing(string name)
{
if (!string.IsNullOrWhiteSpace(name))
{
usings.Add(name);
}
}
public void Build()
{
var liner = new Liner();
var allUsings = classMakers.SelectMany(c => c.GetUsings()).Concat(usings).Distinct().ToList();
allUsings.Sort();
foreach (var u in allUsings)
{
liner.Add("using " + EndStatement(u));
}
liner.AddBlankLine();
AddHeaderComment(liner);
liner.StartClosure("namespace " + @namespace);
foreach (var c in classMakers)
{
c.Write(liner);
}
liner.EndClosure();
liner.Write(filename);
}
private string EndStatement(string s)
{
if (s.EndsWith(";")) return s;
return s + ";";
}
private void AddHeaderComment(Liner liner)
{
if (string.IsNullOrWhiteSpace(config.HeaderComment)) return;
var comment = config.HeaderComment;
if (!comment.StartsWith("//")) comment = "//" + comment;
liner.Add(comment);
liner.Add("");
}
}