-
Notifications
You must be signed in to change notification settings - Fork 1
/
TSLaunchDController.m
104 lines (81 loc) · 2.9 KB
/
TSLaunchDController.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
//
// TSLaunchDController.m
// Brow
//
// Created by Tim Schröder on 10.07.16.
// Copyright © 2016 Tim Schröder. All rights reserved.
//
#import "TSLaunchDController.h"
#import "NSFileManager+Extensions.h"
#import "TSLogger.h"
@implementation TSLaunchDController
static TSLaunchDController *_sharedController = nil;
#pragma mark -
#pragma mark Singleton Methods
+ (TSLaunchDController *)sharedController
{
if (!_sharedController) {
_sharedController = [[super allocWithZone:NULL] init];
}
return _sharedController;
}
+ (id)allocWithZone:(NSZone *)zone
{
return [self sharedController];
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
#pragma mark -
#pragma mark Internal Helper Methods
-(void)invokeLaunchctlWithCommand:(NSString*)cmd argument:(NSString*)arg
{
NSString *launchctlPath = @"/bin/launchctl";
NSTask *task = [NSTask launchedTaskWithLaunchPath:launchctlPath
arguments:[NSArray arrayWithObjects:cmd,
arg,
nil]];
[task waitUntilExit];
}
-(void)removeLaunchdPlistFileAtPath:(NSString*)path
{
TSLog (@"TSLaunchDController: removeLaunchdPlistFileAtPath at %@", path);
if ([[NSFileManager defaultManager] isDeletableFileAtPath:path]) {
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}
}
#pragma mark -
#pragma mark Public Methods
// Loads a service in launchd
// and copies plist file to launchd location so that it is started again at next login
-(void)loadService:(NSString*)bundleIdentifier withPList:(NSDictionary*)pList
{
NSString *path = [self launchdPListPathForBundleIdentifier:bundleIdentifier];
TSLog (@"TSLaunchDController: loadService at %@", path);
// Remove plist file
[self removeLaunchdPlistFileAtPath:path];
// Copy plist file
[[NSFileManager defaultManager] createDirectory:[path stringByDeletingLastPathComponent]];
[pList writeToURL:[NSURL fileURLWithPath:path] atomically:YES];
// Load service
[self invokeLaunchctlWithCommand:@"load" argument:path];
}
// Unloads a service from launchd and removes the plist file also so that
// the service is not started again on next login
-(void)unLoadService:(NSString*)bundleIdentifier
{
NSString *path = [self launchdPListPathForBundleIdentifier:bundleIdentifier];
TSLog (@"TSLaunchDController: unLoadService at %@", path);
[self invokeLaunchctlWithCommand:@"unload" argument:path];
[self removeLaunchdPlistFileAtPath:path];
}
// Computes the full file system path for a launchd plist file for a given bundle identifier
-(NSString*)launchdPListPathForBundleIdentifier:(NSString*)bundleIdentifier
{
return [NSString stringWithFormat:@"%@%@%@.plist",
NSHomeDirectory(),
@"/Library/LaunchAgents/",
bundleIdentifier];
}
@end