Skip to content

feat: Speed up adding native platform #3735

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 2 commits into from
Jul 9, 2018
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
4 changes: 1 addition & 3 deletions lib/npm-installation-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export class NpmInstallationManager implements INpmInstallationManager {
constructor(private $npm: INodePackageManager,
private $childProcess: IChildProcess,
private $logger: ILogger,
private $options: IOptions,
private $settingsService: ISettingsService,
private $fs: IFileSystem,
private $staticConfig: IStaticConfig,
Expand Down Expand Up @@ -39,9 +38,8 @@ export class NpmInstallationManager implements INpmInstallationManager {
return maxSatisfying || latestVersion;
}

public async install(packageName: string, projectDir: string, opts?: INpmInstallOptions): Promise<any> {
public async install(packageToInstall: string, projectDir: string, opts?: INpmInstallOptions): Promise<any> {
try {
const packageToInstall = this.$options.frameworkPath || packageName;
const pathToSave = projectDir;
const version = (opts && opts.version) || null;
const dependencyType = (opts && opts.dependencyType) || null;
Expand Down
16 changes: 4 additions & 12 deletions lib/services/android-project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
const targetSdkVersion = androidToolsInfo && androidToolsInfo.targetSdkVersion;
this.$logger.trace(`Using Android SDK '${targetSdkVersion}'.`);

this.isAndroidStudioTemplate = this.isAndroidStudioCompatibleTemplate(projectData);
this.isAndroidStudioTemplate = this.isAndroidStudioCompatibleTemplate(projectData, frameworkVersion);
if (this.isAndroidStudioTemplate) {
this.copy(this.getPlatformData(projectData).projectRoot, frameworkDir, "*", "-R");
} else {
Expand Down Expand Up @@ -703,20 +703,12 @@ export class AndroidProjectService extends projectServiceBaseLib.PlatformProject
}
}

private isAndroidStudioCompatibleTemplate(projectData: IProjectData): boolean {
private isAndroidStudioCompatibleTemplate(projectData: IProjectData, frameworkVersion?: string): boolean {
const currentPlatformData: IDictionary<any> = this.$projectDataService.getNSValue(projectData.projectDir, constants.TNS_ANDROID_RUNTIME_NAME);
let platformVersion = currentPlatformData && currentPlatformData[constants.VERSION_STRING];
const platformVersion = (currentPlatformData && currentPlatformData[constants.VERSION_STRING]) || frameworkVersion;

if (!platformVersion) {
const tnsAndroidPackageJsonPath = path.join(projectData.projectDir, constants.NODE_MODULES_FOLDER_NAME, constants.TNS_ANDROID_RUNTIME_NAME, constants.PACKAGE_JSON_FILE_NAME);
if (this.$fs.exists(tnsAndroidPackageJsonPath)) {
const projectPackageJson: any = this.$fs.readJson(tnsAndroidPackageJsonPath);
if (projectPackageJson && projectPackageJson.version) {
platformVersion = projectPackageJson.version;
}
} else {
return true;
}
return true;
}

if (platformVersion === constants.PackageVersion.NEXT || platformVersion === constants.PackageVersion.LATEST || platformVersion === constants.PackageVersion.RC) {
Expand Down
39 changes: 18 additions & 21 deletions lib/services/platform-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ export class PlatformService extends EventEmitter implements IPlatformService {
private $mobileHelper: Mobile.IMobileHelper,
private $hostInfo: IHostInfo,
private $devicePathProvider: IDevicePathProvider,
private $npm: INodePackageManager,
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
private $projectChangesService: IProjectChangesService,
private $analyticsService: IAnalyticsService,
private $terminalSpinnerService: ITerminalSpinnerService) {
private $terminalSpinnerService: ITerminalSpinnerService,
private $pacoteService: IPacoteService
) {
super();
}

Expand Down Expand Up @@ -92,10 +93,6 @@ export class PlatformService extends EventEmitter implements IPlatformService {

const platformData = this.$platformsData.getPlatformData(platform, projectData);

if (version === undefined) {
version = this.getCurrentPlatformVersion(platform, projectData);
}

// Log the values for project
this.$logger.trace("Creating NativeScript project for the %s platform", platform);
this.$logger.trace("Path: %s", platformData.projectRoot);
Expand All @@ -105,28 +102,28 @@ export class PlatformService extends EventEmitter implements IPlatformService {
this.$logger.out("Copying template files...");

let packageToInstall = "";
const npmOptions: IStringDictionary = {
pathToSave: path.join(projectData.platformsDir, platform),
dependencyType: "save"
};
if (frameworkPath) {
packageToInstall = path.resolve(frameworkPath);
} else {
if (!version) {
version = this.getCurrentPlatformVersion(platform, projectData) ||
await this.$npmInstallationManager.getLatestCompatibleVersion(platformData.frameworkPackageName);
}

if (!frameworkPath) {
packageToInstall = platformData.frameworkPackageName;
npmOptions["version"] = version;
packageToInstall = `${platformData.frameworkPackageName}@${version}`;
}

const spinner = this.$terminalSpinnerService.createSpinner();
const projectDir = projectData.projectDir;
const platformPath = path.join(projectData.platformsDir, platform);

try {
spinner.start();
const downloadedPackagePath = await this.$npmInstallationManager.install(packageToInstall, projectDir, npmOptions);
const downloadedPackagePath = temp.mkdirSync("runtimeDir");
temp.track();
await this.$pacoteService.extractPackage(packageToInstall, downloadedPackagePath);
let frameworkDir = path.join(downloadedPackagePath, constants.PROJECT_FRAMEWORK_FOLDER_NAME);
frameworkDir = path.resolve(frameworkDir);

const coreModuleName = await this.addPlatformCore(platformData, frameworkDir, platformTemplate, projectData, config, nativePrepare);
await this.$npm.uninstall(coreModuleName, { save: true }, projectData.projectDir);
await this.addPlatformCore(platformData, frameworkDir, platformTemplate, projectData, config, nativePrepare);
} catch (err) {
this.$fs.deleteDirectory(platformPath);
throw err;
Expand All @@ -135,7 +132,7 @@ export class PlatformService extends EventEmitter implements IPlatformService {
}

this.$fs.ensureDirectoryExists(platformPath);
this.$logger.out("Project successfully created.");
this.$logger.out(`Platform ${platform} successfully added.`);
}

private async addPlatformCore(platformData: IPlatformData, frameworkDir: string, platformTemplate: string, projectData: IProjectData, config: IPlatformOptions, nativePrepare?: INativePrepare): Promise<string> {
Expand Down Expand Up @@ -842,15 +839,15 @@ export class PlatformService extends EventEmitter implements IPlatformService {
const data = this.$projectDataService.getNSValue(projectData.projectDir, platformData.frameworkPackageName);
const currentVersion = data && data.version ? data.version : "0.2.0";

const installedModuleDir = temp.mkdirSync("runtime-to-update");
let newVersion = version === constants.PackageVersion.NEXT ?
await this.$npmInstallationManager.getNextVersion(platformData.frameworkPackageName) :
version || await this.$npmInstallationManager.getLatestCompatibleVersion(platformData.frameworkPackageName);
const installedModuleDir = await this.$npmInstallationManager.install(platformData.frameworkPackageName, projectData.projectDir, { version: newVersion, dependencyType: "save" });
await this.$pacoteService.extractPackage(`${platformData.frameworkPackageName}@${newVersion}`, installedModuleDir);
const cachedPackageData = this.$fs.readJson(path.join(installedModuleDir, "package.json"));
newVersion = (cachedPackageData && cachedPackageData.version) || newVersion;

const canUpdate = platformData.platformProjectService.canUpdatePlatform(installedModuleDir, projectData);
await this.$npm.uninstall(platformData.frameworkPackageName, { save: true }, projectData.projectDir);
if (canUpdate) {
if (!semver.valid(newVersion)) {
this.$errors.fail("The version %s is not valid. The version should consists from 3 parts separated by dot.", newVersion);
Expand Down
3 changes: 3 additions & 0 deletions test/npm-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ function createTestInjector(): IInjector {
getChanges: () => Promise.resolve({}),
generateHashes: () => Promise.resolve()
});
testInjector.register("pacoteService", {
extractPackage: async (packageName: string, destinationDirectory: string, options?: IPacoteExtractOptions): Promise<void> => undefined
});

return testInjector;
}
Expand Down
3 changes: 3 additions & 0 deletions test/platform-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ function createTestInjector() {
testInjector.register("platformEnvironmentRequirements", {
checkEnvironmentRequirements: async (platform?: string, projectDir?: string, runtimeVersion?: string): Promise<boolean> => true
});
testInjector.register("pacoteService", {
extractPackage: async (packageName: string, destinationDirectory: string, options?: IPacoteExtractOptions): Promise<void> => undefined
});

return testInjector;
}
Expand Down
Loading