forked from keysight-eggplant/libs-xcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPBXResourcesBuildPhase.m
186 lines (162 loc) · 6.68 KB
/
PBXResourcesBuildPhase.m
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
#import "PBXCommon.h"
#import "PBXResourcesBuildPhase.h"
#import "PBXFileReference.h"
#import "PBXBuildFile.h"
#import "PBXVariantGroup.h"
#import "NSString+PBXAdditions.h"
#import "GSXCBuildContext.h"
extern char **environ;
@implementation PBXResourcesBuildPhase
- (instancetype) init
{
self = [super init];
if (self != nil)
{
NSArray *objs = nil;
objs = [[[GSXCBuildContext sharedBuildContext] objectForKey: @"objects"] allValues];
ASSIGNCOPY(files, objs);
}
return self;
}
- (BOOL) processInfoPlistInput: (NSString *)inputFileName
output: (NSString *)outputFileName
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSString *inputFileString = [NSString stringWithContentsOfFile: inputFileName];
NSString *outputFileString = nil;
ASSIGNCOPY(outputFileString, inputFileString);
// Get env vars...
for (char **env = environ; *env != 0; env++)
{
char *thisEnv = *env;
NSString *envStr = [NSString stringWithCString: thisEnv encoding: NSUTF8StringEncoding];
NSArray *components = [envStr componentsSeparatedByString: @"="];
[dict setObject: [components lastObject]
forKey: [components firstObject]];
}
// Replace all variables in the plist with the values...
NSDebugLog(@"%@", dict);
NSArray *keys = [dict allKeys];
NSEnumerator *en = [keys objectEnumerator];
NSString *k = nil;
while ((k = [en nextObject]) != nil)
{
NSString *v = [dict objectForKey: k];
outputFileString = [outputFileString stringByReplacingOccurrencesOfString: [NSString stringWithFormat: @"$(%@)",k]
withString: v];
}
[outputFileString writeToFile: outputFileName
atomically: YES
encoding: NSUTF8StringEncoding
error: NULL];
NSDebugLog(@"%@", outputFileString);
return YES;
}
- (BOOL) build
{
puts("=== Executing Resources Build Phase");
NSFileManager *mgr = [NSFileManager defaultManager];
NSString *productOutputDir = [NSString stringWithCString: getenv("PRODUCT_OUTPUT_DIR")];
NSString *resourcesDir = [productOutputDir stringByAppendingPathComponent: @"Resources"];
NSError *error = nil;
// Pre create directory....
[[NSFileManager defaultManager] createDirectoryAtPath:resourcesDir
withIntermediateDirectories:YES
attributes:nil
error:&error];
// Copy all resources...
NSEnumerator *en = [files objectEnumerator];
BOOL result = YES;
id file = nil;
while((file = [en nextObject]) != nil && result)
{
id fileRef = [file fileRef];
NSDebugLog(@"fileRef = %@", fileRef);
if ([fileRef isKindOfClass: [PBXVariantGroup class]])
{
NSArray *children = [fileRef children];
NSEnumerator *e = [children objectEnumerator];
id child = nil;
while ((child = [e nextObject]) != nil)
{
NSDebugLog(@"\t%@", child);
NSDebugLog(@"child = %@", child);
NSString *filePath = [child path];
NSString *fileDir = [resourcesDir stringByAppendingPathComponent:
[filePath stringByDeletingLastPathComponent]];
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];
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(@"\t(create error = %@)", error);
}
NSDebugLog(@"\tCopy child %@ -> %@",filePath,destPath);
copyResult = [[NSFileManager defaultManager] copyItemAtPath: filePath
toPath: destPath
error: &error];
if (copyResult == NO)
{
NSLog(@"\t(error = %@)", error);
}
}
continue;
}
NSString *filePath = [file path];
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];
destPath = [resourcesDir stringByAppendingPathComponent: dirs];
destPath = [destPath stringByAppendingPathComponent: fileName];
}
NSDebugLog(@"\tX Copy %@ -> %@",filePath,destPath);
copyResult = [[NSFileManager defaultManager] copyItemAtPath: filePath
toPath: destPath
error: &error];
if(!copyResult)
{
NSDebugLog(@"\tCopy Error: %@ copying %@ -> %@",[error localizedDescription],
filePath, destPath);
}
}
// Handle Info.plist....
NSString *inputPlist = [[NSString stringWithCString:
getenv("INFOPLIST_FILE")] lastPathComponent];
NSString *outputPlist = [resourcesDir
stringByAppendingPathComponent: @"Info-gnustep.plist"];
[self processInfoPlistInput: inputPlist
output: outputPlist];
// Move Base.lproj to English.lproj until Base.lproj is supported..
NSString *baseLproj = [resourcesDir
stringByAppendingPathComponent: @"Base.lproj"];
NSString *engLproj = [resourcesDir
stringByAppendingPathComponent: @"English.lproj"];
[mgr moveItemAtPath: baseLproj
toPath: engLproj
error: NULL];
puts("=== Resources Build Phase Completed");
return result;
}
@end