Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to specify executable .slate's which emit config #145

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Slate.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
47C4A2D11384F3E10066B6DE /* Operation.m in Sources */ = {isa = PBXBuildFile; fileRef = 47C4A2D01384F3E10066B6DE /* Operation.m */; };
47C4A2D41384F4A60066B6DE /* MoveOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 47C4A2D31384F4A60066B6DE /* MoveOperation.m */; };
47C4A2D71384F5DE0066B6DE /* ExpressionPoint.m in Sources */ = {isa = PBXBuildFile; fileRef = 47C4A2D61384F5DE0066B6DE /* ExpressionPoint.m */; };
7B2A679C16BF8A7F0046D695 /* SlateConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = 47C4A2CA1384EA510066B6DE /* SlateConfig.m */; };
B3D087D0160577FB008A7FC6 /* status.pdf in Resources */ = {isa = PBXBuildFile; fileRef = B3D087CF160577FB008A7FC6 /* status.pdf */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -716,6 +717,7 @@
3BA1BA62162DDFEF0026774E /* TestExpressionPoint.m in Sources */,
3BA1BA65162E01A30026774E /* TestStringTokenizer.m in Sources */,
3BA1BA6E162F51760026774E /* TestShellUtils.m in Sources */,
7B2A679C16BF8A7F0046D695 /* SlateConfig.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
25 changes: 13 additions & 12 deletions Slate/ShellUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ + (BOOL)commandExists:(NSString *)command {
pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
[task setStandardInput:[NSPipe pipe]];

NSFileHandle *file;
file = [pipe fileHandleForReading];

Expand All @@ -59,21 +59,22 @@ + (BOOL)commandExists:(NSString *)command {
}

+ (NSTask *)run:(NSString *)command args:(NSArray *)args wait:(BOOL)wait path:(NSString *)path {
NSTask *task;
task = [[NSTask alloc] init];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:command];
[task setArguments:args];
if (path != nil) [task setCurrentDirectoryPath:path];

NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
[task setStandardInput:[NSPipe pipe]];

NSFileHandle *file;
file = [pipe fileHandleForReading];


NSPipe *opipe, *ipipe;
opipe = [NSPipe pipe];
ipipe = [NSPipe pipe];

[task setStandardOutput:opipe];
[task setStandardInput:ipipe];

NSFileHandle *file = [ipipe fileHandleForWriting];

[task launch];
[file closeFile];
if (wait) [task waitUntilExit];
return task;
}
Expand Down
32 changes: 25 additions & 7 deletions Slate/SlateConfig.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@
#import "Snapshot.h"
#import "SnapshotList.h"
#import "JSONKit.h"
#import "ShellUtils.h"
#import "SlateLogger.h"
#import "NSFileManager+ApplicationSupport.h"
#import "NSString+Indicies.h"
#import "ActivateSnapshotOperation.h"

#include <sys/stat.h>

@implementation SlateConfig

@synthesize configs;
Expand Down Expand Up @@ -142,7 +145,7 @@ - (BOOL)load {
}
return [self loadConfigFileWithPath:[[NSBundle mainBundle] pathForResource:@"default" ofType:@"slate"]];
}

if (![self loadSnapshots]) {
SlateLogger(@" ERROR Could not load %@", SNAPSHOTS_FILE);
NSAlert *alert = [SlateConfig warningAlertWithKeyEquivalents: [NSArray arrayWithObjects:@"Quit", @"Skip", nil]];
Expand All @@ -169,9 +172,24 @@ - (BOOL)load {
- (BOOL)loadConfigFileWithPath:(NSString *)file {
if (file == nil) return NO;
NSString *configFile = file;
NSString *fileString;
struct stat _stat;
if ([file rangeOfString:SLASH].location != 0 && [file rangeOfString:TILDA].location != 0)
configFile = [NSString stringWithFormat:@"~/%@", file];
NSString *fileString = [NSString stringWithContentsOfFile:[configFile stringByExpandingTildeInPath] encoding:NSUTF8StringEncoding error:nil];
if ([file rangeOfString:TILDA].location == 0)
configFile = [configFile stringByExpandingTildeInPath];

int ret = stat([configFile cStringUsingEncoding:NSASCIIStringEncoding] , &_stat);

if (_stat.st_mode & S_IXUSR) {
NSLog(@"Trying to use executable config");
NSTask *task = [ShellUtils run:configFile args:[NSArray arrayWithObjects:nil] wait:YES path:nil];
NSData *data = [[[task standardOutput] fileHandleForReading] readDataToEndOfFile];
fileString = [[NSString alloc] initWithData: data encoding:NSUTF8StringEncoding];
} else {
NSLog(@"Trying to use literal config");
fileString = [NSString stringWithContentsOfFile:configFile encoding:NSUTF8StringEncoding error:nil];
}
return [self append:fileString];
}

Expand All @@ -190,7 +208,7 @@ - (BOOL)append:(NSString *)configString {
if (configString == nil)
return NO;
NSArray *lines = [configString componentsSeparatedByString:@"\n"];

NSEnumerator *e = [lines objectEnumerator];
NSString *line = [e nextObject];
while (line) {
Expand Down Expand Up @@ -449,10 +467,10 @@ - (NSDictionary *)snapshotsToDictionary {
- (void)saveSnapshots {
// Build NSDictionary with snapshots
NSDictionary *snapshotDict = [self snapshotsToDictionary];

// Get NSData from NSDictionary
NSData *jsonData = [snapshotDict JSONData];

// Save NSData to file
[jsonData writeToURL:[SlateConfig snapshotsFile] atomically:YES];
}
Expand All @@ -468,7 +486,7 @@ - (void)addSnapshot:(Snapshot *)snapshot name:(NSString *)name saveToDisk:(BOOL)
}
[list addSnapshot:snapshot];
[snapshots setObject:list forKey:name];

[self saveSnapshots];
}

Expand All @@ -481,7 +499,7 @@ - (void)deleteSnapshot:(NSString *)name pop:(BOOL)pop {
} else {
[snapshots removeObjectForKey:name];
}

[self saveSnapshots];
}

Expand Down