publish_qqgame.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // v1.8.8
  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 del = require(ideModuleDir + "del");
  9. const revCollector = require(ideModuleDir + 'gulp-rev-collector');
  10. const { getEngineVersion, getFileMd5, canUsePluginEngine } = require("./pub_utils");
  11. const provider = "1109625052";
  12. const minPluginVersion = "2.1.1";
  13. let fullRemoteEngineList = ["laya.core.js", "laya.filter.js", "laya.ani.js", "laya.tiledmap.js", "laya.d3.js", "laya.html.js", "laya.particle.js", "laya.ui.js", "laya.webgl.js", "laya.filter.js", "laya.d3Plugin.js"];
  14. let copyLibsTask = ["copyPlatformLibsJsFile"];
  15. let versiontask = ["version2"];
  16. let
  17. config,
  18. releaseDir;
  19. let versionCon; // 版本管理version.json
  20. let commandSuffix,
  21. layarepublicPath;
  22. gulp.task("preCreate_QQ", copyLibsTask, function() {
  23. releaseDir = global.releaseDir;
  24. config = global.config;
  25. commandSuffix = global.commandSuffix;
  26. layarepublicPath = global.layarepublicPath;
  27. if (config.useMinJsLibs) {
  28. fullRemoteEngineList = fullRemoteEngineList.map((item, index) => {
  29. return item.replace(".js", ".min.js");
  30. })
  31. }
  32. });
  33. gulp.task("copyPlatformFile_QQ", ["preCreate_QQ"], function() {
  34. let adapterPath = path.join(layarepublicPath, "LayaAirProjectPack", "lib", "data", "qqfiles");
  35. let hasPublishPlatform =
  36. fs.existsSync(path.join(releaseDir, "game.js")) &&
  37. fs.existsSync(path.join(releaseDir, "game.json")) &&
  38. fs.existsSync(path.join(releaseDir, "project.config.json"));
  39. let copyLibsList;
  40. if (hasPublishPlatform) {
  41. copyLibsList = [`${adapterPath}/weapp-adapter.js`];
  42. } else {
  43. copyLibsList = [`${adapterPath}/*.*`];
  44. }
  45. var stream = gulp.src(copyLibsList);
  46. return stream.pipe(gulp.dest(releaseDir));
  47. });
  48. gulp.task("modifyFile_QQ", versiontask, function() {
  49. // 修改game.json文件
  50. let gameJsonPath = path.join(releaseDir, "game.json");
  51. let content = fs.readFileSync(gameJsonPath, "utf8");
  52. let conJson = JSON.parse(content);
  53. conJson.deviceOrientation = config.qqInfo.orientation;
  54. content = JSON.stringify(conJson, null, 4);
  55. fs.writeFileSync(gameJsonPath, content, "utf8");
  56. if (config.version) {
  57. let versionPath = releaseDir + "/version.json";
  58. versionCon = fs.readFileSync(versionPath, "utf8");
  59. versionCon = JSON.parse(versionCon);
  60. }
  61. let indexJsStr = (versionCon && versionCon["index.js"]) ? versionCon["index.js"] : "index.js";
  62. // QQ小游戏项目,修改index.js
  63. let filePath = path.join(releaseDir, indexJsStr);
  64. if (!fs.existsSync(filePath)) {
  65. return;
  66. }
  67. let fileContent = fs.readFileSync(filePath, "utf8");
  68. fileContent = fileContent.replace(/loadLib(\(['"])/gm, "require$1./");
  69. fs.writeFileSync(filePath, fileContent, "utf8");
  70. });
  71. gulp.task("modifyMinJs_QQ", ["modifyFile_QQ"], 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]+)?\.qqmini\.min\.js/gm, "laya.qqmini.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]+)?\.qqmini(\.min)?\.js/gm, "min/laya.qqmini.min.js");
  85. fs.writeFileSync(fileJsPath, content, 'utf-8');
  86. });
  87. gulp.task("version_QQ", ["modifyMinJs_QQ"], 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]+)?\.qqmini/gm, "laya.qqmini");
  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("pluginEngin_QQ", ["version_QQ"], function(cb) {
  106. if (!config.uesEnginePlugin) { // 没有使用引擎插件,还是像以前一样发布
  107. let gameJsonPath = path.join(releaseDir, "game.json");
  108. let gameJsonContent = fs.readFileSync(gameJsonPath, "utf8");
  109. let conJson = JSON.parse(gameJsonContent);
  110. if (conJson.plugins) {
  111. delete conJson.plugins;
  112. gameJsonContent = JSON.stringify(conJson, null, 4);
  113. fs.writeFileSync(gameJsonPath, gameJsonContent, "utf8");
  114. let gameJsPath = path.join(releaseDir, "game.js");
  115. let gameJscontent = fs.readFileSync(gameJsPath, "utf8");
  116. gameJscontent = gameJscontent.replace(/requirePlugin\("[\w\/\.]+"\)(;|,)?\n?/mg, "");
  117. fs.writeFileSync(gameJsPath, gameJscontent, "utf8");
  118. }
  119. return cb();
  120. }
  121. if (config.version) {
  122. let versionPath = releaseDir + "/version.json";
  123. versionCon = fs.readFileSync(versionPath, "utf8");
  124. versionCon = JSON.parse(versionCon);
  125. }
  126. let indexJsStr = (versionCon && versionCon["index.js"]) ? versionCon["index.js"] : "index.js";
  127. // 获取version等信息
  128. let coreLibPath = path.join(workSpaceDir, "bin", "libs", "laya.core.js");
  129. let isHasCoreLib = fs.existsSync(coreLibPath);
  130. let isOldAsProj = fs.existsSync(`${workSpaceDir}/asconfig.json`) && !isHasCoreLib;
  131. let isNewTsProj = fs.existsSync(`${workSpaceDir}/src/tsconfig.json`) && !isHasCoreLib;
  132. let EngineVersion = getEngineVersion();
  133. if (!EngineVersion) {
  134. throw new Error(`读取引擎版本号失败,请于服务提供商联系!`);
  135. }
  136. if (!EngineVersion || EngineVersion.includes("beta") || !canUsePluginEngine(EngineVersion, minPluginVersion)) {
  137. throw new Error(`该版本引擎无法使用引擎插件功能(engineVersion: ${EngineVersion})`);
  138. }
  139. console.log(`通过版本号检查: ${EngineVersion}`);
  140. // 使用引擎插件
  141. let localUseEngineList = [];
  142. let copyEnginePathList;
  143. new Promise(function(resolve, reject) {
  144. console.log(`修改game.js和game.json`);
  145. // 1) 修改game.js和game.json
  146. // 修改game.js
  147. let gameJsPath = path.join(releaseDir, "game.js");
  148. let platformJs = config.useMinJsLibs ? `require("./libs/min/laya.qqmini.min.js");` : `require("./libs/laya.qqmini.js");`;
  149. let gameJscontent = `require("weapp-adapter.js");\n${platformJs}\nrequirePlugin('layaPlugin');\nwindow.loadLib = require;\nrequire("./${indexJsStr}");`;
  150. fs.writeFileSync(gameJsPath, gameJscontent, "utf8");
  151. // 修改game.json,使其支持引擎插件
  152. let gameJsonPath = path.join(releaseDir, "game.json");
  153. let gameJsonContent = fs.readFileSync(gameJsonPath, "utf8");
  154. let conJson = JSON.parse(gameJsonContent);
  155. conJson.plugins = {
  156. "layaPlugin": {
  157. "version": EngineVersion,
  158. "provider": provider,
  159. "path": "laya-libs"
  160. }
  161. }
  162. gameJsonContent = JSON.stringify(conJson, null, 4);
  163. fs.writeFileSync(gameJsonPath, gameJsonContent, "utf8");
  164. resolve();
  165. }).then(function() {
  166. return new Promise(function(resolve, reject) {
  167. console.log(`确定用到的插件引擎`);
  168. // 2) 确定用到了那些插件引擎,并将插件引擎从index.js的引用中去掉
  169. let indexJsPath = path.join(releaseDir, indexJsStr);
  170. let indexJsCon = fs.readFileSync(indexJsPath, "utf8");
  171. let item, fullRequireItem;
  172. for (let i = 0, len = fullRemoteEngineList.length; i < len; i++) {
  173. item = fullRemoteEngineList[i];
  174. fullRequireItem = config.useMinJsLibs ? `require("./libs/min/${item}")` : `require("./libs/${item}")`;
  175. if (indexJsCon.includes(fullRequireItem)) {
  176. let _item = item.replace(".min.js", ".js"), _minItem = item;
  177. localUseEngineList.push(_item);
  178. indexJsCon = indexJsCon.replace(fullRequireItem + ";", "").replace(fullRequireItem + ",", "").replace(fullRequireItem, "");
  179. // 如果引用了压缩的类库,将其重命名为未压缩的类库,并拷贝到libs中
  180. if (config.useMinJsLibs) {
  181. let oldMinlibPath = path.join(releaseDir, "libs", "min", _minItem);
  182. let newMinlibPath = path.join(releaseDir, "libs", "min", _item);
  183. let newlibPath = path.join(releaseDir, "libs", _item);
  184. fs.renameSync(oldMinlibPath, newMinlibPath);
  185. // fs.copyFileSync(newlibPath, newMinlibPath);
  186. let con = fs.readFileSync(newMinlibPath, "utf8");
  187. fs.writeFileSync(newlibPath, con, "utf8");
  188. fs.unlinkSync(newMinlibPath);
  189. }
  190. }
  191. }
  192. if (isOldAsProj || isNewTsProj) { // 如果as||ts_new语言,开发者将laya.js也写入index.js中了,将其删掉
  193. fullRequireItem = `require("./laya.js")`;
  194. if (indexJsCon.includes(fullRequireItem)) {
  195. indexJsCon = indexJsCon.replace(fullRequireItem + ";", "").replace(fullRequireItem + ",", "").replace(fullRequireItem, "");
  196. }
  197. }
  198. fs.writeFileSync(indexJsPath, indexJsCon, "utf8");
  199. // ts/js再次修改game.js,仅引用使用到的类库
  200. // as||ts_new因为本地只有laya.js,无法仅引用使用到的类库
  201. if (!isOldAsProj && !isNewTsProj) {
  202. let pluginCon = "";
  203. localUseEngineList.forEach(function(item) {
  204. pluginCon += `requirePlugin("layaPlugin/${item}");\n`;
  205. });
  206. let gameJsPath = path.join(releaseDir, "game.js");
  207. let gameJsCon = fs.readFileSync(gameJsPath, "utf8");
  208. gameJsCon = gameJsCon.replace(`requirePlugin('layaPlugin');`, pluginCon);
  209. fs.writeFileSync(gameJsPath, gameJsCon, "utf8");
  210. }
  211. resolve();
  212. });
  213. }).then(function() {
  214. return new Promise(function(resolve, reject) {
  215. console.log(`将本地的引擎插件移动到laya-libs中`);
  216. // 3) 将本地的引擎插件移动到laya-libs中
  217. let libsPath = /** config.useMinJsLibs ? `${releaseDir}/libs/min` : */`${releaseDir}/libs`;
  218. copyEnginePathList = [`${libsPath}/{${localUseEngineList.join(",")}}`];
  219. if (isOldAsProj || isNewTsProj) { // 单独拷贝laya.js
  220. copyEnginePathList = [`${releaseDir}/laya.js`];
  221. }
  222. gulp.src(copyEnginePathList).pipe(gulp.dest(`${releaseDir}/laya-libs`));
  223. setTimeout(resolve, 500);
  224. });
  225. }).then(function() {
  226. return new Promise(function(resolve, reject) {
  227. console.log(`将libs中的本地引擎插件删掉`);
  228. // 4) 将libs中的本地引擎插件删掉
  229. del(copyEnginePathList, { force: true }).then(resolve);
  230. });
  231. }).then(function() {
  232. return new Promise(async function(resolve, reject) {
  233. console.log(`完善引擎插件目录`);
  234. // 5) 引擎插件目录laya-libs中还需要新建几个文件,使该目录能够使用
  235. if (isOldAsProj || isNewTsProj) { // 单独拷贝laya.js
  236. localUseEngineList.push("laya.js");
  237. }
  238. let
  239. layalibsPath = path.join(releaseDir, "laya-libs"),
  240. engineIndex = path.join(layalibsPath, "index.js"),
  241. engineplugin = path.join(layalibsPath, "plugin.json"),
  242. enginesignature = path.join(layalibsPath, "signature.json");
  243. // index.js
  244. if (!fs.existsSync(layalibsPath)) {
  245. throw new Error("引擎插件目录创建失败,请与服务提供商联系!");
  246. }
  247. let indexCon = "";
  248. localUseEngineList.forEach(function(item) {
  249. indexCon += `require("./${item}");\n`;
  250. });
  251. fs.writeFileSync(engineIndex, indexCon, "utf8");
  252. // plugin.json
  253. let pluginCon = {"main": "index.js"};
  254. fs.writeFileSync(engineplugin, JSON.stringify(pluginCon, null, 4), "utf8");
  255. // signature.json,目前平台方将其作为保留用途,不会做插件的md5校验;IDE仍将生成md5
  256. let signatureCon = {
  257. "provider": provider,
  258. "signature": []
  259. };
  260. localUseEngineList.unshift("index.js");
  261. let fileName, md5Str;
  262. for (let i = 0, len = localUseEngineList.length; i < len; i++) {
  263. fileName = localUseEngineList[i];
  264. let md5Str = await getFileMd5(path.join(releaseDir, "laya-libs", fileName));
  265. signatureCon.signature.push({
  266. "path": fileName,
  267. "md5": md5Str
  268. });
  269. }
  270. fs.writeFileSync(enginesignature, JSON.stringify(signatureCon, null, 4), "utf8");
  271. resolve();
  272. });
  273. })
  274. .then(function() {
  275. cb();
  276. }).catch(function(e) {
  277. throw e;
  278. })
  279. });
  280. gulp.task("buildQQProj", ["pluginEngin_QQ"], function() {
  281. console.log("all tasks completed");
  282. });