assets-android-pack.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const fs = require('fs');
  2. const path = require('fire-path');
  3. const setting = require('./build-setting');
  4. const utils = require('./libs/utils');
  5. module.exports = (function (){
  6. var _instance;
  7. function AssetsAndroidPack (params) {
  8. return {
  9. _ready: false,
  10. _dest: null,
  11. _temp: null,
  12. _list: [],
  13. init (actualPlatform) {
  14. this._ready = false;
  15. let androidPack = `androidPack`;
  16. let pack = setting.getInstance().getItem(androidPack);
  17. if (!pack) { Editor.warn('没有配置信息'); return false; }
  18. if (!pack.enable) { Editor.warn('未开启安卓打包!'); return false; }
  19. if (actualPlatform !== 'android') {
  20. Editor.warn(actualPlatform, '平台不支持安卓打包');
  21. return false;
  22. }
  23. if (!pack.dest || !pack.list) {
  24. Editor.warn('配置信息不全');
  25. return false;
  26. }
  27. let destPath = path.join(Editor.Project.path, pack.dest);
  28. let stat = fs.statSync(destPath);
  29. if (!stat.isDirectory()) {
  30. Editor.error('目标根目录不存在');
  31. return false;
  32. }
  33. let tempPath;
  34. if (pack.temp) {
  35. let aPath = path.join(Editor.Project.path, pack.temp);
  36. let stat = fs.statSync(aPath);
  37. if (stat.isDirectory()) {
  38. tempPath = aPath;
  39. }
  40. }
  41. if (pack.list.length === 0) {
  42. Editor.error('没有需要拷贝的资源');
  43. return false;
  44. }
  45. this._dest = destPath;
  46. this._temp = tempPath;
  47. this._list = pack.list;
  48. this._ready = true;
  49. return true;
  50. },
  51. isReady () {
  52. return this._ready;
  53. },
  54. copyAssets (callback) {
  55. for (const aPath of this._list) {
  56. let index = aPath.lastIndexOf('/');
  57. let destPath = this._dest + aPath.substr(index);
  58. // 临时工程目录
  59. let tempPath;
  60. if (this._temp) {
  61. tempPath = this._temp + aPath.substr(index);
  62. }
  63. let srcPath = path.join(Editor.Project.path, aPath);
  64. let stat = fs.statSync(srcPath);
  65. if (stat.isDirectory()) {
  66. if (fs.existsSync(destPath)) {
  67. utils.rmdirSync(destPath);
  68. }
  69. fs.mkdirSync(destPath);
  70. utils.copyFolder(srcPath, destPath);
  71. // 临时工程目录
  72. if (tempPath) {
  73. if (fs.existsSync(tempPath)) {
  74. utils.rmdirSync(tempPath);
  75. }
  76. fs.mkdirSync(tempPath);
  77. utils.copyFolder(srcPath, tempPath);
  78. }
  79. } else {
  80. fs.copyFileSync(srcPath, destPath);
  81. // 临时工程目录
  82. if (tempPath) {
  83. fs.copyFileSync(srcPath, tempPath);
  84. }
  85. }
  86. callback(srcPath);
  87. }
  88. },
  89. getCopyCount () {
  90. return this._list.length;
  91. }
  92. };
  93. }
  94. return {
  95. getInstance (params) {
  96. if (!_instance){
  97. _instance = AssetsAndroidPack(params);
  98. }
  99. return _instance;
  100. }
  101. };
  102. })();