OSDN Git Service

na-get-lib,タスクセットの改修(NaGetTaskSet2)および、そのUnitTestを追加
[applistation/AppliStation.git] / na-get-lib / NaGet.SubCommands / NaGetTaskSet2.cs
1 using System;
2 using System.Collections.Generic;
3 using NaGet.SubCommands.SubTask;
4 using NaGet.Tasks;
5
6 namespace NaGet.SubCommands
7 {
8         /// <summary>
9         /// NaGetタスク処理のパック
10         /// </summary>
11         public abstract class NaGetTaskSet2 : NaGetTaskSet
12         {
13                 /// <summary>
14                 /// サブタスクのハンドラ
15                 /// </summary>
16                 public virtual event EventHandler<TaskEventArgs> SubTaskEventRaised;
17                 
18                 /// <summary>
19                 /// サブタスクのリスト
20                 /// </summary>
21                 protected IList<NaGetSubTask> subTasks;
22                 
23                 /// <summary>
24                 /// 現在実行中のサブタスクのインデックス
25                 /// </summary>
26                 private int currentSubTaskIndex = -1;
27                 
28                 /// <summary>
29                 /// 終了したか
30                 /// </summary>
31                 protected bool isDone = false;
32                 
33                 /// <summary>
34                 /// キャンセルされたか
35                 /// </summary>
36                 protected bool isCancelled = false;
37                 
38                 /// <summary>
39                 /// キャンセルが呼ばれたか
40                 /// </summary>
41                 protected bool cancelCalled = false;
42                 
43                 /// <summary>
44                 /// キャンセル呼び出し時のイベント。
45                 /// </summary>
46                 protected event Action<object> onCancelCalled;
47                 
48                 public NaGetTaskSet2()
49                 {
50                 }
51                 
52                 /// <summary>
53                 /// キャンセル処理を行う
54                 /// </summary>
55                 /// <returns>成功したか否か</returns>
56                 public override bool Cancel()
57                 {
58                         if (Cancelable) {
59                                 if (! cancelCalled && ! isDone) {
60                                         cancelCalled = true;
61                                         if (onCancelCalled != null) {
62                                                 onCancelCalled(null);
63                                         }
64                                         
65                                         if (currentSubTask != null && currentSubTask.Cancelable) {
66                                                 return currentSubTask.Cancel();
67                                         } else {
68                                                 return true;
69                                         }
70                                 } else {
71                                         return false;
72                                 }
73                         } else {
74                                 return false;
75                         }
76                 }
77                 
78                 public override int CurrentTaskSetIndex {
79                         get { return currentSubTaskIndex; }
80                 }
81                 
82                 public override bool Done {
83                         get { return isDone; }
84                 }
85                 
86                 public virtual bool Cancelled {
87                         get { return isCancelled; }
88                 }
89                 
90                 #region フラグ処理の便利メソッド
91                 
92                 /// <summary>
93                 /// 開始時に関するフラグの処理を行う
94                 /// </summary>
95                 protected virtual void NotifyStarted()
96                 {
97                         currentSubTaskIndex = 0;
98                         isDone = false;
99                 }
100                 
101                 /// <summary>
102                 /// キャンセル中断時に関するフラグの処理を行う
103                 /// </summary>
104                 protected virtual void NotifyCancelled()
105                 {
106                         isCancelled = true;
107                 }
108                 
109                 /// <summary>
110                 /// 成功終了時に関するフラグの処理を行う
111                 /// </summary>
112                 protected virtual void NotifyCompleted()
113                 {
114                         isDone = true;
115                 }
116                 
117                 /// <summary>
118                 /// サブタスク実行を次へ。
119                 /// </summary>
120                 protected virtual void NotifyGoToNextSubTask()
121                 {
122                         currentSubTaskIndex ++;
123                 }
124                 
125                 /// <summary>
126                 /// サブタスク実行をジャンプする。
127                 /// </summary>
128                 /// <param name="subTaskIndex">ジャンプ先のサブタスク番号</param>
129                 protected virtual void NotifyGoToSubTask(int subTaskIndex)
130                 {
131                         currentSubTaskIndex = subTaskIndex;
132                 }
133                 
134                 #endregion
135                 
136                 #region サブタスク実行時における便利メソッド
137                                 
138                 protected virtual string currentSubTaskName {
139                         get { return taskSetNames[currentSubTaskIndex]; }
140                 }
141                 
142                 protected virtual NaGetSubTask currentSubTask {
143                         get { return subTasks[currentSubTaskIndex]; }
144                 }
145                 
146                 protected virtual bool hasMoreSubTask {
147                         get { return currentSubTaskIndex < taskSetNames.Count; }
148                 }
149                 
150                 #endregion
151                 
152                 #region サブタスク初期化・登録便利メソッド
153                 
154                 protected void initSubTask()
155                 {
156                         taskSetNames = new List<string>();
157                         subTasks = new List<NaGetSubTask>();
158                 }
159                 
160                 /// <summary>
161                 /// サブタスクを登録する
162                 /// </summary>
163                 /// <param name="name">名称</param>
164                 /// <param name="task">サブタスクオブジェクト</param>
165                 protected void registSubTask(string name, NaGetSubTask task)
166                 {
167                         taskSetNames.Add(name);
168                         subTasks.Add(task);
169                         
170                         // サブタスクのイベントをSubTaskEventRaised,TaskEventRaisedに転送
171                         task.TaskEventRaised += new EventHandler<TaskEventArgs>(subtaskEventTransformer);
172                 }
173                 
174                 /// <summary>
175                 /// サブタスクのイベントをSubTaskEventRaised,TaskEventRaisedに転送する関数
176                 /// </summary>
177                 /// <param name="sender">送信元のサブタスク</param>
178                 /// <param name="e">イベントオブジェクト</param>
179                 private void subtaskEventTransformer(object sender, TaskEventArgs e)
180                 {
181                         if (SubTaskEventRaised != null) {
182                                 SubTaskEventRaised(sender, e);
183                         }
184                         if (e.ProgressPercent >= 0) {
185                                 RaiseTaskSetEvent(TaskEventType.PING, null, GetProgressPercent(TaskEventType.PING, e.ProgressPercent));
186                         }
187                 }
188                 
189                 #endregion
190
191         }
192 }