From: Owen R. <exo...@us...> - 2005-12-12 07:30:47
|
Update of /cvsroot/ccnet/ccnet/project/core/sourcecontrol In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31058/project/core/sourcecontrol Added Files: Alienbrain.cs AlienbrainHistoryParser.cs Log Message: CCNET-385: committing alienbrain sourcecontrol provider -- contributed by Francis Tremblay -- thanks Francis! --- NEW FILE: Alienbrain.cs --- using System; using System.IO; using Exortech.NetReflector; using ThoughtWorks.CruiseControl.Core.Util; namespace ThoughtWorks.CruiseControl.Core.Sourcecontrol { [ReflectorType("alienbrain")] public class Alienbrain : ProcessSourceControl { public static readonly string NO_CHANGE = "No files or folders found!"; public const string AB_REGISTRY_PATH = @"SOFTWARE\NxN\alienbrain"; public const string AB_REGISTRY_KEY = "InstallDir"; public const string AB_COMMMAND_PATH = @"Client\Application\Tools"; public const string AB_EXE = "ab.exe"; public const string BRANCH_COMMAND_TEMPLATE = @"setactivebranch ""{0}"" -s ""{1}"" -d ""{2}"" -u ""{3}"" -p ""{4}"""; public const string MODIFICATIONS_COMMAND_TEMPLATE = @"find ""{0}"" -s ""{1}"" -d ""{2}"" -u ""{3}"" -p ""{4}"" -regex ""SCIT > {5} AND SCIT < {6}"" -format ""#CheckInComment#|#Name#|#DbPath#|#SCIT#|#Mime Type#|#LocalPath#|#Changed By#|#NxN_VersionNumber#"""; public const string LABEL_COMMAND_TEMPLATE = @"setlabel ""{0}"" -s ""{1}"" -d ""{2}"" -u ""{3}"" -p ""{4}"" -name ""{5}"" -comment ""This label is brought to you by CruiseControl.NET"""; public const string GET_COMMAND_TEMPLATE = @"getlatest ""{0}"" -s ""{1}"" -d ""{2}"" -u ""{3}"" -p ""{4}"" -localpath ""{5}"" -overwritewritable replace -overwritecheckedout replace -response:GetLatest.PathInvalid y -response:GetLatest.Writable y -response:GetLatest.CheckedOut y"; private IRegistry registry; private string executable; public Alienbrain() : this(new AlienbrainHistoryParser(), new ProcessExecutor(), new Registry()) { } public Alienbrain(IHistoryParser parser, ProcessExecutor executor, IRegistry registry) : base(parser, executor) { this.registry = registry; } [ReflectorProperty("executable", Required = false)] public string Executable { get { if (StringUtil.IsBlank(executable)) executable = GetExecutableFromRegistry(); return executable; } set { executable = value; } } [ReflectorProperty("server")] public string Server = string.Empty; [ReflectorProperty("database")] public string Database = string.Empty; [ReflectorProperty("username")] public string Username = string.Empty; [ReflectorProperty("password")] public string Password = string.Empty; [ReflectorProperty("branch", Required = false)] public string Branch = string.Empty; [ReflectorProperty("project", Required = true)] public string Project = string.Empty; [ReflectorProperty("autoGetSource", Required = false)] public bool AutoGetSource = false; [ReflectorProperty("workingDirectory", Required = false)] public string WorkingDirectory = string.Empty; [ReflectorProperty("labelOnSuccess", Required=false)] public bool LabelOnSuccess = false; // Actions // I had to bake something here because Alienbrain return ERRORLEVEL 1 when nothing is found // that caused an exception in CruiseControl.NET that think that the process failed (not pretty in the logs) // So I'm checking the result to see if there is changes, if not I return nothing public override Modification[] GetModifications(IIntegrationResult from, IIntegrationResult to) { ProcessInfo processinfo = CreateModificationProcess(MODIFICATIONS_COMMAND_TEMPLATE, from.StartTime, to.StartTime); ProcessResult result = Execute(processinfo); if (!result.StandardOutput.TrimEnd().EndsWith(NO_CHANGE)) { return base.ParseModifications(result, from.StartTime, to.StartTime); } else { return new Modification[0]; } } public override void LabelSourceControl(IIntegrationResult result) { if (LabelOnSuccess && result.Succeeded) { SelectBranch(); ProcessInfo process = CreateLabelProcess(LABEL_COMMAND_TEMPLATE, result); Execute(process); } } public override void GetSource(IIntegrationResult result) { if (AutoGetSource == true) { SelectBranch(); if (result.Modifications.Length > 0) { foreach (Modification m in result.Modifications) { ProcessInfo process = CreateGetProcess(GET_COMMAND_TEMPLATE, m.FolderName + "/" + m.FileName); Execute(process); } } else { ProcessInfo process = CreateGetProcess(GET_COMMAND_TEMPLATE); Execute(process); } } } private string GetExecutableFromRegistry() { string comServerPath = registry.GetExpectedLocalMachineSubKeyValue(AB_REGISTRY_PATH, AB_REGISTRY_KEY); return Path.Combine(comServerPath, AB_COMMMAND_PATH + "\\" + AB_EXE); } private void SelectBranch() { if (! StringUtil.IsBlank(Branch)) { ProcessInfo process = CreateBranchProcess(BRANCH_COMMAND_TEMPLATE); Execute(process); } } public bool HasChanges(ProcessInfo p) { ProcessResult result = Execute(p); return !result.StandardOutput.TrimEnd().EndsWith(NO_CHANGE); } // Process Creations public ProcessInfo CreateModificationProcess(string processCommand, DateTime from, DateTime to) { string arguments = String.Format(processCommand, Project, Server, Database, Username, Password, from.ToFileTime(), to.ToFileTime()); return new ProcessInfo(Executable, arguments); } public ProcessInfo CreateLabelProcess(string processCommand, IIntegrationResult result) { string arguments = String.Format(processCommand, Project, Server, Database, Username, Password, result.Label); return new ProcessInfo(Executable, arguments); } public ProcessInfo CreateGetProcess(string processCommand, string filename) { string arguments = String.Format(processCommand, filename, Server, Database, Username, Password, WorkingDirectory); return new ProcessInfo(Executable, arguments); } public ProcessInfo CreateGetProcess(string processCommand) { string arguments = String.Format(processCommand, Project, Server, Database, Username, Password, WorkingDirectory); return new ProcessInfo(Executable, arguments); } public ProcessInfo CreateBranchProcess(string processCommand) { string arguments = String.Format(processCommand, Branch, Server, Database, Username, Password); return new ProcessInfo(Executable, arguments); } } } --- NEW FILE: AlienbrainHistoryParser.cs --- using System; using System.Collections; using System.IO; using System.Text.RegularExpressions; namespace ThoughtWorks.CruiseControl.Core.Sourcecontrol { public class AlienbrainHistoryParser : IHistoryParser { public static readonly string FILE_REGEX = ".*|.*|.*|.*|.*|.*|.*|.*"; public static readonly char DELIMITER = '|'; public Modification[] Parse(TextReader history, DateTime from, DateTime to) { string historyLog = history.ReadToEnd(); Regex regex = new Regex(Alienbrain.NO_CHANGE); if (regex.Match(historyLog).Success == true) { return new Modification[0]; } regex = new Regex(FILE_REGEX); ArrayList result = new ArrayList(); string oldfile = ","; for (Match match = regex.Match(historyLog); match.Success; match = match.NextMatch()) { string[] modificationParams = AllModificationParams(match.Value); if (modificationParams.Length > 1) { string file = modificationParams[1]; if (file != oldfile) { result.Add(ParseModification(modificationParams)); oldfile = file; } } } return (Modification[]) result.ToArray(typeof (Modification)); } // strip carriage return, new line, and all leading and trailing characters from parameters public string[] AllModificationParams(string matchedLine) { matchedLine = matchedLine.Replace("\n", ""); matchedLine = matchedLine.Replace("\r", ""); string[] modificationParams = matchedLine.Split(DELIMITER); for (int ii = 0; ii < modificationParams.Length; ii++) { modificationParams[ii] = modificationParams[ii].Trim(' '); } return modificationParams; } // #CheckInComment#|#Name#|#DbPath#|#SCIT#|#Mime Type#|#LocalPath#|#Changed By#|#NxN_VersionNumber# public Modification ParseModification(string[] modificationParams) { Modification modification = new Modification(); modification.Comment = modificationParams[0]; modification.FileName = modificationParams[1]; modification.FolderName = "ab:/" + modificationParams[2].Replace("/" + modificationParams[1], ""); modification.ModifiedTime = DateTime.FromFileTime(long.Parse(modificationParams[3])); modification.Type = modificationParams[4]; modification.Url = modificationParams[5]; modification.UserName = modificationParams[6]; modification.Version = modificationParams[7]; return modification; } } } |