一个用于操作Xcode项目文件的API。
XCProject* project = [[XCProject alloc] initWithFilePath:@"MyProject.xcodeproj"];
XCGroup* group = [project groupWithPathFromRoot:@"Main"];
XCClassDefinition* classDefinition = [[XCClassDefinition alloc] initWithName:@"MyNewClass"];
[classDefinition setHeader:@"<some-header-text>"];
[classDefinition setSource:@"<some-impl-text>"];
[group addClass:classDefinition];
[project save];
它将作为项目的一部分被添加。
XCTarget* target = [project targetWithName:@"SomeTarget"];
XCTarget* duplicated = [target duplicateWithTargetName:@"DuplicatedTarget" productName:@"NewProduct"];
XCSourceFile* sourceFile = [project fileWithName:@"MyNewClass.m"];
XCTarget* examples = [project targetWithName:@"Examples"];
[examples addMember:sourceFile];
[project save];
这次,我们将使用XCGroup上的便利方法同时指定目标
XCXibDefinition* xibDefinition = [[XCXibDefinition alloc] initWithName:@"MyXibFile" content:@"<xibXml>"];
[group addXib:xibDefinition toTargets:[project targets]];
[project save];
XCFrameworkDefinition* frameworkDefinition =
[[XCFrameworkDefinition alloc] initWithFilePath:@"<framework path>" copyToDestination:NO];
[group addFramework:frameworkDefinition toTargets:[project targets]];
[project save];
将copyToDestination设置为YES,将导致框架首先被复制到项目中的组的目录内,然后从该处链接。
XCSourceFileDefinition* sourceFileDefinition = [[XCSourceFileDefinition alloc]
initWithName:@"MyImageFile.png" data:[NSData dataWithContentsOfFile:<your image file name>]
type:ImageResourcePNG];
[group addSourceFile:sourceFileDefinition];
[project save];
XCSourceFileDefinition* sourceFileDefinition = [XCSourceFileDefinition sourceDefinitionWithAssetCatalogName:<path to asset catalog>];
[group addSourceFile:sourceFileDefinition];
[project save];
XCSourceFileDefinition* header = [[XCSourceFileDefinition alloc]
initWithName:@"SomeHeader.h" text:<your header text> type:SourceCodeHeader];
[group addSourceFile:header];
[project save];
subProjectDefinition = [XCSubProjectDefinition withName:@"mySubproject" projPath=@"/Path/To/Subproject" type:XcodeProject];
[group addSubProject:subProjectDefinition toTargets:[project targets]];
[group removeSubProject:subProjectDefinition]; //TODO: project should be able to remove itself from parent.
我们可以向目标添加/更新链接器标志、头文件搜索路径、C-标志等。这里我们将添加头文件搜索路径
XCTarget* target = [_project targetWithName:_projectName];
for (NSString* configName in [target configurations])
{
XCBuildConfiguration* configuration = [target configurationWithName:configName];
NSMutableArray* headerPaths = [[NSMutableArray alloc] init];
[headerPaths addObject:@"$(inherited)"];
[headerPaths addObject:@"$(SRCROOT)/include"];
[configuration addOrReplaceSetting:headerPaths forKey:@"HEADER_SEARCH_PATHS"];
}
...这些设置通过键添加,正如它们在make文件中出现的样子。(Xcode提供了更人性化的描述)。要为给定的构建设置找到键,请查阅编译器文档。常见设置有:
XCSourceFile * libSourceFile = [project fileWithName:@"libAmazing.a"];
XCTarget* target = [project targetWithName:self.mProject.projectName];
[target addMember:libSourceFile];
for (NSString* configName in [target configurations]) {
XCProjectBuildConfig* configuration = [target configurationWithName:configName];
NSMutableArray* headerPaths = [[NSMutableArray alloc] init];
[headerPaths addObject:@"$(inherited)"];
[headerPaths addObject:@"$(PROJECT_DIR)/Amazing"];
[configuration addOrReplaceSetting:headerPaths forKey:@"LIBRARY_SEARCH_PATHS"];
}
//Creates the reference in the project and writes the contents to disk. If a file already exists at the
//specified location, its contents will be updated.
[definition setFileOperationStyle:FileOperationStyleOverwrite];
//Creates the reference in the project. If a file already exists at the specified location, the contents will
//not be updated.
[definition setFileOperationStyle:FileOperationStyleAcceptExisting];
//Creates the reference in the project, but does not write to disk. The filesystem is expected to be updated
//through some other means.
[definition setFileOperationStyle:FileOperationStyleReferenceOnly];
在XCode中打开项目并选择Product/Build。或者使用CocoaPods进行安装。
非常欢迎。
如果您正在使用API,请给我发一封电子邮件并告诉我您用它做什么。
谢谢!
Apache License,版本2.0,2004年1月,https://apache.ac.cn/licenses/