| 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 <Foundation/NSJSONSerialization.h> |
| 26 |
|
| 27 |
#import "PBXCommon.h" |
| 28 |
#import "PBXGroup.h" |
| 29 |
#import "PBXResourcesBuildPhase.h" |
| 30 |
#import "PBXFileReference.h" |
| 31 |
#import "PBXBuildFile.h" |
| 32 |
#import "PBXVariantGroup.h" |
| 33 |
#import "NSString+PBXAdditions.h" |
| 34 |
#import "GSXCBuildContext.h" |
| 35 |
#import "XCBuildConfiguration.h" |
| 36 |
#import "XCConfigurationList.h" |
| 37 |
|
| 38 |
@implementation PBXResourcesBuildPhase |
| 39 |
|
| 40 |
- (instancetype) init |
| 41 |
{ |
| 42 |
self = [super init]; |
| 43 |
if (self != nil) |
| 44 |
{ |
| 45 |
NSArray *objs = nil; |
| 46 |
objs = [[[GSXCBuildContext sharedBuildContext] |
| 47 |
objectForKey: @"objects"] |
| 48 |
allValues]; |
| 49 |
|
| 50 |
[self setFiles: [objs mutableCopy]]; |
| 51 |
} |
| 52 |
return self; |
| 53 |
} |
| 54 |
|
| 55 |
- (NSString *) productName |
| 56 |
{ |
| 57 |
GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext]; |
| 58 |
NSDictionary *ctx = [context currentContext]; |
| 59 |
XCConfigurationList *xcl = [ctx objectForKey: @"buildConfig"]; |
| 60 |
XCBuildConfiguration *xbc = [xcl defaultConfiguration]; |
| 61 |
NSDictionary *bs = [xbc buildSettings]; |
| 62 |
NSString *productName = [bs objectForKey: @"PRODUCT_NAME"]; |
| 63 |
|
| 64 |
// NSProcessInfo *info = [NSProcessInfo processInfo]; |
| 65 |
// NSDictionary *env = [info environment]; |
| 66 |
NSDebugLog(@"bs = %@", bs); |
| 67 |
|
| 68 |
// This is kind of a kludge, but better than what was here before. |
| 69 |
// I believe that when the context has the variable name it means to use |
| 70 |
// the product name from the target. |
| 71 |
if ([productName isEqualToString: @"$(TARGET_NAME)"]) |
| 72 |
{ |
| 73 |
productName = [_target productName]; |
| 74 |
NSDebugLog(@"* 2nd try %@", productName); |
| 75 |
if ([productName isEqualToString: @"$(TARGET_NAME)"]) |
| 76 |
{ |
| 77 |
productName = [_target name]; |
| 78 |
NSDebugLog(@"* 3rd try %@", productName); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
return productName; |
| 83 |
} |
| 84 |
|
| 85 |
- (NSString *) processAssets |
| 86 |
{ |
| 87 |
NSFileManager *mgr = [NSFileManager defaultManager]; |
| 88 |
NSString *filename = nil; |
| 89 |
NSString *productName = [_target name]; // [self productName]; |
| 90 |
NSString *assetsDir = [productName stringByAppendingPathComponent: @"Assets.xcassets"]; |
| 91 |
NSString *appIconDir = [assetsDir stringByAppendingPathComponent: @"AppIcon.appiconset"]; |
| 92 |
NSString *contentsJson = [appIconDir stringByAppendingPathComponent: @"Contents.json"]; |
| 93 |
NSData *data = [NSData dataWithContentsOfFile: contentsJson]; |
| 94 |
|
| 95 |
if (data != nil) |
| 96 |
{ |
| 97 |
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData: data |
| 98 |
options: 0L |
| 99 |
error: NULL]; |
| 100 |
NSArray *imagesArray = [dict objectForKey: @"images"]; |
| 101 |
NSDictionary *imageDict = nil; |
| 102 |
NSEnumerator *en = [imagesArray objectEnumerator]; |
| 103 |
|
| 104 |
while ((imageDict = [en nextObject]) != nil) |
| 105 |
{ |
| 106 |
NSString *size = [imageDict objectForKey: @"size"]; |
| 107 |
NSString *scale = [imageDict objectForKey: @"scale"]; |
| 108 |
|
| 109 |
if ([size isEqualToString: @"32x32"] && |
| 110 |
[scale isEqualToString: @"1x"]) |
| 111 |
{ |
| 112 |
filename = [imageDict objectForKey: @"filename"]; |
| 113 |
break; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// Copy icons to resource dir... |
| 118 |
GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext]; |
| 119 |
NSString *productOutputDir = [context objectForKey: @"PRODUCT_OUTPUT_DIR"]; |
| 120 |
NSString *resourcesDir = [productOutputDir stringByAppendingPathComponent: @"Resources"]; |
| 121 |
NSString *imagePath = [appIconDir stringByAppendingPathComponent: filename]; |
| 122 |
NSString *destPath = [resourcesDir stringByAppendingPathComponent: filename]; |
| 123 |
|
| 124 |
// Copy the item, remove it first to make sure there is no issue. |
| 125 |
//[mgr removeItemAtPath: destPath |
| 126 |
// error: NULL]; |
| 127 |
|
| 128 |
[mgr copyItemAtPath: imagePath |
| 129 |
toPath: destPath |
| 130 |
error: NULL]; |
| 131 |
} |
| 132 |
|
| 133 |
return filename; |
| 134 |
} |
| 135 |
|
| 136 |
- (BOOL) processInfoPlistInput: (NSString *)inputFileName |
| 137 |
output: (NSString *)outputFileName |
| 138 |
{ |
| 139 |
if (inputFileName != nil) |
| 140 |
{ |
| 141 |
GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext]; |
| 142 |
NSString *settings = [context objectForKey: @"PRODUCT_SETTINGS_XML"]; |
| 143 |
if(settings == nil) |
| 144 |
{ |
| 145 |
NSString *inputFileString = [NSString stringWithContentsOfFile: inputFileName]; |
| 146 |
NSString *outputFileString = [inputFileString stringByReplacingEnvironmentVariablesWithValues]; |
| 147 |
NSMutableDictionary *plistDict = [NSMutableDictionary dictionaryWithDictionary: [outputFileString propertyList]]; |
| 148 |
NSString *filename = [self processAssets]; |
| 149 |
|
| 150 |
if (filename != nil) |
| 151 |
{ |
| 152 |
[plistDict setObject: filename forKey: @"NSIcon"]; |
| 153 |
} |
| 154 |
|
| 155 |
[plistDict writeToFile: outputFileName |
| 156 |
atomically: YES]; |
| 157 |
|
| 158 |
NSDebugLog(@"%@", plistDict); |
| 159 |
} |
| 160 |
else |
| 161 |
{ |
| 162 |
[settings writeToFile: outputFileName |
| 163 |
atomically: YES |
| 164 |
encoding: NSUTF8StringEncoding |
| 165 |
error: NULL]; |
| 166 |
} |
| 167 |
} |
| 168 |
else |
| 169 |
{ |
| 170 |
NSArray *keys = [NSArray arrayWithObjects: @"NSPrincipalClass", @"NSMainNibFile", nil]; |
| 171 |
NSArray *objs = [NSArray arrayWithObjects: @"NSApplication", @"MainMenu", nil]; |
| 172 |
NSDictionary *ipl = [NSDictionary dictionaryWithObjects: objs |
| 173 |
forKeys: keys]; |
| 174 |
[ipl writeToFile: outputFileName |
| 175 |
atomically: YES]; |
| 176 |
} |
| 177 |
|
| 178 |
return YES; |
| 179 |
} |
| 180 |
|
| 181 |
- (BOOL) copyResourceFrom: (NSString *)srcPath to: (NSString *)dstPath |
| 182 |
{ |
| 183 |
BOOL result = NO; |
| 184 |
NSFileManager *mgr = [NSFileManager defaultManager]; |
| 185 |
NSError *error = nil; |
| 186 |
|
| 187 |
NSDebugLog(@"\t* Copy child %@ -> %@",srcPath,dstPath); |
| 188 |
|
| 189 |
// Copy the item |
| 190 |
result = [mgr copyItemAtPath: srcPath |
| 191 |
toPath: dstPath |
| 192 |
error: &error]; |
| 193 |
if (error != nil) |
| 194 |
{ |
| 195 |
xcputs([[NSString stringWithFormat: @"\t* Updating resource \"%s%@%s\" --> \"%s%@%s\"", CYAN, srcPath, RESET, GREEN, dstPath, RESET] cString]); |
| 196 |
[mgr removeItemAtPath: dstPath |
| 197 |
error: NULL]; |
| 198 |
|
| 199 |
result = [mgr copyItemAtPath: srcPath |
| 200 |
toPath: dstPath |
| 201 |
error: &error]; |
| 202 |
} |
| 203 |
else |
| 204 |
{ |
| 205 |
xcputs([[NSString stringWithFormat: @"\t* Copy resource \"%s%@%s\" --> \"%s%@%s\"", CYAN, srcPath, RESET, GREEN, dstPath, RESET] cString]); |
| 206 |
} |
| 207 |
return result; |
| 208 |
} |
| 209 |
|
| 210 |
- (BOOL) build |
| 211 |
{ |
| 212 |
xcputs("=== Executing Resources Build Phase"); |
| 213 |
NSFileManager *mgr = [NSFileManager defaultManager]; |
| 214 |
GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext]; |
| 215 |
NSString *productOutputDir = [context objectForKey: @"PRODUCT_OUTPUT_DIR"]; // [NSString stringWithCString: getenv("PRODUCT_OUTPUT_DIR")]; |
| 216 |
NSString *resourcesDir = [productOutputDir stringByAppendingPathComponent: @"Resources"]; |
| 217 |
NSError *error = nil; |
| 218 |
NSString *productName = [self productName]; // @""; // [_target productName]; |
| 219 |
|
| 220 |
NSDebugLog(@"productName = %@", productName); |
| 221 |
|
| 222 |
// Pre create directory.... |
| 223 |
[mgr createDirectoryAtPath:resourcesDir |
| 224 |
withIntermediateDirectories:YES |
| 225 |
attributes:nil |
| 226 |
error:&error]; |
| 227 |
|
| 228 |
// Copy all resources... |
| 229 |
NSArray *synchronizedFiles = [_target synchronizedResources]; |
| 230 |
NSArray *files = [_files arrayByAddingObjectsFromArray: synchronizedFiles]; |
| 231 |
|
| 232 |
NSEnumerator *en = [files objectEnumerator]; |
| 233 |
BOOL result = YES; |
| 234 |
id file = nil; |
| 235 |
while((file = [en nextObject]) != nil && result) |
| 236 |
{ |
| 237 |
id fileRef = [file fileRef]; |
| 238 |
|
| 239 |
if ([fileRef isKindOfClass: [PBXVariantGroup class]]) |
| 240 |
{ |
| 241 |
NSArray *children = [fileRef children]; |
| 242 |
NSEnumerator *e = [children objectEnumerator]; |
| 243 |
id child = nil; |
| 244 |
while ((child = [e nextObject]) != nil) |
| 245 |
{ |
| 246 |
NSString *filePath = [child path]; |
| 247 |
NSDebugLog(@"FILEPATH = %@", filePath); |
| 248 |
NSString *resourceFilePath = [filePath stringByDeletingLastPathComponent]; |
| 249 |
BOOL edited = NO; |
| 250 |
if ([mgr fileExistsAtPath: [child path]] == NO) |
| 251 |
{ |
| 252 |
edited = YES; |
| 253 |
filePath = [productName stringByAppendingPathComponent: [child path]]; |
| 254 |
if ([mgr fileExistsAtPath: filePath] == NO) |
| 255 |
{ |
| 256 |
filePath = [child buildPath]; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
NSString *fileDir = [resourcesDir stringByAppendingPathComponent: |
| 261 |
resourceFilePath]; |
| 262 |
NSString *fileName = [filePath lastPathComponent]; |
| 263 |
NSString *destPath = [resourcesDir stringByAppendingPathComponent: fileName]; |
| 264 |
NSError *error = nil; |
| 265 |
BOOL copyResult = NO; |
| 266 |
|
| 267 |
// If there is more than one path component... |
| 268 |
// then the intervening directories need to |
| 269 |
// be created. |
| 270 |
if([[filePath pathComponents] count] > 1) |
| 271 |
{ |
| 272 |
NSString *dirs = [filePath stringByDeletingLastPathComponent]; |
| 273 |
if (edited) |
| 274 |
{ |
| 275 |
dirs = [dirs stringByReplacingOccurrencesOfString: productName withString: @""]; |
| 276 |
} |
| 277 |
destPath = [resourcesDir stringByAppendingPathComponent: dirs]; |
| 278 |
destPath = [destPath stringByAppendingPathComponent: fileName]; |
| 279 |
} |
| 280 |
|
| 281 |
NSDebugLog(@"\tCreate %@",fileDir); |
| 282 |
copyResult = [mgr createDirectoryAtPath: fileDir |
| 283 |
withIntermediateDirectories: YES |
| 284 |
attributes: nil |
| 285 |
error: &error]; |
| 286 |
if (copyResult == NO) |
| 287 |
{ |
| 288 |
NSLog(@"\tFILE CREATION ERROR: %@, %@", error, fileDir); |
| 289 |
} |
| 290 |
|
| 291 |
// kludge since Base/en etc is not supported yet. |
| 292 |
destPath = [destPath stringByReplacingOccurrencesOfString: @"Base.lproj/" |
| 293 |
withString: @""]; |
| 294 |
|
| 295 |
destPath = [destPath stringByReplacingOccurrencesOfString: @"en.lproj/" |
| 296 |
withString: @""]; |
| 297 |
|
| 298 |
copyResult = [self copyResourceFrom: filePath to: destPath]; |
| 299 |
if (copyResult == NO) |
| 300 |
{ |
| 301 |
NSLog(@"\tFILE COPY ERROR: %@", destPath); |
| 302 |
} |
| 303 |
} |
| 304 |
continue; |
| 305 |
} |
| 306 |
|
| 307 |
NSString *filePath = [file path]; |
| 308 |
if ([mgr fileExistsAtPath: [file path]] == NO) |
| 309 |
{ |
| 310 |
filePath = [file buildPath]; |
| 311 |
if ([mgr fileExistsAtPath: filePath] == NO) |
| 312 |
{ |
| 313 |
filePath = [productName stringByAppendingPathComponent: [file path]]; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
NSString *fileName = [filePath lastPathComponent]; |
| 318 |
NSString *destPath = [resourcesDir stringByAppendingPathComponent: fileName]; |
| 319 |
BOOL copyResult = NO; |
| 320 |
NSDebugLog(@"\tXXXX Copy %@ -> %@",filePath,destPath); |
| 321 |
|
| 322 |
copyResult = [self copyResourceFrom: filePath to: destPath]; |
| 323 |
if (!copyResult) |
| 324 |
{ |
| 325 |
NSLog(@"File not copied: %@ -> %@", filePath, destPath); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
// Handle Info.plist.... |
| 330 |
NSDictionary *ctx = [context currentContext]; |
| 331 |
XCConfigurationList *xcl = [ctx objectForKey: @"buildConfig"]; |
| 332 |
XCBuildConfiguration *xbc = [xcl defaultConfiguration]; |
| 333 |
NSDictionary *bs = [xbc buildSettings]; |
| 334 |
NSString *infoPlist = [bs objectForKey: @"INFOPLIST_FILE"]; |
| 335 |
|
| 336 |
if ([mgr fileExistsAtPath: infoPlist] == NO) |
| 337 |
{ |
| 338 |
infoPlist = [infoPlist lastPathComponent]; |
| 339 |
} |
| 340 |
|
| 341 |
NSString *outputPlist = [resourcesDir |
| 342 |
stringByAppendingPathComponent: @"Info-gnustep.plist"]; |
| 343 |
[self processInfoPlistInput: infoPlist |
| 344 |
output: outputPlist]; |
| 345 |
|
| 346 |
xcputs("=== Resources Build Phase Completed"); |
| 347 |
fflush(stdout); |
| 348 |
|
| 349 |
return result; |
| 350 |
} |
| 351 |
|
| 352 |
- (BOOL) generate |
| 353 |
{ |
| 354 |
GSXCBuildContext *context = [GSXCBuildContext sharedBuildContext]; |
| 355 |
NSMutableArray *resources = [NSMutableArray arrayWithCapacity: [_files count]]; |
| 356 |
|
| 357 |
xcputs("=== Generating Resources Entries Build Phase"); |
| 358 |
NSFileManager *mgr = [NSFileManager defaultManager]; |
| 359 |
NSString *productName = [_target productName]; |
| 360 |
NSString *appName = [productName stringByDeletingPathExtension]; |
| 361 |
|
| 362 |
// Copy all resources... |
| 363 |
NSEnumerator *en = [_files objectEnumerator]; |
| 364 |
BOOL result = YES; |
| 365 |
id file = nil; |
| 366 |
while((file = [en nextObject]) != nil && result) |
| 367 |
{ |
| 368 |
id fileRef = [file fileRef]; |
| 369 |
if ([fileRef isKindOfClass: [PBXVariantGroup class]]) |
| 370 |
{ |
| 371 |
NSArray *children = [fileRef children]; |
| 372 |
NSEnumerator *e = [children objectEnumerator]; |
| 373 |
id child = nil; |
| 374 |
while ((child = [e nextObject]) != nil) |
| 375 |
{ |
| 376 |
NSString *filePath = [child path]; |
| 377 |
|
| 378 |
if ([mgr fileExistsAtPath: [child path]] == NO) |
| 379 |
{ |
| 380 |
filePath = [productName stringByAppendingPathComponent: [child path]]; |
| 381 |
} |
| 382 |
|
| 383 |
xcputs([[NSString stringWithFormat: @"\tAdd child resource entry %@", filePath] cString]); |
| 384 |
[resources addObject: filePath]; |
| 385 |
} |
| 386 |
continue; |
| 387 |
} |
| 388 |
|
| 389 |
NSString *filePath = [file path]; |
| 390 |
if ([mgr fileExistsAtPath: [file path]] == NO) |
| 391 |
{ |
| 392 |
filePath = [productName stringByAppendingPathComponent: [file path]]; |
| 393 |
} |
| 394 |
|
| 395 |
xcputs([[NSString stringWithFormat: @"\tAdd resource entry %@",filePath] cString]); |
| 396 |
|
| 397 |
[resources addObject: filePath]; |
| 398 |
} |
| 399 |
|
| 400 |
// Handle Info.plist... |
| 401 |
NSDictionary *ctx = [context currentContext]; |
| 402 |
XCConfigurationList *xcl = [ctx objectForKey: @"buildConfig"]; |
| 403 |
XCBuildConfiguration *xbc = [xcl defaultConfiguration]; |
| 404 |
NSDictionary *bs = [xbc buildSettings]; |
| 405 |
NSString *inputPlist = [bs objectForKey: @"INFOPLIST_FILE"]; |
| 406 |
if ([mgr fileExistsAtPath: inputPlist] == NO) |
| 407 |
{ |
| 408 |
inputPlist = [inputPlist lastPathComponent]; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
NSString *outputPlist = [NSString stringWithFormat: @"%@Info.plist",appName] ; |
| 413 |
[self processInfoPlistInput: inputPlist |
| 414 |
output: outputPlist]; |
| 415 |
|
| 416 |
// Move Base.lproj to English.lproj until Base.lproj is supported.. |
| 417 |
// NSString *baseLproj = @"Base.lproj/*"; |
| 418 |
// NSString *engLproj = @"English.lproj"; |
| 419 |
// [resources addObject: engLproj]; |
| 420 |
[resources addObject: outputPlist]; |
| 421 |
|
| 422 |
[context setObject: resources forKey: @"RESOURCES"]; |
| 423 |
xcputs("=== Resources Build Phase Completed (generate)"); |
| 424 |
|
| 425 |
return result; |
| 426 |
} |
| 427 |
|
| 428 |
- (BOOL) link |
| 429 |
{ |
| 430 |
return [self build]; |
| 431 |
} |
| 432 |
|
| 433 |
@end |