From 9b9de460c031aedd86d913ebf745772d2a813dc6 Mon Sep 17 00:00:00 2001 From: Kristian Dimitrov Date: Mon, 14 Oct 2019 13:14:59 +0300 Subject: [PATCH 1/2] feat: cache shouldMigrate result --- lib/controllers/migrate-controller.ts | 59 ++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/lib/controllers/migrate-controller.ts b/lib/controllers/migrate-controller.ts index de69f7d7b2..2aec10b775 100644 --- a/lib/controllers/migrate-controller.ts +++ b/lib/controllers/migrate-controller.ts @@ -3,7 +3,7 @@ import * as semver from "semver"; import * as constants from "../constants"; import * as glob from "glob"; import { UpdateControllerBase } from "./update-controller-base"; -import { fromWindowsRelativePathToUnix } from "../common/helpers"; +import { fromWindowsRelativePathToUnix, getHash } from "../common/helpers"; export class MigrateController extends UpdateControllerBase implements IMigrateController { private static COMMON_MIGRATE_MESSAGE = "not affect the codebase of the application and you might need to do additional changes manually – for more information, refer to the instructions in the following blog post: https://www.nativescript.org/blog/nativescript-6.0-application-migration"; @@ -27,7 +27,10 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`; private $pluginsService: IPluginsService, private $projectDataService: IProjectDataService, private $platformValidationService: IPlatformValidationService, - private $resources: IResourceLoader) { + private $resources: IResourceLoader, + private $injector: IInjector, + private $settingsService: ISettingsService, + private $staticConfig: Config.IStaticConfig) { super($fs, $platformCommandHelper, $platformsDataService, $packageInstallationManager, $packageManager, $pacoteService); } @@ -46,6 +49,12 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`; constants.KARMA_CONFIG_NAME ]; + private get $jsonFileSettingsService(): IJsonFileSettingsService { + const cliVersion = semver.coerce(this.$staticConfig.version); + const shouldMigrateCacheFilePath = path.join(this.$settingsService.getProfileDir(), `should-migrate-cache-${cliVersion}.json`); + return this.$injector.resolve("jsonFileSettingsService", { jsonFileSettingsPath: shouldMigrateCacheFilePath }); + } + private migrationDependencies: IMigrationDependency[] = [ { packageName: constants.TNS_CORE_MODULES_NAME, verifiedVersion: "6.0.1" }, { packageName: constants.TNS_CORE_MODULES_WIDGETS_NAME, verifiedVersion: "6.0.1" }, @@ -147,6 +156,30 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`; } public async shouldMigrate({ projectDir, platforms, allowInvalidVersions = false }: IMigrationData): Promise { + const remainingPlatforms = []; + let shouldMigrate = false; + + for (const platform of platforms) { + const cachedResult = await this.getCachedShouldMigrate(projectDir, platform); + if (cachedResult !== false) { + remainingPlatforms.push(platform); + } + } + + if (remainingPlatforms.length > 0) { + shouldMigrate = await this._shouldMigrate({ projectDir, platforms: remainingPlatforms, allowInvalidVersions }); + + if (!shouldMigrate) { + for (const remainingPlatform of remainingPlatforms) { + await this.setCachedShouldMigrate(projectDir, remainingPlatform); + } + } + } + + return shouldMigrate; + } + + private async _shouldMigrate({ projectDir, platforms, allowInvalidVersions }: IMigrationData): Promise { const projectData = this.$projectDataService.getProjectData(projectDir); const shouldMigrateCommonMessage = "The app is not compatible with this CLI version and it should be migrated. Reason: "; @@ -196,6 +229,28 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`; } } + private async getCachedShouldMigrate(projectDir: string, platform: string): Promise { + let cachedShouldMigrateValue = null; + + const cashedHash = await this.$jsonFileSettingsService.getSettingValue(getHash(`${projectDir}${platform.toLowerCase()}`)); + const packageJsonShasum = await this.getPachageJsonHash(projectDir); + if (cashedHash === packageJsonShasum) { + cachedShouldMigrateValue = false; + } + + return cachedShouldMigrateValue; + } + + private async setCachedShouldMigrate(projectDir: string, platform: string): Promise { + const packageJsonShasum = await this.getPachageJsonHash(projectDir); + await this.$jsonFileSettingsService.saveSetting(getHash(`${projectDir}${platform.toLowerCase()}`), packageJsonShasum); + } + + private async getPachageJsonHash(projectDir: string) { + const projectPackageJsonFilePath = path.join(projectDir, constants.PACKAGE_JSON_FILE_NAME); + return await this.$fs.getFileShasum(projectPackageJsonFilePath); + } + private async migrateOldAndroidAppResources(projectData: IProjectData, backupDir: string) { const appResourcesPath = projectData.getAppResourcesDirectoryPath(); if (!this.$androidResourcesMigrationService.hasMigrated(appResourcesPath)) { From 5cdd5c1447231c6d8711ae87cb6c476c4b0599bc Mon Sep 17 00:00:00 2001 From: Kristian Dimitrov Date: Mon, 14 Oct 2019 20:33:30 +0300 Subject: [PATCH 2/2] chore: fix comments --- lib/controllers/migrate-controller.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/controllers/migrate-controller.ts b/lib/controllers/migrate-controller.ts index 2aec10b775..0b7d041408 100644 --- a/lib/controllers/migrate-controller.ts +++ b/lib/controllers/migrate-controller.ts @@ -232,9 +232,9 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`; private async getCachedShouldMigrate(projectDir: string, platform: string): Promise { let cachedShouldMigrateValue = null; - const cashedHash = await this.$jsonFileSettingsService.getSettingValue(getHash(`${projectDir}${platform.toLowerCase()}`)); - const packageJsonShasum = await this.getPachageJsonHash(projectDir); - if (cashedHash === packageJsonShasum) { + const cachedHash = await this.$jsonFileSettingsService.getSettingValue(getHash(`${projectDir}${platform.toLowerCase()}`)); + const packageJsonHash = await this.getPachageJsonHash(projectDir); + if (cachedHash === packageJsonHash) { cachedShouldMigrateValue = false; } @@ -242,8 +242,8 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`; } private async setCachedShouldMigrate(projectDir: string, platform: string): Promise { - const packageJsonShasum = await this.getPachageJsonHash(projectDir); - await this.$jsonFileSettingsService.saveSetting(getHash(`${projectDir}${platform.toLowerCase()}`), packageJsonShasum); + const packageJsonHash = await this.getPachageJsonHash(projectDir); + await this.$jsonFileSettingsService.saveSetting(getHash(`${projectDir}${platform.toLowerCase()}`), packageJsonHash); } private async getPachageJsonHash(projectDir: string) {