Skip to content

feat: cache shouldMigrate result #5074

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
Oct 15, 2019
Merged
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
59 changes: 57 additions & 2 deletions lib/controllers/migrate-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
}

Expand All @@ -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" },
Expand Down Expand Up @@ -147,6 +156,30 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
}

public async shouldMigrate({ projectDir, platforms, allowInvalidVersions = false }: IMigrationData): Promise<boolean> {
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<boolean> {
const projectData = this.$projectDataService.getProjectData(projectDir);
const shouldMigrateCommonMessage = "The app is not compatible with this CLI version and it should be migrated. Reason: ";

Expand Down Expand Up @@ -196,6 +229,28 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
}
}

private async getCachedShouldMigrate(projectDir: string, platform: string): Promise<boolean> {
let cachedShouldMigrateValue = null;

const cachedHash = await this.$jsonFileSettingsService.getSettingValue(getHash(`${projectDir}${platform.toLowerCase()}`));
const packageJsonHash = await this.getPachageJsonHash(projectDir);
if (cachedHash === packageJsonHash) {
cachedShouldMigrateValue = false;
}

return cachedShouldMigrateValue;
}

private async setCachedShouldMigrate(projectDir: string, platform: string): Promise<void> {
const packageJsonHash = await this.getPachageJsonHash(projectDir);
await this.$jsonFileSettingsService.saveSetting(getHash(`${projectDir}${platform.toLowerCase()}`), packageJsonHash);
}

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)) {
Expand Down