forked from dempseyatgithub/BuildSettingExtractor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppDelegate.m
More file actions
190 lines (148 loc) · 7.85 KB
/
AppDelegate.m
File metadata and controls
190 lines (148 loc) · 7.85 KB
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
//
// AppDelegate.m
// BuildSettingExtractor
//
// Created by James Dempsey on 9/9/14.
// Copyright (c) 2014 Tapas Software. All rights reserved.
//
#import "AppDelegate.h"
#import "DragFileView.h"
#import "BuildSettingExtractor.h"
#import "Constants+Categories.h"
// During development it is useful to turn off the overwrite checking
#define OVERWRITE_CHECKING_DISABLED 0
@interface AppDelegate () <NSOpenSavePanelDelegate>
@property (weak) IBOutlet NSWindow *window;
@property (weak) IBOutlet DragFileView *dragFileView;
@property (weak) IBOutlet NSWindow *preferencesWindow;
@property BOOL shouldOverwriteFiles;
@end
@implementation AppDelegate
- (void)awakeFromNib {
self.dragFileView.target = self;
self.dragFileView.action = @selector(handleDroppedFile:);
}
- (IBAction)chooseXcodeProject:(id)sender {
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
openPanel.canCreateDirectories = NO;
openPanel.allowsMultipleSelection = NO;
openPanel.canChooseDirectories = NO;
openPanel.canChooseFiles = YES;
openPanel.allowedFileTypes = @[[NSString tps_projectBundleTypeIdentifier]];
openPanel.message = @"Choose an Xcode project to extract its build settings.";
openPanel.prompt = @"Choose";
[openPanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
if (result == NSModalResponseOK) {
NSURL *projectURL = openPanel.URL;
dispatch_async(dispatch_get_main_queue(), ^{
[self processXcodeProjectAtURL:projectURL];
});
}
}];
}
- (IBAction)handleDroppedFile:(DragFileView *)sender {
NSURL *fileURL = sender.fileURL;
[self processXcodeProjectAtURL:fileURL];
}
- (void)processXcodeProjectAtURL:(NSURL *)fileURL {
NSString *typeIdentifier = nil;
NSString *fileName = nil;
NSError *error = nil;
[fileURL getResourceValue:&typeIdentifier forKey:NSURLTypeIdentifierKey error:&error];
[fileURL getResourceValue:&fileName forKey:NSURLLocalizedNameKey error:&error];
if (fileURL && [typeIdentifier isEqualToString:[NSString tps_projectBundleTypeIdentifier]]) {
self.shouldOverwriteFiles = OVERWRITE_CHECKING_DISABLED;
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
openPanel.delegate = self;
openPanel.canCreateDirectories = YES;
openPanel.allowsMultipleSelection = YES;
openPanel.canChooseDirectories = YES;
openPanel.canChooseFiles = NO;
openPanel.allowedFileTypes = @[(NSString *)kUTTypeFolder];
openPanel.message = [NSString stringWithFormat:@"Choose location to save configuration files for project ‘%@’.", fileName];
openPanel.prompt = @"Choose";
[openPanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
if (result == NSModalResponseOK) {
NSURL *destinationURL = openPanel.URL;
// Perform the extraction in the background.
// Using DISPATCH_QUEUE_PRIORITY_HIGH which is available on 10.9
// Move to QOS_CLASS_USER_INITIATED when 10.10 is the deployment target
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
BuildSettingExtractor *buildSettingExtractor = [[BuildSettingExtractor alloc] init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
buildSettingExtractor.sharedConfigName = [defaults stringForKey:TPSOutputFileNameShared];
buildSettingExtractor.projectConfigName = [defaults stringForKey:TPSOutputFileNameProject];
buildSettingExtractor.nameSeparator = [defaults stringForKey:TPSOutputFileNameSeparator];
buildSettingExtractor.includeBuildSettingInfoComments = [[NSUserDefaults standardUserDefaults] boolForKey:TPSIncludeBuildSettingInfoComments];
BOOL success = [buildSettingExtractor extractBuildSettingsFromProject:fileURL toDestinationFolder:destinationURL];
BOOL openInFinder = [[NSUserDefaults standardUserDefaults] boolForKey:TPSOpenDirectoryInFinder];
if (success && openInFinder) {
[[NSWorkspace sharedWorkspace] openURL:destinationURL];
}
});
}
}];
}
}
- (IBAction)presentPreferencesWindow:(id)sender {
[self.window beginSheet:self.preferencesWindow completionHandler:nil];
}
- (IBAction)dismissPreferencesWindow:(id)sender {
// make sure current edit field gets bound
[self.preferencesWindow makeFirstResponder:nil];
[self.window endSheet:self.preferencesWindow];
}
#pragma mark - NSOpenSavePanelDelegate
/* We want to protect against overwriting the contents of a folder that already has xcconfig files in it. So validate the contents of the selected folder.
*/
- (BOOL)panel:(id)panel validateURL:(NSURL *)url error:(NSError *__autoreleasing *)outError {
NSError *error = nil;
NSArray *filesInDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:url includingPropertiesForKeys:@[NSURLTypeIdentifierKey] options:NSDirectoryEnumerationSkipsSubdirectoryDescendants | NSDirectoryEnumerationSkipsHiddenFiles error:&error];
__block BOOL foundBuildConfigFile = NO;
[filesInDirectory enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *typeIdentifier = nil;
NSError *resourceError = nil;
[obj getResourceValue:&typeIdentifier forKey:NSURLTypeIdentifierKey error:&resourceError];
if ([typeIdentifier isEqualToString:[NSString tps_buildConfigurationFileTypeIdentifier]]) {
foundBuildConfigFile = YES;
*stop = YES;
}
}];
BOOL valid = (!foundBuildConfigFile || self.shouldOverwriteFiles);
if (!valid) {
NSDictionary *errorUserInfo = @{NSLocalizedDescriptionKey:@"Build config files already exist in this folder. Do you want to replace them?", NSLocalizedRecoveryOptionsErrorKey:@[@"Cancel", @"Replace"], NSLocalizedRecoverySuggestionErrorKey:@"Build configuration files already exist in this folder. Replacing will overwrite any files with the same file names.", NSRecoveryAttempterErrorKey: self};
NSError *error = [NSError errorWithDomain:[[NSBundle mainBundle] bundleIdentifier] code:DirectoryContainsBuildConfigFiles userInfo:errorUserInfo];
*outError = error;
}
return valid;
}
#pragma mark - NSError Recovery
/* The user can choose to replace / overwrite the contents of the folder as a recovery option. If so, the open panel goes through validation again.
*/
- (void)attemptRecoveryFromError:(NSError *)error optionIndex:(NSUInteger)recoveryOptionIndex delegate:(id)delegate didRecoverSelector:(SEL)didRecoverSelector contextInfo:(void *)contextInfo {
BOOL success = NO;
NSInvocation *invoke = [NSInvocation invocationWithMethodSignature:[delegate methodSignatureForSelector:didRecoverSelector]];
[invoke setSelector:didRecoverSelector];
if (recoveryOptionIndex == 1) { // Recovery requested.
self.shouldOverwriteFiles = YES;
success = YES;
}
[invoke setArgument:(void *)&success atIndex:2];
[invoke setArgument:(void *)&contextInfo atIndex:3];
[invoke invokeWithTarget:delegate];
}
#pragma mark - NSApplicationDelegate
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
return YES;
}
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
NSDictionary *defaults = @{
TPSOpenDirectoryInFinder:@(YES),
TPSIncludeBuildSettingInfoComments:@(YES),
TPSOutputFileNameShared:BuildSettingExtractor.defaultSharedConfigName,
TPSOutputFileNameProject:BuildSettingExtractor.defaultProjectConfigName,
TPSOutputFileNameSeparator:BuildSettingExtractor.defaultNameSeparator,
};
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
}
@end