Skip to content

Commit

Permalink
use export only in builder
Browse files Browse the repository at this point in the history
  • Loading branch information
metelkin committed Sep 5, 2024
1 parent ce558c5 commit b054e5d
Show file tree
Hide file tree
Showing 16 changed files with 140 additions and 115 deletions.
2 changes: 1 addition & 1 deletion bin/heta-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { Command } = require('commander');
const program = new Command();
const fs = require('fs');
const path = require('path');
const { Builder } = require('../src/builder');
const { Builder } = require('../src');
const { load } = require('js-yaml'); // https://www.npmjs.com/package/js-yaml
const _merge = require('lodash/merge');
const semver = require('semver');
Expand Down
56 changes: 56 additions & 0 deletions cases/0-hello-world/platform copy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
id: mm
builderVersion: '*'
options:
logLevel: info
debug: false
unitsCheck: true
importModule:
type: heta
source: src/index.heta
export:
- format: HetaCode
filepath: heta_code
id: heta_code
- format: SBML
filepath: mm_sbml_l2v4
version: L2V4
spaceFilter: mm
- format: SBML
filepath: mm_sbml_l3v1
version: L3V1
spaceFilter: mm
- format: Simbio
filepath: mm_simbio
spaceFilter: mm
- format: JSON
filepath: full_json
spaceFilter: mm
- format: YAML
filepath: full_yaml
spaceFilter: mm
- format: SLV
filepath: mm_slv
version: 25
spaceFilter: mm
- format: Julia
filepath: mm_julia
spaceFilter: mm
- format: DBSolve
filepath: mm_dbsolve
spaceFilter: mm
- format: Mrgsolve
filepath: mm_mrg
spaceFilter: mm
- format: Matlab
filepath: matlab1
spaceFilter: mm
- format: XLSX
filepath: table
spaceFilter: mm
- format: Table
filepath: table2
spaceFilter: mm
bookType: biff8
- format: Dot
filepath: graph
- format: Summary
48 changes: 1 addition & 47 deletions cases/0-hello-world/platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,4 @@ options:
importModule:
type: heta
source: src/index.heta
export:
- format: HetaCode
filepath: heta_code
id: heta_code
- format: SBML
filepath: mm_sbml_l2v4
version: L2V4
spaceFilter: mm
- format: SBML
filepath: mm_sbml_l3v1
version: L3V1
spaceFilter: mm
- format: Simbio
filepath: mm_simbio
spaceFilter: mm
- format: JSON
filepath: full_json
spaceFilter: mm
- format: YAML
filepath: full_yaml
spaceFilter: mm
- format: SLV
filepath: mm_slv
version: 25
spaceFilter: mm
- format: Julia
filepath: mm_julia
spaceFilter: mm
- format: DBSolve
filepath: mm_dbsolve
spaceFilter: mm
- format: Mrgsolve
filepath: mm_mrg
spaceFilter: mm
- format: Matlab
filepath: matlab1
spaceFilter: mm
- format: XLSX
filepath: table
spaceFilter: mm
- format: Table
filepath: table2
spaceFilter: mm
bookType: biff8
- format: Dot
filepath: graph
- format: Summary

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Programming platform for Quantitative Systems Pharmacology modeling in NodeJS",
"main": "src/index.js",
"scripts": {
"test:dev": "mocha test/cases/0 --config=./test/.mocharc.json",
"test:dev": "mocha test/export/export-check --config=./test/.mocharc.json",
"test": "mocha test --config=./test/.mocharc.json",
"jsdoc": "jsdoc -r -c .jsdoc.json --readme api-references.md -d docs/dev src",
"test:cov": "nyc --reporter=lcov npm run test",
Expand Down
4 changes: 3 additions & 1 deletion src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
or precompilation of templates is also possible
*/

const { Builder } = require('./builder');
const Container = require('./container');
const coreItems = require('./container/core-items');
const ModuleSystem = require('./module-system');
Expand Down Expand Up @@ -44,7 +45,7 @@ global.compiledTemplates = {
'slv-template.slv.njk': nunjucksEnv.getTemplate('slv-template.slv.njk'),
};

Container._exportClasses = {
Builder._exportClasses = {
DBSolve: require('./dbsolve-export'),
YAML: require('./yaml-export'),
JSON: require('./json-export'),
Expand All @@ -63,6 +64,7 @@ Container._exportClasses = {
};

module.exports = {
Builder,
Container,
coreItems,
nunjucksEnv,
Expand Down
45 changes: 32 additions & 13 deletions src/builder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const fs = require('fs-extra');
const declarationSchema = require('./declaration-schema');
const Ajv = require('ajv');
const ajv = new Ajv({ useDefaults: true }); //.addSchema(declarationSchema);
const { Container, HetaLevelError } = require('../index');
const Container = require('../container');
const HetaLevelError = require('../heta-level-error');
const ModuleSystem = require('../module-system');
const { StdoutTransport } = require('../logger');

Expand Down Expand Up @@ -32,16 +33,20 @@ const { StdoutTransport } = require('../logger');
* Calculated from `declaration.options.logPath`.
* @property ... Other properties inherit from `declaration` object, see
* [CLI references]{@link https://hetalang.github.io/#/heta-compiler/cli-references?id=declaration-file-format}
* @property {object} _exportClasses map-like structure for storing all available constructors describing `_Export`s.
* @property {object[]} export Storage for `_Export` instances.
*/
class Builder {
constructor(declaration, coreDirname = '.'){
// create container
this.container = new Container();
this.container._builder = this; // back reference to parent builder
this.export = []; // storing Export objects
this.exportClasses = {}; // storing Export classes bound to builder (XXX: maybe it must be static)

// set transport and logger
this.logger = this.container.logger;
let minLogLevel = declaration.options?.logLevel || 'info';
let minLogLevel = declaration?.options?.logLevel || 'info';
this.logger.addTransport(new StdoutTransport(minLogLevel));

// check based on schema
Expand All @@ -66,13 +71,28 @@ class Builder {
this.logger.info(`Builder initialized in directory "${this._coreDirname}".`);
if (this.id) this.logger.info(`Platform id: "${this.id}"`);

// index file not found
//let indexFilepath = path.resolve(coreDirname, declaration.importModule.source);
//if (!fs.existsSync(indexFilepath)) {
// this.logger.error(`Index file "${indexFilepath}" does not exist.`, {type: 'BuilderError'});
//}
// create "export" classes bound to this container
Builder._exportClasses && Object.entries(Builder._exportClasses).forEach(([key, _Class]) => {
this.exportClasses[key] = class extends _Class {};
this.exportClasses[key].prototype._builder = this; // XXX: not sure it required
this.exportClasses[key].prototype._container = this.container; // XXX: not sure it required
});

this.exportArray = [];
// create "export" instances
declaration.export && declaration.export.forEach((exportItem) => {
let ExportClass = this.exportClasses.hasOwnProperty(exportItem.format)
&& this.exportClasses[exportItem.format];
if (ExportClass) {
this.exportArray.push(new ExportClass(exportItem));
} else {
this.logger.error(`Export format "${exportItem.format}" is not supported.`, {type: 'BuilderError'});
}
});
}

static _exportClasses = {}; // storing abstract Export classes

/**
* The method runs building of a platform declared with `Builder` object.
* If the execution throws an error platform building will stop.
Expand Down Expand Up @@ -184,11 +204,10 @@ class Builder {
*
* @method Builder#exportMany
*/
exportMany(){
let exportElements = this.container.exportArray;
this.logger.info(`Start exporting to files, total: ${exportElements.length}.`);
exportMany() {
this.logger.info(`Start exporting to files, total: ${this.exportArray.length}.`);

exportElements.forEach((exportItem) => _makeAndSave(exportItem, this._distDirname));
this.exportArray.forEach((exportItem) => _makeAndSave(exportItem, this._distDirname));
}

/**
Expand All @@ -199,7 +218,7 @@ class Builder {
*/
exportJuliaOnly(){
// create export without putting it to exportArray
let Julia = this.container.classes['Julia'];
let Julia = this.exportClasses['Julia'];
let exportItem = new Julia({
format: 'Julia',
filepath: '_julia'
Expand All @@ -209,7 +228,7 @@ class Builder {
}
}

function _makeAndSave(exportItem, pathPrefix){
function _makeAndSave(exportItem, pathPrefix) {
let logger = exportItem._container.logger;
let absPath = path.resolve(pathPrefix, exportItem.filepath);
let msg = `Exporting to "${absPath}" of format "${exportItem.format}"...`;
Expand Down
25 changes: 14 additions & 11 deletions src/container/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,29 @@ const reservedWords = [
*
* @returns {AbstractExport} The created object.
*/
Container.prototype.export = function(q = {}, isCore = false){
// TODO: this is part which will be removed later
// invisible => deprecated => removed
Container.prototype.export = function(q = {}, isCore = false) {
let { exportClasses, exportArray } = this._builder;
if (q.format === undefined) {
this.logger.error(
'Empty "format" option in #export',
{type: 'QError'}
);
return;
return; // BRAKE
}
if (typeof this.classes[q.format] !== 'function') {
this.logger.error(
`Unknown format "${q.format}" in #export action.`,
{type: 'QError'}
);
return;
if (typeof exportClasses[q.format] !== 'function') {
this.logger.error(`Unknown format "${q.format}" in #export action.`, {type: 'QError'});

return; // BRAKE
}

// create and push to storage
let exportInstance = new this.classes[q.format](q, isCore);
if (!exportInstance.errored) this.exportArray.push(exportInstance);

let exportInstance = new exportClasses[q.format](q, isCore);
if (!exportInstance.errored) {
exportArray.push(exportInstance);
}

return exportInstance;
};

Expand Down
10 changes: 1 addition & 9 deletions src/container/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const { Top } = require('../core/top');
const { UnitDef } = require('../core/unit-def');
const { FunctionDef } = require('../core/function-def');
const { Scenario } = require('../core/scenario');
// const { AbstractExport } = require('../abstract-export');
// Component classes
const { Component } = require('../core/component');
const { Record } = require('../core/record');
Expand Down Expand Up @@ -38,15 +37,14 @@ const TopoSort = require('@insysbio/topo-sort');
* @property {Logger} logger object providing transport of errors, warnings and info messages on Heta platform level.
* @property {object[]} defaultLogs Default storage of errors which will be used for diagnostics.
* The {@link JSONTransport} is used here.
* @property {object[]} exportArray Storage for `_Export` instances.
* @property {Map<string,UnitDef>} unitDefStorage Storage for `UnitDef` instances. Key is a string identifier.
* @property {Map<string,FunctionDef>} functionDefStorage Storage for `FunctionDef` instances. Key is a string identifier.
* @property {Map<string,Scenario>} scenarioStorage Storage for `Scenario` instances. Key is a string identifier.
* @property {Map<string,Namespace>} namespaceStorage Storage for `Namespace` instances. Key is a string identifier.
* There is a default namespace with identifier `nameless` which will be used as a default namespace
* for all components where namespace name is not set.
* @property {object} _componentClasses map-like structure for storing all available constructors for `Component`s.
* @property {object} _exportClasses map-like structure for storing all available constructors describing `_Export`s.
* @property {object} _builder reference to the parent builder object (if exists).
*/
class Container {
/* constructor can be run many times */
Expand All @@ -62,12 +60,6 @@ class Container {
this.classes.FunctionDef.prototype._container = this;
this.classes.Scenario = class extends Scenario {};
this.classes.Scenario.prototype._container = this;
// create "export" classes bound to this container
Container._exportClasses && Object.entries(Container._exportClasses)
.forEach(([key, _Class]) => {
this.classes[key] = class extends _Class {};
this.classes[key].prototype._container = this;
});

// logger
this.logger = new Logger();
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { Builder } = require('./builder');
const Container = require('./container');
const coreItems = require('./container/core-items');
const ModuleSystem = require('./module-system');
Expand Down Expand Up @@ -33,7 +34,7 @@ global.compiledTemplates = {
'slv-template.slv.njk': nunjucksEnv.getTemplate('slv-template.slv.njk'),
};

Container._exportClasses = {
Builder._exportClasses = {
DBSolve: require('./dbsolve-export'),
YAML: require('./yaml-export'),
JSON: require('./json-export'),
Expand All @@ -52,6 +53,7 @@ Container._exportClasses = {
};

module.exports = {
Builder,
Container,
coreItems,
nunjucksEnv,
Expand Down
4 changes: 3 additions & 1 deletion src/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module: {
]
}
*/
const { Builder } = require('./builder');
const Container = require('./container');
const coreItems = require('./container/core-items');
const ModuleSystem = require('./module-system');
Expand Down Expand Up @@ -49,7 +50,7 @@ global.compiledTemplates = {
'slv-template.slv.njk': require('./templates/slv-template.slv.njk'),
};

Container._exportClasses = {
Builder._exportClasses = {
DBSolve: require('./dbsolve-export'),
YAML: require('./yaml-export'),
JSON: require('./json-export'),
Expand All @@ -68,6 +69,7 @@ Container._exportClasses = {
};

module.exports = {
Builder,
Container,
coreItems,
//nunjucksEnv,
Expand Down
Loading

0 comments on commit b054e5d

Please sign in to comment.