Debian Codegraph @

XCode/PBXContainer.m

raw
1 /*
2 Copyright (C) 2018, 2019, 2020, 2021 Free Software Foundation, Inc.
3
4 Written by: Gregory John Casamento <[email protected]>
5 Date: 2022
6
7 This file is part of the GNUstep XCode Library
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free
21 Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110 USA.
23 */
24
25 #import "PBXContainer.h"
26 #import "PBXCommon.h"
27 #import "PBXProject.h"
28 #import "PBXFileReference.h"
29
30 @implementation PBXContainer
31
32 - (instancetype) init
33 {
34 self = [super init];
35
36 if (self != nil)
37 {
38 _archiveVersion = @"1";
39 _classes = [[NSMutableDictionary alloc] init];
40 _objectVersion = @"56";
41 _objects = [[NSMutableDictionary alloc] init];
42 _rootObject = nil;
43 _filename = nil;
44 _parameter = nil;
45 _workspaceLink = nil;
46 _workspaceLibs = nil;
47 _workspaceIncludes = nil;
48 }
49
50 return self;
51 }
52
53 - (instancetype) initWithRootObject: (id)object
54 {
55 self = [self init];
56 if (self != nil)
57 {
58 [self setRootObject: object];
59 }
60 return self;
61 }
62
63 - (void) dealloc
64 {
65 RELEASE(_archiveVersion);
66 RELEASE(_classes);
67 RELEASE(_objectVersion);
68 RELEASE(_objects);
69 RELEASE(_filename);
70 RELEASE(_parameter);
71 RELEASE(_workspaceLink);
72 RELEASE(_workspaceLibs);
73 RELEASE(_workspaceIncludes);
74
75 [super dealloc];
76 }
77
78 - (void) setWorkspaceIncludes: (NSString *)i
79 {
80 ASSIGN(_workspaceIncludes, i);
81 }
82
83 - (NSString *) workspaceIncludes
84 {
85 return _workspaceIncludes;
86 }
87
88 - (void) setWorkspaceLibs: (NSString *)l
89 {
90 ASSIGN(_workspaceLibs, l);
91 }
92
93 - (NSString *) workspaceLibs
94 {
95 return _workspaceLibs;
96 }
97
98 - (void) setWorkspaceLink: (NSString *)w
99 {
100 ASSIGN(_workspaceLink, w);
101 }
102
103 - (NSString *) workspaceLink
104 {
105 return _workspaceLink;
106 }
107
108 - (void) setParameter: (NSString *)p
109 {
110 ASSIGN(_parameter, p);
111 }
112
113 - (NSString *) parameter
114 {
115 return _parameter;
116 }
117
118 - (void) setFilename: (NSString *)fn
119 {
120 ASSIGN(_filename, fn);
121 }
122
123 - (NSString *) filename
124 {
125 return _filename;
126 }
127
128 - (void) setArchiveVersion: (NSString *)version
129 {
130 ASSIGN(_archiveVersion,version);
131 }
132
133 - (NSString *) archiveVersion
134 {
135 return _archiveVersion;
136 }
137
138 - (void) setClasses: (NSMutableDictionary *)dict
139 {
140 ASSIGN(_classes,dict);
141 }
142
143 - (NSMutableDictionary *) classes
144 {
145 return _classes;
146 }
147
148 - (void) setObjectVersion: (NSString *)version
149 {
150 ASSIGN(_objectVersion,version);
151 }
152
153 - (NSString *) objectVersion
154 {
155 return _objectVersion;
156 }
157
158 - (void) setObjects: (NSMutableDictionary *)dict
159 {
160 ASSIGN(_objects,dict);
161 }
162
163 - (NSMutableDictionary *) objects
164 {
165 return _objects;
166 }
167
168 - (id) rootObject
169 {
170 return _rootObject;
171 }
172
173 - (void) setRootObject: (id)object
174 {
175 ASSIGN(_rootObject, object);
176 }
177
178 - (void) collectHeaderFileReferences
179 {
180 NSString *includeDirs = @"";
181 NSMutableArray *dirs = [NSMutableArray array];
182 NSArray *array = [_objects allValues];
183 NSEnumerator *en = [array objectEnumerator];
184 id obj = nil;
185 NSMutableDictionary *context = [NSMutableDictionary dictionary];
186
187 while((obj = [en nextObject]) != nil)
188 {
189 if([obj isKindOfClass:[PBXFileReference class]])
190 {
191 if([[obj lastKnownFileType] isEqualToString:@"sourcecode.c.h"])
192 {
193 NSString *includePath = [[obj path] stringByDeletingLastPathComponent];
194 if([includePath isEqualToString:@""] == NO)
195 {
196 if([dirs containsObject:includePath] == NO)
197 {
198 [dirs addObject:includePath];
199 includeDirs = [includeDirs stringByAppendingFormat: @" -I./%@ ",includePath];
200 }
201 }
202 }
203 }
204 }
205
206 // Add to the dictionary...
207 [context setObject: includeDirs forKey:@"INCLUDE_DIRS"];
208
209 // Add workspace info to the context...
210 if (_workspaceLink != nil)
211 {
212 [context setObject: _workspaceLink forKey: @"WORKSPACE_LINK_LINE"];
213 }
214
215 NSDebugLog(@"\n\n\nlibs = %@\n\n\n", _workspaceLibs);
216 if (_workspaceLibs != nil)
217 {
218 [context setObject: _workspaceLibs forKey: @"WORKSPACE_LIBS_LINE"];
219 }
220
221 if (_workspaceIncludes != nil)
222 {
223 [context setObject: _workspaceIncludes forKey: @"WORKSPACE_INCLUDE_LINE"];
224 }
225
226 [_rootObject setContext: context];
227 }
228
229 - (BOOL) build
230 {
231 [self collectHeaderFileReferences];
232 [_rootObject setContainer: self];
233 return [_rootObject build];
234 }
235
236 - (BOOL) clean
237 {
238 return [_rootObject clean];
239 }
240
241 - (BOOL) install
242 {
243 return [_rootObject install];
244 }
245
246 - (BOOL) generate
247 {
248 [self collectHeaderFileReferences];
249 [_rootObject setContainer: self];
250 return [_rootObject generate];
251 }
252
253 - (BOOL) link
254 {
255 // Likely need to collect .o artifacts...
256 [_rootObject setContainer: self];
257 return [_rootObject link];
258 }
259
260 - (BOOL) save
261 {
262 NSString *fn = [[self filename]
263 stringByDeletingLastPathComponent];
264 NSString *of = @"project.pbxproj";
265 NSString *dn = _parameter;
266 NSFileManager *fm = [NSFileManager defaultManager];
267 NSError *error = nil;
268 BOOL created = [fm createDirectoryAtPath: dn
269 withIntermediateDirectories: YES
270 attributes: NULL
271 error: &error];
272
273 xcprintf("=== Saving Project %s%s%s%s -> %s%s%s\n",
274 BOLD, YELLOW, [fn cString], RESET, GREEN,
275 [dn cString], RESET);
276
277 // Clear the cached objects dictionary;
278 [_rootObject save]; // Setup to save...
279
280 // Save the project...
281 if (created && !error)
282 {
283 PBXCoder *coder = [[PBXCoder alloc] initWithRootObject: self];
284 NSDictionary *dictionary = [coder archive];
285 NSString *path = [dn stringByAppendingPathComponent: of];
286 BOOL result = [dictionary writeToFile: path atomically: YES];
287
288 if (result)
289 {
290 xcprintf("=== Done Saving Project %s%s%s%s\n",
291 BOLD, GREEN, [dn cString], RESET);
292 }
293 else
294 {
295 xcprintf("=== Error Saving Project %s%s%s%s\n",
296 BOLD, GREEN, [dn cString], RESET);
297 }
298
299 return result;
300 }
301 else
302 {
303 xcprintf("=== Error creating directory %s", dn);
304 }
305
306 return NO;
307 }
308
309 @end