From b4da079a66d77765de2bd50c17203d4c031ae61e Mon Sep 17 00:00:00 2001 From: Renaud Paquay Date: Mon, 19 Jul 2010 13:38:35 -0700 Subject: [PATCH] Add target to msbuild file to set a version# in module.txt files --HG-- branch : dev --- Orchard.proj | 19 +++++ .../MSBuild.Orchard.Tasks/FileUpdateLines.cs | 84 +++++++++++++++++++ .../MSBuild.Orchard.Tasks.csproj | 1 + 3 files changed, 104 insertions(+) create mode 100644 src/Tools/MSBuild.Orchard.Tasks/FileUpdateLines.cs diff --git a/Orchard.proj b/Orchard.proj index ab3841ce6..0bec4deaf 100644 --- a/Orchard.proj +++ b/Orchard.proj @@ -20,6 +20,9 @@ x64 x86 + + + $(BUILD_NUMBER) @@ -91,6 +94,7 @@ + @@ -218,5 +222,20 @@ + + + + + + + + + + + + + diff --git a/src/Tools/MSBuild.Orchard.Tasks/FileUpdateLines.cs b/src/Tools/MSBuild.Orchard.Tasks/FileUpdateLines.cs new file mode 100644 index 000000000..c5ddfb360 --- /dev/null +++ b/src/Tools/MSBuild.Orchard.Tasks/FileUpdateLines.cs @@ -0,0 +1,84 @@ +using System; +using System.IO; +using System.Text; +using System.Text.RegularExpressions; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace MSBuild.Orchard.Tasks { + public class FileUpdateLines : Task { + private bool _multiline; + private bool _singleline; + private int _replacementCount = -1; + private Encoding _encodingValue { get; set; } + + public ITaskItem[] Files { get; set; } + + public string Regex { get; set; } + public string ReplacementText { get; set; } + public bool IgnoreCase { get; set; } + public string Encoding { + get { + return this._encodingValue.WebName; + } + set { + this._encodingValue = System.Text.Encoding.GetEncoding(value); + } + } + + public FileUpdateLines() { + _replacementCount = -1; + _encodingValue = System.Text.Encoding.UTF8; + } + + public override bool Execute() { + RegexOptions options = RegexOptions.None; + if (this.IgnoreCase) { + options |= RegexOptions.IgnoreCase; + } + if (this._multiline) { + options |= RegexOptions.Multiline; + } + if (this._singleline) { + options |= RegexOptions.Singleline; + } + if (this._replacementCount == 0) { + this._replacementCount = -1; + } + Regex regex = new Regex(Regex, options); + try { + foreach (ITaskItem item in Files) { + string itemSpec = item.ItemSpec; + Log.LogMessage("Updating File \"{0}\".", itemSpec); + int replacementCount = ProcessLines(itemSpec, regex); + Log.LogMessage(" Replaced {0} occurence(s) of \"{1}\" with \"{2}\".", replacementCount, Regex, ReplacementText); + } + } + catch (Exception exception) { + Log.LogErrorFromException(exception); + return false; + } + return true; + } + + private int ProcessLines(string itemSpec, Regex regex) { + int replacementCount = 0; + + using (var writer = new StringWriter()) { + using (var reader = new StreamReader(itemSpec, _encodingValue)) { + for (var line = reader.ReadLine(); line != null; line = reader.ReadLine()) { + var newLine = regex.Replace(line, ReplacementText, _replacementCount); + if (newLine != line) + replacementCount++; + writer.WriteLine(newLine); + } + } + + writer.Flush(); + File.WriteAllText(itemSpec, writer.ToString(), _encodingValue); + } + + return replacementCount; + } + } +} diff --git a/src/Tools/MSBuild.Orchard.Tasks/MSBuild.Orchard.Tasks.csproj b/src/Tools/MSBuild.Orchard.Tasks/MSBuild.Orchard.Tasks.csproj index 32554a3f5..1e7dc3a19 100644 --- a/src/Tools/MSBuild.Orchard.Tasks/MSBuild.Orchard.Tasks.csproj +++ b/src/Tools/MSBuild.Orchard.Tasks/MSBuild.Orchard.Tasks.csproj @@ -66,6 +66,7 @@ +