1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+
5+ namespace Fengb3 . EasyCodeBuilder . Csharp ;
6+
7+ /// <summary>
8+ ///
9+ /// </summary>
10+ public delegate CodeBuilder CodeRenderFragment ( CodeBuilder builder ) ;
11+
12+ public class CodeOption
13+ {
14+ public CodeRenderFragment ? OnBegin ;
15+ public CodeRenderFragment ? OnEnd ;
16+ public CodeRenderFragment ? OnChildren ;
17+ }
18+
19+ public class NamespaceOption : CodeOption
20+ {
21+ public string Name { get ; set ; } = "" ;
22+
23+ public NamespaceOption ( )
24+ {
25+ IDisposable ? indenter = null ;
26+ OnBegin += cb => {
27+ cb . AppendLines ( $ "namespace { Name } ", "{" ) ;
28+ indenter = cb . Indent ;
29+ return cb ;
30+ } ;
31+
32+ OnEnd += cb => {
33+ indenter ? . Dispose ( ) ;
34+ cb . AppendLine ( "}" ) ;
35+ return cb ;
36+ } ;
37+ }
38+ }
39+
40+ public class TypeOption : CodeOption
41+ {
42+ public enum Type
43+ {
44+ Class ,
45+ Struct ,
46+ Interface ,
47+ Enum ,
48+ Record
49+ }
50+
51+ public Type TypeKind { get ; set ; } = Type . Class ;
52+
53+ public ICollection < string > Keywords { get ; set ; } = [ ] ;
54+
55+ public string Name { get ; set ; }
56+
57+ public TypeOption ( )
58+ {
59+ var indenterStack = new Stack < IDisposable > ( ) ;
60+ OnBegin += cb => {
61+ var keywords = string . Join ( " " , Keywords ) ;
62+ var typeKeyword = TypeKind . ToString ( ) . ToLower ( ) ;
63+ cb . AppendLines ( $ "{ keywords } { typeKeyword } { Name } ", "{" ) ;
64+ indenterStack . Push ( cb . Indent ) ;
65+ return cb ;
66+ } ;
67+
68+ OnEnd += cb => {
69+ if ( indenterStack . Count > 0 )
70+ {
71+ indenterStack . Pop ( ) . Dispose ( ) ;
72+ }
73+ cb . AppendLine ( "}" ) ;
74+ return cb ;
75+ } ;
76+ }
77+ }
78+
79+ /// <summary>
80+ ///
81+ /// </summary>
82+ public static class CsharpCode
83+ {
84+ /// <summary>
85+ ///
86+ /// </summary>
87+ /// <returns></returns>
88+ public static CodeOption Create ( )
89+ {
90+ return new ( ) ;
91+ }
92+
93+ public static CodeOption Using ( this CodeOption option , params string [ ] usings )
94+ {
95+ option . OnBegin += cb => {
96+ foreach ( var u in usings )
97+ {
98+ cb . AppendLine ( $ "using { u } ;") ;
99+ }
100+ cb . AppendLine ( ) ;
101+ return cb ;
102+ } ;
103+ return option ;
104+ }
105+
106+ public static CodeOption Namespace ( this CodeOption root , Action < NamespaceOption > configure )
107+ {
108+ var option = new NamespaceOption ( ) ;
109+ configure ( option ) ;
110+ root . OnBegin += option . OnBegin ;
111+ root . OnEnd += option . OnEnd ;
112+ root . OnChildren += option . OnChildren ;
113+ return root ;
114+ }
115+
116+ public static CodeOption Type ( this CodeOption @namespace , Action < TypeOption > configure )
117+ {
118+ var option = new TypeOption ( ) ;
119+ configure ( option ) ;
120+
121+ @namespace . OnChildren += option . OnBegin ;
122+ @namespace . OnChildren += option . OnChildren ;
123+ @namespace . OnChildren += option . OnEnd ;
124+
125+ return @namespace ;
126+ }
127+
128+ public static CodeOption Class ( this CodeOption @namespace , Action < TypeOption > configure )
129+ {
130+ return @namespace . Type ( option => {
131+ option . TypeKind = TypeOption . Type . Class ;
132+ configure ( option ) ;
133+ } ) ;
134+ }
135+
136+ public static string Build ( this CodeOption root )
137+ {
138+ var cb = new CodeBuilder ( " " , 2 , "{" , "}" , 1024 ) ;
139+
140+ root . OnBegin ? . Invoke ( cb ) ;
141+ root . OnChildren ? . Invoke ( cb ) ;
142+ root . OnEnd ? . Invoke ( cb ) ;
143+
144+ return cb . ToString ( ) ;
145+ }
146+ }
0 commit comments