Skip to content

Commit 40d35e4

Browse files
author
jblues
committed
Add failing test for pruning empty groups.
1 parent 3c34b1c commit 40d35e4

33 files changed

+100
-1682
lines changed

README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ An API for manipulating Xcode project files.
99

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

17-
[_group addClass:classDefinition];
17+
[group addClass:classDefinition];
1818
[project save];
1919
```
2020
@@ -44,7 +44,7 @@ This time, we'll use a convenience method on XCGroup to specify the targets at t
4444
4545
```objective-c
4646
XCXibDefinition* xibDefinition = [[XCXibDefinition alloc] initWithName:@"MyXibFile" content:@"<xibXml>"];
47-
[_group addXib:xibDefinition toTargets:[project targets]];
47+
[group addXib:xibDefinition toTargets:[project targets]];
4848
[project save];
4949
```
5050

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

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

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

81-
[_group addSourceFile:sourceFileDefinition];
81+
[group addSourceFile:sourceFileDefinition];
8282
[project save];
8383
```
8484
@@ -88,20 +88,20 @@ XCSourceFileDefinition* sourceFileDefinition = [XCSourceFileDefinition sourceDef
8888
XCSourceFileDefinition* header = [[XCSourceFileDefinition alloc]
8989
initWithName:@"SomeHeader.h" text:<your header text> type:SourceCodeHeader];
9090
91-
[_group addSourceFile:header];
91+
[group addSourceFile:header];
9292
[project save];
9393
```
9494

9595
### Adding a sub-project
9696

9797
```objective-c
9898
subProjectDefinition = [XCSubProjectDefinition withName:@"mySubproject" projPath=@"/Path/To/Subproject" type:XcodeProject];
99-
[_group addSubProject:subProjectDefinition toTargets:[project targets]];
99+
[group addSubProject:subProjectDefinition toTargets:[project targets]];
100100
```
101101
102102
### Removing a sub-project
103103
```objective-c
104-
[_group removeSubProject:subProjectDefinition]; //TODO: project should be able to remove itself from parent.
104+
[group removeSubProject:subProjectDefinition]; //TODO: project should be able to remove itself from parent.
105105
```
106106

107107
### Configuring targets

Source/Utils/XCKeyBuilder.m

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ @implementation XCKeyBuilder
2424

2525
+ (XCKeyBuilder*)forItemNamed:(NSString*)name
2626
{
27-
NSData* data = [name dataUsingEncoding:NSUTF8StringEncoding];
28-
return [[XCKeyBuilder alloc] initHashValueMD5HashWithBytes:[data bytes] length:[data length]];
29-
27+
return [self createUnique];
3028
}
3129

3230
+ (XCKeyBuilder*)createUnique

Source/XCGroup.h

+10-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626

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

44-
XCFileOperationQueue* _fileOperationQueue; // weak
44+
XCFileOperationQueue* _fileOperationQueue;
4545
XCProject* _project;
4646

4747
}
4848

4949

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

5757
/**
58-
* The path of the _group relative to the _group's parent.
58+
* The path of the group relative to the group's parent.
5959
*
6060
* See: [XcodeGroupMember displayName]
6161
*/
6262
@property(nonatomic, strong, readonly) NSString* pathRelativeToParent;
6363

6464
/**
65-
* The _group's unique key.
65+
* The group's unique key.
6666
*/
6767
@property(nonatomic, strong, readonly) NSString* key;
6868

@@ -78,7 +78,7 @@
7878

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

81-
#pragma mark Parent _group
81+
#pragma mark Parent group
8282

8383
- (void)removeFromParentGroup;
8484

@@ -88,6 +88,8 @@
8888

8989
- (BOOL)isRootGroup;
9090

91+
- (BOOL)isEmpty;
92+
9193
#pragma mark Adding children
9294
/**
9395
* Adds a class to the _group, as specified by the ClassDefinition. If the _group already contains a class by the same
@@ -162,12 +164,12 @@
162164
- (NSArray<id<XcodeGroupMember>>*)members;
163165

164166
/**
165-
* Keys of members from this _group and any child groups.
167+
* Keys of members from this group and any child groups.
166168
*/
167169
- (NSArray<NSString*>*)recursiveMembers;
168170

169171
/**
170-
* Keys of members from this _group
172+
* Keys of members from this group
171173
*/
172174
- (NSArray<NSString*>*)buildFileKeys;
173175

Source/XCGroup.m

+5
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ - (BOOL)isRootGroup
103103
return [self pathRelativeToParent] == nil && [self displayName] == nil;
104104
}
105105

106+
- (BOOL)isEmpty
107+
{
108+
return [self.members count] == 0;
109+
}
110+
106111

107112
//-------------------------------------------------------------------------------------------
108113
#pragma mark Adding children

Source/XCProject.h

+12-4
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@
9191

9292
- (NSArray<XCSourceFile*>*)imagePNGFiles;
9393

94-
- (NSString*)containingFolderPath;
95-
9694
- (NSString*)filePath;
9795

9896

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

116114
/**
117-
* Returns the _group with the given key, or nil.
115+
* Returns the group with the given key, or nil.
118116
*/
119117
- (XCGroup*)groupWithKey:(NSString*)key;
120118

119+
/**
120+
* Returns the _first_ group in the project with the given name, or nil.
121+
*/
122+
- (XCGroup*)groupWithDisplayName:(NSString*)name;
123+
121124
/**
122125
* Returns the _group with the specified display name path - the directory relative to the root _group. Eg Source/Main
123126
*/
@@ -129,10 +132,15 @@
129132
- (XCGroup*)groupForGroupMemberWithKey:(NSString*)key;
130133

131134
/**
132-
* Returns the parent _group for the _group or file with the source file
135+
* Returns the parent group for the group or file with the source file
133136
*/
134137
- (XCGroup*)groupWithSourceFile:(XCSourceFile*)sourceFile;
135138

139+
/**
140+
* Removes all empty groups from the project.
141+
*/
142+
- (void)pruneEmptyGroups;
143+
136144
//-------------------------------------------------------------------------------------------
137145
#pragma mark Targets
138146
/**

Source/XCProject.m

+22-6
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,6 @@ - (NSArray*)imagePNGFiles
148148
return [self projectFilesOfType:ImageResourcePNG];
149149
}
150150

151-
- (NSString *)containingFolderPath
152-
{
153-
return [_filePath stringByDeletingLastPathComponent];
154-
}
155-
156151

157152
// need this value to construct relative path in XcodeprojDefinition
158153
- (NSString*)filePath
@@ -235,6 +230,17 @@ - (XCGroup*)groupWithKey:(NSString*)key
235230
return nil;
236231
}
237232

233+
- (XCGroup *)groupWithDisplayName:(NSString *)name
234+
{
235+
for (XCGroup *group in [self groups]) {
236+
if ([[group displayName] isEqualToString:name]) {
237+
return group;
238+
}
239+
}
240+
return nil;
241+
}
242+
243+
238244
- (XCGroup*)groupForGroupMemberWithKey:(NSString*)key
239245
{
240246
for (XCGroup* group in [self groups])
@@ -262,7 +268,17 @@ - (XCGroup*)groupWithSourceFile:(XCSourceFile*)sourceFile
262268
return nil;
263269
}
264270

265-
//TODO: This could fail if the path attribute on a given _group is more than one directory. Start with candidates and
271+
- (void)pruneEmptyGroups
272+
{
273+
for (XCGroup *group in [self groups]) {
274+
if ([group isEmpty]) {
275+
[group removeFromParentGroup];
276+
}
277+
}
278+
}
279+
280+
281+
//TODO: This could fail if the path attribute on a given group is more than one directory. Start with candidates and
266282
//TODO: search backwards.
267283
- (XCGroup*)groupWithPathFromRoot:(NSString*)path
268284
{
Binary file not shown.

0 commit comments

Comments
 (0)