Skip to content

fix: speed up the prepare process #4972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 0 additions & 32 deletions lib/common/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <string>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;
Expand Down
17 changes: 0 additions & 17 deletions lib/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>} whether an error occurred or not.
*/
verifyXcproj(opts: IVerifyXcprojOptions): Promise<boolean>;
/**
* Collects information about xcproj.
* @return {Promise<XcprojInfo>} collected info about xcproj.
*/
getXcprojInfo(): Promise<IXcprojInfo>;
/**
* Checks if xcproj is available and throws an error in case when it is not available.
* @return {Promise<boolean>}
*/
checkIfXcodeprojIsRequired(): Promise<boolean>;
}

/**
Expand Down
15 changes: 0 additions & 15 deletions lib/services/cocoapods-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand All @@ -30,16 +29,6 @@ export class CocoaPodsService implements ICocoaPodsService {
}

public async executePodInstall(projectRoot: string, xcodeProjPath: string): Promise<ISpawnResult> {
// 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.
Expand All @@ -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;
}

Expand Down
6 changes: 1 addition & 5 deletions lib/services/xcconfig-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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, "\\'");

Expand Down
74 changes: 0 additions & 74 deletions lib/services/xcproj-service.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
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<IXcprojInfo> {
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<boolean> {
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);
96 changes: 1 addition & 95 deletions test/cocoapods-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,45 +740,6 @@ end`
stderr: "",
exitCode: 0
});

const xcprojService = testInjector.resolve<IXcprojService>("xcprojService");
xcprojService.verifyXcproj = async (opts: IVerifyXcprojOptions): Promise<boolean> => false;
xcprojService.getXcprojInfo = async (): Promise<IXcprojInfo> => (<any>{});
});

it("fails when pod executable is not found", async () => {
const childProcess = testInjector.resolve<IChildProcess>("childProcess");
childProcess.exec = async (command: string, options?: any, execOptions?: IExecOptions): Promise<any> => {
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<IChildProcess>("childProcess");
childProcess.exec = async (command: string, options?: any, execOptions?: IExecOptions): Promise<any> => {
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<IXcprojService>("xcprojService");
xcprojService.verifyXcproj = async (opts: IVerifyXcprojOptions): Promise<boolean> => {
throw expectedError;
};

await assert.isRejected(cocoapodsService.executePodInstall(projectRoot, xcodeProjPath), expectedError);
});

["pod", "sandbox-pod"].forEach(podExecutable => {
Expand All @@ -801,18 +762,6 @@ end`
});
});

it("calls xcprojService.verifyXcproj with correct arguments", async () => {
const xcprojService = testInjector.resolve<IXcprojService>("xcprojService");
let optsPassedToVerifyXcproj: any = null;
xcprojService.verifyXcproj = async (opts: IVerifyXcprojOptions): Promise<boolean> => {
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<IChildProcess>("childProcess");
let commandCalled = "";
Expand Down Expand Up @@ -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<IChildProcess>("childProcess");
const expectedResult = {
stdout: "pod install finished",
Expand All @@ -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<IXcprojService>("xcprojService");
xcprojService.getXcprojInfo = async (): Promise<IXcprojInfo> => (<any>{
shouldUseXcproj: true
});

const spawnFromEventCalls: any[] = [];
const childProcess = testInjector.resolve<IChildProcess>("childProcess");
childProcess.spawnFromEvent = async (command: string, args: string[], event: string, options?: any, spawnFromEventOptions?: ISpawnFromEventOptions): Promise<ISpawnResult> => {
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", () => {
Expand Down