mirror of
https://github.com/MacDownApp/macdown.git
synced 2026-05-17 12:40:37 +00:00
6192294908
Implemented logic for CLI installation from the GUI app. A preference pane is added. The pane detects whether the user has something installed that looks like the MacDown CLI utility by checking common installtion locations, e.g. Homebrew. If the util is not detected to be installed, the user can click a button to install it. For now the only option is to install it to /usr/local/bin/macdown.
94 lines
2.3 KiB
Objective-C
94 lines
2.3 KiB
Objective-C
//
|
|
// MPHomebrewSubprocessController.m
|
|
// MacDown
|
|
//
|
|
// Created by Tzu-ping Chung on 18/2.
|
|
// Copyright © 2017 Tzu-ping Chung . All rights reserved.
|
|
//
|
|
|
|
#import "MPHomebrewSubprocessController.h"
|
|
|
|
|
|
@interface MPHomebrewSubprocessController ()
|
|
|
|
@property (readonly) NSTask *task;
|
|
@property (readwrite) void(^completionHandler)(NSString *);
|
|
|
|
@end
|
|
|
|
|
|
@implementation MPHomebrewSubprocessController
|
|
|
|
- (instancetype)initWithArguments:(NSArray *)args
|
|
{
|
|
self = [super init];
|
|
if (!self)
|
|
return nil;
|
|
|
|
NSPipe *stdoutPipe = [[NSPipe alloc] init];
|
|
NSFileHandle *stdoutReadHandle = stdoutPipe.fileHandleForReading;
|
|
|
|
_task = [[NSTask alloc] init];
|
|
_task.launchPath = @"brew";
|
|
if (args)
|
|
_task.arguments = args;
|
|
_task.standardOutput = stdoutPipe;
|
|
|
|
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
|
|
[center addObserver:self
|
|
selector:@selector(homebrewReadDidComplete:)
|
|
name:NSFileHandleReadToEndOfFileCompletionNotification
|
|
object:stdoutReadHandle];
|
|
[stdoutReadHandle readToEndOfFileInBackgroundAndNotify];
|
|
|
|
return self;
|
|
}
|
|
|
|
- (instancetype)init
|
|
{
|
|
return [self initWithArguments:nil];
|
|
}
|
|
|
|
- (void)dealloc
|
|
{
|
|
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
|
|
[center removeObserver:self
|
|
name:NSFileHandleReadToEndOfFileCompletionNotification
|
|
object:nil];
|
|
}
|
|
|
|
- (void)runWithCompletionHandler:(void(^)(NSString *))handler
|
|
{
|
|
self.completionHandler = handler;
|
|
@try
|
|
{
|
|
[self.task launch];
|
|
}
|
|
@catch (NSException *exception) // Homebrew not installed.
|
|
{
|
|
if (handler)
|
|
handler(nil);
|
|
}
|
|
}
|
|
|
|
- (void)homebrewReadDidComplete:(NSNotification *)notification
|
|
{
|
|
NSData *outData = notification.userInfo[NSFileHandleNotificationDataItem];
|
|
NSString *output = [[NSString alloc] initWithData:outData
|
|
encoding:NSUTF8StringEncoding];
|
|
if (self.completionHandler)
|
|
self.completionHandler(output);
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
void MPDetectHomebrewPrefixWithCompletionhandler(void(^handler)(NSString *))
|
|
{
|
|
NSArray *args = @[@"--prefix"];
|
|
MPHomebrewSubprocessController *c =
|
|
[[MPHomebrewSubprocessController alloc] initWithArguments:args];
|
|
[c runWithCompletionHandler:handler];
|
|
}
|
|
|