mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-05-03 09:24:00 +00:00
workflow processor
This commit is contained in:
@@ -1,13 +1,16 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const yaml = require('js-yaml');
|
const yaml = require('js-yaml');
|
||||||
|
const rimraf = require('rimraf');
|
||||||
|
const _ = require('lodash');
|
||||||
|
|
||||||
const indir = path.resolve(path.join(__dirname, '..', 'workflow-templates'));
|
const indir = path.resolve(path.join(__dirname, '..', 'workflow-templates'));
|
||||||
const outdir = path.resolve(path.join(__dirname, '..', 'workflow-templates'));
|
const outdir = path.resolve(path.join(__dirname, '..', 'workflow-templates'));
|
||||||
|
|
||||||
const includes = {};
|
const includes = {};
|
||||||
|
|
||||||
for (const file of fs.readdirSync(indir)) {
|
function readIncludes() {
|
||||||
|
for (const file of fs.readdirSync(indir)) {
|
||||||
const text = fs.readFileSync(path.join(indir, file), { encoding: 'utf-8' });
|
const text = fs.readFileSync(path.join(indir, file), { encoding: 'utf-8' });
|
||||||
const json = yaml.load(text);
|
const json = yaml.load(text);
|
||||||
if (json._module) {
|
if (json._module) {
|
||||||
@@ -18,9 +21,78 @@ for (const file of fs.readdirSync(indir)) {
|
|||||||
includes[key] = json[key];
|
includes[key] = json[key];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const file of fs.readdirSync(indir)) {
|
let modified = false;
|
||||||
|
|
||||||
|
function conditionMatch(condition, args) {
|
||||||
|
if (_.isString(condition)) {
|
||||||
|
return args.name == condition;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function processJsonStep(json, args) {
|
||||||
|
return _.cloneDeepWith(value, x => {
|
||||||
|
if (_.isArray(value)) {
|
||||||
|
const res = [];
|
||||||
|
for (const item of value) {
|
||||||
|
if (item._if) {
|
||||||
|
modified = true;
|
||||||
|
if (conditionMatch(item._if, args)) {
|
||||||
|
res.push(_.omit(item, ['_if']));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value?.run && _.isArray(value.run)) {
|
||||||
|
const newrun = [];
|
||||||
|
for (const item of value.run) {
|
||||||
|
let replaced = false;
|
||||||
|
for (const repl of args.run) {
|
||||||
|
if (item == repl.from) {
|
||||||
|
replaced = true;
|
||||||
|
modified = true;
|
||||||
|
newrun.push(...repl.to);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!replaced) {
|
||||||
|
newrun.push(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
...value,
|
||||||
|
run: newrun,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_.isPlainObject(value)) {
|
||||||
|
if (_.intersection(args.allNames ?? [], Object.keys(values))?.length > 0) {
|
||||||
|
modified = true;
|
||||||
|
return value[args.name];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function processJson(json, args = {}) {
|
||||||
|
const MAX_STEPS = 64;
|
||||||
|
for (let i = 0; i < MAX_STEPS; i++) {
|
||||||
|
modified = false;
|
||||||
|
json = processJsonStep(json, args);
|
||||||
|
if (!modified) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
|
function processFiles() {
|
||||||
|
for (const file of fs.readdirSync(indir)) {
|
||||||
const text = fs.readFileSync(path.join(indir, file), { encoding: 'utf-8' });
|
const text = fs.readFileSync(path.join(indir, file), { encoding: 'utf-8' });
|
||||||
const json = yaml.load(text);
|
const json = yaml.load(text);
|
||||||
|
|
||||||
@@ -29,14 +101,27 @@ for (const file of fs.readdirSync(indir)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (json._templates) {
|
if (json._templates) {
|
||||||
for (const template of json._templates) {
|
for (const key in json._templates) {
|
||||||
const outfile = template.file;
|
const allNames = Object.keys(json._templates);
|
||||||
const text = template.text;
|
const args = {
|
||||||
const json = template.json;
|
key,
|
||||||
const out = path.join(outdir, name);
|
run: json._templates[key],
|
||||||
fs.writeFileSync(out, text);
|
allNames,
|
||||||
|
};
|
||||||
|
const converted = processJson(_.omit(json, ['_templates']), args);
|
||||||
|
const out = path.join(outdir, json._templates[key].file);
|
||||||
|
fs.writeFileSync(out, yaml.dump(converted));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fs.writeFileSync(path.join(outdir, file), yaml.dump(processJson(json)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function run() {
|
||||||
|
await new Promise(resolve => rimraf(outdir, resolve));
|
||||||
|
readIncludes();
|
||||||
|
processFiles();
|
||||||
|
}
|
||||||
|
|
||||||
|
run();
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ jobs:
|
|||||||
id: meta
|
id: meta
|
||||||
uses: docker/metadata-action@v4
|
uses: docker/metadata-action@v4
|
||||||
with:
|
with:
|
||||||
images: |
|
images:
|
||||||
_community: dbgate/dbgate
|
_community: dbgate/dbgate
|
||||||
_premium: dbgate/dbgate-premium
|
_premium: dbgate/dbgate-premium
|
||||||
flavor: |
|
flavor: |
|
||||||
|
|||||||
Reference in New Issue
Block a user