14
14
static NSString *const qBaseUrlValue = @" http://localhost:26498" ;
15
15
static NSString *const qDefaultTemplates = @" default-templates" ;
16
16
17
+ static NSString *const qWasUpdated = @" was-updated" ;
18
+
17
19
@interface NOAppDelegate ()
18
20
19
21
@end
@@ -37,11 +39,12 @@ @implementation NOAppDelegate{
37
39
38
40
- (void )applicationDidFinishLaunching : (NSNotification *)aNotification {
39
41
templatesPath = [NSString stringWithFormat: @" %@ /%@ " , NSHomeDirectory (), @" /Library/Application Support/nonky/public/templates/" ];
42
+ userDefaults = [NSUserDefaults standardUserDefaults ];
43
+ [self setDefaultsIfNecessary ];
40
44
[self createAppDirectory ];
41
45
[self startServer ];
42
46
windows = [[NSMutableDictionary alloc ] initWithCapacity: 10 ];
43
- userDefaults = [NSUserDefaults standardUserDefaults ];
44
- [self setDefaultsIfNecessary ];
47
+
45
48
defaultTemplates = [[userDefaults objectForKey: qDefaultTemplates] mutableCopy ];
46
49
statusItem = [self addStatusItemToMenu: statusMenu];
47
50
preferences = [[NOPreferencesController alloc ]initWithWindowNibName:@" Preferences" ];
@@ -53,6 +56,10 @@ -(void)setDefaultsIfNecessary{
53
56
if ([userDefaults objectForKey: qDefaultTemplates] == nil ) {
54
57
NSDictionary *DefaultTemplates = [[NSDictionary alloc ] initWithObjectsAndKeys: @" YES" ,@" jquery" , nil ];
55
58
[userDefaults setObject: DefaultTemplates forKey: qDefaultTemplates];
59
+
60
+ }
61
+ if ([userDefaults objectForKey: qWasUpdated] == nil ) {
62
+ [userDefaults setBool: NO forKey: qWasUpdated];
56
63
}
57
64
}
58
65
- (NSStatusItem *)addStatusItemToMenu : (NSMenu *)menu
@@ -85,10 +92,36 @@ -(void)createAppDirectory{
85
92
NSLog (@" Error: Create folder failed %@ " , userpath);
86
93
userpath = [userpath stringByAppendingPathComponent: executableName]; // The file will go in this directory
87
94
NSString *savePath = [userpath stringByAppendingPathComponent: @" public" ];
95
+ NSString *srcPath = [thisBundle pathForResource: @" public" ofType: nil ];
88
96
89
97
if ([fileManager fileExistsAtPath: userpath] == NO ){
90
98
[fileManager createDirectoryAtPath: userpath withIntermediateDirectories: YES attributes: nil error: nil ];
91
- [fileManager copyItemAtPath: [thisBundle pathForResource: @" public" ofType: nil ] toPath: savePath error: NULL ];
99
+ [fileManager copyItemAtPath: srcPath toPath: savePath error: NULL ];
100
+ }else {
101
+ if (![userDefaults boolForKey: qWasUpdated]){
102
+ NSDirectoryEnumerator *de = [fileManager enumeratorAtPath: [thisBundle pathForResource: @" public" ofType: nil ]];
103
+ NSString *subPath;
104
+ while ((subPath = [de nextObject ])) {
105
+
106
+ NSLog (@" subPath: %@ " , subPath);
107
+ NSString *srcFullPath = [srcPath stringByAppendingPathComponent: subPath];
108
+ NSString *potentialDstPath = [savePath stringByAppendingPathComponent: subPath];
109
+
110
+ // Need to also check if file exists because if it doesn't, value of `isDirectory` is undefined.
111
+ BOOL isDirectory = ([[NSFileManager defaultManager ] fileExistsAtPath: srcFullPath isDirectory: &isDirectory] && isDirectory);
112
+
113
+ // Create directory, if doesn't exist
114
+ if (isDirectory && [fileManager fileExistsAtPath: potentialDstPath] == NO ) {
115
+ NSLog (@" create directory" );
116
+ [fileManager createDirectoryAtPath: potentialDstPath withIntermediateDirectories: YES attributes: nil error: NULL ];
117
+ }
118
+ if (!isDirectory && ![fileManager fileExistsAtPath: potentialDstPath]) {
119
+ [fileManager copyItemAtPath: srcFullPath toPath: potentialDstPath error: NULL ];
120
+
121
+ }
122
+ }
123
+ [userDefaults setBool: YES forKey: qWasUpdated];
124
+ }
92
125
}
93
126
}
94
127
-(void )startServer {
0 commit comments