From f5f6783f9c4a26f3300d9b1dfac8dbe8a9c194e7 Mon Sep 17 00:00:00 2001 From: fatme Date: Mon, 26 Aug 2019 09:35:29 +0300 Subject: [PATCH] fix: speed up the prepare process --- lib/common/helpers.ts | 32 ----------- lib/declarations.d.ts | 17 ------ lib/services/cocoapods-service.ts | 15 ----- lib/services/xcconfig-service.ts | 6 +- lib/services/xcproj-service.ts | 74 ------------------------ test/cocoapods-service.ts | 96 +------------------------------ 6 files changed, 2 insertions(+), 238 deletions(-) diff --git a/lib/common/helpers.ts b/lib/common/helpers.ts index 52b731e146..31e5e64a8b 100644 --- a/lib/common/helpers.ts +++ b/lib/common/helpers.ts @@ -273,38 +273,6 @@ export function getRelativeToRootPath(rootPath: string, filePath: string): strin return relativeToRootPath; } -function getVersionArray(version: string | IVersionData): number[] { - let result: number[] = []; - const parseLambda = (x: string) => parseInt(x, 10); - const filterLambda = (x: number) => !isNaN(x); - - if (typeof version === "string") { - const versionString = version.split("-")[0]; - result = _.map(versionString.split("."), parseLambda); - } else { - result = _(version).map(parseLambda).filter(filterLambda).value(); - } - - return result; -} - -export function versionCompare(version1: string | IVersionData, version2: string | IVersionData): number { - const v1array = getVersionArray(version1), - v2array = getVersionArray(version2); - - if (v1array.length !== v2array.length) { - throw new Error("Version strings are not in the same format"); - } - - for (let i = 0; i < v1array.length; ++i) { - if (v1array[i] !== v2array[i]) { - return v1array[i] > v2array[i] ? 1 : -1; - } - } - - return 0; -} - export function isInteractive(): boolean { const result = isRunningInTTY() && !isCIEnvironment(); return result; diff --git a/lib/declarations.d.ts b/lib/declarations.d.ts index 7272e7cfd5..97f66953ed 100644 --- a/lib/declarations.d.ts +++ b/lib/declarations.d.ts @@ -850,23 +850,6 @@ interface IXcprojService { * @return {string} The full path to the xcodeproj */ getXcodeprojPath(projectData: IProjectData, projectRoot: string): string; - /** - * Checks whether the system needs xcproj to execute ios builds successfully. - * In case the system does need xcproj but does not have it, prints an error message. - * @param {IVerifyXcprojOptions} opts whether to fail with error message or not - * @return {Promise} whether an error occurred or not. - */ - verifyXcproj(opts: IVerifyXcprojOptions): Promise; - /** - * Collects information about xcproj. - * @return {Promise} collected info about xcproj. - */ - getXcprojInfo(): Promise; - /** - * Checks if xcproj is available and throws an error in case when it is not available. - * @return {Promise} - */ - checkIfXcodeprojIsRequired(): Promise; } /** diff --git a/lib/services/cocoapods-service.ts b/lib/services/cocoapods-service.ts index e150222553..e4868f3571 100644 --- a/lib/services/cocoapods-service.ts +++ b/lib/services/cocoapods-service.ts @@ -12,7 +12,6 @@ export class CocoaPodsService implements ICocoaPodsService { private $fs: IFileSystem, private $childProcess: IChildProcess, private $errors: IErrors, - private $xcprojService: IXcprojService, private $logger: ILogger, private $config: IConfiguration, private $xcconfigService: IXcconfigService) { } @@ -30,16 +29,6 @@ export class CocoaPodsService implements ICocoaPodsService { } public async executePodInstall(projectRoot: string, xcodeProjPath: string): Promise { - // Check availability - try { - await this.$childProcess.exec("which pod"); - await this.$childProcess.exec("which xcodeproj"); - } catch (e) { - this.$errors.fail("CocoaPods or ruby gem 'xcodeproj' is not installed. Run `sudo gem install cocoapods` and try again."); - } - - await this.$xcprojService.verifyXcproj({ shouldFail: true }); - this.$logger.info("Installing pods..."); const podTool = this.$config.USE_POD_SANDBOX ? "sandbox-pod" : "pod"; // cocoapods print a lot of non-error information on stderr. Pipe the `stderr` to `stdout`, so we won't polute CLI's stderr output. @@ -49,10 +38,6 @@ export class CocoaPodsService implements ICocoaPodsService { this.$errors.fail(`'${podTool} install' command failed.${podInstallResult.stderr ? " Error is: " + podInstallResult.stderr : ""}`); } - if ((await this.$xcprojService.getXcprojInfo()).shouldUseXcproj) { - await this.$childProcess.spawnFromEvent("xcproj", ["--project", xcodeProjPath, "touch"], "close"); - } - return podInstallResult; } diff --git a/lib/services/xcconfig-service.ts b/lib/services/xcconfig-service.ts index 896778536f..8fcdc1edb4 100644 --- a/lib/services/xcconfig-service.ts +++ b/lib/services/xcconfig-service.ts @@ -4,8 +4,7 @@ import { Configurations } from "../common/constants"; export class XcconfigService implements IXcconfigService { constructor( private $childProcess: IChildProcess, - private $fs: IFileSystem, - private $xcprojService: IXcprojService) { } + private $fs: IFileSystem) { } public getPluginsXcconfigFilePaths(projectRoot: string): IStringDictionary { return { @@ -27,9 +26,6 @@ export class XcconfigService implements IXcconfigService { this.$fs.writeFile(destinationFile, ""); } - // TODO: Consider to remove this method - await this.$xcprojService.checkIfXcodeprojIsRequired(); - const escapedDestinationFile = destinationFile.replace(/'/g, "\\'"); const escapedSourceFile = sourceFile.replace(/'/g, "\\'"); diff --git a/lib/services/xcproj-service.ts b/lib/services/xcproj-service.ts index 066466ab14..0a14ca77e2 100644 --- a/lib/services/xcproj-service.ts +++ b/lib/services/xcproj-service.ts @@ -1,84 +1,10 @@ -import * as semver from "semver"; -import * as helpers from "../common/helpers"; -import { EOL } from "os"; import * as path from "path"; import { IosProjectConstants } from "../constants"; class XcprojService implements IXcprojService { - private xcprojInfoCache: IXcprojInfo; - - constructor(private $childProcess: IChildProcess, - private $errors: IErrors, - private $logger: ILogger, - private $sysInfo: ISysInfo, - private $xcodeSelectService: IXcodeSelectService) { - } - public getXcodeprojPath(projectData: IProjectData, projectRoot: string): string { return path.join(projectRoot, projectData.projectName + IosProjectConstants.XcodeProjExtName); } - - public async verifyXcproj(opts: IVerifyXcprojOptions): Promise { - const xcprojInfo = await this.getXcprojInfo(); - if (xcprojInfo.shouldUseXcproj && !xcprojInfo.xcprojAvailable) { - const errorMessage = `You are using CocoaPods version ${xcprojInfo.cocoapodVer} which does not support Xcode ${xcprojInfo.xcodeVersion.major}.${xcprojInfo.xcodeVersion.minor} yet.${EOL}${EOL}You can update your cocoapods by running $sudo gem install cocoapods from a terminal.${EOL}${EOL}In order for the NativeScript CLI to be able to work correctly with this setup you need to install xcproj command line tool and add it to your PATH. Xcproj can be installed with homebrew by running $ brew install xcproj from the terminal`; - if (opts.shouldFail) { - this.$errors.fail(errorMessage); - } else { - this.$logger.warn(errorMessage); - } - - return true; - } - - return false; - } - - public async getXcprojInfo(): Promise { - if (!this.xcprojInfoCache) { - let cocoapodVer = await this.$sysInfo.getCocoaPodsVersion(); - const xcodeVersion = await this.$xcodeSelectService.getXcodeVersion(); - - if (cocoapodVer && !semver.valid(cocoapodVer)) { - // Cocoapods betas have names like 1.0.0.beta.8 - // These 1.0.0 betas are not valid semver versions, but they are working fine with XCode 7.3 - // So get only the major.minor.patch version and consider them as 1.0.0 - cocoapodVer = _.take(cocoapodVer.split("."), 3).join("."); - } - - xcodeVersion.patch = xcodeVersion.patch || "0"; - // CocoaPods with version lower than 1.0.0 don't support Xcode 7.3 yet - // https://github.com/CocoaPods/CocoaPods/issues/2530#issuecomment-210470123 - // as a result of this all .pbxprojects touched by CocoaPods get converted to XML plist format - const shouldUseXcproj = cocoapodVer && !!(semver.lt(cocoapodVer, "1.0.0") && ~helpers.versionCompare(xcodeVersion, "7.3.0")); - let xcprojAvailable: boolean; - - if (shouldUseXcproj) { - // if that's the case we can use xcproj gem to convert them back to ASCII plist format - try { - await this.$childProcess.exec("xcproj --version"); - xcprojAvailable = true; - } catch (e) { - xcprojAvailable = false; - } - } - - this.xcprojInfoCache = { cocoapodVer, xcodeVersion, shouldUseXcproj, xcprojAvailable }; - } - - return this.xcprojInfoCache; - } - - public async checkIfXcodeprojIsRequired(): Promise { - const xcprojInfo = await this.getXcprojInfo(); - if (xcprojInfo.shouldUseXcproj && !xcprojInfo.xcprojAvailable) { - const errorMessage = `You are using CocoaPods version ${xcprojInfo.cocoapodVer} which does not support Xcode ${xcprojInfo.xcodeVersion.major}.${xcprojInfo.xcodeVersion.minor} yet.${EOL}${EOL}You can update your cocoapods by running $sudo gem install cocoapods from a terminal.${EOL}${EOL}In order for the NativeScript CLI to be able to work correctly with this setup you need to install xcproj command line tool and add it to your PATH. Xcproj can be installed with homebrew by running $ brew install xcproj from the terminal`; - - this.$errors.fail(errorMessage); - - return true; - } - } } $injector.register("xcprojService", XcprojService); diff --git a/test/cocoapods-service.ts b/test/cocoapods-service.ts index c511e70804..52ff704b3b 100644 --- a/test/cocoapods-service.ts +++ b/test/cocoapods-service.ts @@ -740,45 +740,6 @@ end` stderr: "", exitCode: 0 }); - - const xcprojService = testInjector.resolve("xcprojService"); - xcprojService.verifyXcproj = async (opts: IVerifyXcprojOptions): Promise => false; - xcprojService.getXcprojInfo = async (): Promise => ({}); - }); - - it("fails when pod executable is not found", async () => { - const childProcess = testInjector.resolve("childProcess"); - childProcess.exec = async (command: string, options?: any, execOptions?: IExecOptions): Promise => { - assert.equal(command, "which pod"); - throw new Error("Missing pod executable"); - }; - - await assert.isRejected(cocoapodsService.executePodInstall(projectRoot, xcodeProjPath), "CocoaPods or ruby gem 'xcodeproj' is not installed. Run `sudo gem install cocoapods` and try again."); - }); - - it("fails when xcodeproj executable is not found", async () => { - const childProcess = testInjector.resolve("childProcess"); - childProcess.exec = async (command: string, options?: any, execOptions?: IExecOptions): Promise => { - if (command === "which pod") { - return; - } - - assert.equal(command, "which xcodeproj"); - throw new Error("Missing xcodeproj executable"); - - }; - - await assert.isRejected(cocoapodsService.executePodInstall(projectRoot, xcodeProjPath), "CocoaPods or ruby gem 'xcodeproj' is not installed. Run `sudo gem install cocoapods` and try again."); - }); - - it("fails with correct error when xcprojService.verifyXcproj throws", async () => { - const expectedError = new Error("err"); - const xcprojService = testInjector.resolve("xcprojService"); - xcprojService.verifyXcproj = async (opts: IVerifyXcprojOptions): Promise => { - throw expectedError; - }; - - await assert.isRejected(cocoapodsService.executePodInstall(projectRoot, xcodeProjPath), expectedError); }); ["pod", "sandbox-pod"].forEach(podExecutable => { @@ -801,18 +762,6 @@ end` }); }); - it("calls xcprojService.verifyXcproj with correct arguments", async () => { - const xcprojService = testInjector.resolve("xcprojService"); - let optsPassedToVerifyXcproj: any = null; - xcprojService.verifyXcproj = async (opts: IVerifyXcprojOptions): Promise => { - optsPassedToVerifyXcproj = opts; - return false; - }; - - await cocoapodsService.executePodInstall(projectRoot, xcodeProjPath); - assert.deepEqual(optsPassedToVerifyXcproj, { shouldFail: true }); - }); - it("calls pod install spawnFromEvent with correct arguments", async () => { const childProcess = testInjector.resolve("childProcess"); let commandCalled = ""; @@ -846,7 +795,7 @@ end` await assert.isRejected(cocoapodsService.executePodInstall(projectRoot, xcodeProjPath), "'pod install' command failed."); }); - it("returns the result of the pod install spawnFromEvent methdo", async () => { + it("returns the result of the pod install spawnFromEvent method", async () => { const childProcess = testInjector.resolve("childProcess"); const expectedResult = { stdout: "pod install finished", @@ -860,49 +809,6 @@ end` const result = await cocoapodsService.executePodInstall(projectRoot, xcodeProjPath); assert.deepEqual(result, expectedResult); }); - - it("executes xcproj command with correct arguments when is true", async () => { - const xcprojService = testInjector.resolve("xcprojService"); - xcprojService.getXcprojInfo = async (): Promise => ({ - shouldUseXcproj: true - }); - - const spawnFromEventCalls: any[] = []; - const childProcess = testInjector.resolve("childProcess"); - childProcess.spawnFromEvent = async (command: string, args: string[], event: string, options?: any, spawnFromEventOptions?: ISpawnFromEventOptions): Promise => { - spawnFromEventCalls.push({ - command, - args, - event, - options, - spawnFromEventOptions - }); - return { - stdout: "", - stderr: "", - exitCode: 0 - }; - }; - - await cocoapodsService.executePodInstall(projectRoot, xcodeProjPath); - assert.deepEqual(spawnFromEventCalls, [ - { - command: "pod", - args: ["install"], - event: "close", - options: { cwd: projectRoot, stdio: ['pipe', process.stdout, process.stdout] }, - spawnFromEventOptions: { throwError: false } - }, - { - command: "xcproj", - args: ["--project", xcodeProjPath, "touch"], - event: "close", - options: undefined, - spawnFromEventOptions: undefined - } - ]); - - }); }); describe("remove duplicated platfoms from project podfile", () => {