|
| 1 | +///<reference path="../.d.ts"/> |
| 2 | +"use strict"; |
| 3 | +import * as path from "path"; |
| 4 | +import * as temp from "temp"; |
| 5 | +let md5 = require("cryptojs").Crypto.MD5; |
| 6 | + |
| 7 | +export class ITMSTransporterService implements IITMSTransporterService { |
| 8 | + private _itmsTransporterPath: string = null; |
| 9 | + |
| 10 | + constructor(private $childProcess: IChildProcess, |
| 11 | + private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants, |
| 12 | + private $errors: IErrors, |
| 13 | + private $fs: IFileSystem, |
| 14 | + private $platformService: IPlatformService, |
| 15 | + private $staticConfig: IStaticConfig, |
| 16 | + private $sysInfo: ISysInfo) { } |
| 17 | + |
| 18 | + public upload(appId: string, username: string, password: string, mobileProvisionIdentifier?: string, codeSignIdentity?: string): IFuture<void> { |
| 19 | + return (() => { |
| 20 | + temp.track(); |
| 21 | + let itmsTransporterPath = this.getITMSTransporterPath().wait(), |
| 22 | + ipaFileName = "app.ipa", |
| 23 | + itmsDirectory = temp.mkdirSync("itms-"), |
| 24 | + innerDirectory = path.join(itmsDirectory, "mybundle.itmsp"), |
| 25 | + ipaFileLocation = path.join(innerDirectory, ipaFileName), |
| 26 | + platform = this.$devicePlatformsConstants.iOS, |
| 27 | + forDevice = true, |
| 28 | + iosBuildConfig: IiOSBuildConfig = { |
| 29 | + buildForDevice: forDevice, |
| 30 | + mobileProvisionIdentifier: mobileProvisionIdentifier, |
| 31 | + codeSignIdentity: codeSignIdentity |
| 32 | + }; |
| 33 | + |
| 34 | + this.$fs.createDirectory(innerDirectory).wait(); |
| 35 | + |
| 36 | + this.$platformService.preparePlatform(platform).wait(); |
| 37 | + this.$platformService.buildPlatform(platform, iosBuildConfig).wait(); |
| 38 | + this.$platformService.copyLastOutput(platform, ipaFileLocation, { isForDevice: forDevice }).wait(); |
| 39 | + |
| 40 | + let ipaFileHash = md5(this.$fs.readFile(ipaFileLocation).wait()), |
| 41 | + ipaFileSize = this.$fs.getFileSize(ipaFileLocation).wait(), |
| 42 | + metadata = `<?xml version="1.0" encoding="UTF-8"?> |
| 43 | +<package version="software4.7" xmlns="http://apple.com/itunes/importer"> |
| 44 | + <software_assets apple_id="${appId}"> |
| 45 | + <asset type="bundle"> |
| 46 | + <data_file> |
| 47 | + <file_name>${ipaFileName}</file_name> |
| 48 | + <checksum type="md5">${ipaFileHash}</checksum> |
| 49 | + <size>${ipaFileSize}</size> |
| 50 | + </data_file> |
| 51 | + </asset> |
| 52 | + </software_assets> |
| 53 | +</package>`; |
| 54 | + |
| 55 | + this.$fs.writeFile(path.join(innerDirectory, "metadata.xml"), metadata).wait(); |
| 56 | + |
| 57 | + this.$childProcess.spawnFromEvent(itmsTransporterPath, ["-m", "upload", "-f", itmsDirectory, "-u", username, "-p", password, "-v", "informational"], "close", { stdio: "inherit" }).wait(); |
| 58 | + }).future<void>()(); |
| 59 | + } |
| 60 | + |
| 61 | + private getITMSTransporterPath(): IFuture<string> { |
| 62 | + return (() => { |
| 63 | + if (!this._itmsTransporterPath) { |
| 64 | + let sysInfo = this.$sysInfo.getSysInfo(path.join(__dirname, "..", "..", this.$staticConfig.PROJECT_FILE_NAME)).wait(), |
| 65 | + xcodeVersionMatch = sysInfo.xcodeVer.match(/Xcode (.*)/), |
| 66 | + result = path.join("/Applications", "Xcode.app", "Contents", "Applications", "Application Loader.app", "Contents"); |
| 67 | + |
| 68 | + if (xcodeVersionMatch && xcodeVersionMatch[1]) { |
| 69 | + let [major, minor] = xcodeVersionMatch[1].split("."); |
| 70 | + // iTMS Transporter's path has been modified in Xcode 6.3 |
| 71 | + // https://github.com/nomad/shenzhen/issues/243 |
| 72 | + if (+major <= 6 && +minor < 3) { |
| 73 | + result = path.join(result, "MacOS"); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + this._itmsTransporterPath = path.join(result, "itms", "bin", "iTMSTransporter"); |
| 78 | + } |
| 79 | + |
| 80 | + if(!this.$fs.exists(this._itmsTransporterPath).wait()) { |
| 81 | + this.$errors.failWithoutHelp('iTMS Transporter not found on this machine - make sure your Xcode installation is not damaged.'); |
| 82 | + } |
| 83 | + |
| 84 | + return this._itmsTransporterPath; |
| 85 | + }).future<string>()(); |
| 86 | + } |
| 87 | +} |
| 88 | +$injector.register("itmsTransporterService", ITMSTransporterService); |
0 commit comments