2 using System.Collections.Generic;
3 using System.Collections.ObjectModel;
4 using NaGet.SubCommands.SubTask;
7 namespace NaGet.SubCommands
12 public abstract class NaGetTaskSet2 : Task
17 public virtual event EventHandler<TaskEventArgs> SubTaskEventRaised;
22 public event NaGetTaskQueryHandler TaskQueryRaised;
27 protected IList<NaGetSubTask> subTasks;
32 protected IList<string> taskSetNames;
35 /// 現在実行中のサブタスクのインデックス
37 private int currentSubTaskIndex = -1;
42 protected bool isDone = false;
47 protected bool isCancelled = false;
52 protected bool cancelCalled = false;
57 protected event Action<object> onCancelCalled;
59 public NaGetTaskSet2()
66 /// <returns>成功したか否か</returns>
67 public override bool Cancel()
70 if (! cancelCalled && ! isDone) {
72 if (onCancelCalled != null) {
76 if (currentSubTask != null && currentSubTask.Cancelable) {
77 return currentSubTask.Cancel();
89 public virtual int CurrentTaskSetIndex {
90 get { return currentSubTaskIndex; }
93 public override bool Done {
94 get { return isDone; }
97 public virtual bool Cancelled {
98 get { return isCancelled; }
101 public override bool Running {
102 get { return CurrentTaskSetIndex >= 0 && !Done; }
108 public virtual IList<string> TaskSetNames {
110 return new ReadOnlyCollection<string>(taskSetNames);
117 /// <param name="type">作業の状態</param>
118 /// <param name="subTaskProgress">サブタスクの進捗</param>
119 /// <returns>現在の進捗</returns>
120 protected virtual float GetProgressPercent(TaskEventType type, float subTaskProgress)
122 if (CurrentTaskSetIndex >= 0) {
123 if (subTaskProgress >= 0) {
124 return (CurrentTaskSetIndex * 100 + subTaskProgress) / taskSetNames.Count;
127 case TaskEventType.STARTED:
129 case TaskEventType.COMPLETED:
131 case TaskEventType.COMPLETED_SUBTASK:
132 return ((CurrentTaskSetIndex+1) * 100) / taskSetNames.Count;
134 return (CurrentTaskSetIndex * 100) / taskSetNames.Count;
146 protected virtual void NotifyStarted()
148 currentSubTaskIndex = 0;
153 /// キャンセル中断時に関するフラグの処理を行う
155 protected virtual void NotifyCancelled()
162 /// 成功終了時に関するフラグの処理を行う
164 protected virtual void NotifyCompleted()
172 protected virtual void NotifyGoToNextSubTask()
174 currentSubTaskIndex ++;
180 /// <param name="subTaskIndex">ジャンプ先のサブタスク番号</param>
181 protected virtual void NotifyGoToSubTask(int subTaskIndex)
183 currentSubTaskIndex = subTaskIndex;
188 #region サブタスク実行時における便利メソッド
190 protected virtual string currentSubTaskName {
191 get { return taskSetNames[currentSubTaskIndex]; }
194 protected virtual NaGetSubTask currentSubTask {
195 get { return subTasks[currentSubTaskIndex]; }
198 protected virtual bool hasMoreSubTask {
199 get { return currentSubTaskIndex < taskSetNames.Count; }
204 #region TaskEvent便利メソッド
206 protected virtual void RaiseTaskSetEvent(TaskEventType type, string message)
208 RaiseTaskSetEvent(type, message, GetProgressPercent(type, -1));
211 protected virtual void ReceivedErrorData(object sender, NaGet.Utils.AnyDataEventArgs<string> e)
213 if (! string.IsNullOrEmpty(e.Data)) {
214 RaiseTaskSetEvent(TaskEventType.WARNING, e.Data);
218 protected virtual void ReceivedOutputData(object sender, NaGet.Utils.AnyDataEventArgs<string> e)
220 if (! string.IsNullOrEmpty(e.Data)) {
221 RaiseTaskSetEvent(TaskEventType.INFO, e.Data);
225 protected virtual NaGetTaskQueryResult RaiseTaskSetQueryEvent(string message, NaGetTaskQueryResult selection)
227 if (TaskQueryRaised != null) {
228 return TaskQueryRaised(this, new NaGetTaskQueryArgs(message, selection));
230 return NaGetTaskQueryResult.CANCELED_AUTOMATICALLY;
235 #region サブタスク初期化・登録便利メソッド
237 protected void initSubTask()
239 taskSetNames = new List<string>();
240 subTasks = new List<NaGetSubTask>();
246 /// <param name="name">名称</param>
247 /// <param name="task">サブタスクオブジェクト</param>
248 protected void registSubTask(string name, NaGetSubTask task)
250 taskSetNames.Add(name);
253 // サブタスクのイベントをSubTaskEventRaised,TaskEventRaisedに転送
254 task.TaskEventRaised += new EventHandler<TaskEventArgs>(subtaskEventTransformer);
258 /// サブタスクのイベントをSubTaskEventRaised,TaskEventRaisedに転送する関数
260 /// <param name="sender">送信元のサブタスク</param>
261 /// <param name="e">イベントオブジェクト</param>
262 private void subtaskEventTransformer(object sender, TaskEventArgs e)
264 if (SubTaskEventRaised != null) {
265 SubTaskEventRaised(sender, e);
267 if (e.ProgressPercent >= 0) {
268 RaiseTaskSetEvent(TaskEventType.PING, null, GetProgressPercent(TaskEventType.PING, e.ProgressPercent));