-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSKTaskGroup.m
More file actions
172 lines (132 loc) · 5.33 KB
/
Copy pathSKTaskGroup.m
File metadata and controls
172 lines (132 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2017 Jean-David Gadina - www.xs-labs.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
/*!
* @file SKTaskGroup.m
* @copyright (c) 2017, Jean-David Gadina - www.xs-labs.com
*/
#import <ShellKit/ShellKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface SKTaskGroup()
@property( atomic, readwrite, assign ) BOOL running;
@property( atomic, readwrite, strong, nullable ) NSError * error;
@property( atomic, readwrite, strong ) NSString * name;
@property( atomic, readwrite, strong ) NSArray< id< SKRunableObject > > * tasks;
@property( atomic, readwrite, strong, nullable ) id< SKRunableObject > currentTask;
@end
NS_ASSUME_NONNULL_END
@implementation SKTaskGroup
+ ( instancetype )taskGroupWithName: ( NSString * )name tasks: ( NSArray< id< SKRunableObject > > * )tasks
{
return [ [ self alloc ] initWithName: name tasks: tasks ];
}
- ( instancetype )init
{
return [ self initWithName: @"" tasks: @[] ];
}
- ( instancetype )initWithName: ( NSString * )name tasks: ( NSArray< id< SKRunableObject > > * )tasks
{
if( ( self = [ super init ] ) )
{
self.name = name;
self.tasks = tasks;
}
return self;
}
#pragma mark - SKRunableObject
- ( BOOL )run
{
return [ self run: nil ];
}
- ( BOOL )run: ( nullable NSDictionary< NSString *, NSString * > * )variables
{
id< SKRunableObject > task;
NSDate * date;
NSString * time;
NSUInteger i;
@synchronized( self )
{
self.running = YES;
if( self.name.length )
{
[ [ SKShell currentShell ] addPromptPart: self.name ];
}
if( self.tasks.count == 0 )
{
self.error = [ self errorWithDescription: @"No task defined" ];
[ [ SKShell currentShell ] printError: self.error ];
self.running = NO;
if( self.name.length )
{
[ [ SKShell currentShell ] removeLastPromptPart ];
}
return NO;
}
if( self.tasks.count > 1 )
{
[ [ SKShell currentShell ] printMessage: @"Running %lu tasks" status: SKStatusExecute color: SKColorNone, self.tasks.count ];
}
i = 0;
date = [ NSDate date ];
for( task in self.tasks )
{
self.currentTask = task;
[ [ SKShell currentShell ] addPromptPart: [ NSString stringWithFormat: @"#%li", ( unsigned long )++i ] ];
if( [ task run: variables ] == NO )
{
[ [ SKShell currentShell ] removeLastPromptPart ];
self.currentTask = nil;
self.error = task.error;
[ [ SKShell currentShell ] printErrorMessage: @"Failed to execute task group" ];
self.running = NO;
if( self.name.length )
{
[ [ SKShell currentShell ] removeLastPromptPart ];
}
return NO;
}
[ [ SKShell currentShell ] removeLastPromptPart ];
}
time = date.elapsedTimeStringSinceNow;
self.currentTask = nil;
if( self.tasks.count > 1 )
{
if( time )
{
time = [ [ NSString stringWithFormat: @"(%@)", time ] stringWithShellColor: SKColorNone ];
[ [ SKShell currentShell ] printSuccessMessage: @"%lu tasks completed successfully %@", self.tasks.count, time ];
}
else
{
[ [ SKShell currentShell ] printSuccessMessage: @"%lu tasks completed successfully", self.tasks.count ];
}
}
self.running = NO;
if( self.name.length )
{
[ [ SKShell currentShell ] removeLastPromptPart ];
}
return YES;
}
}
@end