-
Notifications
You must be signed in to change notification settings - Fork 1
/
ScriptPlugins.m
321 lines (259 loc) · 8.91 KB
/
ScriptPlugins.m
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//
// ScriptPlugins.m
// StatusItem
//
// Created by Zack Smith on 8/15/11.
// Copyright 2011 318. All rights reserved.
//
#import "ScriptPlugins.h"
#import "Constants.h"
#import "StatusItemAppDelegate.h"
@implementation ScriptPlugins
// Our Method overrides
-(id)init
{
[ super init];
mainBundle = [NSBundle bundleForClass:[self class]];
[ self readInSettings];
// and Return
if (!self) return nil;
return self;
}
- (void)readInSettings
{
NSString *settingsPath = [mainBundle pathForResource:MySettingsFileResourceID
ofType:@"plist"];
settings = [[NSDictionary alloc] initWithContentsOfFile:settingsPath];
}
-(IBAction)updatePluginMenus:(id)sender
{
if(debugEnabled) NSLog(@"User clicked the plugins button...");
//[self runPluginScripts];
}
-(void)runPluginScripts:(id)sender
{
if(debugEnabled) NSLog(@"Running Plugin Scripts...");
[self addConfigScriptArguments];
NSDictionary * scriptPlugins = [ settings objectForKey:@"scriptPlugins"];
NSDictionary * nonPrivilegedScripts = [ scriptPlugins objectForKey:@"nonPrivilegedScripts"];
// Enumerate our Script headers (Menu Headers)
for(NSString *headerGUID in nonPrivilegedScripts){
NSDictionary *scriptHeader = [nonPrivilegedScripts objectForKey:headerGUID];
// Grab the Header Menu Item Title
NSString *headerTitle = [scriptHeader objectForKey:@"headerTitle"];
if(debugEnabled)NSLog(@"Found Script Title: %@",headerTitle);
// Add the header item to the Menu
NSInteger menuHeaderTag = [ sender addPluginMenuHeader:headerTitle];
NSArray * itemScripts = [scriptHeader objectForKey:@"itemScripts"];
// Enumerate through the items array to add items below our header
for (id scriptDictionary in itemScripts) {
[ self runScript:scriptDictionary
withArguments:configScriptArguments
forMenu:menuHeaderTag
controller:sender];
}
}
// Notifiy our observers that plugins have loaded
[[NSNotificationCenter defaultCenter]
postNotificationName:PluginsHaveLoadedNotification
object:self];
}
- (void) addConfigScriptArguments
{
configScriptArguments = [[NSMutableArray alloc] init];
[ configScriptArguments addObject:@"-v"];
}
- (BOOL)runScript:(NSDictionary *)scriptDictionary
withArguments:(NSMutableArray *)scriptArguments
forMenu:(NSInteger)menuTag
controller:(id)sender
{
// Check for any scripts running at the moment
[self waitForLastScriptToFinish];
// Take control of the run lock
scriptIsRunning = YES;
/*[self setScriptIsRunning:scriptDictionary forMenu:myMenuItem];*/
// Create a pool so we don't leak on our NSThread
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *scriptPath = [scriptDictionary objectForKey:@"scriptPath"];
NSString *scriptExtention = [scriptDictionary objectForKey:@"scriptExtention"];
if ([[scriptDictionary objectForKey:@"scriptIsInBundle"] boolValue]){
scriptPath = [mainBundle pathForResource:scriptPath ofType:scriptExtention];
// Z1 an't get this to work without build phase addtions to copy the directory
//scriptPath = [mainBundle pathForResource:scriptPath ofType:scriptExtention inDirectory:@"bin"];
if (!scriptPath) {
NSLog(@" No Script path found");
}
else {
if(debugEnabled) NSLog(@"Found script path:%@",scriptPath);
}
}
// Validate script exits and is executable
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:scriptPath]){
if(debugEnabled) NSLog(@"Script exists at path:%@",scriptPath);
}
else {
NSException *anException;
NSLog(@"Script does NOT exist at path:%@",scriptPath);
NSString *aReason = [ NSString stringWithFormat:@"Script missing: %@",scriptPath];
anException = [NSException exceptionWithName:@"Missing Script"
reason:aReason
userInfo:nil];
return NO;
}
// Check script is executable
if ([[NSFileManager defaultManager]isExecutableFileAtPath:scriptPath]) {
NSLog(@"Validated script is executable");
}
else {
NSException *anException;
NSLog(@"Script is NOT executable at path:%@",scriptPath);
NSString *aReason = [ NSString stringWithFormat:@"Script not executable: %@",scriptPath];
anException = [NSException exceptionWithName:@"Script Attributes"
reason:aReason
userInfo:nil];
return NO;
}
// Run the Task - Z1: Needs to be broken out
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: scriptPath];
[task setArguments: scriptArguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
//Set to help with Xcode debug log issues
[task setStandardInput:[NSPipe pipe]];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *scriptOutput;
[task waitUntilExit];
scriptOutput = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
if (!scriptOutput) {
scriptOutput = @"Error";
}
int status = [task terminationStatus];
scriptIsRunning = NO;
//[ self stopMainProgressIndicator];
if ( status > 0 ){
[self setFailedEndStatusFromScript:scriptDictionary
withError:scriptOutput
withExitCode:status
forMenu:menuTag
controller:sender];
return NO;
}
else {
// exit 0
[self setEndStatusFromScript:scriptDictionary
withOutPut:scriptOutput
forMenu:menuTag
controller:sender];
return YES;
}
[pool drain];
}
-(void)setFailedEndStatusFromScript:(NSDictionary *)scriptDictionary
withError:(NSString *)errorMessage
withExitCode:(int)exitStatus
forMenu:(NSInteger)menuTag
controller:(id)sender
{
NSString *scriptFailedTitle;
NSString *scriptFailedDescription;
BOOL isAlternate;
// Set if the menu is alternate or not
isAlternate = [[scriptDictionary objectForKey:@"isAlternate"]boolValue];
if (exitStatus == 1) {
// Use Generic Message for non custom statuses
scriptFailedTitle = [scriptDictionary valueForKey:@"scriptFailedTitle"];
NSLog(@"Found Script Failed message: %@",scriptFailedTitle);
NSLog(@"Script: %@ exited 1 setting warning icon",scriptFailedTitle);
}
scriptFailedDescription = @"Script Output Supressed";
/*
if (!scriptFailedDescription) {
if (errorMessage = NULL) {
scriptFailedDescription = @"Unkown";
}
else {
scriptFailedDescription = errorMessage;
}
}*/
if (exitStatus > 1) {
NSString *exitStatusKey = [ NSString stringWithFormat:@"%d",exitStatus ];
if(debugEnabled)NSLog(@"Found exit status:%@",exitStatusKey);
// Read in our exit status info from keys
NSDictionary *exitCodes = [ scriptDictionary objectForKey:@"exitCodes" ];
NSDictionary *exitCode = [ exitCodes objectForKey:exitStatusKey ];
// Red status for exit codes that are greater then 1
if (exitStatus >= 192){
if(debugEnabled) NSLog(@"#####################--STATE CHANGE--####################--[NO]--");
}
// Grab Our Specific Error code
scriptFailedTitle = [ exitCode objectForKey:@"scriptFailedTitle"];
scriptFailedDescription = [exitCode objectForKey:@"scriptFailedDescription"];
// Just in case we forgot to add an exit code string
if (!exitCode) {
scriptFailedTitle = [ scriptDictionary objectForKey:@"scriptFailedTitle"];
scriptFailedDescription = [scriptDictionary objectForKey:@"scriptFailedDescription"];
}
}
if (exitStatus = 192) {
// Grey status for Exit 192
}
[self setStatus:scriptFailedTitle
withMessage:scriptFailedDescription
forMenu:menuTag
controller:sender
asAlternate:isAlternate];
}
-(void)setStatus:(NSString *)scriptTitle
withMessage:(NSString *)scriptDescription
forMenu:(NSInteger)menuTag
controller:(id)sender
asAlternate:(BOOL)alternate
{
NSLog(@"Finished script: %@",scriptTitle);
// Set the menu items Tool Tip
// Set the menu Items text
[ sender addPluginMenuChild:scriptTitle
withToolTip:scriptDescription
asAlternate:alternate];
}
-(void)waitForLastScriptToFinish
{
while (scriptIsRunning) {
[NSThread sleepForTimeInterval:0.5f];
NSLog(@"Waiting for last script to run...");
}
}
-(void)setEndStatusFromScript:(NSDictionary *)scriptDictionary
withOutPut:scriptOutput
forMenu:(NSInteger)menuTag
controller:(id)sender
{
// Check if we were passed an empty value.
NSString *scriptTitle;
if (![scriptOutput isEqualToString:@""]) {
scriptTitle = scriptOutput;
}
else {
scriptTitle = [scriptDictionary objectForKey:@"scriptEndTitle"];
}
NSString *scriptDescription = [scriptDictionary objectForKey:@"scriptEndDescription"];
BOOL isAlternate = [[scriptDictionary objectForKey:@"isAlternate"]boolValue];
if(debugEnabled)NSLog(@"Successful Script run:%@",scriptTitle);
if(debugEnabled)NSLog(@"Successful Script Description:%@",scriptDescription);
// Update the menu to Green
if(debugEnabled) NSLog(@"%@: Shell Script exited 0",scriptTitle);
// Add the Child Menu on Successful Exits
[ sender addPluginMenuChild:scriptTitle
withToolTip:scriptDescription
asAlternate:isAlternate];
}
@end