Repository navigation
Expand file tree
/
Copy pathXCSubProjectDefinition.m
More file actions
166 lines (142 loc) · 6.36 KB
/
Copy pathXCSubProjectDefinition.m
File metadata and controls
166 lines (142 loc) · 6.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
////////////////////////////////////////////////////////////////////////////////
//
// JASPER BLUES
// Copyright 2012 Jasper Blues
// All Rights Reserved.
//
// NOTICE: Jasper Blues permits you to use, modify, and distribute this file
// in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////
#import "XCProject.h"
#import "XCProject+SubProject.h"
#import "XCSubProjectDefinition.h"
@interface XCSubProjectDefinition ()
@property (nonatomic, strong, readwrite) NSString *relativePath;
@end
@implementation XCSubProjectDefinition
@synthesize name = _name;
@synthesize path = _path;
@synthesize type = _type;
@synthesize parentProject = _parentProject;
@synthesize subProject = _subProject;
@synthesize relativePath = _relativePath;
@synthesize key = _key;
@synthesize fullProjectPath = _fullProjectPath;
//-------------------------------------------------------------------------------------------
#pragma mark - Class Methods
//-------------------------------------------------------------------------------------------
+ (XCSubProjectDefinition *)withName:(NSString *)name path:(NSString *)path parentProject:(XCProject *)parentProject
{
return [[XCSubProjectDefinition alloc] initWithName:name path:path parentProject:parentProject];
}
//-------------------------------------------------------------------------------------------
#pragma mark - Initialization & Destruction
//-------------------------------------------------------------------------------------------
// Note - _path is most often going to be an absolute path. The method pathRelativeToProjectRoot below should be
// used to get the form that's stored in the main project file.
- (id)initWithName:(NSString *)name path:(NSString *)path parentProject:(XCProject *)parentProject
{
self = [super init];
if (self) {
_name = [name copy];
_path = [path copy];
_type = XcodeProject;
_parentProject = parentProject;
_subProject = [[XCProject alloc] initWithFilePath:[NSString stringWithFormat:@"%@/%@.xcodeproj", path, name]];
}
return self;
}
//-------------------------------------------------------------------------------------------
#pragma mark - Interface Methods
//-------------------------------------------------------------------------------------------
- (NSString *)projectFileName
{
return [_name stringByAppendingString:@".xcodeproj"];
}
- (NSString *)fullPathName
{
return [NSString stringWithFormat:@"%@/%@", _path, [_name stringByAppendingString:@".xcodeproj"]];
}
// returns an array of names of the build products of this project
- (NSArray *)buildProductNames
{
NSMutableArray *results = [NSMutableArray array];
NSDictionary *objects = [_subProject objects];
[objects enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSDictionary *obj, BOOL *stop) {
if ([[obj valueForKey:@"isa"] xce_hasProjectType]) {
NSString *productRefGroupKey = [obj valueForKey:@"productRefGroup"];
NSDictionary *products = [objects valueForKey:productRefGroupKey];
NSArray *children = [products valueForKey:@"children"];
for (NSString *childKey in children) {
NSDictionary *child = [objects valueForKey:childKey];
[results addObject:[child valueForKey:@"path"]];
}
}
}];
return results;
}
// returns the key of the PBXFileReference of the xcodeproj file
- (NSString *)projectKey
{
if (_key == nil) {
NSArray *xcodeprojKeys = [_parentProject keysForProjectObjectsOfType:PBXFileReferenceType
withIdentifier:[self pathRelativeToProjectRoot] singleton:YES required:YES];
_key = [[xcodeprojKeys objectAtIndex:0] copy];
}
return [_key copy];
}
- (void)initFullProjectPath:(NSString *)fullProjectPath groupPath:(NSString *)groupPath
{
if (groupPath != nil) {
NSMutableArray *fullPathComponents = [[fullProjectPath pathComponents] mutableCopy];
[fullPathComponents removeLastObject];
fullProjectPath = [[NSString pathWithComponents:fullPathComponents] stringByAppendingFormat:@"/%@", groupPath];
}
_fullProjectPath = [fullProjectPath copy];
}
// compares the given path to the filePath of the project, and returns a relative version. _fullProjectPath, which has
// to hve been previously set, is the full path to the project *plus* the path to the xcodeproj's group, if any.
- (NSString *)pathRelativeToProjectRoot
{
if (_relativePath == nil) {
if (_fullProjectPath == nil) {
[NSException raise:NSInvalidArgumentException format:@"fullProjectPath has not been set"];
}
NSMutableArray *projectPathComponents = [[_fullProjectPath pathComponents] mutableCopy];
NSArray *objectPathComponents = [[self fullPathName] pathComponents];
NSString *convertedPath = @"";
// skip over path components from root that are equal
NSInteger limit =
([projectPathComponents count] < [objectPathComponents count]) ? [projectPathComponents count] :
[objectPathComponents count];
NSInteger index1 = 0;
for (; index1 < limit; index1++) {
if ([[projectPathComponents objectAtIndex:index1]
isEqualToString:[objectPathComponents objectAtIndex:index1]]) {
continue;
}
else {
break;
}
}
// insert "../" for each remaining path component in project's xcodeproj path
for (NSInteger index2 = 0; index2 < ([projectPathComponents count] - index1); index2++) {
convertedPath = [convertedPath stringByAppendingString:@"../"];
}
// tack on the unique part of the object's path
for (NSInteger index3 = index1; index3 < [objectPathComponents count] - 1; index3++) {
convertedPath = [convertedPath stringByAppendingFormat:@"%@/", objectPathComponents[index3]];
}
_relativePath = [[convertedPath stringByAppendingString:[objectPathComponents lastObject]] copy];
}
return [_relativePath copy];
}
//-------------------------------------------------------------------------------------------
#pragma mark - Utility Methods
- (NSString *)description
{
return [NSString stringWithFormat:@"XcodeprojDefinition: sourceFileName = %@, path=%@, type=%li", _name, _path,
(long)_type];
}
@end