-
Notifications
You must be signed in to change notification settings - Fork 508
/
Copy pathandroid-install.js
93 lines (76 loc) · 3.39 KB
/
android-install.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env node
module.exports = function (context) {
var path = require('path'),
fs = require('fs'),
shell = require('shelljs'),
semver = require('semver'),
// var path = context.requireCordovaModule('path'),
// fs = context.requireCordovaModule('fs'),
// shell = context.requireCordovaModule('shelljs'),
// semver = context.requireCordovaModule('semver'),
projectRoot = context.opts.projectRoot,
plugins = context.opts.plugins || [];
// The plugins array will be empty during platform add
if (plugins.length > 0 && plugins.indexOf('cordova-plugin-wechat') === -1) {
return ;
}
var ConfigParser = null;
try {
ConfigParser = context.requireCordovaModule('cordova-common').ConfigParser;
} catch(e) {
// fallback
ConfigParser = context.requireCordovaModule('cordova-lib/src/configparser/ConfigParser');
}
var config = new ConfigParser(path.join(context.opts.projectRoot, "config.xml")),
packageName = config.android_packageName() || config.packageName();
// replace dash (-) with underscore (_)
packageName = packageName.replace(/-/g , "_");
console.info("Running android-install.Hook: " + context.hook + ", Package: " + packageName + ", Path: " + projectRoot + ".");
if (!packageName) {
console.error("Package name could not be found!");
return ;
}
// android platform available?
if (context.opts.cordova.platforms.indexOf("android") === -1) {
console.info("Android platform has not been added.");
return ;
}
var targetDir = path.join(projectRoot, "platforms", "android", "src", packageName.replace(/\./g, path.sep), "wxapi");
if (!fs.existsSync(targetDir)) {
targetDir = path.join(projectRoot, "platforms", "android", "app", "src", "main", "java", packageName.replace(/\./g, path.sep), "wxapi");
}
// var engines = config.getEngines();
// engines.forEach(function(item,index) {
// if(item.name == 'android') {
// var sepc = item.spec.replace('~','').replace('^','');
// console.log(sepc);
// if (semver.gte(sepc,'7.0.0')) {
// console.info("Android platform Version above 7.0.0");
// targetDir = path.join(projectRoot, "platforms", "android", "app", "src", "main", "java", packageName.replace(/\./g, path.sep), "wxapi");
// }
// }
// });
console.log(targetDir);
var targetFiles = ["EntryActivity.java", "WXEntryActivity.java", "WXPayEntryActivity.java"];
if (['after_plugin_add', 'after_plugin_install'].indexOf(context.hook) === -1) {
// remove it?
targetFiles.forEach(function (f) {
try {
fs.unlinkSync(path.join(targetDir, f));
} catch (err) {}
});
} else {
// create directory
shell.mkdir('-p', targetDir);
// sync the content
targetFiles.forEach(function (f) {
fs.readFile(path.join(context.opts.plugin.dir, 'src', 'android', f), {encoding: 'utf-8'}, function (err, data) {
if (err) {
throw err;
}
data = data.replace(/__PACKAGE_NAME__/ig, packageName);
fs.writeFileSync(path.join(targetDir, f), data);
});
});
}
};