Created
October 4, 2014 07:35
-
-
Save drkameleon/50f2a38834f463936fb4 to your computer and use it in GitHub Desktop.
Pseudo-asynchronous Block-based Objective-C -> PHP/Python/Ruby Bridge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PPTerminal.m | |
// | |
// Created by Dr.Kameleon on 9/28/14. | |
// Copyright (c) 2011-2014 InSili.co. All rights reserved. | |
// | |
//----------------------------------------------------------- | |
// Pseudo-asynchronous Block-based | |
// Objective-C -> PHP/Python/Ruby Bridge | |
// | |
// + Generic NSTask handker | |
//----------------------------------------------------------- | |
#import "PPTerminal.h" | |
#define RESOURCES_PATH [[NSBundle mainBundle] resourcePath] | |
#define RSRC_PATH(X) [RESOURCES_PATH stringByAppendingPathComponent:(X)] | |
#define DO_ASYNC dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
#define DO_ASYNC_END }); | |
@implementation PPTerminal | |
+ (NSString*)exec:(NSArray *)args | |
{ | |
NSTask* task = [NSTask new]; | |
NSPipe* output = [NSPipe new]; | |
NSPipe* error = [NSPipe new]; | |
NSFileHandle* outputF = [output fileHandleForReading]; | |
NSFileHandle* errorF = [error fileHandleForReading]; | |
[task setLaunchPath:@"/usr/bin/env"]; | |
[task setArguments:args]; | |
[task setStandardOutput:output]; | |
[task setStandardError:error]; | |
[task launch]; | |
[task waitUntilExit]; | |
NSData* outputD = [outputF readDataToEndOfFile]; | |
NSData* errorD = [errorF readDataToEndOfFile]; | |
NSString* outputS = [[NSString alloc] initWithData:outputD encoding:NSUTF8StringEncoding]; | |
NSString* errorS = [[NSString alloc] initWithData:errorD encoding:NSUTF8StringEncoding]; | |
return [outputS stringByAppendingString:errorS]; | |
} | |
+ (NSString*)compile:(NSString *)compiler args:(NSArray*)args script:(NSString *)script data:(NSString *)data | |
{ | |
NSString* scriptPath = RSRC_PATH(script); | |
NSMutableArray* arguments = [[NSMutableArray alloc] initWithObjects: nil]; | |
[arguments addObject:compiler]; | |
[arguments addObjectsFromArray:args]; | |
[arguments addObject:scriptPath]; | |
[arguments addObject:data]; | |
return [self exec:arguments]; | |
} | |
+ (NSString*)php:(NSString*)script data:(NSString *)data | |
{ | |
return [self compile:@"php" args:@[@"-f"] script:script data:data]; | |
} | |
+ (void)php:(NSString*)script data:(NSString *)data action:(void (^)(NSString*))action | |
{ | |
DO_ASYNC | |
action([self php:script data:data]); | |
DO_ASYNC_END | |
} | |
+ (NSString*)python:(NSString*)script data:(NSString *)data | |
{ | |
return [self compile:@"php" args:@[] script:script data:data]; | |
} | |
+ (void)python:(NSString*)script data:(NSString *)data action:(void (^)(NSString*))action | |
{ | |
DO_ASYNC | |
action([self php:script data:data]); | |
DO_ASYNC_END | |
} | |
+ (NSString*)ruby:(NSString*)script data:(NSString *)data | |
{ | |
return [self compile:@"ruby" args:@[] script:script data:data]; | |
} | |
+ (void)ruby:(NSString*)script data:(NSString *)data action:(void (^)(NSString*))action | |
{ | |
DO_ASYNC | |
action([self php:script data:data]); | |
DO_ASYNC_END | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment