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