publish_youkugame.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // v1.0.0
  2. const ideModuleDir = global.ideModuleDir;
  3. const workSpaceDir = global.workSpaceDir;
  4. //引用插件模块
  5. const gulp = require(ideModuleDir + "gulp");
  6. const fs = require("fs");
  7. const path = require("path");
  8. const revCollector = require(ideModuleDir + 'gulp-rev-collector');
  9. let copyLibsTask = ["copyPlatformLibsJsFile"];
  10. let versiontask = ["version2"];
  11. let
  12. config,
  13. releaseDir;
  14. let versionCon; // 版本管理version.json
  15. let commandSuffix,
  16. layarepublicPath;
  17. gulp.task("preCreate_youku", copyLibsTask, function() {
  18. releaseDir = global.releaseDir;
  19. config = global.config;
  20. commandSuffix = global.commandSuffix;
  21. layarepublicPath = global.layarepublicPath;
  22. });
  23. gulp.task("copyPlatformFile_youku", ["preCreate_youku"], function() {
  24. let adapterPath = path.join(layarepublicPath, "LayaAirProjectPack", "lib", "data", "youkufiles");
  25. let hasPublishPlatform =
  26. fs.existsSync(path.join(releaseDir, "game.js")) &&
  27. fs.existsSync(path.join(releaseDir, "game.json")) &&
  28. fs.existsSync(path.join(releaseDir, "project.config.json"));
  29. let copyLibsList;
  30. if (hasPublishPlatform) {
  31. copyLibsList = [`${adapterPath}/my-adapter.js`];
  32. } else {
  33. copyLibsList = [`${adapterPath}/*.*`];
  34. }
  35. var stream = gulp.src(copyLibsList);
  36. return stream.pipe(gulp.dest(releaseDir));
  37. });
  38. gulp.task("modifyFile_youku", versiontask, function() {
  39. // 修改game.json文件
  40. let gameJsonPath = path.join(releaseDir, "game.json");
  41. let content = fs.readFileSync(gameJsonPath, "utf8");
  42. let conJson = JSON.parse(content);
  43. conJson.screenOrientation = config.youkuInfo.orientation;
  44. content = JSON.stringify(conJson, null, 4);
  45. fs.writeFileSync(gameJsonPath, content, "utf8");
  46. // 修改game.js
  47. let filePath = path.join(releaseDir, "game.js");
  48. let fileContent = fs.existsSync(filePath) && fs.readFileSync(filePath, "utf8");
  49. let reWriteMainJs = !fs.existsSync(filePath) || !fileContent.includes("ykmini");
  50. if (reWriteMainJs) {
  51. fileContent = `window.navigator.userAgent += " youku";
  52. require("./my-adapter.js");
  53. require("./libs/laya.ykmini.js");\nrequire("./index.js");`;
  54. fs.writeFileSync(filePath, fileContent, "utf8");
  55. }
  56. if (config.version || config.enableVersion) {
  57. let versionPath = releaseDir + "/version.json";
  58. versionCon = fs.readFileSync(versionPath, "utf8");
  59. versionCon = JSON.parse(versionCon);
  60. }
  61. // 修改index.js
  62. let indexJsStr = (versionCon && versionCon["index.js"]) ? versionCon["index.js"] : "index.js";
  63. let indexFilePath = path.join(releaseDir, indexJsStr);
  64. if (!fs.existsSync(indexFilePath)) {
  65. return;
  66. }
  67. let indexFileContent = fs.readFileSync(indexFilePath, "utf8");
  68. indexFileContent = indexFileContent.replace(/loadLib(\(['"])/gm, "require$1./");
  69. fs.writeFileSync(indexFilePath, indexFileContent, "utf8");
  70. })
  71. gulp.task("modifyMinJs_youku", ["modifyFile_youku"], function() {
  72. // 如果保留了平台文件,如果同时取消使用min类库,就会出现文件引用不正确的问题
  73. if (config.keepPlatformFile) {
  74. let fileJsPath = path.join(releaseDir, "game.js");
  75. let content = fs.readFileSync(fileJsPath, "utf-8");
  76. content = content.replace(/min\/laya(-[\w\d]+)?\.ykmini\.min\.js/gm, "laya.ykmini.js");
  77. fs.writeFileSync(fileJsPath, content, 'utf-8');
  78. }
  79. if (!config.useMinJsLibs) {
  80. return;
  81. }
  82. let fileJsPath = path.join(releaseDir, "game.js");
  83. let content = fs.readFileSync(fileJsPath, "utf-8");
  84. content = content.replace(/(min\/)?laya(-[\w\d]+)?\.ykmini(\.min)?\.js/gm, "min/laya.ykmini.min.js");
  85. fs.writeFileSync(fileJsPath, content, 'utf-8');
  86. });
  87. gulp.task("version_youku", ["modifyMinJs_youku"], function () {
  88. // 如果保留了平台文件,如果同时开启版本管理,就会出现文件引用不正确的问题
  89. if (config.keepPlatformFile) {
  90. let fileJsPath = path.join(releaseDir, "game.js");
  91. let content = fs.readFileSync(fileJsPath, "utf-8");
  92. content = content.replace(/laya(-[\w\d]+)?\.ykmini/gm, "laya.ykmini");
  93. content = content.replace(/index(-[\w\d]+)?\.js/gm, "index.js");
  94. fs.writeFileSync(fileJsPath, content, 'utf-8');
  95. }
  96. if (config.version) {
  97. let versionPath = releaseDir + "/version.json";
  98. let gameJSPath = releaseDir + "/game.js";
  99. let srcList = [versionPath, gameJSPath];
  100. return gulp.src(srcList)
  101. .pipe(revCollector())
  102. .pipe(gulp.dest(releaseDir));
  103. }
  104. });
  105. gulp.task("buildYKProj", ["version_youku"], function() {
  106. console.log("all tasks completed");
  107. });