Skip to content
This repository has been archived by the owner on Feb 6, 2022. It is now read-only.

Commit

Permalink
Merge pull request #77 from NorthernOceanS/issue76
Browse files Browse the repository at this point in the history
Automatically generate index.js of plugin.
  • Loading branch information
callstackexceed authored Jul 25, 2021
2 parents 94a7e87 + 246fb83 commit c6b1947
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 6 deletions.
173 changes: 173 additions & 0 deletions file-generation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/// <reference types="minecraft-addon-toolchain/v1" />
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;
4 changes: 4 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -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();
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion packs/behaviors/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"version": [
0,
13,
12
13
]
},
"modules": [
Expand Down
5 changes: 0 additions & 5 deletions packs/behaviors/scripts/plugin/index.js

This file was deleted.

6 changes: 6 additions & 0 deletions packs/behaviors/scripts/plugin/plugins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"nc": {
"type": "inner",
"enable": true
}
}

0 comments on commit c6b1947

Please sign in to comment.