Skip to content

Commit

Permalink
Add failing test for pruning empty groups.
Browse files Browse the repository at this point in the history
  • Loading branch information
jblues committed Nov 22, 2015
1 parent 3c34b1c commit 40d35e4
Show file tree
Hide file tree
Showing 33 changed files with 100 additions and 1,682 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ An API for manipulating Xcode project files.

```objective-c
XCProject* project = [[XCProject alloc] initWithFilePath:@"MyProject.xcodeproj"];
XCGroup* _group = [project groupWithPathFromRoot:@"Main"];
XCGroup* group = [project groupWithPathFromRoot:@"Main"];
XCClassDefinition* classDefinition = [[XCClassDefinition alloc] initWithName:@"MyNewClass"];
[classDefinition setHeader:@"<some-header-text>"];
[classDefinition setSource:@"<some-impl-text>"];

[_group addClass:classDefinition];
[group addClass:classDefinition];
[project save];
```
Expand Down Expand Up @@ -44,7 +44,7 @@ This time, we'll use a convenience method on XCGroup to specify the targets at t
```objective-c
XCXibDefinition* xibDefinition = [[XCXibDefinition alloc] initWithName:@"MyXibFile" content:@"<xibXml>"];
[_group addXib:xibDefinition toTargets:[project targets]];
[group addXib:xibDefinition toTargets:[project targets]];
[project save];
```

Expand All @@ -54,10 +54,10 @@ XCXibDefinition* xibDefinition = [[XCXibDefinition alloc] initWithName:@"MyXibFi
```objective-c
XCFrameworkDefinition* frameworkDefinition =
[[XCFrameworkDefinition alloc] initWithFilePath:@"<framework path>" copyToDestination:NO];
[_group addFramework:frameworkDefinition toTargets:[project targets]];
[group addFramework:frameworkDefinition toTargets:[project targets]];
[project save];
```
Setting copyToDestination to YES, will cause the framework to be first copied to the _group's directory within the
Setting copyToDestination to YES, will cause the framework to be first copied to the group's directory within the
project, and subsequently linked from there.
### Adding an Image Resource
Expand All @@ -68,7 +68,7 @@ XCSourceFileDefinition* sourceFileDefinition = [[XCSourceFileDefinition alloc]
initWithName:@"MyImageFile.png" data:[NSData dataWithContentsOfFile:<your image file name>]
type:ImageResourcePNG];
[_group addSourceFile:sourceFileDefinition];
[group addSourceFile:sourceFileDefinition];
[project save];
```

Expand All @@ -78,7 +78,7 @@ XCSourceFileDefinition* sourceFileDefinition = [[XCSourceFileDefinition alloc]

XCSourceFileDefinition* sourceFileDefinition = [XCSourceFileDefinition sourceDefinitionWithAssetCatalogName:<path to asset catalog>];

[_group addSourceFile:sourceFileDefinition];
[group addSourceFile:sourceFileDefinition];
[project save];
```
Expand All @@ -88,20 +88,20 @@ XCSourceFileDefinition* sourceFileDefinition = [XCSourceFileDefinition sourceDef
XCSourceFileDefinition* header = [[XCSourceFileDefinition alloc]
initWithName:@"SomeHeader.h" text:<your header text> type:SourceCodeHeader];
[_group addSourceFile:header];
[group addSourceFile:header];
[project save];
```

### Adding a sub-project

```objective-c
subProjectDefinition = [XCSubProjectDefinition withName:@"mySubproject" projPath=@"/Path/To/Subproject" type:XcodeProject];
[_group addSubProject:subProjectDefinition toTargets:[project targets]];
[group addSubProject:subProjectDefinition toTargets:[project targets]];
```
### Removing a sub-project
```objective-c
[_group removeSubProject:subProjectDefinition]; //TODO: project should be able to remove itself from parent.
[group removeSubProject:subProjectDefinition]; //TODO: project should be able to remove itself from parent.
```

### Configuring targets
Expand Down
4 changes: 1 addition & 3 deletions Source/Utils/XCKeyBuilder.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ @implementation XCKeyBuilder

+ (XCKeyBuilder*)forItemNamed:(NSString*)name
{
NSData* data = [name dataUsingEncoding:NSUTF8StringEncoding];
return [[XCKeyBuilder alloc] initHashValueMD5HashWithBytes:[data bytes] length:[data length]];

return [self createUnique];
}

+ (XCKeyBuilder*)createUnique
Expand Down
18 changes: 10 additions & 8 deletions Source/XCGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


/**
* Represents a _group container in an Xcode project. A _group can contain members of type `XCSourceFile` or other
* Represents a _group container in an Xcode project. A group can contain members of type `XCSourceFile` or other
* groups.
*/
@interface XCGroup : NSObject <XcodeGroupMember>
Expand All @@ -41,28 +41,28 @@
NSMutableArray* _children;
NSMutableArray* _members;

XCFileOperationQueue* _fileOperationQueue; // weak
XCFileOperationQueue* _fileOperationQueue;
XCProject* _project;

}


/**
* The alias of the _group, which can be used to give the _group a name other than the last path component.
* The alias of the group, which can be used to give the group a name other than the last path component.
*
* See: [XcodeGroupMember displayName]
*/
@property(nonatomic, strong, readonly) NSString* alias;

/**
* The path of the _group relative to the _group's parent.
* The path of the group relative to the group's parent.
*
* See: [XcodeGroupMember displayName]
*/
@property(nonatomic, strong, readonly) NSString* pathRelativeToParent;

/**
* The _group's unique key.
* The group's unique key.
*/
@property(nonatomic, strong, readonly) NSString* key;

Expand All @@ -78,7 +78,7 @@

- (id)initWithProject:(XCProject*)project key:(NSString*)key alias:(NSString*)alias path:(NSString*)path children:(NSArray<id<XcodeGroupMember>>*)children;

#pragma mark Parent _group
#pragma mark Parent group

- (void)removeFromParentGroup;

Expand All @@ -88,6 +88,8 @@

- (BOOL)isRootGroup;

- (BOOL)isEmpty;

#pragma mark Adding children
/**
* Adds a class to the _group, as specified by the ClassDefinition. If the _group already contains a class by the same
Expand Down Expand Up @@ -162,12 +164,12 @@
- (NSArray<id<XcodeGroupMember>>*)members;

/**
* Keys of members from this _group and any child groups.
* Keys of members from this group and any child groups.
*/
- (NSArray<NSString*>*)recursiveMembers;

/**
* Keys of members from this _group
* Keys of members from this group
*/
- (NSArray<NSString*>*)buildFileKeys;

Expand Down
5 changes: 5 additions & 0 deletions Source/XCGroup.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ - (BOOL)isRootGroup
return [self pathRelativeToParent] == nil && [self displayName] == nil;
}

- (BOOL)isEmpty
{
return [self.members count] == 0;
}


//-------------------------------------------------------------------------------------------
#pragma mark Adding children
Expand Down
16 changes: 12 additions & 4 deletions Source/XCProject.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@

- (NSArray<XCSourceFile*>*)imagePNGFiles;

- (NSString*)containingFolderPath;

- (NSString*)filePath;


Expand All @@ -114,10 +112,15 @@
- (NSArray<XCGroup*>*)rootGroups;

/**
* Returns the _group with the given key, or nil.
* Returns the group with the given key, or nil.
*/
- (XCGroup*)groupWithKey:(NSString*)key;

/**
* Returns the _first_ group in the project with the given name, or nil.
*/
- (XCGroup*)groupWithDisplayName:(NSString*)name;

/**
* Returns the _group with the specified display name path - the directory relative to the root _group. Eg Source/Main
*/
Expand All @@ -129,10 +132,15 @@
- (XCGroup*)groupForGroupMemberWithKey:(NSString*)key;

/**
* Returns the parent _group for the _group or file with the source file
* Returns the parent group for the group or file with the source file
*/
- (XCGroup*)groupWithSourceFile:(XCSourceFile*)sourceFile;

/**
* Removes all empty groups from the project.
*/
- (void)pruneEmptyGroups;

//-------------------------------------------------------------------------------------------
#pragma mark Targets
/**
Expand Down
28 changes: 22 additions & 6 deletions Source/XCProject.m
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,6 @@ - (NSArray*)imagePNGFiles
return [self projectFilesOfType:ImageResourcePNG];
}

- (NSString *)containingFolderPath
{
return [_filePath stringByDeletingLastPathComponent];
}


// need this value to construct relative path in XcodeprojDefinition
- (NSString*)filePath
Expand Down Expand Up @@ -235,6 +230,17 @@ - (XCGroup*)groupWithKey:(NSString*)key
return nil;
}

- (XCGroup *)groupWithDisplayName:(NSString *)name
{
for (XCGroup *group in [self groups]) {
if ([[group displayName] isEqualToString:name]) {
return group;
}
}
return nil;
}


- (XCGroup*)groupForGroupMemberWithKey:(NSString*)key
{
for (XCGroup* group in [self groups])
Expand Down Expand Up @@ -262,7 +268,17 @@ - (XCGroup*)groupWithSourceFile:(XCSourceFile*)sourceFile
return nil;
}

//TODO: This could fail if the path attribute on a given _group is more than one directory. Start with candidates and
- (void)pruneEmptyGroups
{
for (XCGroup *group in [self groups]) {
if ([group isEmpty]) {
[group removeFromParentGroup];
}
}
}


//TODO: This could fail if the path attribute on a given group is more than one directory. Start with candidates and
//TODO: search backwards.
- (XCGroup*)groupWithPathFromRoot:(NSString*)path
{
Expand Down
Binary file modified XcodeEditorTests/Resources/TestProjects/ProjectToEdit.zip
Binary file not shown.
Loading

0 comments on commit 40d35e4

Please sign in to comment.