-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuildWithAntPlugin.m
104 lines (95 loc) · 3.36 KB
/
BuildWithAntPlugin.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
//
// BuildWithAntPlugin.m
// BuildWithAnt
//
// Created by Mark Lilback on 2/4/11.
// Copyright 2011 Agile Monks, LLC. All rights reserved.
//
#import "BuildWithAntPlugin.h"
@implementation BuildWithAntPlugin
- (id)initWithPlugInController:(CodaPlugInsController*)aController bundle:(NSBundle*)yourBundle
{
self = [super init];
self.pluginController = aController;
[self.pluginController registerActionWithTitle:@"Build With Ant" underSubmenuWithTitle:nil
target:self selector:@selector(buildWithAnt:) representedObject:nil keyEquivalent:@"$@k"
pluginName:self.name];
NSString *path = [yourBundle pathForResource:@"correct" ofType:@"mp3"];
_successSound = [[NSSound alloc] initWithContentsOfFile:path byReference:NO];
path = [yourBundle pathForResource:@"wrong" ofType:@"mp3"];
_errorSound = [[NSSound alloc] initWithContentsOfFile:path byReference:NO];
return self;
}
-(void)dealloc
{
[_successSound release];_successSound=nil;
[_errorSound release];_errorSound=nil;
self.pluginController=nil;
[super dealloc];
}
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
CodaTextView *tview = [self.pluginController focusedTextView:self];
NSString *sitePath = [tview siteLocalPath];
NSString *buildPath = [sitePath stringByAppendingPathComponent:@"WEB-INF"];
NSString *buildFile = [buildPath stringByAppendingPathComponent:@"build.xml"];
return [[NSFileManager defaultManager] fileExistsAtPath:buildFile];
}
-(NSString*)name
{
return @"Build With Ant";
}
-(NSString*)parseErrors:(NSString*)xmlContent
{
NSError *err=nil;
NSXMLDocument *doc = [[[NSXMLDocument alloc] initWithXMLString:xmlContent options:0 error:&err] autorelease];
if (err) {
NSLog(@"error: %@", err);
return xmlContent;
}
NSArray *nodes = [doc nodesForXPath:@"//target/task[@name='javac']/message" error:&err];
NSMutableString *str = [NSMutableString stringWithFormat:@"<html><head><title>javac Errors</title><?head><body>"
"<h1>javac reported the following:</h1>\n<pre>"];
for (NSXMLNode *node in nodes) {
[str appendFormat:@"%@\n", [node stringValue]];
}
[str appendFormat:@"</pre></body></html>\n"];
return str;
}
-(IBAction)buildWithAnt:(id)sender
{
CodaTextView *tview = [self.pluginController focusedTextView:self];
NSString *sitePath = [tview siteLocalPath];
NSString *buildPath = [sitePath stringByAppendingPathComponent:@"WEB-INF"];
NSString *buildFile = [buildPath stringByAppendingPathComponent:@"build.xml"];
if (![[NSFileManager defaultManager] fileExistsAtPath:buildFile]) {
NSBeep();
return;
}
//build a task
NSTask *task = [[NSTask alloc] init];
NSPipe *outpipe = [NSPipe pipe];
[task setStandardOutput:outpipe];
NSMutableArray *args = [NSMutableArray array];
[args addObject:@"-q"];
[args addObject:@"-logger"];
[args addObject:@"org.apache.tools.ant.XmlLogger"];
[task setCurrentDirectoryPath:buildPath];
[task setLaunchPath:@"/usr/bin/ant"];
[task setArguments:args];
[task launch];
[task waitUntilExit];
if (0 != [task terminationStatus]) {
[_errorSound play];
NSFileHandle *fh = [outpipe fileHandleForReading];
NSData *d = [fh readDataToEndOfFile];
NSString *str = [[[NSString alloc] initWithData:d encoding:NSUTF8StringEncoding] autorelease];
str = [self parseErrors:str];
str = [NSString stringWithFormat:@"<pre>%@</pre>", str];
[self.pluginController displayHTMLString:str];
} else {
[_successSound play];
}
}
@synthesize pluginController=_pluginController;
@end