Skip to content

Keep track of prepared plugins and avoid unnecessary cleans. #2194

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 1 commit into from
Nov 7, 2016
Merged
Show file tree
Hide file tree
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: 54 additions & 5 deletions lib/tools/node-modules/node-modules-dest-copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,69 @@ export class NpmPluginPrepare {
) {
}

protected beforePrepare(dependencies: IDictionary<IDependencyData>, platform: string): void {
this.$platformsData.getPlatformData(platform).platformProjectService.beforePrepareAllPlugins(dependencies).wait();
}

protected afterPrepare(dependencies: IDictionary<IDependencyData>, platform: string): void {
this.$platformsData.getPlatformData(platform).platformProjectService.afterPrepareAllPlugins().wait();
this.writePreparedDependencyInfo(dependencies, platform);
}

private writePreparedDependencyInfo(dependencies: IDictionary<IDependencyData>, platform: string): void {
let prepareData: IDictionary<boolean> = {};
_.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<boolean> {
if (!this.$fs.exists(this.preparedPlatformsFile(platform)).wait()) {
return {};
}
return this.$fs.readJson(this.preparedPlatformsFile(platform), "utf8").wait();
}

private allPrepared(dependencies: IDictionary<IDependencyData>, 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<IDependencyData>, 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);
}
}
66 changes: 66 additions & 0 deletions test/plugin-prepare.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> = {};

constructor(private previouslyPrepared: IDictionary<boolean>) {
super(null, null, null);
}

protected getPreviouslyPreparedDependencies(platform: string): IDictionary<boolean> {
return this.previouslyPrepared;
}

protected beforePrepare(dependencies: IDictionary<IDependencyData>, platform: string): void {
_.values(dependencies).forEach(d => {
this.preparedDependencies[d.name] = true;
});
}

protected afterPrepare(dependencies: IDictionary<IDependencyData>, 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<IDependencyData> = {
"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<IDependencyData> = {
"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);
});
});