Add target to msbuild file to set a version# in module.txt files

--HG--
branch : dev
This commit is contained in:
Renaud Paquay 2010-07-19 13:38:35 -07:00
parent e180ce8b23
commit b4da079a66
3 changed files with 104 additions and 0 deletions

View File

@ -20,6 +20,9 @@
<BuildPlatform Condition="$(ProgramW6432) != ''">x64</BuildPlatform>
<BuildPlatform Condition="$(BuildPlatform) == ''">x86</BuildPlatform>
<!-- TeamCity build number -->
<Version>$(BUILD_NUMBER)</Version>
</PropertyGroup>
<Import Project="$(LibFolder)\msbuild\MSBuild.Community.Tasks.Targets"/>
@ -91,6 +94,7 @@
<UsingTask AssemblyFile="$(CompileFolder)\MSBuild.Orchard.Tasks.dll" TaskName="MSBuild.Orchard.Tasks.StageProjectAlteration" />
<UsingTask AssemblyFile="$(CompileFolder)\MSBuild.Orchard.Tasks.dll" TaskName="MSBuild.Orchard.Tasks.FilterModuleBinaries" />
<UsingTask AssemblyFile="$(CompileFolder)\MSBuild.Orchard.Tasks.dll" TaskName="MSBuild.Orchard.Tasks.FileUpdateLines" />
<Target Name="Package-Stage">
<ItemGroup>
@ -218,5 +222,20 @@
<Exec Command="$(ProfilingFolder)\bin\Orchard.exe @$(SrcFolder)\Orchard.Profile\profiling-setup-commands.txt" WorkingDirectory="$(ProfilingFolder)"/>
</Target>
<!-- Version -->
<!-- Update all AssemblyInfo.cs and module.txt files to contain $(Version) -->
<Target Name="SetVersion">
<ItemGroup>
<Version-AssemblyInfos Include="$(SrcFolder)\**\AssemblyInfo.cs" />
<Version-Modules Include="$(SrcFolder)\**\Module.txt" />
</ItemGroup>
<FileUpdateLines Files="@(Version-Modules)"
Regex="^(orchardversion|version)(\s*):(\s*)(.*)"
ReplacementText="$1$2:${3}$(Version)"/>
</Target>
</Project>

View File

@ -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;
}
}
}

View File

@ -66,6 +66,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="FileUpdateLines.cs" />
<Compile Include="FilterModuleBinaries.cs" />
<Compile Include="StageProjectAlteration.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />