21
21
22
22
using System ;
23
23
using System . Collections . Generic ;
24
- using System . Xml ;
25
24
using System . IO ;
25
+ using System . Reflection ;
26
+ using System . Text . RegularExpressions ;
27
+ using System . Xml ;
28
+ using SourcePro . Csharp . Lab . Commons . Entity ;
29
+ using SourcePro . Csharp . Lab . ComponentModel . Trace ;
30
+ using Ex = System . Text . RegularExpressions . Regex ;
26
31
27
32
namespace SourcePro . Csharp . Lab . Commons . RegularExpressions
28
33
{
@@ -48,6 +53,25 @@ public class Regex
48
53
private static readonly string ConfigFile = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "aieditor.regex.xml" ) ;
49
54
private XmlNamespaceManager _xNamespaces ;
50
55
private string _xmlns ;
56
+ private string _specifiedFileName ;
57
+ private Platform _platform ;
58
+ private string _commentRegexpr ;
59
+
60
+ #region CommentRegexpr
61
+ private string CommentRegexpr
62
+ {
63
+ get { return _commentRegexpr ; }
64
+ set { _commentRegexpr = value ; }
65
+ }
66
+ #endregion
67
+
68
+ #region SpecifiedFileName
69
+ private string SpecifiedFileName
70
+ {
71
+ get { return _specifiedFileName ; }
72
+ set { _specifiedFileName = value ; }
73
+ }
74
+ #endregion
51
75
52
76
#region Expressions
53
77
private Dictionary < RegexCategory , string > Expressions
@@ -81,6 +105,14 @@ private string Xmlns
81
105
}
82
106
#endregion
83
107
108
+ #region Platform
109
+ private Platform Platform
110
+ {
111
+ get { return _platform ; }
112
+ set { _platform = value ; }
113
+ }
114
+ #endregion
115
+
84
116
#region Regex Constructors
85
117
86
118
/// <summary>
@@ -89,6 +121,7 @@ private string Xmlns
89
121
/// <param name="platform"><see cref="Platform"/>中的一个值。</param>
90
122
public Regex ( Platform platform )
91
123
{
124
+ this . Platform = platform ;
92
125
this . InitializeXmlnsPrefix ( platform ) ;
93
126
this . Expressions = new Dictionary < RegexCategory , string > ( ) ;
94
127
if ( ! File . Exists ( ConfigFile ) ) throw new FileNotFoundException ( "The regularexpressions config file not found!" , ConfigFile ) ;
@@ -97,6 +130,7 @@ public Regex(Platform platform)
97
130
this . XNamespaces = new XmlNamespaceManager ( this . XConfigure . NameTable ) ;
98
131
this . XNamespaces . AddNamespace ( "wyc" , "https://github.com/SourceproStudio" ) ;
99
132
this . XNamespaces . AddNamespace ( this . Xmlns , string . Format ( "urn:{0}" , platform ) ) ;
133
+ this . InitializeExpressions ( ) ;
100
134
}
101
135
102
136
#endregion
@@ -106,16 +140,82 @@ private void InitializeXmlnsPrefix(Platform platform)
106
140
{
107
141
switch ( platform )
108
142
{
109
- case Platform . CsharpAndVB : this . Xmlns = "all" ; break ;
110
- case Platform . Csharp : this . Xmlns = "cs" ; break ;
111
- case Platform . VisualBasic : this . Xmlns = "vb" ; break ;
143
+ case Platform . CsharpAndVB :
144
+ this . Xmlns = "all" ;
145
+ this . SpecifiedFileName = "AssemblyInfo.cs or AssemblyInfo.vb" ;
146
+ break ;
147
+ case Platform . Csharp :
148
+ this . Xmlns = "cs" ;
149
+ this . SpecifiedFileName = "AssemblyInfo.cs" ;
150
+ break ;
151
+ case Platform . VisualBasic :
152
+ this . Xmlns = "vb" ;
153
+ this . SpecifiedFileName = "AssemblyInfo.vb" ;
154
+ break ;
112
155
}
113
156
}
114
157
#endregion
115
158
116
159
#region InitializeExpressions
117
160
private void InitializeExpressions ( )
118
161
{
162
+ XmlNodeList xNodes = this . XConfigure . SelectNodes ( string . Format ( "{0}:sourcepro.utility.aieditor/{0}:regularExpressions/{0}:regex" , "wyc" ) , this . XNamespaces ) ;
163
+ foreach ( XmlNode item in xNodes )
164
+ {
165
+ RegexCategory category = ( RegexCategory ) Enum . Parse ( typeof ( RegexCategory ) , item . Attributes [ "wyc:category" ] . Value ) ;
166
+ this . Expressions . Add ( category ,
167
+ item . SelectSingleNode ( string . Format ( "{0}:text" , this . Xmlns ) , this . XNamespaces ) . InnerText ) ;
168
+ }
169
+ this . CommentRegexpr = this . XConfigure . SelectSingleNode ( string . Format ( "{0}:sourcepro.utility.aieditor/{0}:regularExpressions" , "wyc" ) , this . XNamespaces ) . Attributes [ "all:comment" ] . Value ;
170
+ }
171
+ #endregion
172
+
173
+ #region ValidateFileExtensionName
174
+ public bool ValidateFileExtensionName ( FileInfo file , TraceManager output )
175
+ {
176
+ output . Output ( new TraceViewerInvokerArgs ( ) { Status = JobProgress . Doing , Message = string . Format ( "Validating the [{0}] extension." , file . FullName ) } ) ;
177
+ bool isMatch = Ex . IsMatch ( file . Name , this . Expressions [ RegexCategory . FileFilter ] , RegexOptions . IgnoreCase ) ;
178
+ if ( ! isMatch )
179
+ output . Output ( new TraceViewerInvokerArgs ( ) { Status = JobProgress . Skip , Message = string . Format ( "The file name [{0}] is not equal to {1}, so skip it!" , file . FullName , this . SpecifiedFileName ) } ) ;
180
+ return isMatch ;
181
+ }
182
+ #endregion
183
+
184
+ #region MatchAndReplace
185
+ public string MatchAndReplace ( bool isVisualBasic , string text , TraceManager output , AssemblyInformation info )
186
+ {
187
+ output . Output ( new TraceViewerInvokerArgs ( ) { Status = JobProgress . Reading | JobProgress . Doing , Message = "Reading files, and perform the matching and replacement!" } ) ;
188
+ string expr = this . Expressions [ RegexCategory . AssemblyTitle ] ;
189
+ string template = isVisualBasic ? "<Assembly: {0}(\" {1}\" )>" : "[assembly: {0}(\" {1}\" )]" ;
190
+ if ( Ex . IsMatch ( text , expr ) )
191
+ text = Ex . Replace ( text , expr , string . Format ( template , RegexCategory . AssemblyTitle , info . Title ) ) ;
192
+ expr = this . Expressions [ RegexCategory . AssemblyDescription ] ;
193
+ if ( Ex . IsMatch ( text , expr ) )
194
+ text = Ex . Replace ( text , expr , string . Format ( template , RegexCategory . AssemblyDescription , info . Description ) ) ;
195
+ expr = this . Expressions [ RegexCategory . AssemblyCompany ] ;
196
+ if ( Ex . IsMatch ( text , expr ) )
197
+ text = Ex . Replace ( text , expr , string . Format ( template , RegexCategory . AssemblyCompany , info . Manufacturer ) ) ;
198
+ expr = this . Expressions [ RegexCategory . AssemblyProduct ] ;
199
+ if ( Ex . IsMatch ( text , expr ) )
200
+ text = Ex . Replace ( text , expr , string . Format ( template , RegexCategory . AssemblyProduct , info . ProductName ) ) ;
201
+ expr = this . Expressions [ RegexCategory . AssemblyCopyright ] ;
202
+ if ( Ex . IsMatch ( text , expr ) )
203
+ text = Ex . Replace ( text , expr , string . Format ( template , RegexCategory . AssemblyCopyright , info . CopyrightDeclaration ) ) ;
204
+ expr = this . Expressions [ RegexCategory . AssemblyTrademark ] ;
205
+ if ( Ex . IsMatch ( text , expr ) )
206
+ text = Ex . Replace ( text , expr , string . Format ( template , RegexCategory . AssemblyTrademark , info . Trademark ) ) ;
207
+ expr = this . Expressions [ RegexCategory . AssemblyVersion ] ;
208
+ if ( info . GenerateAssemblyVersion && Ex . IsMatch ( text , expr ) )
209
+ text = Ex . Replace ( text , expr , string . Format ( template , RegexCategory . AssemblyVersion , info . Version . ToString ( ) ) ) ;
210
+ expr = this . Expressions [ RegexCategory . AssemblyFileVersion ] ;
211
+ if ( info . GenerateAssemblyFileVersion && Ex . IsMatch ( text , expr ) )
212
+ text = Ex . Replace ( text , expr , string . Format ( template , RegexCategory . AssemblyFileVersion , info . FileVersion . ToString ( ) ) ) ;
213
+ string comment = string . Format ( "/*Generate By AssemblyInfo Editor {0}, Update Time {1}*/" , Assembly . GetExecutingAssembly ( ) . GetName ( ) . Version , DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss" ) ) ;
214
+ if ( Ex . IsMatch ( text , this . CommentRegexpr ) )
215
+ text = Ex . Replace ( text , this . CommentRegexpr , comment ) ;
216
+ else text = string . Format ( "{0}{1}{1}{2}" , text , Environment . NewLine , comment ) ;
217
+ output . Output ( new TraceViewerInvokerArgs ( ) { Status = JobProgress . Successful , Message = "Matching and replacement complete!" } ) ;
218
+ return text ;
119
219
}
120
220
#endregion
121
221
}
0 commit comments