-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathxcodeSticker.ts
More file actions
409 lines (349 loc) · 12.3 KB
/
xcodeSticker.ts
File metadata and controls
409 lines (349 loc) · 12.3 KB
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
import { type XcodeProject } from "expo/config-plugins";
import path from "path";
import util from "util";
// @ts-ignore
import pbxFile from "xcode/lib/pbxFile";
export function getMainPBXGroup(proj: XcodeProject) {
const project = proj.pbxProjectSection()[proj.getFirstProject().uuid];
if (!project || !project.mainGroup) {
return null;
}
const groupObj = proj.hash.project.objects.PBXGroup[project.mainGroup];
if (!groupObj) {
return null;
}
return { id: project.mainGroup, obj: groupObj };
}
export function addStickerResourceFile(
proj: XcodeProject,
path: string,
rootFolderName: string,
) {
const opt: Record<string, any> = {};
let file = new pbxFile(path, opt);
if (proj.hasFile(file.path)) {
return false;
}
file.uuid = proj.generateUuid();
file.target = opt ? opt.target : undefined;
correctForResourcesPath(file, proj);
file.fileRef = proj.generateUuid();
// create stickers group
const stickersKey = proj.pbxCreateGroup(
// Without quotes, this breaks the xcode project
`"${rootFolderName}"`,
`"${rootFolderName}"`,
);
proj.addToPbxBuildFileSection(file); // PBXBuildFile
// proj.addToPbxResourcesBuildPhase(file); // PBXResourcesBuildPhase
// ^ the above was written as a shortcut, I guess nobody expected there to be another BuildPhase
// var self = proj;
const addToPbxStickersBuildPhase = function (file: any) {
// use the name Stickers instead of Resources to identify the new BuildPhase
const sources = proj.buildPhaseObject(
"PBXResourcesBuildPhase",
// "Resources",
rootFolderName,
// Resources,
file.target,
);
sources.files.push(pbxBuildPhaseObj(file));
};
addToPbxStickersBuildPhase(file);
// PBXFileReference
proj.addToPbxFileReferenceSection(file);
proj.addToPbxGroup(file, stickersKey);
// Push the Stickers Info.plist
file = new pbxFile("Info.plist", opt);
if (proj.hasFile(file.path)) {
return false;
}
file.uuid = proj.generateUuid();
correctForResourcesPath(file, proj, rootFolderName);
file.fileRef = proj.generateUuid();
// PBXFileReference
proj.addToPbxFileReferenceSection(file);
proj.addToPbxGroup(file, stickersKey);
return stickersKey;
}
const isaXCBuildConfiguration = "XCBuildConfiguration";
const pbxTargetDependency = "PBXTargetDependency";
const pbxContainerItemProxy = "PBXContainerItemProxy";
function getMainTargetDevelopmentTeam(
proj: XcodeProject,
): string | undefined {
const configs = proj.pbxXCBuildConfigurationSection();
for (const key in configs) {
const config = configs[key];
if (typeof config === "string") continue;
const bs = config.buildSettings;
if (!bs?.PRODUCT_NAME) continue;
const productName = bs.PRODUCT_NAME.replace(/"/g, "");
if (
productName.includes("Extension") ||
productName.includes("Widget") ||
productName.includes("Sticker")
) {
continue;
}
const team = bs.DEVELOPMENT_TEAM?.replace(/"/g, "");
if (team) {
return team;
}
}
return undefined;
}
export function addStickersTarget(
proj: XcodeProject,
name: string,
bundleId: string,
subfolder: string,
stickerBundleId?: string,
) {
// Setup uuid and name of new target
const targetUuid = proj.generateUuid();
const targetType = "app_extension_messages_sticker_pack";
const targetName = name.trim();
const bundleName = subfolder.trim().split(" ").join("-");
// Check type against list of allowed target types
if (!targetName) {
throw new Error("Target name missing.");
}
// Check type against list of allowed target types
if (!targetType) {
throw new Error("Target type missing.");
}
// Check type against list of allowed target types
if (!producttypeForTargettype(targetType)) {
throw new Error("Target type invalid: " + targetType);
}
// Either use the given bundleId for the stickers or
// generate one from the app bundleId and stickers project folder
const PRODUCT_BUNDLE_IDENTIFIER =
stickerBundleId ?? `"${bundleId}.${bundleName}"`;
const INFOPLIST_FILE = `"${subfolder}/Info.plist"`;
const developmentTeam = getMainTargetDevelopmentTeam(proj);
const commonBuildSettings: Record<string, string> = {
ASSETCATALOG_COMPILER_APPICON_NAME: '"iMessage App Icon"',
CLANG_ANALYZER_NONNULL: "YES",
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION: "YES_AGGRESSIVE",
CLANG_CXX_LANGUAGE_STANDARD: '"gnu++14"',
CLANG_ENABLE_OBJC_WEAK: "YES",
CLANG_WARN_DOCUMENTATION_COMMENTS: "YES",
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER: "YES",
CLANG_WARN_UNGUARDED_AVAILABILITY: "YES_AGGRESSIVE",
CODE_SIGN_STYLE: "Automatic",
DEBUG_INFORMATION_FORMAT: "dwarf",
GCC_C_LANGUAGE_STANDARD: "gnu11",
INFOPLIST_FILE,
IPHONEOS_DEPLOYMENT_TARGET: "14.5",
MTL_FAST_MATH: "YES",
PRODUCT_BUNDLE_IDENTIFIER,
PRODUCT_NAME: `"$(TARGET_NAME)"`,
SKIP_INSTALL: "YES",
TARGETED_DEVICE_FAMILY: `"1,2"`,
};
if (developmentTeam) {
commonBuildSettings.DEVELOPMENT_TEAM = developmentTeam;
}
// Build Configuration: Create
const buildConfigurationsList = [
{
name: "Debug",
isa: isaXCBuildConfiguration,
buildSettings: {
...commonBuildSettings,
MTL_ENABLE_DEBUG_INFO: "INCLUDE_SOURCE",
},
},
{
name: "Release",
isa: isaXCBuildConfiguration,
buildSettings: {
...commonBuildSettings,
COPY_PHASE_STRIP: "NO",
},
},
];
const existing = proj.hash.project.objects[isaXCBuildConfiguration];
for (const [, config] of Object.entries(existing)) {
if (typeof config === "string") continue;
if (
(config as any).buildSettings.ASSETCATALOG_COMPILER_APPICON_NAME &&
(config as any).buildSettings.ASSETCATALOG_COMPILER_APPICON_NAME.match(
/iMessage App Icon/,
)
) {
// Has existing setup...
// TODO: sync old values with potentially new values...
return false;
}
}
// Build Configuration: Add
const buildConfigurations = proj.addXCConfigurationList(
buildConfigurationsList,
"Release",
`Build configuration list for PBXNativeTarget ${quoted(targetName)} `,
);
// Product: Create
const productName = targetName;
const productType = producttypeForTargettype(targetType);
const productFileType = filetypeForProducttype(productType);
const productFile = proj.addProductFile(productName, {
group: "Embed App Extensions",
target: targetUuid,
explicitFileType: productFileType,
});
// stickers
productFile.settings = productFile.settings || {};
productFile.settings.ATTRIBUTES = ["RemoveHeadersOnCopy"];
// Product: Add to build file list
proj.addToPbxBuildFileSection(productFile);
const strippedTargetName = path.basename(targetName, ".appex").trim();
const quotedTargetName = quoted(strippedTargetName);
// Target: Create
const target = {
uuid: targetUuid,
pbxNativeTarget: {
isa: "PBXNativeTarget",
name: quotedTargetName,
productName: quotedTargetName,
productReference: productFile.fileRef,
productType: quoted(producttypeForTargettype(targetType)),
buildConfigurationList: buildConfigurations.uuid,
buildPhases: [],
buildRules: [],
dependencies: [],
},
};
// Target: Add to PBXNativeTarget section
proj.addToPbxNativeTargetSection(target);
// Product: Embed (only for "extension"-type targets)
// Create CopyFiles phase in first target
const { buildPhase } = proj.addBuildPhase(
[],
"PBXCopyFilesBuildPhase",
"Embed App Extensions",
proj.getFirstTarget().uuid,
// targetType,
"app_extension",
);
// TODO: Add to https://github.com/apache/cordova-node-xcode/blob/8b98cabc5978359db88dc9ff2d4c015cba40f150/lib/pbxProject.js#L1604
buildPhase.dstSubfolderSpec = 13;
addToPbxCopyfilesBuildPhase(proj, productFile, "Embed App Extensions");
// need to add another buildphase
// filePathsArray, buildPhaseType, comment, target
proj.addBuildPhase([], "PBXResourcesBuildPhase", subfolder, targetUuid);
// Target: Add uuid to root project
proj.addToPbxProjectSection(target);
// const pbxTargetDependencySection = proj.hash.project.objects[pbxTargetDependency];
// These need to be defined in projects that don't have them already
if (!proj.hash.project.objects[pbxTargetDependency]) {
proj.hash.project.objects[pbxTargetDependency] = {};
}
if (!proj.hash.project.objects[pbxContainerItemProxy]) {
proj.hash.project.objects[pbxContainerItemProxy] = {};
}
proj.addTargetDependency(proj.getFirstTarget().uuid, [target.uuid]);
// Set the creation tools and provisioning....
if (
!proj.pbxProjectSection()[proj.getFirstProject().uuid].attributes
.TargetAttributes
) {
proj.pbxProjectSection()[
proj.getFirstProject().uuid
].attributes.TargetAttributes = {};
}
const targetAttributes: Record<string, string> = {
CreatedOnToolsVersion: "12.5",
ProvisioningStyle: "Automatic",
};
if (developmentTeam) {
targetAttributes.DevelopmentTeam = developmentTeam;
}
proj.pbxProjectSection()[
proj.getFirstProject().uuid
].attributes.TargetAttributes[target.uuid] = targetAttributes;
return target;
}
type PBXFile = any;
// Copied over from xcode package for public
function correctForResourcesPath(
file: PBXFile,
project: XcodeProject,
name: string = "Resources",
) {
return correctForPath(file, project, name);
}
function correctForPath(file: PBXFile, project: XcodeProject, group: string) {
const r_group_dir = new RegExp("^" + group + "[\\\\/]");
const _group = project.pbxGroupByName(group);
if (_group && _group.path) {
file.path = file.path.replace(r_group_dir, "");
}
return file;
}
function addToPbxCopyfilesBuildPhase(
proj: XcodeProject,
file: PBXFile,
name: string,
) {
const sources = proj.buildPhaseObject(
"PBXCopyFilesBuildPhase",
name || "Copy Files",
file.target,
);
sources.files.push(pbxBuildPhaseObj(file));
}
function producttypeForTargettype(targetType: string): string {
const PRODUCTTYPE_BY_TARGETTYPE: Record<string, string> = {
application: "com.apple.product-type.application",
app_extension: "com.apple.product-type.app-extension",
bundle: "com.apple.product-type.bundle",
command_line_tool: "com.apple.product-type.tool",
dynamic_library: "com.apple.product-type.library.dynamic",
framework: "com.apple.product-type.framework",
static_library: "com.apple.product-type.library.static",
unit_test_bundle: "com.apple.product-type.bundle.unit-test",
watch_app: "com.apple.product-type.application.watchapp",
watch2_app: "com.apple.product-type.application.watchapp2",
watch_extension: "com.apple.product-type.watchkit-extension",
watch2_extension: "com.apple.product-type.watchkit2-extension",
// Custom
app_extension_messages_sticker_pack:
"com.apple.product-type.app-extension.messages-sticker-pack",
};
return PRODUCTTYPE_BY_TARGETTYPE[targetType];
}
function filetypeForProducttype(productType: string) {
const FILETYPE_BY_PRODUCTTYPE: Record<string, string> = {
"com.apple.product-type.application": '"wrapper.application"',
"com.apple.product-type.app-extension": '"wrapper.app-extension"',
"com.apple.product-type.bundle": '"wrapper.plug-in"',
"com.apple.product-type.tool": '"compiled.mach-o.dylib"',
"com.apple.product-type.library.dynamic": '"compiled.mach-o.dylib"',
"com.apple.product-type.framework": '"wrapper.framework"',
"com.apple.product-type.library.static": '"archive.ar"',
"com.apple.product-type.bundle.unit-test": '"wrapper.cfbundle"',
"com.apple.product-type.application.watchapp": '"wrapper.application"',
"com.apple.product-type.application.watchapp2": '"wrapper.application"',
"com.apple.product-type.watchkit-extension": '"wrapper.app-extension"',
"com.apple.product-type.watchkit2-extension": '"wrapper.app-extension"',
// Custom
"com.apple.product-type.app-extension.messages-sticker-pack":
'"wrapper.app-extension"',
};
return FILETYPE_BY_PRODUCTTYPE[productType];
}
function pbxBuildPhaseObj(file: PBXFile) {
const obj = Object.create(null);
obj.value = file.uuid;
obj.comment = longComment(file);
return obj;
}
function longComment(file: PBXFile) {
return util.format("%s in %s", file.basename, file.group);
}
function quoted(str: string) {
return util.format(`"%s"`, str);
}