|
41 | 41 |
|
42 | 42 | @implementation YCodeProject |
43 | 43 |
|
| 44 | +#pragma mark - Class Methods |
| 45 | + |
| 46 | ++ (YCodeProject *)createNewProjectAtPath:(NSString *)path name:(NSString *)name type:(NSString *)type |
| 47 | +{ |
| 48 | + NSFileManager *fileManager = [NSFileManager defaultManager]; |
| 49 | + |
| 50 | + // Create project directory if it doesn't exist |
| 51 | + if (![fileManager fileExistsAtPath:path]) { |
| 52 | + NSError *error; |
| 53 | + if (![fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error]) { |
| 54 | + NSLog(@"Failed to create project directory: %@", [error localizedDescription]); |
| 55 | + return nil; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // Create project instance |
| 60 | + YCodeProject *project = [[YCodeProject alloc] init]; |
| 61 | + [project setProjectPath:path]; |
| 62 | + |
| 63 | + if ([type isEqualToString:@"Xcode"]) { |
| 64 | + [project createXcodeProjectWithName:name atPath:path]; |
| 65 | + } else { |
| 66 | + [project createProjectCenterProjectWithName:name atPath:path]; |
| 67 | + } |
| 68 | + |
| 69 | + return AUTORELEASE(project); |
| 70 | +} |
| 71 | + |
| 72 | +- (BOOL)createXcodeProjectWithName:(NSString *)name atPath:(NSString *)path |
| 73 | +{ |
| 74 | + // Create basic Xcode project structure |
| 75 | + NSString *projectFile = [NSString stringWithFormat:@"%@.xcodeproj", name]; |
| 76 | + NSString *projectPath = [path stringByAppendingPathComponent:projectFile]; |
| 77 | + |
| 78 | + NSFileManager *fileManager = [NSFileManager defaultManager]; |
| 79 | + if (![fileManager createDirectoryAtPath:projectPath withIntermediateDirectories:YES attributes:nil error:nil]) { |
| 80 | + return NO; |
| 81 | + } |
| 82 | + |
| 83 | + // Create project structure |
| 84 | + PBXProject *pbxProject = [[PBXProject alloc] init]; |
| 85 | + [pbxProject setProjectDirPath:@""]; |
| 86 | + [pbxProject setProjectRoot:@""]; |
| 87 | + [pbxProject setCompatibilityVersion:@"Xcode 15.0"]; |
| 88 | + [pbxProject setDevelopmentRegion:@"en"]; |
| 89 | + |
| 90 | + // Create main group |
| 91 | + PBXGroup *mainGroup = [[PBXGroup alloc] init]; |
| 92 | + [mainGroup setName:name]; |
| 93 | + [pbxProject setMainGroup:mainGroup]; |
| 94 | + |
| 95 | + // Create a simple target |
| 96 | + PBXNativeTarget *target = [[PBXNativeTarget alloc] init]; |
| 97 | + [target setName:name]; |
| 98 | + [target setProductName:name]; |
| 99 | + [pbxProject addTarget:target]; |
| 100 | + |
| 101 | + // Create container |
| 102 | + PBXContainer *container = [[PBXContainer alloc] init]; |
| 103 | + [container setProject:pbxProject]; |
| 104 | + |
| 105 | + [self setProject:pbxProject]; |
| 106 | + [self setContainer:container]; |
| 107 | + |
| 108 | + // Create basic main.m file |
| 109 | + [self createMainFileAtPath:path]; |
| 110 | + |
| 111 | + RELEASE(pbxProject); |
| 112 | + RELEASE(mainGroup); |
| 113 | + RELEASE(target); |
| 114 | + RELEASE(container); |
| 115 | + |
| 116 | + return YES; |
| 117 | +} |
| 118 | + |
| 119 | +- (BOOL)createProjectCenterProjectWithName:(NSString *)name atPath:(NSString *)path |
| 120 | +{ |
| 121 | + // Create ProjectCenter project file |
| 122 | + NSString *projectFile = [NSString stringWithFormat:@"%@.pcproj", name]; |
| 123 | + NSString *projectPath = [path stringByAppendingPathComponent:projectFile]; |
| 124 | + |
| 125 | + NSMutableDictionary *projectDict = [NSMutableDictionary dictionary]; |
| 126 | + [projectDict setObject:name forKey:@"PROJECT_NAME"]; |
| 127 | + [projectDict setObject:@"Application" forKey:@"PROJECT_TYPE"]; |
| 128 | + [projectDict setObject:name forKey:@"PRINCIPAL_CLASS"]; |
| 129 | + [projectDict setObject:[NSArray array] forKey:@"CLASS_FILES"]; |
| 130 | + [projectDict setObject:[NSArray array] forKey:@"HEADER_FILES"]; |
| 131 | + [projectDict setObject:[NSArray array] forKey:@"LOCALIZED_RESOURCES"]; |
| 132 | + [projectDict setObject:[NSArray array] forKey:@"LIBRARIES"]; |
| 133 | + |
| 134 | + BOOL success = [projectDict writeToFile:projectPath atomically:YES]; |
| 135 | + |
| 136 | + if (success) { |
| 137 | + // Load the project we just created |
| 138 | + [self loadProjectCenterProject:projectDict fromPath:path]; |
| 139 | + |
| 140 | + // Create basic main.m file |
| 141 | + [self createMainFileAtPath:path]; |
| 142 | + |
| 143 | + // Create basic GNUmakefile |
| 144 | + [self createGNUMakefileAtPath:path withName:name]; |
| 145 | + } |
| 146 | + |
| 147 | + return success; |
| 148 | +} |
| 149 | + |
| 150 | +- (void)createMainFileAtPath:(NSString *)path |
| 151 | +{ |
| 152 | + NSString *mainContent = @"#import <Foundation/Foundation.h>\n" |
| 153 | + @"#import <AppKit/AppKit.h>\n\n" |
| 154 | + @"int main(int argc, const char *argv[])\n" |
| 155 | + @"{\n" |
| 156 | + @" return NSApplicationMain(argc, argv);\n" |
| 157 | + @"}\n"; |
| 158 | + |
| 159 | + NSString *mainPath = [path stringByAppendingPathComponent:@"main.m"]; |
| 160 | + [mainContent writeToFile:mainPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; |
| 161 | +} |
| 162 | + |
| 163 | +- (void)createGNUMakefileAtPath:(NSString *)path withName:(NSString *)name |
| 164 | +{ |
| 165 | + NSString *makefileContent = [NSString stringWithFormat: |
| 166 | + @"include $(GNUSTEP_MAKEFILES)/common.make\n\n" |
| 167 | + @"APP_NAME = %@\n" |
| 168 | + @"%@_OBJC_FILES = main.m\n" |
| 169 | + @"%@_RESOURCE_FILES = \n\n" |
| 170 | + @"include $(GNUSTEP_MAKEFILES)/application.make\n", |
| 171 | + name, name, name]; |
| 172 | + |
| 173 | + NSString *makefilePath = [path stringByAppendingPathComponent:@"GNUmakefile"]; |
| 174 | + [makefileContent writeToFile:makefilePath atomically:YES encoding:NSUTF8StringEncoding error:nil]; |
| 175 | +} |
| 176 | + |
44 | 177 | - (instancetype)init |
45 | 178 | { |
46 | 179 | self = [super init]; |
|
0 commit comments