Skip to content

Commit cef0c15

Browse files
authored
Merge pull request #2194 from NativeScript/hdeshev/plugin-clean
Keep track of prepared plugins and avoid unnecessary cleans.
2 parents d416cce + 44d115e commit cef0c15

File tree

2 files changed

+120
-5
lines changed

2 files changed

+120
-5
lines changed

lib/tools/node-modules/node-modules-dest-copy.ts

+54-5
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,69 @@ export class NpmPluginPrepare {
6262
) {
6363
}
6464

65+
protected beforePrepare(dependencies: IDictionary<IDependencyData>, platform: string): void {
66+
this.$platformsData.getPlatformData(platform).platformProjectService.beforePrepareAllPlugins(dependencies).wait();
67+
}
68+
69+
protected afterPrepare(dependencies: IDictionary<IDependencyData>, platform: string): void {
70+
this.$platformsData.getPlatformData(platform).platformProjectService.afterPrepareAllPlugins().wait();
71+
this.writePreparedDependencyInfo(dependencies, platform);
72+
}
73+
74+
private writePreparedDependencyInfo(dependencies: IDictionary<IDependencyData>, platform: string): void {
75+
let prepareData: IDictionary<boolean> = {};
76+
_.values(dependencies).forEach(d => {
77+
prepareData[d.name] = true;
78+
});
79+
this.$fs.createDirectory(this.preparedPlatformsDir(platform)).wait();
80+
this.$fs.writeJson(this.preparedPlatformsFile(platform), prepareData, " ", "utf8").wait();
81+
}
82+
83+
private preparedPlatformsDir(platform: string): string {
84+
const platformRoot = this.$platformsData.getPlatformData(platform).projectRoot;
85+
if (/android/i.test(platform)) {
86+
return path.join(platformRoot, "build", "intermediates");
87+
} else if (/ios/i.test(platform)) {
88+
return path.join(platformRoot, "build");
89+
} else {
90+
throw new Error("Invalid platform: " + platform);
91+
}
92+
}
93+
94+
private preparedPlatformsFile(platform: string): string {
95+
return path.join(this.preparedPlatformsDir(platform), "prepared-platforms.json");
96+
}
97+
98+
protected getPreviouslyPreparedDependencies(platform: string): IDictionary<boolean> {
99+
if (!this.$fs.exists(this.preparedPlatformsFile(platform)).wait()) {
100+
return {};
101+
}
102+
return this.$fs.readJson(this.preparedPlatformsFile(platform), "utf8").wait();
103+
}
104+
105+
private allPrepared(dependencies: IDictionary<IDependencyData>, platform: string): boolean {
106+
let result = true;
107+
const previouslyPrepared = this.getPreviouslyPreparedDependencies(platform);
108+
_.values(dependencies).forEach(d => {
109+
if (!previouslyPrepared[d.name]) {
110+
result = false;
111+
}
112+
});
113+
return result;
114+
}
115+
65116
public preparePlugins(dependencies: IDictionary<IDependencyData>, platform: string): void {
66-
if (_.isEmpty(dependencies)) {
117+
if (_.isEmpty(dependencies) || this.allPrepared(dependencies, platform)) {
67118
return;
68119
}
69120

70-
this.$platformsData.getPlatformData(platform).platformProjectService.beforePrepareAllPlugins(dependencies).wait();
71-
121+
this.beforePrepare(dependencies, platform);
72122
_.each(dependencies, dependency => {
73123
let isPlugin = !!dependency.nativescript;
74124
if (isPlugin) {
75-
console.log("preparing: " + dependency.name);
76125
this.$pluginsService.prepare(dependency, platform).wait();
77126
}
78127
});
79-
this.$platformsData.getPlatformData(platform).platformProjectService.afterPrepareAllPlugins().wait();
128+
this.afterPrepare(dependencies, platform);
80129
}
81130
}

test/plugin-prepare.ts

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import {assert} from "chai";
2+
import {NpmPluginPrepare} from "../lib/tools/node-modules/node-modules-dest-copy";
3+
4+
require("should");
5+
6+
class TestNpmPluginPrepare extends NpmPluginPrepare {
7+
public preparedDependencies: IDictionary<boolean> = {};
8+
9+
constructor(private previouslyPrepared: IDictionary<boolean>) {
10+
super(null, null, null);
11+
}
12+
13+
protected getPreviouslyPreparedDependencies(platform: string): IDictionary<boolean> {
14+
return this.previouslyPrepared;
15+
}
16+
17+
protected beforePrepare(dependencies: IDictionary<IDependencyData>, platform: string): void {
18+
_.values(dependencies).forEach(d => {
19+
this.preparedDependencies[d.name] = true;
20+
});
21+
}
22+
23+
protected afterPrepare(dependencies: IDictionary<IDependencyData>, platform: string): void {
24+
// DO NOTHING
25+
}
26+
}
27+
28+
describe("Plugin preparation", () => {
29+
it("skips prepare if no plugins", () => {
30+
const pluginPrepare = new TestNpmPluginPrepare({});
31+
pluginPrepare.preparePlugins({}, "android");
32+
assert.deepEqual({}, pluginPrepare.preparedDependencies);
33+
});
34+
35+
it("skips prepare if every plugin prepared", () => {
36+
const pluginPrepare = new TestNpmPluginPrepare({"tns-core-modules-widgets": true});
37+
const testDependencies: IDictionary<IDependencyData> = {
38+
"0": {
39+
name: "tns-core-modules-widgets",
40+
version: "1.0.0",
41+
nativescript: null,
42+
}
43+
};
44+
pluginPrepare.preparePlugins(testDependencies, "android");
45+
assert.deepEqual({}, pluginPrepare.preparedDependencies);
46+
});
47+
48+
it("saves prepared plugins after preparation", () => {
49+
const pluginPrepare = new TestNpmPluginPrepare({"tns-core-modules-widgets": true});
50+
const testDependencies: IDictionary<IDependencyData> = {
51+
"0": {
52+
name: "tns-core-modules-widgets",
53+
version: "1.0.0",
54+
nativescript: null,
55+
},
56+
"1": {
57+
name: "nativescript-calendar",
58+
version: "1.0.0",
59+
nativescript: null,
60+
}
61+
};
62+
pluginPrepare.preparePlugins(testDependencies, "android");
63+
const prepareData = {"tns-core-modules-widgets": true, "nativescript-calendar": true};
64+
assert.deepEqual(prepareData, pluginPrepare.preparedDependencies);
65+
});
66+
});

0 commit comments

Comments
 (0)