Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core+parser) Move CTO parser/printer out of core into its own package #353

Merged
merged 8 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/concerto-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ require('yargs')
Logger.error(err.message);
});
})
.command('import', 'import a cto string into its metamodel', (yargs) => {
yargs.demandOption(['input'], 'Please provide an input cto');
.command('parse', 'parse a cto string to a JSON syntax tree', (yargs) => {
yargs.demandOption(['input'], 'Please provide an input cto string');
yargs.option('model', {
describe: 'array of concerto model files',
type: 'string',
Expand All @@ -171,7 +171,7 @@ require('yargs')
type: 'string'
});
}, (argv) => {
return Commands.import(argv.input, argv.model, argv.resolve, argv.all, argv.output)
return Commands.parse(argv.input, argv.model, argv.resolve, argv.all, argv.output)
.then((result) => {
if (result) {
Logger.info(result);
Expand All @@ -181,8 +181,8 @@ require('yargs')
Logger.error(err.message);
});
})
.command('export', 'export a metamodel to cto syntax', (yargs) => {
yargs.demandOption(['input'], 'Please provide an input metamodel');
.command('print', 'print a JSON syntax tree to a cto string', (yargs) => {
yargs.demandOption(['input'], 'Please provide an input Concerto syntax tree');
yargs.option('input', {
describe: 'the metamodel to export',
type: 'string'
Expand All @@ -192,7 +192,7 @@ require('yargs')
type: 'string'
});
}, (argv) => {
return Commands.export(argv.input, argv.model, argv.all, argv.output)
return Commands.print(argv.input, argv.model, argv.all, argv.output)
.then((result) => {
if (result) {
Logger.info(result);
Expand Down
19 changes: 10 additions & 9 deletions packages/concerto-cli/lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');

const Logger = require('@accordproject/concerto-core').Logger;
const Logger = require('@accordproject/concerto-util').Logger;
const Printer = require('@accordproject/concerto-cto').Printer;
const Parser = require('@accordproject/concerto-cto').Parser;
const ModelLoader = require('@accordproject/concerto-core').ModelLoader;
const Factory = require('@accordproject/concerto-core').Factory;
const Serializer = require('@accordproject/concerto-core').Serializer;
Expand Down Expand Up @@ -190,7 +192,7 @@ class Commands {
}

/**
* Import a CTO string to its metamodel
* Parse a cto string to a JSON syntax tree
*
* @param {string} input - CTO
* @param {string[]} [ctoFiles] - the CTO files used for import resolution
Expand All @@ -199,7 +201,7 @@ class Commands {
* @param {string} outputPath to an output file
* @param {string} the metamodel
*/
static async import(input, ctoFiles = [], resolve = false, all = false, outputPath) {
static async parse(input, ctoFiles = [], resolve = false, all = false, outputPath) {
// Add input to ctoFiles for convenience
if (!ctoFiles.includes(input)) {
ctoFiles.push(input);
Expand All @@ -212,10 +214,9 @@ class Commands {
if (all) {
result = MetaModel.modelManagerToMetaModel(modelManager, resolve);
} else {
result = Parser.parse(inputString);
if (resolve) {
result = MetaModel.ctoToMetaModelAndResolve(modelManager, inputString);
} else {
result = MetaModel.ctoToMetaModel(inputString);
result = MetaModel.resolveMetaModel(modelManager, result);
}
}
if (outputPath) {
Expand All @@ -227,16 +228,16 @@ class Commands {
}

/**
* Export a metamodel to a CTO string
* Print a JSON syntax tree to a cto string
*
* @param {string} input metamodel
* @param {string} outputPath to an output file
* @param {string} transformed (meta)model
*/
static async export(input, outputPath) {
static async print(input, outputPath) {
const inputString = fs.readFileSync(input, 'utf8');
const json = JSON.parse(inputString);
const result = MetaModel.ctoFromMetaModel(json);
const result = Printer.toCTO(json);
if (outputPath) {
Logger.info('Creating file: ' + outputPath);
fs.writeFileSync(outputPath, result);
Expand Down
2 changes: 2 additions & 0 deletions packages/concerto-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
"tmp-promise": "3.0.2"
},
"dependencies": {
"@accordproject/concerto-util": "1.2.1",
"@accordproject/concerto-core": "1.2.1",
"@accordproject/concerto-cto": "1.2.1",
"@accordproject/concerto-tools": "1.2.1",
"mkdirp": "1.0.4",
"yargs": "17.1.0"
Expand Down
16 changes: 8 additions & 8 deletions packages/concerto-cli/test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,17 +209,17 @@ describe('cicero-cli', () => {
});
});

describe('#import', async () => {
describe('#parse', async () => {
it('should transform cto to metamodel', async () => {
const expected = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'models/contract.json')));
const result = JSON.parse(await Commands.import(path.resolve(__dirname, 'models/contract.cto')));
const result = JSON.parse(await Commands.parse(path.resolve(__dirname, 'models/contract.cto')));
result.should.deep.equal(expected);
});

it('should transform cto to metamodel and save it', async () => {
const output = await tmp.file({ unsafeCleanup: true });
const expected = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'models/contract.json')));
await Commands.import(path.resolve(__dirname, 'models/contract.cto'), undefined, undefined, undefined, output.path);
await Commands.parse(path.resolve(__dirname, 'models/contract.cto'), undefined, undefined, undefined, output.path);
const result = JSON.parse(fs.readFileSync(output.path));
result.should.deep.equal(expected);
output.cleanup();
Expand All @@ -228,31 +228,31 @@ describe('cicero-cli', () => {
it('should transform cto to metamodel and resolve names', async () => {
const expected = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'models/contractResolved.json')));
const contractFile = path.resolve(__dirname, 'models/contract.cto');
const result = JSON.parse(await Commands.import(contractFile, [contractFile], true));
const result = JSON.parse(await Commands.parse(contractFile, [contractFile], true));
result.should.deep.equal(expected);
});

it('should transform cto to metamodel and resolve names', async () => {
const expected = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'models/contractResolvedAll.json')));
const contractFile = path.resolve(__dirname, 'models/contract.cto');
const result = JSON.parse(await Commands.import(contractFile, [contractFile], true, true));
const result = JSON.parse(await Commands.parse(contractFile, [contractFile], true, true));
result.should.deep.equal(expected);
});
});

describe('#export', async () => {
describe('#print', async () => {
it('should transform a metamodel to cto', async () => {
const expected = fs.readFileSync(path.resolve(__dirname, 'models/contract2.cto'), 'utf-8');
const metamodel = path.resolve(__dirname, 'models/contract.json');
const result = await Commands.export(metamodel);
const result = await Commands.print(metamodel);
result.should.equal(expected);
});

it('should transform a metamodel to cto and save it', async () => {
const output = await tmp.file({ unsafeCleanup: true });
const expected = fs.readFileSync(path.resolve(__dirname, 'models/contract2.cto'), 'utf-8');
const metamodel = path.resolve(__dirname, 'models/contract.json');
await Commands.export(metamodel, output.path);
await Commands.print(metamodel, output.path);
const result = fs.readFileSync(output.path, 'utf-8');
result.should.equal(expected);
output.cleanup();
Expand Down
2 changes: 1 addition & 1 deletion packages/concerto-core/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Concerto Core

Main library for Concerto model parsing, model manager, JSON instance validation and serialization.
Main library for Concerto models, model manager, JSON instance validation and serialization.

# Installation

Expand Down
36 changes: 4 additions & 32 deletions packages/concerto-core/api.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
class BaseException extends Error {
+ void constructor(string,string)
}
class BaseFileException extends BaseException {
+ void constructor(string,string,string,string?,string?)
+ string getFileLocation()
+ string getShortMessage()
+ string getFileName()
}
class Concerto {
+ void constructor(modelManager)
+ void validate(obj,options?) throws Error
Expand Down Expand Up @@ -99,33 +90,20 @@ class Introspector {
+ ClassDeclaration[] getClassDeclarations()
+ ClassDeclaration getClassDeclaration(String) throws Error
}
class ModelFileDownloader {
+ void constructor(ModelFileLoader,Number)
+ Promise downloadExternalDependencies(ModelFile[],Object?)
+ Promise runJob(Object,Object)
}
class MetaModel {
+ string getMetaModelCto()
+ void createMetaModelManager()
+ object validateMetaModel(object)
+ object createNameTable(modelManager,object)
+ string resolveName(string,object)
+ object resolveTypeNames(object,object)
+ object resolveMetaModel(object,object)
+ object resolveMetaModel(object,object,boolean?)
+ object modelFileToMetaModel(object,boolean?)
+ object modelManagerToMetaModel(object,boolean?,boolean?)
+ string decoratorArgFromMetaModel(object)
+ string decoratorFromMetaModel(object)
+ string decoratorsFromMetaModel(object,string)
+ string propertyFromMetaModel(object)
+ string declFromMetaModel(object)
+ string ctoFromMetaModel(object,boolean?)
+ object modelManagerFromMetaModel(object,boolean?)
+ object ctoToMetaModel(string,boolean?)
+ object ctoToMetaModelAndResolve(modelManager,string,boolean?)
}
class ModelFile {
+ void constructor(ModelManager,string,string?) throws IllegalModelException
+ void constructor(ModelManager,object,string?,string?) throws IllegalModelException
+ boolean isModelFile()
+ Boolean isSystemModelFile()
+ boolean isExternal()
Expand All @@ -148,6 +126,7 @@ class ModelFile {
+ ClassDeclaration[] getDeclarations(Function)
+ ClassDeclaration[] getAllDeclarations()
+ string getDefinitions()
+ object getAst()
+ string getConcertoVersion()
+ void isCompatibleVersion()
}
Expand Down Expand Up @@ -224,7 +203,7 @@ class ModelManager {
+ void deleteModelFile(string)
+ Object[] addModelFiles(|,string[],boolean?)
+ void validateModelFiles()
+ Promise updateExternalModels(Object?,ModelFileDownloader?) throws IllegalModelException
+ Promise updateExternalModels(Object?,FileDownloader?) throws IllegalModelException
+ void writeModelsToFileSystem(string,Object?,boolean)
+ Object[] getModels(Object?,boolean)
+ void clearModelFiles()
Expand All @@ -251,13 +230,6 @@ class Serializer {
+ Object toJSON(Resource,Object?,boolean?,boolean?,boolean?,boolean?,boolean?,number?) throws Error
+ Resource fromJSON(Object,Object,boolean,boolean,number?)
}
class TypedStack {
+ void constructor(Object)
+ void push(Object,Object)
+ Object pop(Object)
+ Object peek(Object)
+ void clear()
}
class TypeNotFoundException extends BaseException {
+ void constructor(string,string?,string)
+ string getTypeName()
Expand Down
2 changes: 1 addition & 1 deletion packages/concerto-core/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# Note that the latest public API is documented using JSDocs and is available in api.txt.
#

Version 1.2.2 {82c68f1a011449a7783092b0aff0b928} 2021-11-24
Version 1.2.2 {f3671c1416edd096c788dffd63639b48} 2021-11-24
- Remove custom instanceof and add methods to check runtime type
- Remove support for Node 12
- Generate Typescript definitions from JSDoc
Expand Down
15 changes: 0 additions & 15 deletions packages/concerto-core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
*/

// Exceptions
module.exports.BaseException = require('./lib/baseexception');
module.exports.BaseFileException = require('./lib/basefileexception');
module.exports.ParseException = require('./lib/introspect/parseexception');
module.exports.SecurityException = require('./lib/securityexception');
module.exports.IllegalModelException = require('./lib/introspect/illegalmodelexception');
module.exports.TypeNotFoundException = require('./lib/typenotfoundexception');
Expand Down Expand Up @@ -57,9 +54,6 @@ module.exports.Identifiable = require('./lib/model/identifiable');
module.exports.Relationship = require('./lib/model/relationship');
module.exports.Resource = require('./lib/model/resource');

// Writers
module.exports.Writer = require('./lib/writer');

// Factory
module.exports.Factory = require('./lib/factory');

Expand All @@ -81,22 +75,13 @@ module.exports.Serializer = require('./lib/serializer');
// ModelUtil
module.exports.ModelUtil = require('./lib/modelutil');

// ModelFileLoaders
module.exports.DefaultModelFileLoader = require('./lib/introspect/loaders/defaultmodelfileloader');

// ModelLoader
module.exports.ModelLoader = require('./lib/modelloader');


// DateTimeUtil
module.exports.DateTimeUtil = require('./lib/datetimeutil');

// Logger
module.exports.Logger = require('./lib/logger');

// TypedStack
module.exports.TypedStack = require('./lib/serializer/typedstack');

// Concerto
module.exports.Concerto = require('./lib/concerto');

Expand Down
2 changes: 1 addition & 1 deletion packages/concerto-core/lib/concerto.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

const URIJS = require('urijs');
const RESOURCE_SCHEME = 'resource';
const TypedStack = require('./serializer/typedstack');
const TypedStack = require('@accordproject/concerto-util').TypedStack;
const ObjectValidator = require('./serializer/objectvalidator');

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/concerto-core/lib/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

'use strict';

const TypedStack = require('@accordproject/concerto-util').TypedStack;

const debug = require('debug')('concerto:Factory');
const Globalize = require('./globalize');

Expand All @@ -22,7 +24,6 @@ const ModelUtil = require('./modelutil');
const InstanceGenerator = require('./serializer/instancegenerator');
const ValueGeneratorFactory = require('./serializer/valuegenerator');
const ResourceValidator = require('./serializer/resourcevalidator');
const TypedStack = require('./serializer/typedstack');

const Relationship = require('./model/relationship');
const Resource = require('./model/resource');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

'use strict';

const BaseFileException = require('../basefileexception');
const BaseFileException = require('@accordproject/concerto-cto').BaseFileException;

/**
* Exception throws when a composer file is semantically invalid
Expand Down

This file was deleted.

Loading