forked from appsquickly/XcodeEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXCSourceFile.m
More file actions
283 lines (238 loc) · 9.8 KB
/
XCSourceFile.m
File metadata and controls
283 lines (238 loc) · 9.8 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
////////////////////////////////////////////////////////////////////////////////
//
// 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 "XCKeyBuilder.h"
#import "XCGroup.h"
#import "XCTarget.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 == LocalizableStrings;
}
- (XcodeMemberType)buildPhase
{
if (_type == SourceCodeObjC || _type == SourceCodeObjCPlusPlus || _type == SourceCodeCPlusPlus || _type == XibFile || _type == SourceCodeSwift) {
return PBXSourcesBuildPhaseType;
}
else if (_type == Framework) {
return PBXFrameworksBuildPhaseType;
}
else if (_type == ImageResourcePNG || _type == HTML || _type == Bundle || _type == AssetCatalog || _type == PropertyList || _type == LocalizableStrings) {
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)removeBuildFile
{
if ([self isBuildFile]) {
XCSourceFile* file = [_project fileWithName:_path];
[[_project objects] enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:[NSDictionary class]]) {
id fileRef = [obj valueForKey:@"fileRef"];
if (fileRef && [fileRef isKindOfClass:[NSString class]]) {
if ([fileRef isEqualToString:file.key]) {
[_project removeObjectWithKey:key];
}
}
}
}];
}
}
- (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;
}
}
}];
}
- (void)setWeakReference
{
[self addBuildAttribute:@"Weak"];
}
- (void)addBuildAttribute:(NSString*)attribute
{
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];
if (![replaceBuildFile objectForKey:@"settings"]) {
replaceBuildFile[@"settings"] = @{@"ATTRIBUTES": @[attribute]};
}
else {
NSMutableDictionary *settingsDict = [NSMutableDictionary dictionaryWithDictionary:replaceBuildFile[@"settings"]];
if (![settingsDict objectForKey:@"ATTRIBUTES"]) {
settingsDict[@"ATTRIBUTES"] = @[attribute];
}
else {
NSMutableArray* attr = [NSMutableArray arrayWithArray:settingsDict[@"ATTRIBUTES"]];
if ([attr containsObject:attribute]) {
return;
}
[attr addObject:attribute];
settingsDict[@"ATTRIBUTES"] = attr;
}
}
[[_project objects] removeObjectForKey:key];
[_project objects][key] = replaceBuildFile;
}
}
}];
}
//-------------------------------------------------------------------------------------------
#pragma mark - Protocol Methods
- (XcodeMemberType)groupMemberType
{
return PBXFileReferenceType;
}
- (NSString *)displayName
{
return _name;
}
- (NSString *)pathRelativeToProjectRoot
{
NSString *parentPath = [[_project groupForGroupMemberWithKey:_key] pathRelativeToProjectRoot];
NSString *result = [parentPath stringByAppendingPathComponent:_name];
return result;
}
//-------------------------------------------------------------------------------------------
#pragma mark - Utility Methods
- (NSString *)description
{
return [NSString stringWithFormat:@"Project file: key=%@, name=%@, fullPath=%@", _key, _name,
[self pathRelativeToProjectRoot]];
}
@end