-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathERSassPlugIn.m
243 lines (201 loc) · 8.41 KB
/
ERSassPlugIn.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
//
// ERSassPlugIn.m
// Copyright ©2011 Eric Roccasecca. All rights reserved.
//
// 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.
//
#import "ERSassPlugIn.h"
#import "CodaPlugInsController.h"
#include <objc/runtime.h>
#import "ERSassPreferences.h"
@implementation ERSassPlugIn
- (id)initWithPlugInController:(CodaPlugInsController*)inController bundle:(NSBundle*)aBundle
{
if ( (self = [super init]) != nil )
{
controller = inController;
sassPluginBundle = aBundle;
// Migration of a default to a more correct key name.
if ([[NSUserDefaults standardUserDefaults] valueForKey:@"CodaSassPlugin_MonitorSave"] != nil && [[NSUserDefaults standardUserDefaults] valueForKey:@"ERSassPlugin_MonitorSave"] == nil)
[[NSUserDefaults standardUserDefaults] setValue:[[NSUserDefaults standardUserDefaults] valueForKey:@"CodaSassPlugin_MonitorSave"] forKey:@"ERSassPlugin_MonitorSave"];
[controller registerActionWithTitle:NSLocalizedString(@"Convert Scss to Css", @"Convert Scss to Css")
underSubmenuWithTitle:nil
target:self
selector:@selector(convertScssToCss:)
representedObject:nil
keyEquivalent:@"^$S"
pluginName:[self name]];
[controller registerActionWithTitle:NSLocalizedString(@"Sass Preferences…", @"Sass Preferences…")
underSubmenuWithTitle:nil
target:self
selector:@selector(openSassPreferences:)
representedObject:nil
keyEquivalent:nil
pluginName:[self name]];
// TSWrapperDidSaveNotification was derived by observing notifications being sent in Coda.
// This could change in future versions of Coda.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(codaDocumentSavedNotification:) name:@"TSWrapperDidSaveNotification" object:nil];
}
return self;
}
- (NSString*)name
{
return @"Sass";
}
- (IBAction)openSassPreferences:(id)sender
{
ERSassPreferences *prefs = [[ERSassPreferences alloc] init];
[prefs runModal];
[prefs release];
}
- (IBAction)convertScssToCss:(id)sender
{
NSString *scssPath = [self currentEditedScssPath];
if (scssPath)
{
[[controller focusedTextView:self] save];
[self generateCssForPath:scssPath];
}
}
- (BOOL)validateMenuItem:(NSMenuItem*)menuItem
{
BOOL result = NO;
if ( [menuItem action] == @selector(convertScssToCss:) )
result = ([self currentEditedScssPath] != nil);
else if ( [menuItem action] == @selector(openSassPreferences:) )
result = YES;
return result;
}
- (void)codaDocumentSavedNotification:(NSNotification*)notification
{
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"ERSassPlugin_MonitorSave"])
return;
// The object for TSWrapperDidSaveNotification is currently a subclass of NSDocument.
// This could change in future versions of Coda. Sanity check the object before using.
NSDocument *document = [notification object];
if (document == nil || ![document isKindOfClass:[NSDocument class]])
{
NSLog (@"The Sass plug-in Save notification received unexpected data.");
return;
}
NSURL *documentURL = [document fileURL];
if ([documentURL isFileURL])
{
NSString *documentPath = [documentURL path];
if ([self isPathScss:documentPath])
[self generateCssForPath:documentPath];
}
}
- (NSString*)currentEditedScssPath
{
NSString *filePath = [[controller focusedTextView:self] path];
if ([self isPathScss:filePath])
return filePath;
return nil;
}
- (BOOL)isPathScss:(NSString*)filePath
{
return [[[filePath pathExtension] lowercaseString] isEqualToString:@"scss"];
}
- (NSString*)cssPathForScssPath:(NSString*)scssPath
{
// Default css path is next to the scss path
NSString *cssPath = [[scssPath stringByDeletingPathExtension] stringByAppendingPathExtension:@"css"];
NSString *cssUserSaveFolderPath = [[NSUserDefaults standardUserDefaults] stringForKey:@"ERSassPlugin_UserSaveFolderPath"];
if (cssUserSaveFolderPath.length != 0)
{
NSString *cssFolderPath = nil;
int relativePathMode = [[NSUserDefaults standardUserDefaults] integerForKey:@"ERSassPlugin_UserSaveFolderPathRelativeMode"];
if (relativePathMode == SaveRelativeToScssFile)
{
cssFolderPath = [[scssPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:cssUserSaveFolderPath];
}
else if (relativePathMode == SaveAbsolute)
{
cssFolderPath = [cssUserSaveFolderPath stringByExpandingTildeInPath];
}
if (![[NSFileManager defaultManager] fileExistsAtPath:cssFolderPath])
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"ERSassPlugin_CreateUserSaveFolder"])
{
if (![[NSFileManager defaultManager] createDirectoryAtPath:cssFolderPath withIntermediateDirectories:YES attributes:nil error:NULL])
{
NSAlert *alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Css save folder does not exist and can't be created.",@"Css save folder does not exist and can't be created.")
defaultButton:NSLocalizedString(@"OK",@"OK")
alternateButton:nil
otherButton:nil
informativeTextWithFormat:cssFolderPath];
[alert runModal];
return nil;
}
}
else
{
NSAlert *alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Css save folder does not exist.",@"Css save folder does not exist.")
defaultButton:NSLocalizedString(@"OK",@"OK")
alternateButton:nil
otherButton:nil
informativeTextWithFormat:cssFolderPath];
[alert runModal];
return nil;
}
}
else if (![[NSFileManager defaultManager] isWritableFileAtPath:cssFolderPath])
{
NSAlert *alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Unable to save to the css folder.",@"Unable to save to the css folder")
defaultButton:NSLocalizedString(@"OK",@"OK")
alternateButton:nil
otherButton:nil
informativeTextWithFormat:cssFolderPath];
[alert runModal];
return nil;
}
NSString *scssBaseFileName = [[scssPath lastPathComponent] stringByDeletingPathExtension];
cssPath = [[cssFolderPath stringByAppendingPathComponent:scssBaseFileName] stringByAppendingPathExtension:@"css"];
}
return cssPath;
}
- (void)generateCssForPath:(NSString*)scssPath
{
if (scssPath == nil) return;
if (![[NSFileManager defaultManager] fileExistsAtPath:@"/usr/bin/sass"])
{
NSAlert *alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Sass is not installed.",@"Sass is not installed.")
defaultButton:NSLocalizedString(@"OK",@"OK")
alternateButton:nil
otherButton:nil
informativeTextWithFormat:NSLocalizedString(@"Visit sass-lang.com for more information.",@"Visit sass-lang.com for more information.")];
[alert runModal];
return;
}
NSString *cssPath = [self cssPathForScssPath:scssPath];
if (cssPath == nil)
return;
NSTask *sassTask = [[NSTask alloc] init];
[sassTask setLaunchPath:@"/usr/bin/sass"];
[sassTask setCurrentDirectoryPath:[scssPath stringByDeletingLastPathComponent]];
[sassTask setArguments:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%@:%@",scssPath,cssPath], nil]];
[sassTask launch];
[sassTask waitUntilExit];
if ([sassTask terminationStatus] != 0)
{
NSAlert *alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Sass could not be completed.",@"Sass could not be completed.")
defaultButton:NSLocalizedString(@"OK",@"OK")
alternateButton:nil
otherButton:nil
informativeTextWithFormat:@"Scss: %@\n\nCss:%@", scssPath, cssPath];
[alert runModal];
}
[sassTask release];
}
@end