diff --git a/common/processWorkflows.js b/common/processWorkflows.js index 8fc2c7350..41ed6d4d3 100644 --- a/common/processWorkflows.js +++ b/common/processWorkflows.js @@ -1,42 +1,127 @@ const fs = require('fs'); const path = require('path'); const yaml = require('js-yaml'); +const rimraf = require('rimraf'); +const _ = require('lodash'); const indir = path.resolve(path.join(__dirname, '..', 'workflow-templates')); const outdir = path.resolve(path.join(__dirname, '..', 'workflow-templates')); const includes = {}; -for (const file of fs.readdirSync(indir)) { - const text = fs.readFileSync(path.join(indir, file), { encoding: 'utf-8' }); - const json = yaml.load(text); - if (json._module) { - for (const key in json) { - if (key === '_module') { - continue; +function readIncludes() { + for (const file of fs.readdirSync(indir)) { + const text = fs.readFileSync(path.join(indir, file), { encoding: 'utf-8' }); + const json = yaml.load(text); + if (json._module) { + for (const key in json) { + if (key === '_module') { + continue; + } + includes[key] = json[key]; } - includes[key] = json[key]; } } } -for (const file of fs.readdirSync(indir)) { - const text = fs.readFileSync(path.join(indir, file), { encoding: 'utf-8' }); - const json = yaml.load(text); +let modified = false; - if (json._module) { - continue; +function conditionMatch(condition, args) { + if (_.isString(condition)) { + return args.name == condition; } + return false; +} - if (json._templates) { - for (const template of json._templates) { - const outfile = template.file; - const text = template.text; - const json = template.json; - const out = path.join(outdir, name); - fs.writeFileSync(out, text); +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 json = yaml.load(text); + + if (json._module) { + continue; + } + + if (json._templates) { + for (const key in json._templates) { + const allNames = Object.keys(json._templates); + const args = { + key, + run: json._templates[key], + 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(); diff --git a/workflow-templates/build-docker.tpl.yaml b/workflow-templates/build-docker.tpl.yaml index 030b8c10e..e5b8f935a 100644 --- a/workflow-templates/build-docker.tpl.yaml +++ b/workflow-templates/build-docker.tpl.yaml @@ -44,7 +44,7 @@ jobs: id: meta uses: docker/metadata-action@v4 with: - images: | + images: _community: dbgate/dbgate _premium: dbgate/dbgate-premium flavor: | @@ -76,12 +76,12 @@ jobs: node-version: 18.x - _include: - _name: checkout-dbgate-pro - _if: _premium + _name: checkout-dbgate-pro + _if: _premium - _include: - _name: merge-dbgate-pro - _if: _premium + _name: merge-dbgate-pro + _if: _premium - name: adjustPackageJson run: |