From 44d115e5b8690fab144557197737714a009d656d Mon Sep 17 00:00:00 2001 From: Hristo Deshev Date: Fri, 4 Nov 2016 15:58:09 +0200 Subject: [PATCH] Keep track of prepared plugins and avoid unnecessary cleans. --- .../node-modules/node-modules-dest-copy.ts | 59 +++++++++++++++-- test/plugin-prepare.ts | 66 +++++++++++++++++++ 2 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 test/plugin-prepare.ts diff --git a/lib/tools/node-modules/node-modules-dest-copy.ts b/lib/tools/node-modules/node-modules-dest-copy.ts index 897c2eebb5..a3dcbdf9dd 100644 --- a/lib/tools/node-modules/node-modules-dest-copy.ts +++ b/lib/tools/node-modules/node-modules-dest-copy.ts @@ -62,20 +62,69 @@ export class NpmPluginPrepare { ) { } + protected beforePrepare(dependencies: IDictionary, platform: string): void { + this.$platformsData.getPlatformData(platform).platformProjectService.beforePrepareAllPlugins(dependencies).wait(); + } + + protected afterPrepare(dependencies: IDictionary, platform: string): void { + this.$platformsData.getPlatformData(platform).platformProjectService.afterPrepareAllPlugins().wait(); + this.writePreparedDependencyInfo(dependencies, platform); + } + + private writePreparedDependencyInfo(dependencies: IDictionary, platform: string): void { + let prepareData: IDictionary = {}; + _.values(dependencies).forEach(d => { + prepareData[d.name] = true; + }); + this.$fs.createDirectory(this.preparedPlatformsDir(platform)).wait(); + this.$fs.writeJson(this.preparedPlatformsFile(platform), prepareData, " ", "utf8").wait(); + } + + private preparedPlatformsDir(platform: string): string { + const platformRoot = this.$platformsData.getPlatformData(platform).projectRoot; + if (/android/i.test(platform)) { + return path.join(platformRoot, "build", "intermediates"); + } else if (/ios/i.test(platform)) { + return path.join(platformRoot, "build"); + } else { + throw new Error("Invalid platform: " + platform); + } + } + + private preparedPlatformsFile(platform: string): string { + return path.join(this.preparedPlatformsDir(platform), "prepared-platforms.json"); + } + + protected getPreviouslyPreparedDependencies(platform: string): IDictionary { + if (!this.$fs.exists(this.preparedPlatformsFile(platform)).wait()) { + return {}; + } + return this.$fs.readJson(this.preparedPlatformsFile(platform), "utf8").wait(); + } + + private allPrepared(dependencies: IDictionary, platform: string): boolean { + let result = true; + const previouslyPrepared = this.getPreviouslyPreparedDependencies(platform); + _.values(dependencies).forEach(d => { + if (!previouslyPrepared[d.name]) { + result = false; + } + }); + return result; + } + public preparePlugins(dependencies: IDictionary, platform: string): void { - if (_.isEmpty(dependencies)) { + if (_.isEmpty(dependencies) || this.allPrepared(dependencies, platform)) { return; } - this.$platformsData.getPlatformData(platform).platformProjectService.beforePrepareAllPlugins(dependencies).wait(); - + this.beforePrepare(dependencies, platform); _.each(dependencies, dependency => { let isPlugin = !!dependency.nativescript; if (isPlugin) { - console.log("preparing: " + dependency.name); this.$pluginsService.prepare(dependency, platform).wait(); } }); - this.$platformsData.getPlatformData(platform).platformProjectService.afterPrepareAllPlugins().wait(); + this.afterPrepare(dependencies, platform); } } diff --git a/test/plugin-prepare.ts b/test/plugin-prepare.ts new file mode 100644 index 0000000000..5ca412cc36 --- /dev/null +++ b/test/plugin-prepare.ts @@ -0,0 +1,66 @@ +import {assert} from "chai"; +import {NpmPluginPrepare} from "../lib/tools/node-modules/node-modules-dest-copy"; + +require("should"); + +class TestNpmPluginPrepare extends NpmPluginPrepare { + public preparedDependencies: IDictionary = {}; + + constructor(private previouslyPrepared: IDictionary) { + super(null, null, null); + } + + protected getPreviouslyPreparedDependencies(platform: string): IDictionary { + return this.previouslyPrepared; + } + + protected beforePrepare(dependencies: IDictionary, platform: string): void { + _.values(dependencies).forEach(d => { + this.preparedDependencies[d.name] = true; + }); + } + + protected afterPrepare(dependencies: IDictionary, platform: string): void { + // DO NOTHING + } +} + +describe("Plugin preparation", () => { + it("skips prepare if no plugins", () => { + const pluginPrepare = new TestNpmPluginPrepare({}); + pluginPrepare.preparePlugins({}, "android"); + assert.deepEqual({}, pluginPrepare.preparedDependencies); + }); + + it("skips prepare if every plugin prepared", () => { + const pluginPrepare = new TestNpmPluginPrepare({"tns-core-modules-widgets": true}); + const testDependencies: IDictionary = { + "0": { + name: "tns-core-modules-widgets", + version: "1.0.0", + nativescript: null, + } + }; + pluginPrepare.preparePlugins(testDependencies, "android"); + assert.deepEqual({}, pluginPrepare.preparedDependencies); + }); + + it("saves prepared plugins after preparation", () => { + const pluginPrepare = new TestNpmPluginPrepare({"tns-core-modules-widgets": true}); + const testDependencies: IDictionary = { + "0": { + name: "tns-core-modules-widgets", + version: "1.0.0", + nativescript: null, + }, + "1": { + name: "nativescript-calendar", + version: "1.0.0", + nativescript: null, + } + }; + pluginPrepare.preparePlugins(testDependencies, "android"); + const prepareData = {"tns-core-modules-widgets": true, "nativescript-calendar": true}; + assert.deepEqual(prepareData, pluginPrepare.preparedDependencies); + }); +});