using System; using System.Collections.Generic; using System.Text; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; using Microsoft.Build.BuildEngine; using P4API; namespace P4.Net.P4MSBuildTasks { public class P4Sync : P4BaseTask { private bool _keep; public bool Keep { get { return _keep; } set { _keep = value; } } private bool _force; public bool Force { get { return _force; } set { _force = value; } } private bool _noSync; public bool NoSync { get { return _noSync; } set { _noSync = value; } } private bool _populateOnly; public bool PopulateOnly { get { return _populateOnly; } set { _populateOnly = value; } } private int[] _ignoredWarnings = { 554768772, // file(s) up-to-date. 554768759 // no such file(s). }; public int[] IgnoredWarnings { get { return _ignoredWarnings; } set { _ignoredWarnings = value; } } private string[] _syncPaths; [Required] public string[] SyncPaths { get { return _syncPaths; } set { _syncPaths = value; } } public override void P4Execute() { List args = new List(); if (_populateOnly) args.Add("-p"); if (_force) args.Add("-f"); if (_keep) args.Add("-k"); if (_noSync) args.Add("-n"); args.AddRange(_syncPaths); SyncCallback callback = new SyncCallback(Log, _ignoredWarnings); _p4.RunCallback(callback, "sync", args.ToArray()); } } internal class SyncCallback : P4Callback { private TaskLoggingHelper Log; private List _ignoredWarnings; public SyncCallback(TaskLoggingHelper log, int[] ignoredWarnings) { Log = log; _ignoredWarnings = new List(ignoredWarnings); } #region P4Callback Members public override void Finished() { } public override void OutputMessage(P4Message message) { if (_ignoredWarnings.Contains(message.Identity)) { Log.LogMessage(message.Format()); } else { Log.LogError("Sync Error. Code: {0}. Message: {1}", message.Identity, message.Format()); } } public override void OutputRecord(P4Record record) { if (record.Fields.ContainsKey("totalFileCount")) { Log.LogMessage("Syncing {0} files, {1} bytes", record["totalFileCount"], record["totalFileSize"]); } string message = string.Format("{0}#{1} - {2} {3}", record["depotFile"], record["rev"], record["action"], record["clientFile"]); Log.LogMessage(message); } #endregion } }