Skip to content

Commit 4016cfa

Browse files
1 parent 6767199 commit 4016cfa

14 files changed

+296
-136
lines changed

AssemblyInfoEditor.csproj

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,8 @@
3939
</PropertyGroup>
4040
<ItemGroup>
4141
<Reference Include="System" />
42-
<Reference Include="System.Configuration" />
4342
<Reference Include="System.Core" />
4443
<Reference Include="System.Design" />
45-
<Reference Include="System.Xml.Linq" />
46-
<Reference Include="System.Data.DataSetExtensions" />
47-
<Reference Include="System.Data" />
48-
<Reference Include="System.Deployment" />
4944
<Reference Include="System.Drawing" />
5045
<Reference Include="System.Windows.Forms" />
5146
<Reference Include="System.Xml" />
@@ -138,6 +133,7 @@
138133
<ItemGroup>
139134
<Content Include="aieditor.regex.xml">
140135
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
136+
<SubType>Designer</SubType>
141137
</Content>
142138
</ItemGroup>
143139
<ItemGroup />

Commons/Entity/AssemblyInformation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ public string ProductName
211211
#endregion
212212

213213
#region Manufacturer
214-
[Category("Assembly Information"), Description("Set or get the assembly's manufacturer."), DisplayName("Assembly Manufacturer"), Browsable(true), ReadOnly(false), DefaultValue("SourcePro Studio")]
214+
[Category("Assembly Information"), Description("Set or get the assembly's manufacturer."), DisplayName("Assembly Manufacturer"), Browsable(true), ReadOnly(false)]
215215
[XmlElement(ElementName = "assemblyManufacturer", Namespace = XNamespaces.NameSpace)]
216216
public string Manufacturer
217217
{

Commons/RegularExpressions/Regex.cs

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@
2121

2222
using System;
2323
using System.Collections.Generic;
24-
using System.Xml;
2524
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;
2631

2732
namespace SourcePro.Csharp.Lab.Commons.RegularExpressions
2833
{
@@ -48,6 +53,25 @@ public class Regex
4853
private static readonly string ConfigFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "aieditor.regex.xml");
4954
private XmlNamespaceManager _xNamespaces;
5055
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
5175

5276
#region Expressions
5377
private Dictionary<RegexCategory, string> Expressions
@@ -81,6 +105,14 @@ private string Xmlns
81105
}
82106
#endregion
83107

108+
#region Platform
109+
private Platform Platform
110+
{
111+
get { return _platform; }
112+
set { _platform = value; }
113+
}
114+
#endregion
115+
84116
#region Regex Constructors
85117

86118
/// <summary>
@@ -89,6 +121,7 @@ private string Xmlns
89121
/// <param name="platform"><see cref="Platform"/>中的一个值。</param>
90122
public Regex(Platform platform)
91123
{
124+
this.Platform = platform;
92125
this.InitializeXmlnsPrefix(platform);
93126
this.Expressions = new Dictionary<RegexCategory, string>();
94127
if (!File.Exists(ConfigFile)) throw new FileNotFoundException("The regularexpressions config file not found!", ConfigFile);
@@ -97,6 +130,7 @@ public Regex(Platform platform)
97130
this.XNamespaces = new XmlNamespaceManager(this.XConfigure.NameTable);
98131
this.XNamespaces.AddNamespace("wyc", "https://github.com/SourceproStudio");
99132
this.XNamespaces.AddNamespace(this.Xmlns, string.Format("urn:{0}", platform));
133+
this.InitializeExpressions();
100134
}
101135

102136
#endregion
@@ -106,16 +140,82 @@ private void InitializeXmlnsPrefix(Platform platform)
106140
{
107141
switch (platform)
108142
{
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;
112155
}
113156
}
114157
#endregion
115158

116159
#region InitializeExpressions
117160
private void InitializeExpressions()
118161
{
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;
119219
}
120220
#endregion
121221
}

ComponentModel/Trace/TraceManager.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919

2020
#endregion
2121

22-
using System;
2322
using System.Windows.Forms;
24-
using SourcePro.Csharp.Lab.Commons;
2523

2624
namespace SourcePro.Csharp.Lab.ComponentModel.Trace
2725
{

ComponentModel/Trace/TraceViewerInvoker.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
#endregion
2121

22-
using System;
2322

2423
namespace SourcePro.Csharp.Lab.ComponentModel.Trace
2524
{

ComponentModel/Trace/TraceViewerInvokerArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
#endregion
2121

2222
using System;
23-
using SourcePro.Csharp.Lab.Commons;
2423
using System.Text;
24+
using SourcePro.Csharp.Lab.Commons;
2525

2626
namespace SourcePro.Csharp.Lab.ComponentModel.Trace
2727
{

Forms/BuildForm.Methods.cs

Lines changed: 104 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
#endregion
2121

2222
using System;
23-
using SourcePro.Csharp.Lab.ComponentModel.Trace;
24-
using System.ComponentModel;
25-
using SourcePro.Csharp.Lab.Commons;
2623
using System.IO;
27-
using SourcePro.Csharp.Lab.Commons.Entity;
28-
using System.Threading;
2924
using System.Linq;
25+
using System.Text;
26+
using System.Threading;
27+
using System.Windows.Forms;
28+
using SourcePro.Csharp.Lab.Commons;
29+
using SourcePro.Csharp.Lab.Commons.Entity;
30+
using SourcePro.Csharp.Lab.Commons.RegularExpressions;
31+
using SourcePro.Csharp.Lab.ComponentModel.Trace;
3032

3133
namespace SourcePro.Csharp.Lab.Forms
3234
{
@@ -47,6 +49,8 @@ namespace SourcePro.Csharp.Lab.Forms
4749
/// <seealso cref="SourcePro.Csharp.Lab.Forms"/>
4850
partial class BuildForm
4951
{
52+
private Regex _regex;
53+
5054
#region InitializeVariables
5155
private void InitializeVariables()
5256
{
@@ -60,6 +64,7 @@ protected override void InitializeControls()
6064
base.InitializeControls();
6165
this.SetProgressImageVisible(true);
6266
this.InitializeVariables();
67+
this._regex = new Regex(this.AssemblyInformation.PlatformID);
6368
this.StartBackgroundJob();
6469
}
6570
#endregion
@@ -68,6 +73,14 @@ protected override void InitializeControls()
6873
protected override void RegisterControlsEventHandlers()
6974
{
7075
this.CtrlButton_Cancel.Click += new EventHandler(RequestCancelBackgroundJob);
76+
this.CtrlButton_Close.Click += new EventHandler(CloseThisForm);
77+
}
78+
#endregion
79+
80+
#region CloseThisForm
81+
private void CloseThisForm(object sender, EventArgs e)
82+
{
83+
this.Close();
7184
}
7285
#endregion
7386

@@ -101,6 +114,21 @@ private void CaptureBackgroundProgress()
101114
}
102115
}
103116
else this.Trace.Output(new TraceViewerInvokerArgs() { Status = JobProgress.Skip, Message = "You do not set the folder you want to search!" });
117+
this.Trace.Output(new TraceViewerInvokerArgs() { Status = JobProgress.End, Message = "AIEDITOR search opeartion is completed !" });
118+
new MethodInvoker(this.AfterCompletedCallback).Invoke();
119+
}
120+
#endregion
121+
122+
#region AfterCompletedCallback
123+
private void AfterCompletedCallback()
124+
{
125+
if (this.InvokeRequired)
126+
this.Invoke(new MethodInvoker(this.AfterCompletedCallback));
127+
else
128+
{
129+
this.CtrlButton_Close.Enabled = true;
130+
this.CtrlButton_Cancel.Enabled = false;
131+
}
104132
}
105133
#endregion
106134

@@ -125,6 +153,7 @@ private void SearchIncludedFolder(DirectoryInfo folder, bool includeSubs)
125153
this.Trace.Output(new TraceViewerInvokerArgs() { Status = JobProgress.Searching, Message = string.Format("Now searching the folder [{0}]", folder.FullName) });
126154
if (!this.IsExcludedFolder(folder.FullName))
127155
{
156+
this.ScanEveryFile(folder);
128157
DirectoryInfo[] subs = folder.GetDirectories();
129158
if (includeSubs && subs.Length > 0)
130159
{
@@ -143,17 +172,79 @@ private bool IsExcludedFolder(string path)
143172
}
144173
#endregion
145174

146-
#region GetAssemblyFiles
147-
private FileInfo[] GetAssemblyFiles(DirectoryInfo folder)
175+
#region ScanEveryFile
176+
private void ScanEveryFile(DirectoryInfo directory)
177+
{
178+
FileInfo[] files = directory.GetFiles();
179+
foreach (var item in files)
180+
{
181+
if (this._regex.ValidateFileExtensionName(item, this.Trace))
182+
{
183+
bool error = false;
184+
string content = this.ReadAndReplace(item, out error);
185+
if (!error)
186+
{
187+
this.Save(item, content);
188+
this.Trace.Output(new TraceViewerInvokerArgs() { Status = JobProgress.Successful | JobProgress.Writing, Message = "Update completed !" });
189+
}
190+
else this.Trace.Output(new TraceViewerInvokerArgs() { Status = JobProgress.Skip, Message = string.Format("Skip the file [{0}] !", item.FullName) });
191+
}
192+
}
193+
}
194+
#endregion
195+
196+
#region ReadAndReplace
197+
private string ReadAndReplace(FileInfo file, out bool error)
148198
{
149-
string filter = "";
150-
switch (this.AssemblyInformation.PlatformID)
199+
StringBuilder str = new StringBuilder();
200+
error = false;
201+
using (Stream stream = file.OpenRead())
151202
{
152-
case Platform.CsharpAndVB: filter = "*.cs|*.vb"; break;
153-
case Platform.Csharp: filter = "*.cs"; break;
154-
case Platform.VisualBasic: filter = "*.vb"; break;
203+
using (StreamReader reader = new StreamReader(stream))
204+
{
205+
try
206+
{
207+
str.Append(this._regex.MatchAndReplace(file.Extension.TrimStart('.').ToLower().Equals("vb"), reader.ReadToEnd(), this.Trace, this.AssemblyInformation));
208+
}
209+
catch (Exception)
210+
{
211+
this.Trace.Output(new TraceViewerInvokerArgs() { Status = JobProgress.Failed, Message = string.Format("Exception thrown when reading the file [{0}]!", file.FullName) });
212+
error = true;
213+
}
214+
finally
215+
{
216+
reader.Close();
217+
stream.Close();
218+
}
219+
}
220+
}
221+
return str.ToString();
222+
}
223+
#endregion
224+
225+
#region Save
226+
private void Save(FileInfo destination, string text)
227+
{
228+
this.Trace.Output(new TraceViewerInvokerArgs() { Status = JobProgress.Writing | JobProgress.Doing, Message = string.Format("Updating file [{0}] !", destination.FullName) });
229+
using (Stream stream = new FileStream(destination.FullName, FileMode.Create, FileAccess.Write))
230+
{
231+
using (StreamWriter writer = new StreamWriter(stream))
232+
{
233+
try
234+
{
235+
writer.Write(text);
236+
}
237+
catch (Exception)
238+
{
239+
this.Trace.Output(new TraceViewerInvokerArgs() { Status = JobProgress.Failed | JobProgress.Writing, Message = "Update failed !" });
240+
}
241+
finally
242+
{
243+
writer.Close();
244+
stream.Close();
245+
}
246+
}
155247
}
156-
return folder.GetFiles(filter, SearchOption.TopDirectoryOnly);
157248
}
158249
#endregion
159250
}

Forms/BuildForm.Variables.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919

2020
#endregion
2121

22-
using System;
22+
using System.Threading;
2323
using SourcePro.Csharp.Lab.Commons.Entity;
2424
using SourcePro.Csharp.Lab.ComponentModel.Trace;
25-
using System.Threading;
2625

2726
namespace SourcePro.Csharp.Lab.Forms
2827
{

0 commit comments

Comments
 (0)