, , :
using System.IO;
using System.Text;
using Exortech.NetReflector;
using ThoughtWorks.CruiseControl.Core;
using ThoughtWorks.CruiseControl.Remote;
namespace ccnet.SharedLabeller.CruiseControl.plugin
{
[ReflectorType("sharedLabeller")]
public class SharedLabeller : ILabeller
{
[ReflectorProperty("sharedLabelFilePath", Required = true)]
public string SharedLabelFilePath { get; set; }
[ReflectorProperty("prefix", Required = false)]
public string Prefix { get; set; }
[ReflectorProperty("incrementOnFailure", Required = false)]
public bool IncrementOnFailure { get; set; }
[ReflectorProperty("increment", Required = false)]
public bool Increment { get; set; }
[ReflectorProperty("initialBuildLabel", Required = false)]
public int InitialBuildLabel { get; set; }
public SharedLabeller()
{
IncrementOnFailure = false;
Increment = true;
InitialBuildLabel = 0;
}
#region ILabeller Members
public string Generate(IIntegrationResult integrationResult)
{
if (ShouldIncrementLabel(integrationResult.LastIntegration))
{
return Prefix + this.GetLabel();
}
else
{
return integrationResult.LastIntegration.Label;
}
}
public void Run(IIntegrationResult integrationResult)
{
integrationResult.Label = Generate(integrationResult);
}
#endregion
private string GetLabel()
{
ThoughtWorks.CruiseControl.Core.Util.Log.Debug("About to read label file. Filename: {0}", SharedLabelFilePath);
using (FileStream fileStream = File.Open(this.SharedLabelFilePath,
FileMode.OpenOrCreate,
FileAccess.ReadWrite,
FileShare.None))
{
var bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, bytes.Length);
string rawBuildNumber = Encoding.UTF8.GetString(bytes);
int previousBuildNumber;
if (!int.TryParse(rawBuildNumber, out previousBuildNumber))
{
previousBuildNumber = InitialBuildLabel - 1;
}
if (!Increment)
{
return previousBuildNumber.ToString();
}
int newBuildNumber = previousBuildNumber + 1;
bytes = Encoding.UTF8.GetBytes(newBuildNumber.ToString());
fileStream.Seek(0, SeekOrigin.Begin);
fileStream.Write(bytes, 0, bytes.Length);
return newBuildNumber.ToString();
}
}
private bool ShouldIncrementLabel(IntegrationSummary integrationSummary)
{
return integrationSummary == null || integrationSummary.Status == IntegrationStatus.Success || IncrementOnFailure;
}
}
}
, , "incrementonfailure". , "increment", , . , :
CruiseControl.NET,