Debian Codegraph @

XCode/XCFileRef.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 "XCFileRef.h"
26 #import "PBXCoder.h"
27 #import "PBXContainer.h"
28 #import "PBXProject.h"
29 #import "NSString+PBXAdditions.h"
30 #import "GSXCBuildContext.h"
31
32 #import <Foundation/NSString.h>
33 #import <Foundation/NSDebug.h>
34
35 @implementation XCFileRef
36
37 + (instancetype) fileRef
38 {
39 return AUTORELEASE([[self alloc] init]);
40 }
41
42 - (instancetype) init
43 {
44 self = [super init];
45
46 if (self)
47 {
48 [self setLocation: nil];
49 [self setWorkspaceLink: nil];
50 }
51
52 return self;
53 }
54
55 - (void) dealloc
56 {
57 RELEASE(_location);
58 RELEASE(_workspaceLink);
59
60 [super dealloc];
61 }
62
63 - (NSString *) location
64 {
65 return _location;
66 }
67
68 - (void) setLocation: (NSString *)loc
69 {
70 ASSIGN(_location, loc);
71 }
72
73 - (NSString *) workspaceLink
74 {
75 return _workspaceLink;
76 }
77
78 - (void) setWorkspaceLink: (NSString *)link
79 {
80 ASSIGN(_workspaceLink, link);
81 }
82
83 - (NSString *) workspaceLibs
84 {
85 return _workspaceLibs;
86 }
87
88 - (void) setWorkspaceLibs: (NSString *)libs
89 {
90 ASSIGN(_workspaceLibs, libs);
91 }
92
93 - (NSString *) workspaceIncludes
94 {
95 return _workspaceIncludes;
96 }
97
98 - (void) setWorkspaceIncludes: (NSString *)inc
99 {
100 ASSIGN(_workspaceIncludes, inc);
101 }
102
103 - (NSArray *) targets
104 {
105 NSString *loc = [[self location] stringByReplacingOccurrencesOfString: @"group:" withString: @""];
106 NSString *p = [loc stringByDeletingLastPathComponent];
107 NSArray *result = nil;
108
109 if (p != nil)
110 {
111 PBXCoder *coder = nil;
112 NSFileManager *mgr = [NSFileManager defaultManager];
113 NSString *cwd = [mgr currentDirectoryPath];
114
115 // If the project is in a subdir, go to the subdir...
116 if ([[p pathExtension] isEqualToString: @"xcodeproj"] == NO)
117 {
118 NSString *nwd = [cwd stringByAppendingPathComponent: p];
119 [mgr changeCurrentDirectoryPath: nwd];
120 }
121
122 coder = [[PBXCoder alloc] initWithProjectFile: [loc lastPathComponent]];
123 if (coder != nil)
124 {
125 PBXContainer *pc = [coder unarchive];
126 PBXProject *prj = [pc rootObject];
127
128 result = [[prj targets] copy];
129 }
130
131 // Go back to the original working dir...
132 if ([[p pathExtension] isEqualToString: @"xcodeproj"] == NO)
133 {
134 [mgr changeCurrentDirectoryPath: cwd];
135 }
136 }
137
138 return result;
139 }
140
141 - (BOOL) performLegacyOperation: (SEL)sel
142 {
143 NSString *loc = [[self location] stringByReplacingOccurrencesOfString: @"group:" withString: @""];
144 NSString *p = [loc stringByDeletingLastPathComponent];
145 NSString *function = NSStringFromSelector(sel);
146 NSString *display = [function stringByCapitalizingFirstCharacter];
147
148 if (p != nil)
149 {
150 PBXCoder *coder = nil;
151 NSFileManager *mgr = [NSFileManager defaultManager];
152 NSString *cwd = [mgr currentDirectoryPath];
153
154 // If the project is in a subdir, go to the subdir...
155 if ([[p pathExtension] isEqualToString: @"xcodeproj"] == NO)
156 {
157 NSString *nwd = [cwd stringByAppendingPathComponent: p];
158
159 printf("++ %s project in dir... %s\n", [display cString], [nwd cString]);
160 [mgr changeCurrentDirectoryPath: nwd];
161 }
162
163 coder = [[PBXCoder alloc] initWithProjectFile: [loc lastPathComponent]];
164 if (coder != nil)
165 {
166 PBXContainer *pc = [coder unarchive];
167
168 [pc setWorkspaceLink: _workspaceLink];
169 [pc setWorkspaceIncludes: _workspaceIncludes];
170 [pc setWorkspaceLibs: _workspaceLibs];
171 [pc performSelector: sel];
172 }
173
174 // If the project is in a subdir, return to the previous dir...
175 if ([[p pathExtension] isEqualToString: @"xcodeproj"] == NO)
176 {
177 printf("++ %s completed\n", [display cString]);
178 [mgr changeCurrentDirectoryPath: cwd];
179 }
180
181 AUTORELEASE(coder);
182
183 }
184 return YES;
185 }
186
187 - (BOOL) build
188 {
189 return [self performLegacyOperation: _cmd];
190 }
191
192 - (BOOL) clean
193 {
194 return [self performLegacyOperation: _cmd];
195 }
196
197 - (BOOL) install
198 {
199 return [self performLegacyOperation: _cmd];
200 }
201
202 - (BOOL) link
203 {
204 return [self performLegacyOperation: _cmd];
205 }
206
207 @end