1 | #Region "Microsoft.VisualBasic::c0d9c4a61146200b08cef92b13df1a28, Microsoft.VisualBasic.Core\Scripting\ScriptBuilder.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Class ScriptBuilder |
35 | ' |
36 | ' Properties: Preview, Script |
37 | ' |
38 | ' Constructor: (+5 Overloads) Sub New |
39 | ' Function: AppendLine, Replace, (+2 Overloads) Save, ToString |
40 | ' Operators: + |
41 | ' |
42 | ' |
43 | ' /********************************************************************************/ |
44 | |
45 | #End Region |
46 | |
47 | Imports System.Runtime.CompilerServices |
48 | Imports System.Text |
49 | Imports Microsoft.VisualBasic.ComponentModel |
50 | Imports Microsoft.VisualBasic.Text |
51 | |
52 | Namespace Scripting.SymbolBuilder |
53 | |
54 | ''' <summary> |
55 | ''' 对<see cref="StringBuilder"/>对象的拓展,添加了操作符凭借字符串,从而能够让生成代码的操作更加的方便 |
56 | ''' </summary> |
57 | Public Class ScriptBuilder : Implements ISaveHandle |
58 | |
59 | Public ReadOnly Property Script As StringBuilder |
60 | |
61 | ''' <summary> |
62 | ''' The variable in target script text should be in format like: ``{$name}`` |
63 | ''' </summary> |
64 | ''' <param name="name"></param> |
65 | Default Public WriteOnly Property Assign(name As String) As String |
66 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
67 | Set |
68 | Call Script.Replace($"{{${name}}}", Value) |
69 | End Set |
70 | End Property |
71 | |
72 | ''' <summary> |
73 | ''' Equals to <see cref="StringBuilder.ToString()"/> |
74 | ''' </summary> |
75 | ''' <returns></returns> |
76 | Public ReadOnly Property Preview As String |
77 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
78 | Get |
79 | Return Script.ToString |
80 | End Get |
81 | End Property |
82 | |
83 | Sub New(sb As StringBuilder) |
84 | Script = sb |
85 | End Sub |
86 | |
87 | Sub New(capacity As Integer) |
88 | Script = New StringBuilder(capacity) |
89 | End Sub |
90 | |
91 | Sub New() |
92 | Call Me.New(capacity:=1024) |
93 | End Sub |
94 | |
95 | Sub New(script$) |
96 | Call Me.New(New StringBuilder(script)) |
97 | End Sub |
98 | |
99 | Sub New(html As XElement) |
100 | Call Me.New(html.ToString) |
101 | End Sub |
102 | |
103 | ''' <summary> |
104 | ''' <see cref="StringBuilder.Replace(String, String)"/> |
105 | ''' </summary> |
106 | ''' <param name="key$"></param> |
107 | ''' <param name="value$"></param> |
108 | ''' <returns></returns> |
109 | Public Function Replace(key$, value$) As ScriptBuilder |
110 | Call Script.Replace(key, value) |
111 | Return Me |
112 | End Function |
113 | |
114 | ''' <summary> |
115 | ''' Display the string text in the <see cref="StringBuilder"/> object. |
116 | ''' </summary> |
117 | ''' <returns></returns> |
118 | ''' |
119 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
120 | Public Overrides Function ToString() As String |
121 | Return Script.ToString |
122 | End Function |
123 | |
124 | ''' <summary> |
125 | ''' Appends a copy of the specified string followed by the default line terminator |
126 | ''' to the end of the current <see cref="ScriptBuilder"/> object. |
127 | ''' </summary> |
128 | ''' <param name="line$">The string to append.</param> |
129 | ''' <returns>A reference to this instance after the append operation has completed.</returns> |
130 | Public Function AppendLine(Optional line$ = "") As ScriptBuilder |
131 | Call Script.AppendLine(line) |
132 | Return Me |
133 | End Function |
134 | |
135 | ''' <summary> |
136 | ''' <see cref="StringBuilder.ToString()"/> |
137 | ''' </summary> |
138 | ''' <param name="sb"></param> |
139 | ''' <returns></returns> |
140 | ''' |
141 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
142 | Public Shared Narrowing Operator CType(sb As ScriptBuilder) As String |
143 | Return sb.Script.ToString |
144 | End Operator |
145 | |
146 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
147 | Public Shared Widening Operator CType(script As String) As ScriptBuilder |
148 | Return New ScriptBuilder(script) |
149 | End Operator |
150 | |
151 | ''' <summary> |
152 | ''' <see cref="StringBuilder.Append"/> |
153 | ''' </summary> |
154 | ''' <param name="sb"></param> |
155 | ''' <param name="s"></param> |
156 | ''' <returns></returns> |
157 | Public Shared Operator &(sb As ScriptBuilder, s As String) As ScriptBuilder |
158 | Call sb.Script.Append(s) |
159 | Return sb |
160 | End Operator |
161 | |
162 | ''' <summary> |
163 | ''' <see cref="StringBuilder.AppendLine"/> |
164 | ''' </summary> |
165 | ''' <param name="sb"></param> |
166 | ''' <param name="s"></param> |
167 | ''' <returns></returns> |
168 | Public Shared Operator +(sb As ScriptBuilder, s As String) As ScriptBuilder |
169 | Call sb.Script.AppendLine(s) |
170 | Return sb |
171 | End Operator |
172 | |
173 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
174 | Public Function Save(Optional path As String = "", Optional encoding As Encoding = Nothing) As Boolean Implements ISaveHandle.Save |
175 | Return Script.ToString.SaveTo(path, encoding) |
176 | End Function |
177 | |
178 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
179 | Public Function Save(Optional path As String = "", Optional encoding As Encodings = Encodings.UTF8) As Boolean Implements ISaveHandle.Save |
180 | Return Script.ToString.SaveTo(path, encoding.CodePage) |
181 | End Function |
182 | End Class |
183 | End Namespace |