1414static NSString *const qBaseUrlValue = @" http://localhost:26498" ;
1515static NSString *const qDefaultTemplates = @" default-templates" ;
1616
17+ static NSString *const qWasUpdated = @" was-updated" ;
18+
1719@interface NOAppDelegate ()
1820
1921@end
@@ -37,11 +39,12 @@ @implementation NOAppDelegate{
3739
3840- (void )applicationDidFinishLaunching : (NSNotification *)aNotification {
3941 templatesPath = [NSString stringWithFormat: @" %@ /%@ " , NSHomeDirectory (), @" /Library/Application Support/nonky/public/templates/" ];
42+ userDefaults = [NSUserDefaults standardUserDefaults ];
43+ [self setDefaultsIfNecessary ];
4044 [self createAppDirectory ];
4145 [self startServer ];
4246 windows = [[NSMutableDictionary alloc ] initWithCapacity: 10 ];
43- userDefaults = [NSUserDefaults standardUserDefaults ];
44- [self setDefaultsIfNecessary ];
47+
4548 defaultTemplates = [[userDefaults objectForKey: qDefaultTemplates] mutableCopy ];
4649 statusItem = [self addStatusItemToMenu: statusMenu];
4750 preferences = [[NOPreferencesController alloc ]initWithWindowNibName:@" Preferences" ];
@@ -53,6 +56,10 @@ -(void)setDefaultsIfNecessary{
5356 if ([userDefaults objectForKey: qDefaultTemplates] == nil ) {
5457 NSDictionary *DefaultTemplates = [[NSDictionary alloc ] initWithObjectsAndKeys: @" YES" ,@" jquery" , nil ];
5558 [userDefaults setObject: DefaultTemplates forKey: qDefaultTemplates];
59+
60+ }
61+ if ([userDefaults objectForKey: qWasUpdated] == nil ) {
62+ [userDefaults setBool: NO forKey: qWasUpdated];
5663 }
5764}
5865- (NSStatusItem *)addStatusItemToMenu : (NSMenu *)menu
@@ -85,10 +92,36 @@ -(void)createAppDirectory{
8592 NSLog (@" Error: Create folder failed %@ " , userpath);
8693 userpath = [userpath stringByAppendingPathComponent: executableName]; // The file will go in this directory
8794 NSString *savePath = [userpath stringByAppendingPathComponent: @" public" ];
95+ NSString *srcPath = [thisBundle pathForResource: @" public" ofType: nil ];
8896
8997 if ([fileManager fileExistsAtPath: userpath] == NO ){
9098 [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+ }
92125 }
93126}
94127-(void )startServer {
0 commit comments