-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathXCSourceFile.m
More file actions
executable file
·250 lines (208 loc) · 8.21 KB
/
XCSourceFile.m
File metadata and controls
executable file
·250 lines (208 loc) · 8.21 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
////////////////////////////////////////////////////////////////////////////////
//
// 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 "XCSourceFile.h"
#import "XCProject.h"
#import "Utils/XCKeyBuilder.h"
#import "XCGroup.h"
@implementation XCSourceFile
@synthesize type = _type;
@synthesize key = _key;
@synthesize sourceTree = _sourceTree;
//-------------------------------------------------------------------------------------------
#pragma mark - Class Methods
//-------------------------------------------------------------------------------------------
+ (XCSourceFile *)sourceFileWithProject:(XCProject *)project key:(NSString *)key type:(XcodeSourceFileType)type
name:(NSString *)name sourceTree:(NSString *)_tree path:(NSString *)path
{
return [[XCSourceFile alloc] initWithProject:project key:key type:type name:name sourceTree:_tree path:path];
}
//-------------------------------------------------------------------------------------------
#pragma mark - Initialization & Destruction
//-------------------------------------------------------------------------------------------
- (id)initWithProject:(XCProject *)project key:(NSString *)key type:(XcodeSourceFileType)type name:(NSString *)name
sourceTree:(NSString *)tree path:(NSString *)path
{
self = [super init];
if (self) {
_project = project;
_key = [key copy];
_type = type;
_name = [name copy];
_sourceTree = [tree copy];
_path = [path copy];
}
return self;
}
//-------------------------------------------------------------------------------------------
#pragma mark - Interface Methods
//-------------------------------------------------------------------------------------------
// Goes to the entry for this object in the project and sets a value for one of the keys, such as name, path, etc.
- (void)setValue:(id)val forProjectItemPropertyWithKey:(NSString *)key
{
NSMutableDictionary *obj = [[[_project objects] objectForKey:_key] mutableCopy];
if (nil == obj) {
[NSException raise:@"Project item not found" format:@"Project item with key %@ not found.", _key];
}
[obj setValue:val forKey:key];
[[_project objects] setValue:obj forKey:_key];
}
- (NSString *)name
{
return _name;
}
- (void)setName:(NSString *)name
{
_name = [name copy];
[self setValue:name forProjectItemPropertyWithKey:@"name"];
}
- (NSString *)path
{
return _path;
}
- (void)setPath:(NSString *)path
{
_path = [path copy];
[self setValue:path forProjectItemPropertyWithKey:@"path"];
}
- (BOOL)isBuildFile
{
if ([self canBecomeBuildFile] && _isBuildFile == nil) {
_isBuildFile = @NO;
[[_project objects] enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSDictionary *obj, BOOL *stop) {
if ([[obj valueForKey:@"isa"] xce_hasBuildFileType]) {
if ([[obj valueForKey:@"fileRef"] isEqualToString:_key]) {
_isBuildFile = nil;
_isBuildFile = @YES;
}
}
}];
}
return [_isBuildFile boolValue];
}
- (BOOL)canBecomeBuildFile
{
return _type == SourceCodeObjC || _type == SourceCodeObjCPlusPlus || _type == SourceCodeCPlusPlus || _type == XibFile || _type == Framework || _type == ImageResourcePNG || _type == HTML || _type == Bundle || _type == Archive || _type == AssetCatalog || _type == SourceCodeSwift || _type == PropertyList || _type == XCDataModel;
}
- (XcodeMemberType)buildPhase
{
if (_type == SourceCodeObjC || _type == SourceCodeObjCPlusPlus || _type == SourceCodeCPlusPlus || _type == XibFile || _type == SourceCodeSwift || _type == XCDataModel) {
return PBXSourcesBuildPhaseType;
}
else if (_type == Framework) {
return PBXFrameworksBuildPhaseType;
}
else if (_type == ImageResourcePNG || _type == HTML || _type == Bundle || _type == AssetCatalog || _type == PropertyList) {
return PBXResourcesBuildPhaseType;
}
else if (_type == Archive) {
return PBXFrameworksBuildPhaseType;
}
return PBXNilType;
}
- (NSString *)buildFileKey
{
if (_buildFileKey == nil) {
[[_project objects] enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSDictionary *obj, BOOL *stop) {
if ([[obj valueForKey:@"isa"] xce_hasBuildFileType]) {
if ([[obj valueForKey:@"fileRef"] isEqualToString:_key]) {
_buildFileKey = [key copy];
}
}
}];
}
return [_buildFileKey copy];
}
- (void)becomeBuildFile
{
if (![self isBuildFile]) {
if ([self canBecomeBuildFile]) {
NSMutableDictionary *sourceBuildFile = [NSMutableDictionary dictionary];
sourceBuildFile[@"isa"] = [NSString xce_stringFromMemberType:PBXBuildFileType];
sourceBuildFile[@"fileRef"] = _key;
NSString *buildFileKey = [[XCKeyBuilder forItemNamed:[_name stringByAppendingString:@".buildFile"]] build];
[_project objects][buildFileKey] = sourceBuildFile;
}
else if (_type == Framework) {
[NSException raise:NSInvalidArgumentException format:@"Add framework to target not implemented yet."];
}
else {
[NSException raise:NSInvalidArgumentException format:@"Project file of type %@ can't become a build file.",
NSStringFromXCSourceFileType(_type)];
}
}
}
- (void)setCompilerFlags:(NSString *)value
{
NSMutableDictionary *objectArrayCopy = [[_project objects] mutableCopy];
[objectArrayCopy enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSDictionary *obj, BOOL *stop) {
if ([[obj valueForKey:@"isa"] xce_hasBuildFileType]) {
if ([obj[@"fileRef"] isEqualToString:self.key]) {
NSMutableDictionary *replaceBuildFile = [NSMutableDictionary dictionaryWithDictionary:obj];
NSDictionary *compilerFlagsDict = @{@"COMPILER_FLAGS" : value};
if ([replaceBuildFile[@"settings"] objectForKey:@"COMPILER_FLAGS"] != nil) {
NSMutableDictionary *newSettings = [NSMutableDictionary dictionaryWithDictionary:replaceBuildFile[@"settings"]];
[newSettings removeObjectForKey:@"COMPILER_FLAGS"];
replaceBuildFile[@"settings"] = compilerFlagsDict;
}
else {
replaceBuildFile[@"settings"] = compilerFlagsDict;
}
[[_project objects] removeObjectForKey:key];
[_project objects][key] = replaceBuildFile;
}
}
}];
}
//-------------------------------------------------------------------------------------------
#pragma mark - Protocol Methods
- (XcodeMemberType)groupMemberType
{
return PBXFileReferenceType;
}
- (NSString *)displayName
{
return _name;
}
- (NSString *)pathRelativeToProjectRoot
{
NSString *result = nil;
if ([_sourceTree isEqualToString:@"<group>"]) {
NSString *component = nil;
if (_path) {
component = _path;
} else if (_name) {
component = _name;
}
NSString *parentPath = [[_project groupForGroupMemberWithKey:_key] pathRelativeToProjectRoot];
result = [parentPath stringByAppendingPathComponent:component];
} else if ([_sourceTree isEqualToString:@"SOURCE_ROOT"]) {
if (_path) {
result = _path;
} else if (_name) {
result = _name;
}
} else if ([_sourceTree isEqualToString:@"SDKROOT"]) {
//pass
} else if ([_sourceTree isEqualToString:@"BUILT_PRODUCTS_DIR"]) {
// pass
} else {
result = _sourceTree;
}
return result;
}
//-------------------------------------------------------------------------------------------
#pragma mark - Utility Methods
- (NSString *)description
{
return [NSString stringWithFormat:@"Project file: key=%@, name=%@, fullPath=%@", _key, _name,
[self pathRelativeToProjectRoot]];
}
@end