android-install.js 3.4 KB

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