forked from wakatime/textmate-wakatime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WakaTime.mm
175 lines (152 loc) · 6.2 KB
/
WakaTime.mm
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
//
// WakaTime.mm
//
//
#import "WakaTime.h"
#import "NSWindow+KKSwizzle.h"
static NSString *VERSION = @"1.1.0";
static NSString *WAKATIME_INSTALL_SCRIPT = @"Library/Application Support/TextMate/PlugIns/WakaTime.tmplugin/Contents/Resources/install_dependencies.sh";
static NSString *WAKATIME_CLI = @".wakatime/wakatime-cli";
static NSString *CONFIG_FILE = @".wakatime.cfg";
static int FREQUENCY = 15; // seconds
@implementation WakaTime
static NSMutableDictionary *_windows;
static NSString *_lastFile;
static CFAbsoluteTime _lastTime;
+ (void)initialize {
static BOOL initialized = NO;
if(!initialized) {
initialized = YES;
_windows = [[ NSMutableDictionary alloc] init];
}
}
- (id)initWithPlugInController:(id <TMPlugInController>)aController {
NSApp = [NSApplication sharedApplication];
if(self = [super init]) {
NSLog(@"Initializing WakaTime plugin v%@ (http://wakatime.com)", VERSION);
// Set runtime constants
CONFIG_FILE = [NSHomeDirectory() stringByAppendingPathComponent:CONFIG_FILE];
// check for wakatime cli
NSString *cli = [NSHomeDirectory() stringByAppendingPathComponent:WAKATIME_CLI];
NSFileManager *filemgr = [NSFileManager defaultManager];
if (![filemgr fileExistsAtPath:cli]) {
[self installCLI];
}
NSString *api_key = [[self getApiKey] stringByReplacingOccurrencesOfString:@" " withString:@""];
if (api_key == NULL || [api_key length] == 0) {
[self promptForApiKey];
}
}
return self;
}
- (void)installCLI {
NSLog(@"Installing wakatime cli...");
NSString *script = [NSHomeDirectory() stringByAppendingPathComponent:WAKATIME_INSTALL_SCRIPT];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/bash"];
NSMutableArray *arguments = [NSMutableArray array];
[arguments addObject:script];
[task setArguments: arguments];
[task launch];
}
// Read api key from config file
- (NSString *)getApiKey {
NSString *contents = [NSString stringWithContentsOfFile:CONFIG_FILE encoding:NSUTF8StringEncoding error:nil];[NSString stringWithContentsOfFile:CONFIG_FILE encoding:NSUTF8StringEncoding error:nil];
NSArray *lines = [contents componentsSeparatedByString:@"\n"];
for (NSString *s in lines) {
NSArray *line = [s componentsSeparatedByString:@"="];
if ([line count] == 2) {
NSString *key = [[line objectAtIndex:0] stringByReplacingOccurrencesOfString:@" " withString:@""];
if ([key isEqualToString:@"api_key"]) {
NSString *value = [[line objectAtIndex:1] stringByReplacingOccurrencesOfString:@" " withString:@""];
return value;
}
}
}
return NULL;
}
// Write api key to config file
- (void)saveApiKey:(NSString *)api_key {
NSString *contents = [NSString stringWithContentsOfFile:CONFIG_FILE encoding:NSUTF8StringEncoding error:nil];[NSString stringWithContentsOfFile:CONFIG_FILE encoding:NSUTF8StringEncoding error:nil];
NSArray *lines = [contents componentsSeparatedByString:@"\n"];
NSMutableArray *new_contents = [NSMutableArray array];
BOOL found = false;
for (NSString *s in lines) {
NSArray *line = [[s stringByReplacingOccurrencesOfString:@" = " withString:@"="] componentsSeparatedByString:@"="];
if ([line count] == 2) {
NSString *key = [line objectAtIndex:0];
if ([key isEqualToString:@"api_key"]) {
found = true;
line = @[@"api_key", api_key];
}
}
[new_contents addObject:[line componentsJoinedByString:@" = "]];
}
if ([new_contents count] == 0 || !found) {
[new_contents removeAllObjects];
[new_contents addObject:@"[settings]"];
[new_contents addObject:[NSString stringWithFormat:@"api_key = %@", api_key]];
}
NSError *error = nil;
NSString *to_write = [new_contents componentsJoinedByString:@"\n"];
[to_write writeToFile:CONFIG_FILE atomically:YES encoding:NSASCIIStringEncoding error:&error];
if (error) {
NSLog(@"Fail: %@", [error localizedDescription]);
}
}
// Prompt for api key
- (void)promptForApiKey {
NSString *api_key = [self getApiKey];
NSAlert *alert = [NSAlert alertWithMessageText:@"Enter your api key from wakatime.com" defaultButton:nil alternateButton:nil otherButton:nil informativeTextWithFormat:@""];
NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
if (api_key != NULL) {
[input setStringValue:api_key];
}
[alert setAccessoryView:input];
[alert runModal];
api_key = [input stringValue];
[self saveApiKey:api_key];
}
- (void)dealloc {
[super dealloc];
}
+ (void)setFileForWindow:(NSString *)filePath forWindow:(int)windowNumber {
if (filePath != nil) {
[_windows setObject:filePath forKey:[NSNumber numberWithInt:windowNumber]];
}
}
+ (NSString*)getFileForWindow:(int)windowNumber {
return _windows[[NSNumber numberWithInt:windowNumber]];
}
+ (int)totalWindows {
return [_windows count];
}
+ (void)handleEditorAction:(NSString*)currentFile isWrite:(bool)isWrite {
CFAbsoluteTime currentTime = CFAbsoluteTimeGetCurrent();
if (![_lastFile isEqualToString:currentFile] || _lastTime + FREQUENCY < currentTime) {
_lastFile = currentFile;
_lastTime = currentTime;
if (![_lastFile isEqualToString:@""])
[self sendHeartbeat:isWrite];
}
}
+ (void)sendHeartbeat:(bool)isWrite {
//NSLog(@"%@", _lastFile);
NSString *cli = [NSHomeDirectory() stringByAppendingPathComponent:WAKATIME_CLI];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: cli];
NSMutableArray *arguments = [NSMutableArray array];
//[arguments addObject:@"--verbose"];
[arguments addObject:@"--entity"];
[arguments addObject:_lastFile];
[arguments addObject:@"--project"];
[arguments addObject:@"TextMate"];
[arguments addObject:@"--plugin"];
[arguments addObject:[NSString stringWithFormat:@"textmate-wakatime/%@", VERSION]];
if (isWrite)
[arguments addObject:@"--write"];
[task setArguments: arguments];
NSLog(@"%@ %@", cli, arguments);
[task launch];
}
@end