-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJSHintPlugIn.m
129 lines (89 loc) · 3.17 KB
/
JSHintPlugIn.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
//
// JSHintPlugin.m
// jshint-codaplugin
//
// Created by Toth,Erik(ertoth) on 6/22/12.
// Copyright 2012 __MyCompanyName__. All rights reserved.
//
#import "JSHintPlugin.h"
#import "CodaPlugInsController.h"
@interface JSHintPlugIn ()
- (id)initWithController:(CodaPlugInsController*)inController;
- (void)runJSHint:(id) sender;
@end
@implementation JSHintPlugIn
//2.0.1 and higher
- (id)initWithPlugInController:(CodaPlugInsController*)aController plugInBundle:(NSObject <CodaPlugInBundle> *)plugInBundle {
return [self initWithController:aController];
}
- (id)initWithController:(CodaPlugInsController*)inController {
if ( (self = [super init]) != nil ) {
controller = inController;
[controller registerActionWithTitle:NSLocalizedString(@"JSHint", @"JSHint") target:self selector:@selector(runOnCurrentView:)];
[controller registerActionWithTitle:NSLocalizedString(@"Preferences", @"") target:self selector:@selector(showDialog)];
}
return self;
}
- (NSString*)name {
return @"JSHint";
}
- (void)textViewWillSave:(CodaTextView*)textView {
[self runJSHint:textView];
}
- (void)runOnCurrentView:(id) sender {
CodaTextView* textView = [controller focusedTextView:self];
[self runJSHint:textView];
}
- (void)showDialog {
}
- (BOOL)isJsFile:(NSString*)filePath {
return [[[filePath pathExtension] lowercaseString] isEqualToString:@"js"];
}
- (void)runJSHint:(CodaTextView*)textView {
if ( !textView || ![self isJsFile:[textView path]]) {
return;
}
// Get the path to the shell script
NSBundle* myBundle = [NSBundle bundleWithIdentifier:@"com.erik-toth.plugin.JSHint"];
NSString* script = [myBundle pathForResource:@"js-call" ofType:@"sh"];
NSString* resourcePath = [script stringByDeletingLastPathComponent];
NSLog(@"Shell script path: %@", script);
// Create a task...
NSTask *task;
task = [[NSTask alloc] init];
// ...and point it to our script
[task setLaunchPath: @"/bin/bash"];
[task setCurrentDirectoryPath:resourcePath];
NSArray *arguments;
arguments = [NSArray arrayWithObjects:@"js-call.sh", @"jshintwrap.js", @"undef", @"LF", nil];
[task setArguments: arguments];
// Create the pipe that will send the input data to the script
NSPipe *inPipe;
inPipe = [NSPipe pipe];
[task setStandardInput: inPipe];
// Setup the output pipe
NSPipe *outPipe;
outPipe = [NSPipe pipe];
[task setStandardOutput: outPipe];
[task launch];
// Convert the input string to data
NSData *textData = [[textView string] dataUsingEncoding:NSUTF8StringEncoding];
[[inPipe fileHandleForWriting] writeData:textData];
[[inPipe fileHandleForWriting] closeFile];
NSMutableData *data = [[NSMutableData alloc] init];
NSData *readData;
while ((readData = [[outPipe fileHandleForReading] availableData]) && [readData length]) {
[data appendData: readData];
}
NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
// NSData *data = [[outPipe fileHandleForReading] readDataToEndOfFile];
// NSString *string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
[task waitUntilExit];
[task release];
[data release];
if ([string length] != 0) {
[controller displayHTMLString:string];
}
}
@end