From 11c2263709fdd88b3c7834fc098305bdb90b4a00 Mon Sep 17 00:00:00 2001 From: fatme Date: Mon, 17 Sep 2018 12:51:38 +0300 Subject: [PATCH] chore: persist warnings per preview app's version --- .../playground/preview-app-plugins-service.ts | 57 +++++++++++-------- .../playground/preview-app-plugins-service.ts | 49 +++++++++++++++- 2 files changed, 82 insertions(+), 24 deletions(-) diff --git a/lib/services/livesync/playground/preview-app-plugins-service.ts b/lib/services/livesync/playground/preview-app-plugins-service.ts index 7e5fa75cfa..7541ea04f8 100644 --- a/lib/services/livesync/playground/preview-app-plugins-service.ts +++ b/lib/services/livesync/playground/preview-app-plugins-service.ts @@ -5,35 +5,27 @@ import { Device } from "nativescript-preview-sdk"; import { PluginComparisonMessages } from "./preview-app-constants"; export class PreviewAppPluginsService implements IPreviewAppPluginsService { + private previewAppVersionWarnings: IDictionary = {}; + constructor(private $fs: IFileSystem, private $logger: ILogger, private $projectData: IProjectData) { } public async comparePluginsOnDevice(device: Device): Promise { - const devicePlugins = this.getDevicePlugins(device); - const localPlugins = this.getLocalPlugins(); - - _.keys(localPlugins).forEach(localPlugin => { - const localPluginVersion = localPlugins[localPlugin]; - const devicePluginVersion = devicePlugins[localPlugin]; - - this.$logger.trace(`Comparing plugin ${localPlugin} with localPluginVersion ${localPluginVersion} and devicePluginVersion ${devicePluginVersion}`); - - if (devicePluginVersion) { - const localPluginVersionData = semver.coerce(localPluginVersion); - const devicePluginVersionData = semver.coerce(devicePluginVersion); - - if (localPluginVersionData.major !== devicePluginVersionData.major) { - this.$logger.warn(util.format(PluginComparisonMessages.LOCAL_PLUGIN_WITH_DIFFERENCE_IN_MAJOR_VERSION, localPlugin, localPluginVersion, devicePluginVersion)); - } + if (!this.previewAppVersionWarnings[device.previewAppVersion]) { + const devicePlugins = this.getDevicePlugins(device); + const localPlugins = this.getLocalPlugins(); + const warnings = _.keys(localPlugins) + .map(localPlugin => { + const localPluginVersion = localPlugins[localPlugin]; + const devicePluginVersion = devicePlugins[localPlugin]; + return this.getWarningForPlugin(localPlugin, localPluginVersion, devicePluginVersion, device.id); + }) + .filter(item => !!item); + this.previewAppVersionWarnings[device.previewAppVersion] = warnings; + } - if (localPluginVersionData.major === devicePluginVersionData.major && localPluginVersionData.minor > devicePluginVersionData.minor) { - this.$logger.warn(util.format(PluginComparisonMessages.LOCAL_PLUGIN_WITH_GREATHER_MINOR_VERSION, localPlugin, localPluginVersion, devicePluginVersion)); - } - } else { - this.$logger.warn(util.format(PluginComparisonMessages.PLUGIN_NOT_INCLUDED_IN_PREVIEW_APP, localPlugin, device.id)); - } - }); + this.previewAppVersionWarnings[device.previewAppVersion].map(warning => this.$logger.warn(warning)); } private getDevicePlugins(device: Device): IStringDictionary { @@ -54,5 +46,24 @@ export class PreviewAppPluginsService implements IPreviewAppPluginsService { return {}; } } + + private getWarningForPlugin(localPlugin: string, localPluginVersion: string, devicePluginVersion: string, deviceId: string): string { + this.$logger.trace(`Comparing plugin ${localPlugin} with localPluginVersion ${localPluginVersion} and devicePluginVersion ${devicePluginVersion}`); + + if (devicePluginVersion) { + const localPluginVersionData = semver.coerce(localPluginVersion); + const devicePluginVersionData = semver.coerce(devicePluginVersion); + + if (localPluginVersionData.major !== devicePluginVersionData.major) { + return util.format(PluginComparisonMessages.LOCAL_PLUGIN_WITH_DIFFERENCE_IN_MAJOR_VERSION, localPlugin, localPluginVersion, devicePluginVersion); + } else if (localPluginVersionData.minor > devicePluginVersionData.minor) { + return util.format(PluginComparisonMessages.LOCAL_PLUGIN_WITH_GREATHER_MINOR_VERSION, localPlugin, localPluginVersion, devicePluginVersion); + } + + return null; + } + + return util.format(PluginComparisonMessages.PLUGIN_NOT_INCLUDED_IN_PREVIEW_APP, localPlugin, deviceId); + } } $injector.register("previewAppPluginsService", PreviewAppPluginsService); diff --git a/test/services/playground/preview-app-plugins-service.ts b/test/services/playground/preview-app-plugins-service.ts index 61c7cfa105..d3a772239b 100644 --- a/test/services/playground/preview-app-plugins-service.ts +++ b/test/services/playground/preview-app-plugins-service.ts @@ -38,7 +38,7 @@ function createDevice(plugins: string): Device { model: "myTestDeviceModel", name: "myTestDeviceName", osVersion: "10.0", - previewAppVersion: "28.0", + previewAppVersion: "28.0.0", runtimeVersion: "4.3.0", plugins, pluginsExpanded: false @@ -58,6 +58,53 @@ function setup(localPlugins: IStringDictionary, previewAppPlugins: IStringDictio describe("previewAppPluginsService", () => { describe("comparePluginsOnDevice", () => { + it("should persist warnings per preview app's version", async () => { + const localPlugins = { + "nativescript-facebook": "2.2.3", + "nativescript-theme-core": "1.0.4", + "tns-core-modules": "4.2.0" + }; + const previewAppPlugins = { + "nativescript-theme-core": "2.0.4", + "tns-core-modules": "4.2.0" + }; + const injector = createTestInjector(localPlugins); + const previewAppPluginsService = injector.resolve("previewAppPluginsService"); + + let isGetDevicePluginsCalled = false; + const originalGetDevicePlugins = (previewAppPluginsService).getDevicePlugins; + (previewAppPluginsService).getDevicePlugins = (device: Device) => { + isGetDevicePluginsCalled = true; + return originalGetDevicePlugins(device); + }; + let isGetLocalPluginsCalled = false; + const originalGetLocalPlugins = (previewAppPluginsService).getLocalPlugins; + (previewAppPluginsService).getLocalPlugins = () => { + isGetLocalPluginsCalled = true; + return originalGetLocalPlugins.apply(previewAppPluginsService); + }; + + await previewAppPluginsService.comparePluginsOnDevice(createDevice(JSON.stringify(previewAppPlugins))); + + const expectedWarnings = [ + util.format(PluginComparisonMessages.PLUGIN_NOT_INCLUDED_IN_PREVIEW_APP, "nativescript-facebook", deviceId), + util.format(PluginComparisonMessages.LOCAL_PLUGIN_WITH_DIFFERENCE_IN_MAJOR_VERSION, "nativescript-theme-core", "1.0.4", "2.0.4") + ]; + assert.isTrue(isGetDevicePluginsCalled); + assert.isTrue(isGetLocalPluginsCalled); + assert.deepEqual(warnParams, expectedWarnings); + + isGetDevicePluginsCalled = false; + isGetLocalPluginsCalled = false; + warnParams = []; + + await previewAppPluginsService.comparePluginsOnDevice(createDevice(JSON.stringify(previewAppPlugins))); + + assert.isFalse(isGetDevicePluginsCalled); + assert.isFalse(isGetLocalPluginsCalled); + assert.deepEqual(warnParams, expectedWarnings); + }); + const testCases = [ { name: "should show warning for plugin not included in preview app",