-
Notifications
You must be signed in to change notification settings - Fork 106
/
BetaBuilderAppDelegate.m
213 lines (160 loc) · 8.32 KB
/
BetaBuilderAppDelegate.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// BetaBuilderAppDelegate.m
// BetaBuilder
//
// Created by Hunter Hillegas on 8/7/10.
// Copyright 2010 Hunter Hillegas. All rights reserved.
//
/*
iOS BetaBuilder - a tool for simpler iOS betas
Version 1.6
Condition of use and distribution:
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#import "BetaBuilderAppDelegate.h"
#import "BuilderController.h"
#import "NSFileManager+DirectoryLocations.h"
@interface BetaBuilderAppDelegate ()
@property (nonatomic) BOOL runningInCommandLineSession;
@property (nonatomic, strong) NSDictionary *indexTemplateAlertPaths;
@end
@implementation BetaBuilderAppDelegate
@synthesize window = _window;
@synthesize deploymentHelpPanel = _deploymentHelpPanel;
@synthesize archiveIPAHelpPanel = _archiveIPAHelpPanel;
@synthesize builderController = _builderController;
@synthesize preferencesPanel = _preferencesPanel;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
//Setup Drag Target for IPA Files
[self.window registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]];
//Process Command Line Arguments, If Any
self.runningInCommandLineSession = NO;
NSArray *commandLineArgs = [[NSProcessInfo processInfo] arguments];
if (commandLineArgs && [commandLineArgs count] > 0) {
self.runningInCommandLineSession = [self processCommandLineArguments:commandLineArgs];
}
if (![[NSUserDefaults standardUserDefaults] valueForKey:kSupressTemplateWarning]) {
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:kSupressTemplateWarning];
}
//Copy HTML Template to App Support
[self copyTemplatesIfNeededCommandLineArgs:commandLineArgs];
}
#pragma mark - Setup Templates
- (void)copyTemplatesIfNeededCommandLineArgs:(NSArray *)commandLineArgs {
NSFileManager *fileManager = [NSFileManager defaultManager];
for (NSString *templatePath in [self htmlTemplatePaths]) {
NSString *filename = [templatePath lastPathComponent];
NSString *fileType = [filename pathExtension];
NSString *templatePathInBundle = [[NSBundle mainBundle] pathForResource:[filename stringByDeletingPathExtension] ofType:fileType];
if (![fileManager fileExistsAtPath:templatePath]) {
NSLog(@"Copying Template: %@", templatePath);
if (templatePathInBundle)
[fileManager copyItemAtPath:templatePathInBundle toPath:templatePath error:nil];
} else {
if ([fileManager contentsEqualAtPath:templatePathInBundle andPath:templatePath]) {
NSLog(@"Index Template Already Exists And They Are the Same - Not Copying From Bundle");
} else {
NSLog(@"Index Template Exists But Has Been Modified");
if (!self.runningInCommandLineSession) { //only present this if we have no command line args
BOOL shouldSuppressAlert = [[NSUserDefaults standardUserDefaults] boolForKey:kSupressTemplateWarning];
if (!shouldSuppressAlert) {
self.indexTemplateAlertPaths = @{@"fromPath" : templatePathInBundle, @"toPath" : templatePath};
NSAlert *indexTemplateAlert = [NSAlert alertWithMessageText:@"A Newer Index Template File Exists" defaultButton:@"Do Nothing" alternateButton:@"Replace File" otherButton:nil informativeTextWithFormat:@"The template index file used to create the HTML output has been updated to include new functionality. It appears you alread have a version of this file in place (%@). Would you like to replace this file? Any customizations will be lost - you may want to backup the file first.", templatePath];
[indexTemplateAlert beginSheetModalForWindow:self.window modalDelegate:self didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:) contextInfo:&_indexTemplateAlertPaths];
}
}
}
}
}
}
- (NSArray *)htmlTemplatePaths {
NSMutableArray *templatePaths = [NSMutableArray array];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *applicationSupportPath = [fileManager applicationSupportDirectory];
NSArray *templateNames = @[@"index_template.html", @"index_template_no_tether.html"];
for (NSString *templateName in templateNames) {
NSString *templatePath = [applicationSupportPath stringByAppendingPathComponent:templateName];
[templatePaths addObject:templatePath];
}
return templatePaths;
}
#pragma mark - Command Line
#define kArgumentSeperator @"="
#define kIPAPathArgument @"-ipaPath"
#define kWebserverArgument @"-webserver"
#define kOutputDirectoryArgument @"-outputDirectory"
#define kTemplateArgument @"-template"
- (BOOL)processCommandLineArguments:(NSArray *)arguments {
NSLog(@"Processing Command Line Arguments");
BOOL processedArgs = NO;
NSString *ipaPath = nil;
NSString *webserverAddress = nil;
NSString *outputPath = nil;
NSString *templateFile = nil;
for (NSString *argument in arguments) {
NSArray *splitArgument = [argument componentsSeparatedByString:kArgumentSeperator];
if ([splitArgument count] == 2) {
if ([[splitArgument objectAtIndex:0] isEqualToString:kIPAPathArgument]) {
ipaPath = [splitArgument objectAtIndex:1];
} else if ([[splitArgument objectAtIndex:0] isEqualToString:kWebserverArgument]) {
webserverAddress = [splitArgument objectAtIndex:1];
} else if ([[splitArgument objectAtIndex:0] isEqualToString:kOutputDirectoryArgument]) {
outputPath = [splitArgument objectAtIndex:1];
} else if ([[splitArgument objectAtIndex:0] isEqualToString:kTemplateArgument]) {
templateFile = [splitArgument objectAtIndex:1];
}
}
}
if (ipaPath && webserverAddress && outputPath) {
if (templateFile) {
self.builderController.templateFile = templateFile;
}
[self.builderController setupFromIPAFile:ipaPath];
[self.builderController generateFilesWithWebserverAddress:webserverAddress andOutputDirectory:outputPath];
[NSApp performSelector:@selector(terminate:) withObject:nil afterDelay:0.0];
processedArgs = YES;
}
return processedArgs;
}
#pragma mark - Alert
- (void)alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
if (returnCode == NSAlertAlternateReturn) {
if (contextInfo) {
NSLog(@"Remove Existing Index File %@", self.indexTemplateAlertPaths[@"toPath"]);
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:self.indexTemplateAlertPaths[@"toPath"] error:nil];
[fileManager copyItemAtPath:self.indexTemplateAlertPaths[@"fromPath"] toPath:self.indexTemplateAlertPaths[@"toPath"] error:nil];
}
} else if (returnCode == NSAlertDefaultReturn) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kSupressTemplateWarning];
}
}
- (IBAction)showDeploymentHelpPanel:(id)sender {
[self.deploymentHelpPanel setIsVisible:YES];
}
- (IBAction)showArchiveHelpPanel:(id)sender {
[self.archiveIPAHelpPanel setIsVisible:YES];
}
- (IBAction)showPreferencesPanel:(id)sender {
[self.preferencesPanel setIsVisible:YES];
}
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename {
[self.builderController setupFromIPAFile:filename];
return YES;
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
return YES;
}
@end