| 1 |
/* |
| 2 |
Copyright (C) 2024 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
Written by: Gregory John Casamento <[email protected]> |
| 5 |
Date: 2024 |
| 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 <Foundation/NSObject.h> |
| 26 |
#import <Foundation/NSArray.h> |
| 27 |
#import <Foundation/NSDictionary.h> |
| 28 |
|
| 29 |
#import "NSObject+KeyExtraction.h" |
| 30 |
#import "NSString+PBXAdditions.h" |
| 31 |
|
| 32 |
// |
| 33 |
// Check dictionary to see if it's equvalent... |
| 34 |
// |
| 35 |
@interface NSDictionary (Private) |
| 36 |
- (BOOL) isEqualDictionary: (NSDictionary *)dict; |
| 37 |
@end |
| 38 |
|
| 39 |
@implementation NSDictionary (Private) |
| 40 |
- (BOOL) isEqualDictionary: (NSDictionary *)dict |
| 41 |
{ |
| 42 |
NSEnumerator *en = [self keyEnumerator]; |
| 43 |
id k = nil; |
| 44 |
BOOL result = YES; |
| 45 |
|
| 46 |
while ((k = [en nextObject]) != nil) |
| 47 |
{ |
| 48 |
id v1 = [self objectForKey: k]; |
| 49 |
id v2 = [dict objectForKey: k]; |
| 50 |
|
| 51 |
if ([v1 isKindOfClass: [NSDictionary class]] |
| 52 |
&& [v2 isKindOfClass: [NSDictionary class]]) |
| 53 |
{ |
| 54 |
if ([v1 isEqualToDictionary: v2] == NO) |
| 55 |
{ |
| 56 |
result = NO; |
| 57 |
break; |
| 58 |
} |
| 59 |
} |
| 60 |
else if ([v1 isEqual: v2] == NO) |
| 61 |
{ |
| 62 |
result = NO; |
| 63 |
break; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
return result; |
| 68 |
} |
| 69 |
@end |
| 70 |
|
| 71 |
// Function to generate a 24-character GUID (uppercase, alphanumeric, no dashes) |
| 72 |
NSString *generateGUID() |
| 73 |
{ |
| 74 |
return [[NSUUID UUID] UUIDString]; |
| 75 |
} |
| 76 |
|
| 77 |
// Move PBXContainer information to the top level dictionary... |
| 78 |
id moveContainerProperties(NSDictionary *input) |
| 79 |
{ |
| 80 |
NSMutableDictionary *result = |
| 81 |
[NSMutableDictionary dictionaryWithDictionary: input]; |
| 82 |
NSMutableDictionary *objects = |
| 83 |
[NSMutableDictionary dictionaryWithDictionary: |
| 84 |
[result objectForKey: @"objects"]]; |
| 85 |
NSEnumerator *en = [objects keyEnumerator]; |
| 86 |
id key = nil; |
| 87 |
id keyToChange = nil; |
| 88 |
|
| 89 |
// NSLog(@"result = %@", result); |
| 90 |
while ((key = [en nextObject]) != nil) |
| 91 |
{ |
| 92 |
id d = [objects objectForKey: key]; |
| 93 |
|
| 94 |
if ([d isKindOfClass: [NSDictionary class]]) |
| 95 |
{ |
| 96 |
NSString *cn = [d objectForKey: @"isa"]; |
| 97 |
|
| 98 |
if ([cn isEqualToString: @"PBXContainer"]) |
| 99 |
{ |
| 100 |
keyToChange = key; |
| 101 |
break; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
// Update objects... |
| 107 |
if (keyToChange != nil) |
| 108 |
{ |
| 109 |
NSMutableDictionary *containerDict = [objects objectForKey: keyToChange]; |
| 110 |
|
| 111 |
[containerDict removeObjectForKey: @"rootObject"]; |
| 112 |
[containerDict removeObjectForKey: @"isa"]; |
| 113 |
[containerDict removeObjectForKey: @"objects"]; |
| 114 |
|
| 115 |
[objects removeObjectForKey: keyToChange]; |
| 116 |
[result addEntriesFromDictionary: containerDict]; |
| 117 |
[result setObject: objects forKey: @"objects"]; |
| 118 |
} |
| 119 |
|
| 120 |
// NSLog(@"result = %@", result); |
| 121 |
|
| 122 |
return result; |
| 123 |
} |
| 124 |
|
| 125 |
NSString *guidInCachedObjects(NSDictionary *objects, NSDictionary *dict) |
| 126 |
{ |
| 127 |
NSString *guid = nil; |
| 128 |
NSEnumerator *en = [objects keyEnumerator]; |
| 129 |
NSString *g = nil; |
| 130 |
|
| 131 |
while ((g = [en nextObject]) != nil) |
| 132 |
{ |
| 133 |
NSDictionary *d = [objects objectForKey: g]; |
| 134 |
|
| 135 |
if ([dict isEqualToDictionary: d]) |
| 136 |
{ |
| 137 |
guid = g; |
| 138 |
break; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return guid; |
| 143 |
} |
| 144 |
|
| 145 |
// Recursive function to flatten the property list |
| 146 |
id flattenPropertyList(id propertyList, NSMutableDictionary *objects, NSString **rootObjectGUID) |
| 147 |
{ |
| 148 |
if ([propertyList isKindOfClass:[NSDictionary class]]) |
| 149 |
{ |
| 150 |
NSDictionary *dict = (NSDictionary *)propertyList; |
| 151 |
|
| 152 |
// Check if the dictionary has an "isa" element |
| 153 |
if ([dict objectForKey:@"isa"]) |
| 154 |
{ |
| 155 |
// Generate a GUID for this dictionary |
| 156 |
NSString *guid = generateGUID(); |
| 157 |
|
| 158 |
// If the "isa" is "PBXProject", set the rootObjectGUID |
| 159 |
if ([[dict objectForKey:@"isa"] isEqualToString:@"PBXProject"]) |
| 160 |
{ |
| 161 |
*rootObjectGUID = guid; |
| 162 |
} |
| 163 |
|
| 164 |
// Add the dictionary to the objects array with its GUID |
| 165 |
NSMutableDictionary *flattenedDict = [NSMutableDictionary dictionary]; |
| 166 |
NSEnumerator *en = [dict keyEnumerator]; |
| 167 |
id key = nil; |
| 168 |
|
| 169 |
while ((key = [en nextObject]) != nil) |
| 170 |
{ |
| 171 |
[flattenedDict setObject: flattenPropertyList([dict objectForKey:key], objects, rootObjectGUID) |
| 172 |
forKey: key]; |
| 173 |
} |
| 174 |
|
| 175 |
NSString *existingGuid = guidInCachedObjects(objects, flattenedDict); |
| 176 |
if (existingGuid != nil) |
| 177 |
{ |
| 178 |
guid = existingGuid; |
| 179 |
} |
| 180 |
else |
| 181 |
{ |
| 182 |
[objects setObject:flattenedDict forKey:guid]; |
| 183 |
} |
| 184 |
|
| 185 |
// Return the GUID to replace the dictionary |
| 186 |
return guid; |
| 187 |
} |
| 188 |
else |
| 189 |
{ |
| 190 |
// Recursively process each value in the dictionary |
| 191 |
NSMutableDictionary *processedDict = [NSMutableDictionary dictionary]; |
| 192 |
NSEnumerator *en = [dict keyEnumerator]; |
| 193 |
id key = nil; |
| 194 |
|
| 195 |
while ((key = [en nextObject]) != nil) |
| 196 |
{ |
| 197 |
[processedDict setObject: flattenPropertyList([dict objectForKey:key], objects, rootObjectGUID) |
| 198 |
forKey: key]; |
| 199 |
} |
| 200 |
return processedDict; |
| 201 |
} |
| 202 |
} |
| 203 |
else if ([propertyList isKindOfClass:[NSArray class]]) |
| 204 |
{ |
| 205 |
// Recursively process each item in the array |
| 206 |
NSMutableArray *processedArray = [NSMutableArray array]; |
| 207 |
NSEnumerator *en = [propertyList objectEnumerator]; |
| 208 |
id item = nil; |
| 209 |
|
| 210 |
while((item = [en nextObject]) != nil) |
| 211 |
{ |
| 212 |
[processedArray addObject:flattenPropertyList(item, objects, rootObjectGUID)]; |
| 213 |
} |
| 214 |
return processedArray; |
| 215 |
} |
| 216 |
else |
| 217 |
{ |
| 218 |
// For non-collection types, return the item as-is |
| 219 |
return propertyList; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
// Main function to initiate the flattening process |
| 224 |
NSDictionary *flattenPlist(id propertyList) |
| 225 |
{ |
| 226 |
NSMutableDictionary *objects = [NSMutableDictionary dictionary]; |
| 227 |
NSString *rootObjectGUID = nil; |
| 228 |
NSMutableDictionary *results = [NSMutableDictionary dictionary]; |
| 229 |
|
| 230 |
// Flatten the property list and find the rootObjectGUID |
| 231 |
flattenPropertyList(propertyList, objects, &rootObjectGUID); |
| 232 |
|
| 233 |
// Put the results together... |
| 234 |
[results setObject: rootObjectGUID |
| 235 |
forKey: @"rootObject"]; |
| 236 |
[results setObject: objects |
| 237 |
forKey: @"objects"]; |
| 238 |
|
| 239 |
// Return the final structure |
| 240 |
return results; |
| 241 |
} |
| 242 |
|
| 243 |
@implementation NSObject (KeyExtraction) |
| 244 |
|
| 245 |
+ (void) getAllMethodsForClass: (Class)cls |
| 246 |
intoArray: (NSMutableArray *)methodsArray |
| 247 |
{ |
| 248 |
if (cls == nil || cls == [NSObject class]) |
| 249 |
{ |
| 250 |
return; |
| 251 |
} |
| 252 |
|
| 253 |
unsigned int methodCount = 0; |
| 254 |
Method *methods = class_copyMethodList(cls, &methodCount); |
| 255 |
unsigned int i = 0; |
| 256 |
|
| 257 |
for (i = 0; i < methodCount; i++) |
| 258 |
{ |
| 259 |
Method method = methods[i]; |
| 260 |
[methodsArray addObject:NSStringFromSelector(method_getName(method))]; |
| 261 |
} |
| 262 |
|
| 263 |
free(methods); // Don't forget to free the list |
| 264 |
|
| 265 |
// Recursively call this method for the superclass |
| 266 |
[self getAllMethodsForClass:class_getSuperclass(cls) intoArray:methodsArray]; |
| 267 |
} |
| 268 |
|
| 269 |
+ (NSArray *) recursiveGetAllMethodsForClass: (Class)cls |
| 270 |
{ |
| 271 |
NSMutableArray *methodsArray = [NSMutableArray array]; |
| 272 |
[self getAllMethodsForClass: cls |
| 273 |
intoArray: methodsArray]; |
| 274 |
return [methodsArray copy]; |
| 275 |
} |
| 276 |
|
| 277 |
+ (NSArray *) skippedKeys |
| 278 |
{ |
| 279 |
return [NSArray arrayWithObjects: @"context", // @"buildConfigurationList", @"buildConfigurations", |
| 280 |
@"array", @"valueforKey", @"objectatIndexedSubscript", @"totalFiles", |
| 281 |
@"filename", @"currentFile", @"parameter", @"showEnvVarsInLog", nil]; |
| 282 |
} |
| 283 |
|
| 284 |
- (NSArray *) keysForObject: (id)object |
| 285 |
{ |
| 286 |
NSArray *methods = [NSObject recursiveGetAllMethodsForClass: [object class]]; |
| 287 |
NSEnumerator *en = [methods objectEnumerator]; |
| 288 |
NSString *selectorName = nil; |
| 289 |
NSMutableArray *result = [NSMutableArray arrayWithCapacity: [methods count]]; |
| 290 |
|
| 291 |
while ((selectorName = [en nextObject]) != nil) |
| 292 |
{ |
| 293 |
if ([selectorName hasPrefix: @"set"] && [selectorName isEqualToString: @"settings"] == NO) |
| 294 |
{ |
| 295 |
NSString *keyName = [selectorName substringFromIndex: 3]; |
| 296 |
|
| 297 |
keyName = [keyName stringByReplacingOccurrencesOfString: @":" withString: @""]; |
| 298 |
keyName = [keyName lowercaseFirstCharacter]; |
| 299 |
[result addObject: keyName]; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
return result; |
| 304 |
} |
| 305 |
|
| 306 |
- (NSDictionary *) recursiveKeysAndValuesForObject: (id)object |
| 307 |
{ |
| 308 |
NSMutableDictionary *keysAndValues = nil; |
| 309 |
|
| 310 |
if (object && [object isKindOfClass: [NSNull class]] == NO) |
| 311 |
{ |
| 312 |
NSArray *properties = [self keysForObject: object]; |
| 313 |
NSEnumerator *pen = [properties objectEnumerator]; |
| 314 |
id key = nil; |
| 315 |
|
| 316 |
keysAndValues = [NSMutableDictionary dictionary]; |
| 317 |
while ((key = [pen nextObject]) != nil) |
| 318 |
{ |
| 319 |
if ([[NSObject skippedKeys] containsObject: key]) |
| 320 |
{ |
| 321 |
continue; |
| 322 |
} |
| 323 |
|
| 324 |
NS_DURING |
| 325 |
{ |
| 326 |
id value = [object valueForKey: key]; |
| 327 |
|
| 328 |
if ([value isKindOfClass: [NSArray class]]) |
| 329 |
{ |
| 330 |
NSMutableArray *arrayValues = [NSMutableArray array]; |
| 331 |
NSEnumerator *en = [value objectEnumerator]; |
| 332 |
id item = nil; |
| 333 |
|
| 334 |
while ((item = [en nextObject]) != nil) |
| 335 |
{ |
| 336 |
[arrayValues addObject: [self recursiveKeysAndValuesForObject: item]]; |
| 337 |
} |
| 338 |
|
| 339 |
[keysAndValues setObject: arrayValues |
| 340 |
forKey: key]; |
| 341 |
} |
| 342 |
else if ([value isKindOfClass: [NSDictionary class]] // add a dictionary representing a class... |
| 343 |
&& [value objectForKey: @"isa"] != nil) |
| 344 |
{ |
| 345 |
NSMutableDictionary *dictValues = [NSMutableDictionary dictionary]; |
| 346 |
NSEnumerator *en = [value keyEnumerator]; |
| 347 |
id dictKey = nil; |
| 348 |
|
| 349 |
while ((dictKey = [en nextObject]) != nil) |
| 350 |
{ |
| 351 |
id dictValue = [value objectForKey: dictKey]; |
| 352 |
[dictValues setObject: [self recursiveKeysAndValuesForObject: dictValue] |
| 353 |
forKey: dictKey]; |
| 354 |
} |
| 355 |
|
| 356 |
[keysAndValues setObject: dictValues |
| 357 |
forKey: key]; |
| 358 |
} |
| 359 |
else if ([value isKindOfClass: [NSDictionary class]] // add a simple dictionary... |
| 360 |
&& [value objectForKey: @"isa"] == nil) |
| 361 |
{ |
| 362 |
[keysAndValues setObject: value |
| 363 |
forKey: key]; |
| 364 |
} |
| 365 |
else if ([value isKindOfClass: [NSObject class]] |
| 366 |
&& ![value isKindOfClass: [NSString class]] |
| 367 |
&& ![value isKindOfClass: [NSNumber class]]) |
| 368 |
{ |
| 369 |
[keysAndValues setObject: NSStringFromClass([object class]) |
| 370 |
forKey: @"isa"]; |
| 371 |
[keysAndValues setObject: [self recursiveKeysAndValuesForObject: value] |
| 372 |
forKey: key]; |
| 373 |
} |
| 374 |
else |
| 375 |
{ |
| 376 |
[keysAndValues setObject: NSStringFromClass([object class]) |
| 377 |
forKey: @"isa"]; |
| 378 |
if (value) |
| 379 |
{ |
| 380 |
[keysAndValues setObject: value |
| 381 |
forKey: key]; |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
NS_HANDLER |
| 386 |
{ |
| 387 |
NSLog(@"Exception %@ while retrieving value for key '%@' on class %@", localException, key, |
| 388 |
NSStringFromClass([object class])); |
| 389 |
} |
| 390 |
NS_ENDHANDLER; |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
// NSLog(@"missingKeys are %@", missingKeys); |
| 395 |
return keysAndValues; |
| 396 |
} |
| 397 |
|
| 398 |
- (NSDictionary *) allKeysAndValues |
| 399 |
{ |
| 400 |
id r = flattenPlist([self recursiveKeysAndValuesForObject: self]); |
| 401 |
NSMutableDictionary *d = [NSMutableDictionary dictionaryWithDictionary: r]; |
| 402 |
return moveContainerProperties(d); |
| 403 |
} |
| 404 |
|
| 405 |
@end |