Skip to content

Commit f5f6783

Browse files
committed
fix: speed up the prepare process
1 parent 2fc81c5 commit f5f6783

File tree

6 files changed

+2
-238
lines changed

6 files changed

+2
-238
lines changed

lib/common/helpers.ts

-32
Original file line numberDiff line numberDiff line change
@@ -273,38 +273,6 @@ export function getRelativeToRootPath(rootPath: string, filePath: string): strin
273273
return relativeToRootPath;
274274
}
275275

276-
function getVersionArray(version: string | IVersionData): number[] {
277-
let result: number[] = [];
278-
const parseLambda = (x: string) => parseInt(x, 10);
279-
const filterLambda = (x: number) => !isNaN(x);
280-
281-
if (typeof version === "string") {
282-
const versionString = <string>version.split("-")[0];
283-
result = _.map(versionString.split("."), parseLambda);
284-
} else {
285-
result = _(version).map(parseLambda).filter(filterLambda).value();
286-
}
287-
288-
return result;
289-
}
290-
291-
export function versionCompare(version1: string | IVersionData, version2: string | IVersionData): number {
292-
const v1array = getVersionArray(version1),
293-
v2array = getVersionArray(version2);
294-
295-
if (v1array.length !== v2array.length) {
296-
throw new Error("Version strings are not in the same format");
297-
}
298-
299-
for (let i = 0; i < v1array.length; ++i) {
300-
if (v1array[i] !== v2array[i]) {
301-
return v1array[i] > v2array[i] ? 1 : -1;
302-
}
303-
}
304-
305-
return 0;
306-
}
307-
308276
export function isInteractive(): boolean {
309277
const result = isRunningInTTY() && !isCIEnvironment();
310278
return result;

lib/declarations.d.ts

-17
Original file line numberDiff line numberDiff line change
@@ -850,23 +850,6 @@ interface IXcprojService {
850850
* @return {string} The full path to the xcodeproj
851851
*/
852852
getXcodeprojPath(projectData: IProjectData, projectRoot: string): string;
853-
/**
854-
* Checks whether the system needs xcproj to execute ios builds successfully.
855-
* In case the system does need xcproj but does not have it, prints an error message.
856-
* @param {IVerifyXcprojOptions} opts whether to fail with error message or not
857-
* @return {Promise<boolean>} whether an error occurred or not.
858-
*/
859-
verifyXcproj(opts: IVerifyXcprojOptions): Promise<boolean>;
860-
/**
861-
* Collects information about xcproj.
862-
* @return {Promise<XcprojInfo>} collected info about xcproj.
863-
*/
864-
getXcprojInfo(): Promise<IXcprojInfo>;
865-
/**
866-
* Checks if xcproj is available and throws an error in case when it is not available.
867-
* @return {Promise<boolean>}
868-
*/
869-
checkIfXcodeprojIsRequired(): Promise<boolean>;
870853
}
871854

872855
/**

lib/services/cocoapods-service.ts

-15
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export class CocoaPodsService implements ICocoaPodsService {
1212
private $fs: IFileSystem,
1313
private $childProcess: IChildProcess,
1414
private $errors: IErrors,
15-
private $xcprojService: IXcprojService,
1615
private $logger: ILogger,
1716
private $config: IConfiguration,
1817
private $xcconfigService: IXcconfigService) { }
@@ -30,16 +29,6 @@ export class CocoaPodsService implements ICocoaPodsService {
3029
}
3130

3231
public async executePodInstall(projectRoot: string, xcodeProjPath: string): Promise<ISpawnResult> {
33-
// Check availability
34-
try {
35-
await this.$childProcess.exec("which pod");
36-
await this.$childProcess.exec("which xcodeproj");
37-
} catch (e) {
38-
this.$errors.fail("CocoaPods or ruby gem 'xcodeproj' is not installed. Run `sudo gem install cocoapods` and try again.");
39-
}
40-
41-
await this.$xcprojService.verifyXcproj({ shouldFail: true });
42-
4332
this.$logger.info("Installing pods...");
4433
const podTool = this.$config.USE_POD_SANDBOX ? "sandbox-pod" : "pod";
4534
// 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 {
4938
this.$errors.fail(`'${podTool} install' command failed.${podInstallResult.stderr ? " Error is: " + podInstallResult.stderr : ""}`);
5039
}
5140

52-
if ((await this.$xcprojService.getXcprojInfo()).shouldUseXcproj) {
53-
await this.$childProcess.spawnFromEvent("xcproj", ["--project", xcodeProjPath, "touch"], "close");
54-
}
55-
5641
return podInstallResult;
5742
}
5843

lib/services/xcconfig-service.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { Configurations } from "../common/constants";
44
export class XcconfigService implements IXcconfigService {
55
constructor(
66
private $childProcess: IChildProcess,
7-
private $fs: IFileSystem,
8-
private $xcprojService: IXcprojService) { }
7+
private $fs: IFileSystem) { }
98

109
public getPluginsXcconfigFilePaths(projectRoot: string): IStringDictionary {
1110
return {
@@ -27,9 +26,6 @@ export class XcconfigService implements IXcconfigService {
2726
this.$fs.writeFile(destinationFile, "");
2827
}
2928

30-
// TODO: Consider to remove this method
31-
await this.$xcprojService.checkIfXcodeprojIsRequired();
32-
3329
const escapedDestinationFile = destinationFile.replace(/'/g, "\\'");
3430
const escapedSourceFile = sourceFile.replace(/'/g, "\\'");
3531

lib/services/xcproj-service.ts

-74
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,10 @@
1-
import * as semver from "semver";
2-
import * as helpers from "../common/helpers";
3-
import { EOL } from "os";
41
import * as path from "path";
52
import { IosProjectConstants } from "../constants";
63

74
class XcprojService implements IXcprojService {
8-
private xcprojInfoCache: IXcprojInfo;
9-
10-
constructor(private $childProcess: IChildProcess,
11-
private $errors: IErrors,
12-
private $logger: ILogger,
13-
private $sysInfo: ISysInfo,
14-
private $xcodeSelectService: IXcodeSelectService) {
15-
}
16-
175
public getXcodeprojPath(projectData: IProjectData, projectRoot: string): string {
186
return path.join(projectRoot, projectData.projectName + IosProjectConstants.XcodeProjExtName);
197
}
20-
21-
public async verifyXcproj(opts: IVerifyXcprojOptions): Promise<boolean> {
22-
const xcprojInfo = await this.getXcprojInfo();
23-
if (xcprojInfo.shouldUseXcproj && !xcprojInfo.xcprojAvailable) {
24-
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`;
25-
if (opts.shouldFail) {
26-
this.$errors.fail(errorMessage);
27-
} else {
28-
this.$logger.warn(errorMessage);
29-
}
30-
31-
return true;
32-
}
33-
34-
return false;
35-
}
36-
37-
public async getXcprojInfo(): Promise<IXcprojInfo> {
38-
if (!this.xcprojInfoCache) {
39-
let cocoapodVer = await this.$sysInfo.getCocoaPodsVersion();
40-
const xcodeVersion = await this.$xcodeSelectService.getXcodeVersion();
41-
42-
if (cocoapodVer && !semver.valid(cocoapodVer)) {
43-
// Cocoapods betas have names like 1.0.0.beta.8
44-
// These 1.0.0 betas are not valid semver versions, but they are working fine with XCode 7.3
45-
// So get only the major.minor.patch version and consider them as 1.0.0
46-
cocoapodVer = _.take(cocoapodVer.split("."), 3).join(".");
47-
}
48-
49-
xcodeVersion.patch = xcodeVersion.patch || "0";
50-
// CocoaPods with version lower than 1.0.0 don't support Xcode 7.3 yet
51-
// https://github.com/CocoaPods/CocoaPods/issues/2530#issuecomment-210470123
52-
// as a result of this all .pbxprojects touched by CocoaPods get converted to XML plist format
53-
const shouldUseXcproj = cocoapodVer && !!(semver.lt(cocoapodVer, "1.0.0") && ~helpers.versionCompare(xcodeVersion, "7.3.0"));
54-
let xcprojAvailable: boolean;
55-
56-
if (shouldUseXcproj) {
57-
// if that's the case we can use xcproj gem to convert them back to ASCII plist format
58-
try {
59-
await this.$childProcess.exec("xcproj --version");
60-
xcprojAvailable = true;
61-
} catch (e) {
62-
xcprojAvailable = false;
63-
}
64-
}
65-
66-
this.xcprojInfoCache = { cocoapodVer, xcodeVersion, shouldUseXcproj, xcprojAvailable };
67-
}
68-
69-
return this.xcprojInfoCache;
70-
}
71-
72-
public async checkIfXcodeprojIsRequired(): Promise<boolean> {
73-
const xcprojInfo = await this.getXcprojInfo();
74-
if (xcprojInfo.shouldUseXcproj && !xcprojInfo.xcprojAvailable) {
75-
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`;
76-
77-
this.$errors.fail(errorMessage);
78-
79-
return true;
80-
}
81-
}
828
}
839

8410
$injector.register("xcprojService", XcprojService);

test/cocoapods-service.ts

+1-95
Original file line numberDiff line numberDiff line change
@@ -740,45 +740,6 @@ end`
740740
stderr: "",
741741
exitCode: 0
742742
});
743-
744-
const xcprojService = testInjector.resolve<IXcprojService>("xcprojService");
745-
xcprojService.verifyXcproj = async (opts: IVerifyXcprojOptions): Promise<boolean> => false;
746-
xcprojService.getXcprojInfo = async (): Promise<IXcprojInfo> => (<any>{});
747-
});
748-
749-
it("fails when pod executable is not found", async () => {
750-
const childProcess = testInjector.resolve<IChildProcess>("childProcess");
751-
childProcess.exec = async (command: string, options?: any, execOptions?: IExecOptions): Promise<any> => {
752-
assert.equal(command, "which pod");
753-
throw new Error("Missing pod executable");
754-
};
755-
756-
await assert.isRejected(cocoapodsService.executePodInstall(projectRoot, xcodeProjPath), "CocoaPods or ruby gem 'xcodeproj' is not installed. Run `sudo gem install cocoapods` and try again.");
757-
});
758-
759-
it("fails when xcodeproj executable is not found", async () => {
760-
const childProcess = testInjector.resolve<IChildProcess>("childProcess");
761-
childProcess.exec = async (command: string, options?: any, execOptions?: IExecOptions): Promise<any> => {
762-
if (command === "which pod") {
763-
return;
764-
}
765-
766-
assert.equal(command, "which xcodeproj");
767-
throw new Error("Missing xcodeproj executable");
768-
769-
};
770-
771-
await assert.isRejected(cocoapodsService.executePodInstall(projectRoot, xcodeProjPath), "CocoaPods or ruby gem 'xcodeproj' is not installed. Run `sudo gem install cocoapods` and try again.");
772-
});
773-
774-
it("fails with correct error when xcprojService.verifyXcproj throws", async () => {
775-
const expectedError = new Error("err");
776-
const xcprojService = testInjector.resolve<IXcprojService>("xcprojService");
777-
xcprojService.verifyXcproj = async (opts: IVerifyXcprojOptions): Promise<boolean> => {
778-
throw expectedError;
779-
};
780-
781-
await assert.isRejected(cocoapodsService.executePodInstall(projectRoot, xcodeProjPath), expectedError);
782743
});
783744

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

804-
it("calls xcprojService.verifyXcproj with correct arguments", async () => {
805-
const xcprojService = testInjector.resolve<IXcprojService>("xcprojService");
806-
let optsPassedToVerifyXcproj: any = null;
807-
xcprojService.verifyXcproj = async (opts: IVerifyXcprojOptions): Promise<boolean> => {
808-
optsPassedToVerifyXcproj = opts;
809-
return false;
810-
};
811-
812-
await cocoapodsService.executePodInstall(projectRoot, xcodeProjPath);
813-
assert.deepEqual(optsPassedToVerifyXcproj, { shouldFail: true });
814-
});
815-
816765
it("calls pod install spawnFromEvent with correct arguments", async () => {
817766
const childProcess = testInjector.resolve<IChildProcess>("childProcess");
818767
let commandCalled = "";
@@ -846,7 +795,7 @@ end`
846795
await assert.isRejected(cocoapodsService.executePodInstall(projectRoot, xcodeProjPath), "'pod install' command failed.");
847796
});
848797

849-
it("returns the result of the pod install spawnFromEvent methdo", async () => {
798+
it("returns the result of the pod install spawnFromEvent method", async () => {
850799
const childProcess = testInjector.resolve<IChildProcess>("childProcess");
851800
const expectedResult = {
852801
stdout: "pod install finished",
@@ -860,49 +809,6 @@ end`
860809
const result = await cocoapodsService.executePodInstall(projectRoot, xcodeProjPath);
861810
assert.deepEqual(result, expectedResult);
862811
});
863-
864-
it("executes xcproj command with correct arguments when is true", async () => {
865-
const xcprojService = testInjector.resolve<IXcprojService>("xcprojService");
866-
xcprojService.getXcprojInfo = async (): Promise<IXcprojInfo> => (<any>{
867-
shouldUseXcproj: true
868-
});
869-
870-
const spawnFromEventCalls: any[] = [];
871-
const childProcess = testInjector.resolve<IChildProcess>("childProcess");
872-
childProcess.spawnFromEvent = async (command: string, args: string[], event: string, options?: any, spawnFromEventOptions?: ISpawnFromEventOptions): Promise<ISpawnResult> => {
873-
spawnFromEventCalls.push({
874-
command,
875-
args,
876-
event,
877-
options,
878-
spawnFromEventOptions
879-
});
880-
return {
881-
stdout: "",
882-
stderr: "",
883-
exitCode: 0
884-
};
885-
};
886-
887-
await cocoapodsService.executePodInstall(projectRoot, xcodeProjPath);
888-
assert.deepEqual(spawnFromEventCalls, [
889-
{
890-
command: "pod",
891-
args: ["install"],
892-
event: "close",
893-
options: { cwd: projectRoot, stdio: ['pipe', process.stdout, process.stdout] },
894-
spawnFromEventOptions: { throwError: false }
895-
},
896-
{
897-
command: "xcproj",
898-
args: ["--project", xcodeProjPath, "touch"],
899-
event: "close",
900-
options: undefined,
901-
spawnFromEventOptions: undefined
902-
}
903-
]);
904-
905-
});
906812
});
907813

908814
describe("remove duplicated platfoms from project podfile", () => {

0 commit comments

Comments
 (0)