| 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 "NSObject+KeyExtraction.h" |
| 26 |
#import "PBXCoder.h" |
| 27 |
#import "PBXContainer.h" |
| 28 |
#import "PBXCommon.h" |
| 29 |
#import "NSString+PBXAdditions.h" |
| 30 |
#import "GSXCBuildContext.h" |
| 31 |
|
| 32 |
#ifdef _WIN32 |
| 33 |
#import "setenv.h" |
| 34 |
#endif |
| 35 |
|
| 36 |
#define DEBUG 1 |
| 37 |
|
| 38 |
@implementation PBXCoder |
| 39 |
|
| 40 |
// Delegate... |
| 41 |
- (XCAbstractDelegate *) delegate |
| 42 |
{ |
| 43 |
return _delegate; |
| 44 |
} |
| 45 |
|
| 46 |
- (void) setDelegate: (XCAbstractDelegate *)delegate |
| 47 |
{ |
| 48 |
_delegate = delegate; // weak since we don't retain the delegate... |
| 49 |
} |
| 50 |
|
| 51 |
// Methods for unarchiving a pbxproj file... |
| 52 |
+ (instancetype) unarchiveWithProjectFile: (NSString *)name |
| 53 |
{ |
| 54 |
return AUTORELEASE([[self alloc] initWithProjectFile: name]); |
| 55 |
} |
| 56 |
|
| 57 |
- (instancetype) initWithContentsOfFile: (NSString *)name |
| 58 |
{ |
| 59 |
if((self = [super init]) != nil) |
| 60 |
{ |
| 61 |
_objectCache = [[NSMutableDictionary alloc] initWithCapacity: 10]; |
| 62 |
|
| 63 |
ASSIGN(_fileName, name); |
| 64 |
ASSIGN(_projectRoot, |
| 65 |
[[_fileName stringByDeletingLastPathComponent] |
| 66 |
stringByDeletingLastPathComponent]); |
| 67 |
ASSIGN(_dictionary, |
| 68 |
[NSMutableDictionary dictionaryWithContentsOfFile: _fileName]); |
| 69 |
ASSIGN(_objects, [_dictionary objectForKey: @"objects"]); |
| 70 |
|
| 71 |
_parents = [[NSMutableDictionary alloc] initWithCapacity: 10]; |
| 72 |
[[GSXCBuildContext sharedBuildContext] |
| 73 |
setObject: _objects forKey: @"objects"]; |
| 74 |
|
| 75 |
setenv("PROJECT_DIR","./",1); |
| 76 |
setenv("PROJECT_ROOT","./",1); |
| 77 |
setenv("SRCROOT","./",1); |
| 78 |
} |
| 79 |
return self; |
| 80 |
} |
| 81 |
|
| 82 |
- (instancetype) initWithProjectFile: (NSString *)name |
| 83 |
{ |
| 84 |
NSString *newName = [name stringByAppendingPathComponent: @"project.pbxproj"]; |
| 85 |
return [self initWithContentsOfFile: newName]; |
| 86 |
} |
| 87 |
|
| 88 |
- (void) dealloc |
| 89 |
{ |
| 90 |
RELEASE(_objectCache); |
| 91 |
RELEASE(_fileName); |
| 92 |
RELEASE(_dictionary); |
| 93 |
RELEASE(_objects); |
| 94 |
RELEASE(_parents); |
| 95 |
DESTROY(_rootObject); |
| 96 |
|
| 97 |
[super dealloc]; |
| 98 |
} |
| 99 |
|
| 100 |
- (id) unarchive |
| 101 |
{ |
| 102 |
return [self unarchiveFromDictionary: _dictionary]; |
| 103 |
} |
| 104 |
|
| 105 |
- (id) unarchiveFromDictionary: (NSDictionary *)dict |
| 106 |
{ |
| 107 |
id object = nil; |
| 108 |
NSString *isaValue = [dict objectForKey: @"isa"]; |
| 109 |
NSString *className = (isaValue == nil) ? @"PBXContainer" : isaValue; |
| 110 |
Class classInstance = NSClassFromString(className); |
| 111 |
|
| 112 |
if(classInstance == nil) |
| 113 |
{ |
| 114 |
xcputs([[NSString stringWithFormat: @"Unknown class: %@",className] cString]); |
| 115 |
return nil; |
| 116 |
} |
| 117 |
|
| 118 |
object = AUTORELEASE([[classInstance alloc] init]); |
| 119 |
object = [self applyKeysAndValuesFromDictionary: dict |
| 120 |
toObject: object]; |
| 121 |
|
| 122 |
if([object isKindOfClass: [PBXContainer class]]) |
| 123 |
{ |
| 124 |
[object setObjects: _objectCache]; |
| 125 |
[object setFilename: _fileName]; |
| 126 |
ASSIGN(_rootObject, object); |
| 127 |
} |
| 128 |
|
| 129 |
return object; |
| 130 |
} |
| 131 |
|
| 132 |
- (id) unarchiveObjectForKey: (NSString *)key |
| 133 |
{ |
| 134 |
id obj = [_objectCache objectForKey: key]; |
| 135 |
if(obj != nil) |
| 136 |
{ |
| 137 |
return obj; |
| 138 |
} |
| 139 |
|
| 140 |
// cache the object, if it exists in objects... if not return nil |
| 141 |
NSDictionary *dict = [_objects objectForKey: key]; |
| 142 |
if(dict != nil) |
| 143 |
{ |
| 144 |
obj = [self unarchiveFromDictionary: dict]; |
| 145 |
[_objectCache setObject: obj forKey: key]; |
| 146 |
} |
| 147 |
|
| 148 |
return obj; |
| 149 |
} |
| 150 |
|
| 151 |
- (NSMutableArray *) resolveArrayMembers: (NSMutableArray *)array |
| 152 |
{ |
| 153 |
NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity: 10]; |
| 154 |
NSEnumerator *en = [array objectEnumerator]; |
| 155 |
id key = nil; |
| 156 |
|
| 157 |
while((key = [en nextObject]) != nil) |
| 158 |
{ |
| 159 |
id obj = [self unarchiveObjectForKey: key]; |
| 160 |
if(obj != nil) |
| 161 |
{ |
| 162 |
[result addObject: obj]; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
return result; |
| 167 |
} |
| 168 |
|
| 169 |
- (id) applyKeysAndValuesFromDictionary: (NSDictionary *)dict |
| 170 |
toObject: (id)object |
| 171 |
{ |
| 172 |
NSEnumerator *en = [dict keyEnumerator]; |
| 173 |
NSString *key = nil; |
| 174 |
|
| 175 |
while((key = [en nextObject]) != nil) |
| 176 |
{ |
| 177 |
// continue if it's the isa pointer... |
| 178 |
if([key isEqualToString: @"isa"]) |
| 179 |
{ |
| 180 |
continue; |
| 181 |
} |
| 182 |
|
| 183 |
id value = [dict objectForKey: key]; |
| 184 |
if(value != nil) |
| 185 |
{ |
| 186 |
NS_DURING |
| 187 |
{ |
| 188 |
// if it's an array, resolve the indexes of the array.... |
| 189 |
if([value isKindOfClass: [NSMutableArray class]]) |
| 190 |
{ |
| 191 |
value = [self resolveArrayMembers: value]; |
| 192 |
} |
| 193 |
|
| 194 |
// search the global dictionary... |
| 195 |
if([key isEqualToString: @"containerPortal"] == NO && |
| 196 |
[key isEqualToString: @"remoteGlobalIDString"] == NO) |
| 197 |
{ |
| 198 |
id newValue = [self unarchiveObjectForKey: value]; |
| 199 |
if(newValue != nil) |
| 200 |
{ |
| 201 |
value = newValue; |
| 202 |
} |
| 203 |
} |
| 204 |
else |
| 205 |
{ |
| 206 |
value = [_objectCache objectForKey: key]; |
| 207 |
} |
| 208 |
|
| 209 |
if(value != nil) |
| 210 |
{ |
| 211 |
id currentValue = [object valueForKey: key]; |
| 212 |
if(currentValue == nil) |
| 213 |
{ |
| 214 |
[object setValue: value |
| 215 |
forKey: key]; |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
NS_HANDLER |
| 220 |
{ |
| 221 |
xcputs([[NSString stringWithFormat: @"%@, key = %@, value = %@, object = %@", |
| 222 |
[localException reason], |
| 223 |
key, |
| 224 |
value, |
| 225 |
object] cString]); |
| 226 |
} |
| 227 |
NS_ENDHANDLER; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
return object; |
| 232 |
} |
| 233 |
|
| 234 |
- (NSString *) projectRoot |
| 235 |
{ |
| 236 |
return _projectRoot; |
| 237 |
} |
| 238 |
|
| 239 |
- (NSString *) fileName |
| 240 |
{ |
| 241 |
return _fileName; |
| 242 |
} |
| 243 |
|
| 244 |
- (NSDictionary *) dictionary |
| 245 |
{ |
| 246 |
return _dictionary; |
| 247 |
} |
| 248 |
|
| 249 |
- (NSDictionary *) objects |
| 250 |
{ |
| 251 |
return _objects; |
| 252 |
} |
| 253 |
|
| 254 |
// Archiving methods... |
| 255 |
+ (id) archiveWithRootObject: (id)root |
| 256 |
{ |
| 257 |
PBXCoder *coder = [[self alloc] initWithRootObject: root]; |
| 258 |
return [coder archive]; |
| 259 |
} |
| 260 |
|
| 261 |
- (instancetype) initWithRootObject: (id)root |
| 262 |
{ |
| 263 |
self = [super init]; |
| 264 |
if (self != nil) |
| 265 |
{ |
| 266 |
ASSIGN(_rootObject, root); |
| 267 |
} |
| 268 |
return self; |
| 269 |
} |
| 270 |
|
| 271 |
- (id) archive |
| 272 |
{ |
| 273 |
return [_rootObject allKeysAndValues]; |
| 274 |
} |
| 275 |
|
| 276 |
@end |