From 5e6b776b9d91a1fa2aaf7962a6388add775e8e2d Mon Sep 17 00:00:00 2001 From: Shen Lang Date: Sun, 25 Jul 2021 23:06:31 +0800 Subject: [PATCH 1/3] feat(toolchain): automatically generate index.js of plugin --- file-generation.js | 173 +++++++++++++++++++++++++++++++++++++++++++++ gulpfile.js | 4 ++ package.json | 1 + 3 files changed, 178 insertions(+) create mode 100644 file-generation.js diff --git a/file-generation.js b/file-generation.js new file mode 100644 index 0000000..f63e03d --- /dev/null +++ b/file-generation.js @@ -0,0 +1,173 @@ +/// + const { series, dest } = require("gulp"); + const fs = require("fs"); +//const fsPromises = require("fs/promises"); + +// const tap = require("gulp-tap"); +// const log = require("gulplog"); +// const pump = require("pump"); + const path = require("path"); + +/** + * @type{IPlugin} + */ +class AutoFileGenerationSupport { + constructor() { + this.intermediateDir = "./out/before-plugin"; + this.bundleSources = [ + "scripts/plugin/index.js", + "scripts/plugin/*/index.js", + "scripts/plugin/plugins.json" + ]; + this.writeBack = true; + this.bundleDir = undefined; + + const _this = this; + this.sourceTasks = [ + { + condition: this.bundleSources, + preventDefault: false, + task: pack => + dest([path.posix.join(this.intermediateDir, pack.relativePath)]) + + } + ]; + } + + set builder(builder) { + if (builder._version < 1) { + throw new Error( + "browserify support requires using a minecraft-addon-toolchain with at least version 1 or higher" + ); + } + this._builder = builder; + } + + addDefaultTasks(gulpTasks) { + const generateFile = this._generateFile.bind(this); + generateFile.displayName = "generateFile"; + + gulpTasks.buildSource = series(gulpTasks.buildSource, generateFile); + } + + _generateFile(done) { + return this._builder.foreachPack( + "browserify", + "behavior", + (pack, packDone) => { + const sourceDir = path.join( + this._builder.sourceDir, + pack.relativePath + ); + const packDir = path.join( + this.intermediateDir, + pack.relativePath + ); + const destination = path.join( + this.bundleDir || this._builder.bundleDir, + pack.relativePath + ); + function getPluginsJSONSync() { + try { + let jsonData = fs.readFileSync(path.join( + packDir, + 'scripts/plugin/plugins.json' + )); + return JSON.parse(jsonData); + } catch { + return null; + } + } + function havePluginJsSync() { + try { + fs.accessSync(path.join( + packDir, + 'scripts/plugin/index.js' + )); + return true; + } catch { + return false; + } + } + function getPluginDirsSync() { + let dirs = fs.readdirSync(path.join( + packDir, + 'scripts/plugin' + )); + return dirs.filter((dir) => { + try { + fs.accessSync(path.join( + packDir, + 'scripts/plugin', + dir, + 'index.js' + )); + return true; + } catch { + return false; + } + }); + } + if(havePluginJsSync()) { + return packDone(); + } + let pluginJSON = getPluginsJSONSync() || {}; + let pluginDirs = getPluginDirsSync(); + for(let dirName of pluginDirs) { + if(pluginJSON[dirName] === undefined) { + console.log(`Add dir ${dirName}`); + pluginJSON[dirName] = { + type: "inner", + enable: true + }; + } + } + let pluginJs = ` +/* +** This file is automatically generated, +** to know more, see file-generation.js +*/ +` + + Object.keys(pluginJSON).map((pluginName) => { + let plugin = pluginJSON[pluginName]; + if(plugin === false) { + return ''; + } + if(!plugin.enable) { + return '\n'; + } + switch(plugin.type) { + case 'inner': + return `import './${pluginName}/index.js';\n`; + break; + default: + let err = new Error( + `Unknown type ${plugin.type} of plugin ${pluginName}` + ); + packDone(err); + throw err; + break; + } + }).join(''); + if(this.writeBack) { + console.log(path.join( + sourceDir, + 'scripts/plugin/plugins.json' + ), JSON.stringify(pluginJSON, null, '\t')); + fs.writeFileSync(path.join( + sourceDir, + 'scripts/plugin/plugins.json' + ), JSON.stringify(pluginJSON, null, '\t')); + } + fs.writeFileSync(path.join( + destination, + 'scripts/plugin/index.js' + ), pluginJs); + return packDone(); + }, + done + ); + } +} + +module.exports = AutoFileGenerationSupport; diff --git a/gulpfile.js b/gulpfile.js index 5934c91..732c19a 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,12 +1,16 @@ const MinecraftAddonBuilder = require("minecraft-addon-toolchain/v1"); const BrowserifySupport = require("minecraft-addon-toolchain-browserify"); +const AutoFileGenerationSupport = require("./file-generation.js"); const builder = new MinecraftAddonBuilder("NormaConstructor"); //!!!!!!!!!!!!!!!!!!!! const browserifySupport = new BrowserifySupport(); +const autoFileGenerationSupport = new AutoFileGenerationSupport(); browserifySupport.bundleSources.push("scripts/**/*.*"); +autoFileGenerationSupport.bundleDir = browserifySupport.intermediateDir; +builder.addPlugin(autoFileGenerationSupport); builder.addPlugin(browserifySupport); module.exports = builder.configureEverythingForMe(); diff --git a/package.json b/package.json index 04f84eb..0665d94 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "test": "gulp build && node test.js" }, "devDependencies": { + "gulp": "^4.0.2", "minecraft-addon-toolchain": "^1.0.3", "minecraft-addon-toolchain-browserify": "^1.0.3" }, From 80fa877be3ddff5db53c147228249dea55a39433 Mon Sep 17 00:00:00 2001 From: Shen Lang Date: Sun, 25 Jul 2021 23:07:56 +0800 Subject: [PATCH 2/3] feat(toolchain): make index.js of plugin automatically generated --- packs/behaviors/scripts/plugin/index.js | 5 ----- packs/behaviors/scripts/plugin/plugins.json | 6 ++++++ 2 files changed, 6 insertions(+), 5 deletions(-) delete mode 100644 packs/behaviors/scripts/plugin/index.js create mode 100644 packs/behaviors/scripts/plugin/plugins.json diff --git a/packs/behaviors/scripts/plugin/index.js b/packs/behaviors/scripts/plugin/index.js deleted file mode 100644 index 9f8e090..0000000 --- a/packs/behaviors/scripts/plugin/index.js +++ /dev/null @@ -1,5 +0,0 @@ -/* -** This file is planed to be automatically upgraded, -** but currently it isn't. -*/ -import './nc/index.js'; diff --git a/packs/behaviors/scripts/plugin/plugins.json b/packs/behaviors/scripts/plugin/plugins.json new file mode 100644 index 0000000..c1ea57b --- /dev/null +++ b/packs/behaviors/scripts/plugin/plugins.json @@ -0,0 +1,6 @@ +{ + "nc": { + "type": "inner", + "enable": true + } +} \ No newline at end of file From 246fb83bfb489efe8655b8c1b93fc0c3a9d5110f Mon Sep 17 00:00:00 2001 From: Shen Lang Date: Sun, 25 Jul 2021 23:09:54 +0800 Subject: [PATCH 3/3] feat(manifest): update the version to 0.13.13 --- packs/behaviors/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packs/behaviors/manifest.json b/packs/behaviors/manifest.json index cfd5978..b95c554 100644 --- a/packs/behaviors/manifest.json +++ b/packs/behaviors/manifest.json @@ -7,7 +7,7 @@ "version": [ 0, 13, - 12 + 13 ] }, "modules": [