using System; using System.Collections.Generic; using NaGet.SubCommands.SubTask; using NaGet.Tasks; namespace NaGet.SubCommands { /// /// NaGetタスク処理のパック /// public abstract class NaGetTaskSet2 : NaGetTaskSet { /// /// サブタスクのハンドラ /// public virtual event EventHandler SubTaskEventRaised; /// /// サブタスクのリスト /// protected IList subTasks; /// /// 現在実行中のサブタスクのインデックス /// private int currentSubTaskIndex = -1; /// /// 終了したか /// protected bool isDone = false; /// /// キャンセルされたか /// protected bool isCancelled = false; /// /// キャンセルが呼ばれたか /// protected bool cancelCalled = false; /// /// キャンセル呼び出し時のイベント。 /// protected event Action onCancelCalled; public NaGetTaskSet2() { } /// /// キャンセル処理を行う /// /// 成功したか否か public override bool Cancel() { if (Cancelable) { if (! cancelCalled && ! isDone) { cancelCalled = true; if (onCancelCalled != null) { onCancelCalled(null); } if (currentSubTask != null && currentSubTask.Cancelable) { return currentSubTask.Cancel(); } else { return true; } } else { return false; } } else { return false; } } public override int CurrentTaskSetIndex { get { return currentSubTaskIndex; } } public override bool Done { get { return isDone; } } public virtual bool Cancelled { get { return isCancelled; } } #region フラグ処理の便利メソッド /// /// 開始時に関するフラグの処理を行う /// protected virtual void NotifyStarted() { currentSubTaskIndex = 0; isDone = false; } /// /// キャンセル中断時に関するフラグの処理を行う /// protected virtual void NotifyCancelled() { isCancelled = true; isDone = true; } /// /// 成功終了時に関するフラグの処理を行う /// protected virtual void NotifyCompleted() { isDone = true; } /// /// サブタスク実行を次へ。 /// protected virtual void NotifyGoToNextSubTask() { currentSubTaskIndex ++; } /// /// サブタスク実行をジャンプする。 /// /// ジャンプ先のサブタスク番号 protected virtual void NotifyGoToSubTask(int subTaskIndex) { currentSubTaskIndex = subTaskIndex; } #endregion #region サブタスク実行時における便利メソッド protected virtual string currentSubTaskName { get { return taskSetNames[currentSubTaskIndex]; } } protected virtual NaGetSubTask currentSubTask { get { return subTasks[currentSubTaskIndex]; } } protected virtual bool hasMoreSubTask { get { return currentSubTaskIndex < taskSetNames.Count; } } #endregion #region サブタスク初期化・登録便利メソッド protected void initSubTask() { taskSetNames = new List(); subTasks = new List(); } /// /// サブタスクを登録する /// /// 名称 /// サブタスクオブジェクト protected void registSubTask(string name, NaGetSubTask task) { taskSetNames.Add(name); subTasks.Add(task); // サブタスクのイベントをSubTaskEventRaised,TaskEventRaisedに転送 task.TaskEventRaised += new EventHandler(subtaskEventTransformer); } /// /// サブタスクのイベントをSubTaskEventRaised,TaskEventRaisedに転送する関数 /// /// 送信元のサブタスク /// イベントオブジェクト private void subtaskEventTransformer(object sender, TaskEventArgs e) { if (SubTaskEventRaised != null) { SubTaskEventRaised(sender, e); } if (e.ProgressPercent >= 0) { RaiseTaskSetEvent(TaskEventType.PING, null, GetProgressPercent(TaskEventType.PING, e.ProgressPercent)); } } #endregion } }