OSDN Git Service

na-get-lib,旧方式のタスク(NaGetTaskSet)系のクラスを削除。
[applistation/AppliStation.git] / na-get-lib / NaGet.SubCommands / NaGetTaskSet2.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Collections.ObjectModel;
4 using NaGet.SubCommands.SubTask;
5 using NaGet.Tasks;
6
7 namespace NaGet.SubCommands
8 {
9         /// <summary>
10         /// NaGetタスク処理のパック
11         /// </summary>
12         public abstract class NaGetTaskSet2 : Task
13         {
14                 /// <summary>
15                 /// サブタスクのハンドラ
16                 /// </summary>
17                 public virtual event EventHandler<TaskEventArgs> SubTaskEventRaised;
18                 
19                 /// <summary>
20                 /// タスク処理中の質問のハンドラ
21                 /// </summary>
22                 public event NaGetTaskQueryHandler TaskQueryRaised;
23                 
24                 /// <summary>
25                 /// サブタスクのリスト
26                 /// </summary>
27                 protected IList<NaGetSubTask> subTasks;
28                 
29                 /// <summary>
30                 /// 文字列で表現した作業一覧リスト
31                 /// </summary>
32                 protected IList<string> taskSetNames;
33                 
34                 /// <summary>
35                 /// 現在実行中のサブタスクのインデックス
36                 /// </summary>
37                 private int currentSubTaskIndex = -1;
38                 
39                 /// <summary>
40                 /// 終了したか
41                 /// </summary>
42                 protected bool isDone = false;
43                 
44                 /// <summary>
45                 /// キャンセルされたか
46                 /// </summary>
47                 protected bool isCancelled = false;
48                 
49                 /// <summary>
50                 /// キャンセルが呼ばれたか
51                 /// </summary>
52                 protected bool cancelCalled = false;
53                 
54                 /// <summary>
55                 /// キャンセル呼び出し時のイベント。
56                 /// </summary>
57                 protected event Action<object> onCancelCalled;
58                 
59                 public NaGetTaskSet2()
60                 {
61                 }
62                 
63                 /// <summary>
64                 /// キャンセル処理を行う
65                 /// </summary>
66                 /// <returns>成功したか否か</returns>
67                 public override bool Cancel()
68                 {
69                         if (Cancelable) {
70                                 if (! cancelCalled && ! isDone) {
71                                         cancelCalled = true;
72                                         if (onCancelCalled != null) {
73                                                 onCancelCalled(null);
74                                         }
75                                         
76                                         if (currentSubTask != null && currentSubTask.Cancelable) {
77                                                 return currentSubTask.Cancel();
78                                         } else {
79                                                 return true;
80                                         }
81                                 } else {
82                                         return false;
83                                 }
84                         } else {
85                                 return false;
86                         }
87                 }
88                 
89                 public virtual int CurrentTaskSetIndex {
90                         get { return currentSubTaskIndex; }
91                 }
92                 
93                 public override bool Done {
94                         get { return isDone; }
95                 }
96                 
97                 public virtual bool Cancelled {
98                         get { return isCancelled; }
99                 }
100                 
101                 public override bool Running {
102                         get { return CurrentTaskSetIndex >= 0 && !Done; }
103                 }
104                 
105                 /// <summary>
106                 /// 文字列で表現した作業一覧リスト
107                 /// </summary>
108                 public virtual IList<string> TaskSetNames {
109                         get {
110                                 return new ReadOnlyCollection<string>(taskSetNames);
111                         }
112                 }
113                 
114                 /// <summary>
115                 /// 現在の進捗を戻す。
116                 /// </summary>
117                 /// <param name="type">作業の状態</param>
118                 /// <param name="subTaskProgress">サブタスクの進捗</param>
119                 /// <returns>現在の進捗</returns>
120                 protected virtual float GetProgressPercent(TaskEventType type, float subTaskProgress)
121                 {
122                         if (CurrentTaskSetIndex >= 0) {
123                                 if (subTaskProgress >= 0) {
124                                         return (CurrentTaskSetIndex * 100 + subTaskProgress) / taskSetNames.Count;
125                                 }
126                                 switch (type) {
127                                         case TaskEventType.STARTED:
128                                                 return 0;
129                                         case TaskEventType.COMPLETED:
130                                                 return 100;
131                                         case TaskEventType.COMPLETED_SUBTASK:
132                                                 return ((CurrentTaskSetIndex+1) * 100) / taskSetNames.Count;
133                                         default:
134                                                 return (CurrentTaskSetIndex * 100) / taskSetNames.Count;
135                                 }
136                         }
137                         
138                         return -1;
139                 }
140                 
141                 #region フラグ処理の便利メソッド
142                 
143                 /// <summary>
144                 /// 開始時に関するフラグの処理を行う
145                 /// </summary>
146                 protected virtual void NotifyStarted()
147                 {
148                         currentSubTaskIndex = 0;
149                         isDone = false;
150                 }
151                 
152                 /// <summary>
153                 /// キャンセル中断時に関するフラグの処理を行う
154                 /// </summary>
155                 protected virtual void NotifyCancelled()
156                 {
157                         isCancelled = true;
158                         isDone = true;
159                 }
160                 
161                 /// <summary>
162                 /// 成功終了時に関するフラグの処理を行う
163                 /// </summary>
164                 protected virtual void NotifyCompleted()
165                 {
166                         isDone = true;
167                 }
168                 
169                 /// <summary>
170                 /// サブタスク実行を次へ。
171                 /// </summary>
172                 protected virtual void NotifyGoToNextSubTask()
173                 {
174                         currentSubTaskIndex ++;
175                 }
176                 
177                 /// <summary>
178                 /// サブタスク実行をジャンプする。
179                 /// </summary>
180                 /// <param name="subTaskIndex">ジャンプ先のサブタスク番号</param>
181                 protected virtual void NotifyGoToSubTask(int subTaskIndex)
182                 {
183                         currentSubTaskIndex = subTaskIndex;
184                 }
185                 
186                 #endregion
187                 
188                 #region サブタスク実行時における便利メソッド
189                                 
190                 protected virtual string currentSubTaskName {
191                         get { return taskSetNames[currentSubTaskIndex]; }
192                 }
193                 
194                 protected virtual NaGetSubTask currentSubTask {
195                         get { return subTasks[currentSubTaskIndex]; }
196                 }
197                 
198                 protected virtual bool hasMoreSubTask {
199                         get { return currentSubTaskIndex < taskSetNames.Count; }
200                 }
201                 
202                 #endregion
203                 
204                 #region TaskEvent便利メソッド
205                 
206                 protected virtual void RaiseTaskSetEvent(TaskEventType type, string message)
207                 {
208                         RaiseTaskSetEvent(type, message, GetProgressPercent(type, -1));
209                 }
210                 
211                 protected virtual void ReceivedErrorData(object sender, NaGet.Utils.AnyDataEventArgs<string> e)
212                 {
213                         if (! string.IsNullOrEmpty(e.Data)) {
214                                 RaiseTaskSetEvent(TaskEventType.WARNING, e.Data);
215                         }
216                 }
217                 
218                 protected virtual void ReceivedOutputData(object sender, NaGet.Utils.AnyDataEventArgs<string> e)
219                 {
220                         if (! string.IsNullOrEmpty(e.Data)) {
221                                 RaiseTaskSetEvent(TaskEventType.INFO, e.Data);
222                         }
223                 }
224                 
225                 protected virtual NaGetTaskQueryResult RaiseTaskSetQueryEvent(string message, NaGetTaskQueryResult selection)
226                 {
227                         if (TaskQueryRaised != null) {
228                                 return TaskQueryRaised(this, new NaGetTaskQueryArgs(message, selection));
229                         }
230                         return NaGetTaskQueryResult.CANCELED_AUTOMATICALLY;
231                 }
232                                 
233                 #endregion
234                 
235                 #region サブタスク初期化・登録便利メソッド
236                 
237                 protected void initSubTask()
238                 {
239                         taskSetNames = new List<string>();
240                         subTasks = new List<NaGetSubTask>();
241                 }
242                 
243                 /// <summary>
244                 /// サブタスクを登録する
245                 /// </summary>
246                 /// <param name="name">名称</param>
247                 /// <param name="task">サブタスクオブジェクト</param>
248                 protected void registSubTask(string name, NaGetSubTask task)
249                 {
250                         taskSetNames.Add(name);
251                         subTasks.Add(task);
252                         
253                         // サブタスクのイベントをSubTaskEventRaised,TaskEventRaisedに転送
254                         task.TaskEventRaised += new EventHandler<TaskEventArgs>(subtaskEventTransformer);
255                 }
256                 
257                 /// <summary>
258                 /// サブタスクのイベントをSubTaskEventRaised,TaskEventRaisedに転送する関数
259                 /// </summary>
260                 /// <param name="sender">送信元のサブタスク</param>
261                 /// <param name="e">イベントオブジェクト</param>
262                 private void subtaskEventTransformer(object sender, TaskEventArgs e)
263                 {
264                         if (SubTaskEventRaised != null) {
265                                 SubTaskEventRaised(sender, e);
266                         }
267                         if (e.ProgressPercent >= 0) {
268                                 RaiseTaskSetEvent(TaskEventType.PING, null, GetProgressPercent(TaskEventType.PING, e.ProgressPercent));
269                         }
270                 }
271                 
272                 #endregion
273
274         }
275 }