forked from NativeScript/nativescript-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathios-signing-service.ts
456 lines (419 loc) · 13 KB
/
ios-signing-service.ts
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import * as path from "path";
import * as mobileProvisionFinder from "ios-mobileprovision-finder";
import {
BUILD_XCCONFIG_FILE_NAME,
iOSAppResourcesFolderName,
IOSNativeTargetProductTypes,
} from "../../constants";
import * as helpers from "../../common/helpers";
import { IOSProvisionService } from "../ios-provision-service";
import { IOSBuildData } from "../../data/build-data";
import {
IProvisioningJSON,
IXcconfigService,
IXcprojService,
} from "../../declarations";
import { IProjectData } from "../../definitions/project";
import { IErrors, IFileSystem } from "../../common/declarations";
import * as _ from "lodash";
import { injector } from "../../common/yok";
import * as constants from "../../constants";
export class IOSSigningService implements IiOSSigningService {
constructor(
private $errors: IErrors,
private $fs: IFileSystem,
private $iOSProvisionService: IOSProvisionService,
private $logger: ILogger,
private $pbxprojDomXcode: IPbxprojDomXcode,
private $prompter: IPrompter,
private $xcconfigService: IXcconfigService,
private $xcprojService: IXcprojService
) {}
public async setupSigningForDevice(
projectRoot: string,
projectData: IProjectData,
iOSBuildData: IOSBuildData
): Promise<void> {
const xcode = this.$pbxprojDomXcode.Xcode.open(
this.getPbxProjPath(projectData, projectRoot)
);
const signing = xcode.getSigning(projectData.projectName);
const hasProvisioningProfileInXCConfig =
this.readXCConfigProvisioningProfileSpecifierForIPhoneOs(projectData) ||
this.readXCConfigProvisioningProfileSpecifier(projectData) ||
this.readXCConfigProvisioningProfileForIPhoneOs(projectData) ||
this.readXCConfigProvisioningProfile(projectData);
if (
hasProvisioningProfileInXCConfig &&
(!signing || signing.style !== "Manual")
) {
xcode.setManualSigningStyle(projectData.projectName);
this.getExtensionNames(projectData).forEach((name) => {
xcode.setManualSigningStyle(name);
});
xcode.save();
} else if (
!iOSBuildData.provision &&
!(signing && signing.style === "Manual" && !iOSBuildData.teamId)
) {
const teamId = await this.getDevelopmentTeam(
projectData,
projectRoot,
iOSBuildData.teamId
);
await this.setupSigningFromTeam(projectRoot, projectData, teamId);
}
}
public async setupSigningFromTeam(
projectRoot: string,
projectData: IProjectData,
teamId: string
): Promise<void> {
const xcode = this.$pbxprojDomXcode.Xcode.open(
this.getPbxProjPath(projectData, projectRoot)
);
const signing = xcode.getSigning(projectData.projectName);
let shouldUpdateXcode = false;
if (signing && signing.style === "Automatic") {
if (signing.team !== teamId) {
// Maybe the provided team is name such as "Telerik AD" and we need to convert it to CH******37
const teamIdsForName =
await this.$iOSProvisionService.getTeamIdsWithName(teamId);
if (!teamIdsForName.some((id) => id === signing.team)) {
shouldUpdateXcode = true;
}
}
} else {
shouldUpdateXcode = true;
}
if (shouldUpdateXcode) {
const teamIdsForName = await this.$iOSProvisionService.getTeamIdsWithName(
teamId
);
if (teamIdsForName.length > 0) {
this.$logger.trace(
`Team id ${teamIdsForName[0]} will be used for team name "${teamId}".`
);
teamId = teamIdsForName[0];
}
xcode.setAutomaticSigningStyle(projectData.projectName, teamId);
xcode.setAutomaticSigningStyleByTargetProductTypesList(
[
IOSNativeTargetProductTypes.appExtension,
IOSNativeTargetProductTypes.watchApp,
IOSNativeTargetProductTypes.watchExtension,
],
teamId
);
this.getExtensionNames(projectData).forEach((name) => {
xcode.setAutomaticSigningStyle(name, teamId);
});
xcode.save();
this.$logger.trace(`Set Automatic signing style and team id ${teamId}.`);
} else {
this.$logger.trace(
`The specified ${teamId} is already set in the Xcode.`
);
}
}
public async setupSigningFromProvision(
projectRoot: string,
projectData: IProjectData,
provision?: string,
mobileProvisionData?: mobileProvisionFinder.provision.MobileProvision
): Promise<void> {
if (!provision) {
// read uuid from Xcode an cache...
return;
}
const xcode = this.$pbxprojDomXcode.Xcode.open(
this.getPbxProjPath(projectData, projectRoot)
);
const signing = xcode.getSigning(projectData.projectName);
let shouldUpdateXcode = false;
if (signing && signing.style === "Manual") {
for (const config in signing.configurations) {
const options = signing.configurations[config];
if (options.name !== provision && options.uuid !== provision) {
shouldUpdateXcode = true;
break;
}
}
} else {
shouldUpdateXcode = true;
}
if (shouldUpdateXcode) {
const projectSigningConfig = await this.getManualSigningConfiguration(
projectData,
provision,
mobileProvisionData
);
xcode.setManualSigningStyle(
projectData.projectName,
projectSigningConfig
);
xcode.setManualSigningStyleByTargetProductTypesList(
[
IOSNativeTargetProductTypes.appExtension,
IOSNativeTargetProductTypes.watchApp,
IOSNativeTargetProductTypes.watchExtension,
],
projectSigningConfig
);
this.$logger.trace(
`Set Manual signing style and provisioning profile: ${projectSigningConfig.name} (${projectSigningConfig.uuid})`
);
const extensionSigningConfig = await Promise.all(
this.getExtensionsManualSigningConfiguration(projectData)
);
extensionSigningConfig.forEach(({ name, configuration }) => {
xcode.setManualSigningStyle(name, configuration);
this.$logger.trace(
`Set Manual signing style and provisioning profile: ${configuration.name} (${configuration.uuid})`
);
});
xcode.save();
// this.cache(uuid);
} else {
this.$logger.trace(
`The specified provisioning profile is already set in the Xcode: ${provision}`
);
}
}
private getExtensionNames(projectData: IProjectData) {
const extensionFolderPath = path.join(
projectData.getAppResourcesDirectoryPath(),
constants.iOSAppResourcesFolderName,
constants.NATIVE_EXTENSION_FOLDER
);
if (this.$fs.exists(extensionFolderPath)) {
const extensionNames = this.$fs
.readDirectory(extensionFolderPath)
.filter((fileName) => {
const extensionPath = path.join(extensionFolderPath, fileName);
const stats = this.$fs.getFsStats(extensionPath);
return stats.isDirectory() && !fileName.startsWith(".");
});
return extensionNames;
}
return [];
}
private getExtensionsManualSigningConfiguration(projectData: IProjectData) {
const provisioningJSONPath = path.join(
projectData.getAppResourcesDirectoryPath(),
constants.iOSAppResourcesFolderName,
constants.NATIVE_EXTENSION_FOLDER,
constants.EXTENSION_PROVISIONING_FILENAME
);
if (this.$fs.exists(provisioningJSONPath)) {
const provisioningJSON = this.$fs.readJson(
provisioningJSONPath
) as IProvisioningJSON;
const extensionNames = this.getExtensionNames(projectData);
const provisioning = Object.entries(provisioningJSON).map(
async ([id, provision]) => {
const name = id.split(".").at(-1);
if (extensionNames.includes(name)) {
const configuration = await this.getManualSigningConfiguration(
projectData,
provision
);
return { name, configuration };
}
return null;
}
);
return provisioning;
}
return [];
}
private async getManualSigningConfiguration(
projectData: IProjectData,
provision: string,
mobileProvisionData?: mobileProvisionFinder.provision.MobileProvision
) {
const pickStart = Date.now();
const mobileprovision =
mobileProvisionData ||
(await this.$iOSProvisionService.pick(
provision,
projectData.projectIdentifiers.ios
));
const pickEnd = Date.now();
this.$logger.trace(
"Searched and " +
(mobileprovision ? "found" : "failed to find ") +
" matching provisioning profile. (" +
(pickEnd - pickStart) +
"ms.)"
);
if (!mobileprovision) {
this.$errors.fail(
"Failed to find mobile provision with UUID or Name: " + provision
);
}
const configuration = {
team:
mobileprovision.TeamIdentifier &&
mobileprovision.TeamIdentifier.length > 0
? mobileprovision.TeamIdentifier[0]
: undefined,
uuid: mobileprovision.UUID,
name: mobileprovision.Name,
identity:
mobileprovision.Type === "Development"
? "iPhone Developer"
: "iPhone Distribution",
};
return configuration;
}
private getBuildXCConfigFilePath(projectData: IProjectData): string {
return path.join(
projectData.appResourcesDirectoryPath,
iOSAppResourcesFolderName,
BUILD_XCCONFIG_FILE_NAME
);
}
private getPbxProjPath(
projectData: IProjectData,
projectRoot: string
): string {
return path.join(
this.$xcprojService.getXcodeprojPath(projectData, projectRoot),
"project.pbxproj"
);
}
private readTeamIdFromFile(projectRoot: string): string | undefined {
try {
const filePath = path.join(projectRoot, "teamid");
if (this.$fs.exists(filePath)) {
return this.$fs.readText(filePath);
}
} catch (e) {
this.$logger.trace("Unable to read file: teamid. Error is: ", e);
}
return undefined;
}
private async getDevelopmentTeam(
projectData: IProjectData,
projectRoot: string,
teamId?: string
): Promise<string> {
teamId = teamId || this.readXCConfigDevelopmentTeam(projectData);
if (!teamId) {
const teams = await this.$iOSProvisionService.getDevelopmentTeams();
this.$logger.warn(
"Xcode requires a team id to be specified when building for device."
);
this.$logger.warn(
"You can specify the team id by setting the DEVELOPMENT_TEAM setting in build.xcconfig file located in App_Resources folder of your app, or by using the --teamId option when calling run, debug or livesync commands."
);
if (teams.length === 1) {
teamId = teams[0].id;
this.$logger.warn(
"Found and using the following development team installed on your system: " +
teams[0].name +
" (" +
teams[0].id +
")"
);
} else if (teams.length > 0) {
if (!helpers.isInteractive()) {
this.$errors.fail(
`Unable to determine default development team. Available development teams are: ${_.map(
teams,
(team) => team.id
)}. Specify team in app/App_Resources/iOS/build.xcconfig file in the following way: DEVELOPMENT_TEAM = <team id>`
);
}
const fromFile = this.readTeamIdFromFile(projectRoot);
if (fromFile) {
const idFromFile = teams.find((value) => value.id === fromFile);
if (idFromFile) {
teamId = idFromFile.id;
this.$logger.info(`Team Id resolved from file: '${teamId}'.`);
}
}
if (!teamId) {
const choices: string[] = [];
for (const team of teams) {
choices.push(team.name + " (" + team.id + ")");
}
const choice = await this.$prompter.promptForChoice(
"Found multiple development teams, select one:",
choices
);
teamId = teams[choices.indexOf(choice)].id;
const choicesPersist = [
"Yes, set the DEVELOPMENT_TEAM setting in build.xcconfig file.",
"Yes, persist the team id in platforms folder.",
"No, don't persist this setting.",
];
const choicePersist = await this.$prompter.promptForChoice(
"Do you want to make teamId: " +
teamId +
" a persistent choice for your app?",
choicesPersist
);
switch (choicesPersist.indexOf(choicePersist)) {
case 0:
const xcconfigFile = path.join(
projectData.appResourcesDirectoryPath,
"iOS",
BUILD_XCCONFIG_FILE_NAME
);
this.$fs.appendFile(
xcconfigFile,
"\nDEVELOPMENT_TEAM = " + teamId + "\n"
);
break;
case 1:
this.$fs.writeFile(path.join(projectRoot, "teamid"), teamId);
break;
default:
break;
}
}
}
}
this.$logger.trace(`Selected teamId is '${teamId}'.`);
return teamId;
}
private readXCConfigDevelopmentTeam(projectData: IProjectData): string {
return this.$xcconfigService.readPropertyValue(
this.getBuildXCConfigFilePath(projectData),
"DEVELOPMENT_TEAM"
);
}
private readXCConfigProvisioningProfile(projectData: IProjectData): string {
return this.$xcconfigService.readPropertyValue(
this.getBuildXCConfigFilePath(projectData),
"PROVISIONING_PROFILE"
);
}
private readXCConfigProvisioningProfileForIPhoneOs(
projectData: IProjectData
): string {
return this.$xcconfigService.readPropertyValue(
this.getBuildXCConfigFilePath(projectData),
"PROVISIONING_PROFILE[sdk=iphoneos*]"
);
}
private readXCConfigProvisioningProfileSpecifier(
projectData: IProjectData
): string {
return this.$xcconfigService.readPropertyValue(
this.getBuildXCConfigFilePath(projectData),
"PROVISIONING_PROFILE_SPECIFIER"
);
}
private readXCConfigProvisioningProfileSpecifierForIPhoneOs(
projectData: IProjectData
): string {
return this.$xcconfigService.readPropertyValue(
this.getBuildXCConfigFilePath(projectData),
"PROVISIONING_PROFILE_SPECIFIER[sdk=iphoneos*]"
);
}
}
injector.register("iOSSigningService", IOSSigningService);