forked from NativeScript/nativescript-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitmstransporter-service.ts
292 lines (260 loc) · 8.24 KB
/
itmstransporter-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
import * as path from "path";
import { ITMSConstants, INFO_PLIST_FILE_NAME } from "../constants";
import { quoteString } from "../common/helpers";
import { cache } from "../common/decorators";
import { IITMSTransporterService, IITMSData } from "../declarations";
import { IProjectData } from "../definitions/project";
import {
IChildProcess,
IErrors,
IFileSystem,
IPlistParser,
IXcodeSelectService,
} from "../common/declarations";
import { IApplePortalApplicationService } from "./apple-portal/definitions";
import { IInjector } from "../common/definitions/yok";
import { injector } from "../common/yok";
import { ITempService } from "../definitions/temp-service";
export class ITMSTransporterService implements IITMSTransporterService {
constructor(
private $applePortalApplicationService: IApplePortalApplicationService,
private $childProcess: IChildProcess,
private $errors: IErrors,
private $fs: IFileSystem,
private $injector: IInjector,
private $logger: ILogger,
private $plistParser: IPlistParser,
private $xcodeSelectService: IXcodeSelectService,
private $tempService: ITempService
) {}
private get $projectData(): IProjectData {
return this.$injector.resolve("projectData");
}
public async validate(appSpecificPassword?: string): Promise<void> {
const itmsTransporterPath = await this.getITMSTransporterPath();
const version = await this.$xcodeSelectService.getXcodeVersion();
if (+version.major < 14) {
if (!this.$fs.exists(itmsTransporterPath)) {
this.$errors.fail(
"iTMS Transporter not found on this machine - make sure your Xcode installation is not damaged."
);
}
} else {
const altoolPath = await this.getAltoolPath();
if (!this.$fs.exists(altoolPath)) {
this.$errors.fail(
"altool not found on this machine - make sure your Xcode installation is not damaged."
);
}
if (!appSpecificPassword) {
this.$errors.fail(
"An app-specific password is required from xCode versions 14 and above, Use the --appleApplicationSpecificPassword to supply it."
);
}
}
}
public async upload(data: IITMSData): Promise<void> {
const version = await this.$xcodeSelectService.getXcodeVersion();
if (+version.major < 14) {
await this.upload_iTMSTransporter(data);
} else {
await this.upload_altool(data);
}
}
public async upload_iTMSTransporter(data: IITMSData): Promise<void> {
const itmsTransporterPath = await this.getITMSTransporterPath();
const ipaFileName = "app.ipa";
const itmsDirectory = await this.$tempService.mkdirSync("itms-");
const innerDirectory = path.join(itmsDirectory, "mybundle.itmsp");
const ipaFileLocation = path.join(innerDirectory, ipaFileName);
const loggingLevel = data.verboseLogging
? ITMSConstants.VerboseLoggingLevels.Verbose
: ITMSConstants.VerboseLoggingLevels.Informational;
const bundleId = await this.getBundleIdentifier(data);
const application = await this.$applePortalApplicationService.getApplicationByBundleId(
data.user,
bundleId
);
this.$fs.createDirectory(innerDirectory);
this.$fs.copyFile(data.ipaFilePath, ipaFileLocation);
const ipaFileHash = await this.$fs.getFileShasum(ipaFileLocation, {
algorithm: "md5",
});
const ipaFileSize = this.$fs.getFileSize(ipaFileLocation);
const metadata = this.getITMSMetadataXml(
application.adamId,
ipaFileName,
ipaFileHash,
ipaFileSize
);
this.$fs.writeFile(
path.join(innerDirectory, ITMSConstants.ApplicationMetadataFile),
metadata
);
const password = data.user.isTwoFactorAuthenticationEnabled
? data.applicationSpecificPassword
: data.credentials.password;
await this.$childProcess.spawnFromEvent(
itmsTransporterPath,
[
"-m",
"upload",
"-f",
itmsDirectory,
"-u",
quoteString(data.credentials.username),
"-p",
quoteString(password),
"-v",
loggingLevel,
],
"close",
{ stdio: "inherit" }
);
}
public async upload_altool(data: IITMSData): Promise<void> {
const altoolPath = await this.getAltoolPath();
const ipaFileName = "app.ipa";
const itmsDirectory = await this.$tempService.mkdirSync("itms-");
const innerDirectory = path.join(itmsDirectory, "mybundle.itmsp");
const ipaFileLocation = path.join(innerDirectory, ipaFileName);
this.$fs.createDirectory(innerDirectory);
this.$fs.copyFile(data.ipaFilePath, ipaFileLocation);
const password = data.applicationSpecificPassword;
const args = [
"--upload-app",
"-t",
"ios",
"-f",
ipaFileLocation,
"-u",
data.credentials.username,
"-p",
password,
"-k 100000",
];
if (data.teamId) {
args.push("--asc-provider");
args.push(data.teamId);
}
if (data.verboseLogging) {
args.push("--verbose");
}
await this.$childProcess.spawnFromEvent(altoolPath, args, "close", {
stdio: "inherit",
});
}
private async getBundleIdentifier(data: IITMSData): Promise<string> {
const { shouldExtractIpa, ipaFilePath } = data;
if (shouldExtractIpa) {
if (
!this.$fs.exists(ipaFilePath) ||
path.extname(ipaFilePath) !== ".ipa"
) {
this.$errors.fail(
`Cannot use specified ipa file ${ipaFilePath}. File either does not exist or is not an ipa file.`
);
}
this.$logger.trace(
"--ipa set - extracting .ipa file to get app's bundle identifier"
);
const destinationDir = await this.$tempService.mkdirSync("ipa-");
await this.$fs.unzip(ipaFilePath, destinationDir);
const payloadDir = path.join(destinationDir, "Payload");
let allFiles = this.$fs.readDirectory(payloadDir);
this.$logger.debug("ITMSTransporter .ipa Payload files:");
allFiles.forEach((f) => this.$logger.debug(" - " + f));
allFiles = allFiles.filter(
(f) => path.extname(f).toLowerCase() === ".app"
);
if (allFiles.length > 1) {
this.$errors.fail(
"In the .ipa the ITMSTransporter is uploading there is more than one .app file. We don't know which one to upload."
);
} else if (allFiles.length <= 0) {
this.$errors.fail(
"In the .ipa the ITMSTransporter is uploading there must be at least one .app file."
);
}
const appFile = path.join(payloadDir, allFiles[0]);
const plistObject = await this.$plistParser.parseFile(
path.join(appFile, INFO_PLIST_FILE_NAME)
);
const bundleId = plistObject && plistObject.CFBundleIdentifier;
if (!bundleId) {
this.$errors.fail(
`Unable to determine bundle identifier from ${ipaFilePath}.`
);
}
this.$logger.trace(`bundle identifier determined to be ${bundleId}`);
return bundleId;
}
return this.$projectData.projectIdentifiers.ios;
}
@cache()
private async getAltoolPath(): Promise<string> {
const xcodePath = await this.$xcodeSelectService.getContentsDirectoryPath();
let itmsTransporterPath = path.join(
xcodePath,
"..",
"Contents",
"Developer",
"usr",
"bin",
ITMSConstants.altoolExecutableName
);
return itmsTransporterPath;
}
@cache()
private async getITMSTransporterPath(): Promise<string> {
const xcodePath = await this.$xcodeSelectService.getContentsDirectoryPath();
let itmsTransporterPath = path.join(
xcodePath,
"..",
"Contents",
"SharedFrameworks",
"ContentDeliveryServices.framework",
"Versions",
"A",
"itms",
"bin",
ITMSConstants.iTMSExecutableName
);
const xcodeVersionData = await this.$xcodeSelectService.getXcodeVersion();
if (+xcodeVersionData.major < 11) {
const loaderAppContentsPath = path.join(
xcodePath,
"Applications",
"Application Loader.app",
"Contents"
);
itmsTransporterPath = path.join(
loaderAppContentsPath,
ITMSConstants.iTMSDirectoryName,
"bin",
ITMSConstants.iTMSExecutableName
);
}
return itmsTransporterPath;
}
private getITMSMetadataXml(
appleId: string,
ipaFileName: string,
ipaFileHash: string,
ipaFileSize: number
): string {
return `<?xml version="1.0" encoding="UTF-8"?>
<package version="software4.7" xmlns="http://apple.com/itunes/importer">
<software_assets apple_id="${appleId}">
<asset type="bundle">
<data_file>
<file_name>${ipaFileName}</file_name>
<checksum type="md5">${ipaFileHash}</checksum>
<size>${ipaFileSize}</size>
</data_file>
</asset>
</software_assets>
</package>`;
}
}
injector.register("itmsTransporterService", ITMSTransporterService);