-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSourceTextBuild.cs
More file actions
128 lines (110 loc) · 3.51 KB
/
SourceTextBuild.cs
File metadata and controls
128 lines (110 loc) · 3.51 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
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System.Collections.Generic;
using System.Text;
namespace FluentHttp.SourceGenerator
{
public class SourceTextBuild
{
private readonly StringBuilder stringBuilder;
private readonly string SingleIndent = new string(' ', 4);
public SourceTextBuild()
{
stringBuilder = new StringBuilder();
}
public void AppendIndent(int indentLevel)
{
for (int i = 0; i < indentLevel; i++)
{
stringBuilder.Append(SingleIndent);
}
}
public string Build()
{
return CSharpSyntaxTree.ParseText(stringBuilder.ToString())
.GetRoot().NormalizeWhitespace()
.SyntaxTree.GetText().ToString()
;
}
public SourceTextBuild WithIndent(int indentLevel)
{
for (int i = 0; i < indentLevel; i++)
{
stringBuilder.Append(SingleIndent);
}
return this;
}
public SourceTextBuild WithUsingNameSpace(string namespaceName)
{
stringBuilder.AppendLine($"using {namespaceName};");
return this;
}
public SourceTextBuild WithDaprNameSpace()
{
stringBuilder.AppendLine("using FluentHttp.Ext;");
stringBuilder.AppendLine("using FluentHttp.Ext.LoadBalancing;");
return this;
}
public SourceTextBuild WithNetNameSpace(bool flag)
{
if (flag)
{
stringBuilder.AppendLine("using System.Net.Http;");
}
return this;
}
public SourceTextBuild WithUsingNameSpaces(List<string> list)
{
list.ForEach(x =>
{
WithUsingNameSpace(x);
});
return this;
}
public SourceTextBuild WithNameSpace(string nameSpaceName)
{
stringBuilder.AppendLine($"namespace {nameSpaceName}");
return this;
}
public SourceTextBuild WithNewLine()
{
stringBuilder.Append(DaprConst.NewLine);
return this;
}
public SourceTextBuild WithLeftBracket()
{
stringBuilder.AppendLine("{");
return this;
}
public SourceTextBuild WithRightBracket()
{
stringBuilder.AppendLine("}");
return this;
}
public SourceTextBuild WithClass(string modifier, string className)
{
stringBuilder.AppendLine($"{modifier} class {className}");
return this;
}
public SourceTextBuild WithInterface(string modifier, string interfaceName)
{
stringBuilder.AppendLine($"{modifier} interface {interfaceName}");
return this;
}
public SourceTextBuild WithImpl(string modifier, string className, string interfaceName)
{
stringBuilder.AppendLine($"{modifier} class {className} : {interfaceName}");
return this;
}
public SourceTextBuild WithMethod(string modifier, string returnType, string methodName, string param)
{
stringBuilder.AppendLine($"{modifier} {returnType} {methodName}({param})");
return this;
}
public SourceTextBuild WithContent(string text)
{
stringBuilder.AppendLine($@"{text}");
return this;
}
}
}