Skip to content

Commit 3627814

Browse files
authored
Merge pull request #5074 from NativeScript/kddimitrov/cache-should-migrate
feat: cache shouldMigrate result
2 parents 0c79359 + 5cdd5c1 commit 3627814

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

lib/controllers/migrate-controller.ts

+57-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as semver from "semver";
33
import * as constants from "../constants";
44
import * as glob from "glob";
55
import { UpdateControllerBase } from "./update-controller-base";
6-
import { fromWindowsRelativePathToUnix } from "../common/helpers";
6+
import { fromWindowsRelativePathToUnix, getHash } from "../common/helpers";
77

88
export class MigrateController extends UpdateControllerBase implements IMigrateController {
99
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}`;
2727
private $pluginsService: IPluginsService,
2828
private $projectDataService: IProjectDataService,
2929
private $platformValidationService: IPlatformValidationService,
30-
private $resources: IResourceLoader) {
30+
private $resources: IResourceLoader,
31+
private $injector: IInjector,
32+
private $settingsService: ISettingsService,
33+
private $staticConfig: Config.IStaticConfig) {
3134
super($fs, $platformCommandHelper, $platformsDataService, $packageInstallationManager, $packageManager, $pacoteService);
3235
}
3336

@@ -46,6 +49,12 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
4649
constants.KARMA_CONFIG_NAME
4750
];
4851

52+
private get $jsonFileSettingsService(): IJsonFileSettingsService {
53+
const cliVersion = semver.coerce(this.$staticConfig.version);
54+
const shouldMigrateCacheFilePath = path.join(this.$settingsService.getProfileDir(), `should-migrate-cache-${cliVersion}.json`);
55+
return this.$injector.resolve("jsonFileSettingsService", { jsonFileSettingsPath: shouldMigrateCacheFilePath });
56+
}
57+
4958
private migrationDependencies: IMigrationDependency[] = [
5059
{ packageName: constants.TNS_CORE_MODULES_NAME, verifiedVersion: "6.0.1" },
5160
{ packageName: constants.TNS_CORE_MODULES_WIDGETS_NAME, verifiedVersion: "6.0.1" },
@@ -147,6 +156,30 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
147156
}
148157

149158
public async shouldMigrate({ projectDir, platforms, allowInvalidVersions = false }: IMigrationData): Promise<boolean> {
159+
const remainingPlatforms = [];
160+
let shouldMigrate = false;
161+
162+
for (const platform of platforms) {
163+
const cachedResult = await this.getCachedShouldMigrate(projectDir, platform);
164+
if (cachedResult !== false) {
165+
remainingPlatforms.push(platform);
166+
}
167+
}
168+
169+
if (remainingPlatforms.length > 0) {
170+
shouldMigrate = await this._shouldMigrate({ projectDir, platforms: remainingPlatforms, allowInvalidVersions });
171+
172+
if (!shouldMigrate) {
173+
for (const remainingPlatform of remainingPlatforms) {
174+
await this.setCachedShouldMigrate(projectDir, remainingPlatform);
175+
}
176+
}
177+
}
178+
179+
return shouldMigrate;
180+
}
181+
182+
private async _shouldMigrate({ projectDir, platforms, allowInvalidVersions }: IMigrationData): Promise<boolean> {
150183
const projectData = this.$projectDataService.getProjectData(projectDir);
151184
const shouldMigrateCommonMessage = "The app is not compatible with this CLI version and it should be migrated. Reason: ";
152185

@@ -196,6 +229,28 @@ Running this command will ${MigrateController.COMMON_MIGRATE_MESSAGE}`;
196229
}
197230
}
198231

232+
private async getCachedShouldMigrate(projectDir: string, platform: string): Promise<boolean> {
233+
let cachedShouldMigrateValue = null;
234+
235+
const cachedHash = await this.$jsonFileSettingsService.getSettingValue(getHash(`${projectDir}${platform.toLowerCase()}`));
236+
const packageJsonHash = await this.getPachageJsonHash(projectDir);
237+
if (cachedHash === packageJsonHash) {
238+
cachedShouldMigrateValue = false;
239+
}
240+
241+
return cachedShouldMigrateValue;
242+
}
243+
244+
private async setCachedShouldMigrate(projectDir: string, platform: string): Promise<void> {
245+
const packageJsonHash = await this.getPachageJsonHash(projectDir);
246+
await this.$jsonFileSettingsService.saveSetting(getHash(`${projectDir}${platform.toLowerCase()}`), packageJsonHash);
247+
}
248+
249+
private async getPachageJsonHash(projectDir: string) {
250+
const projectPackageJsonFilePath = path.join(projectDir, constants.PACKAGE_JSON_FILE_NAME);
251+
return await this.$fs.getFileShasum(projectPackageJsonFilePath);
252+
}
253+
199254
private async migrateOldAndroidAppResources(projectData: IProjectData, backupDir: string) {
200255
const appResourcesPath = projectData.getAppResourcesDirectoryPath();
201256
if (!this.$androidResourcesMigrationService.hasMigrated(appResourcesPath)) {

0 commit comments

Comments
 (0)