Skip to content

Commit

Permalink
refactor(*) Create concerto-util package, make the file loaders gener…
Browse files Browse the repository at this point in the history
…ic for reuse

Signed-off-by: jeromesimeon <[email protected]>
  • Loading branch information
jeromesimeon committed Nov 30, 2021
1 parent 3a42e10 commit 6977dd4
Show file tree
Hide file tree
Showing 63 changed files with 1,164 additions and 384 deletions.
2 changes: 1 addition & 1 deletion packages/concerto-cli/lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');

const Logger = require('@accordproject/concerto-util').Logger;
const Printer = require('@accordproject/concerto-cto').Printer;
const Parser = require('@accordproject/concerto-cto').Parser;
const Logger = require('@accordproject/concerto-core').Logger;
const ModelLoader = require('@accordproject/concerto-core').ModelLoader;
const Factory = require('@accordproject/concerto-core').Factory;
const Serializer = require('@accordproject/concerto-core').Serializer;
Expand Down
1 change: 1 addition & 0 deletions packages/concerto-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"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",
Expand Down
14 changes: 1 addition & 13 deletions packages/concerto-core/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,6 @@ 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()
Expand Down Expand Up @@ -208,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 @@ -235,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 {a17601f3c2c7e81f521c8779093b03fa} 2021-11-24
Version 1.2.2 {f9142bfb889e33e7f04f95fa9a4171aa} 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
12 changes: 0 additions & 12 deletions packages/concerto-core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,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 @@ -78,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

This file was deleted.

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

'use strict';

const TypedStack = require('../serializer/typedstack');
const TypedStack = require('@accordproject/concerto-util').TypedStack;
const Resource = require('./resource');

/**
Expand Down
9 changes: 7 additions & 2 deletions packages/concerto-core/lib/modelloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
const fs = require('fs');

const Parser = require('@accordproject/concerto-cto').Parser;
const DefaultModelFileLoader = require('./introspect/loaders/defaultmodelfileloader');
const DefaultFileLoader = require('@accordproject/concerto-util').DefaultFileLoader;
const ModelFile = require('./introspect/modelfile');
const ModelManager = require('./modelmanager');

Expand Down Expand Up @@ -65,7 +65,12 @@ class ModelLoader {
*/
static async loadModelManager(ctoFiles, options = { offline: false }) {
let modelManager = new ModelManager(options);
const modelFileLoader = new DefaultModelFileLoader(modelManager);
// How to create a modelfile from the external content
const processFile = (name, data) => {
const ast = Parser.parse(data);
return new ModelFile(modelManager, ast, data, name);
};
const modelFileLoader = new DefaultFileLoader(processFile);

// Load user models
for(let ctoFile of ctoFiles) {
Expand Down
24 changes: 17 additions & 7 deletions packages/concerto-core/lib/modelmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ const fs = require('fs');
const fsPath = require('path');
const slash = require('slash');

const DefaultFileLoader = require('@accordproject/concerto-util').DefaultFileLoader;
const FileDownloader = require('@accordproject/concerto-util').FileDownloader;
const Parser = require('@accordproject/concerto-cto').Parser;
const DefaultModelFileLoader = require('./introspect/loaders/defaultmodelfileloader');

const Factory = require('./factory');
const Globalize = require('./globalize');
const IllegalModelException = require('./introspect/illegalmodelexception');
const ModelFile = require('./introspect/modelfile');
const ModelFileDownloader = require('./introspect/loaders/modelfiledownloader');
const ModelUtil = require('./modelutil');
const Serializer = require('./serializer');
const TypeNotFoundException = require('./typenotfoundexception');
Expand Down Expand Up @@ -277,20 +278,29 @@ abstract concept Event {}
* Downloads all ModelFiles that are external dependencies and adds or
* updates them in this ModelManager.
* @param {Object} [options] - Options object passed to ModelFileLoaders
* @param {ModelFileDownloader} [modelFileDownloader] - an optional ModelFileDownloader
* @param {FileDownloader} [fileDownloader] - an optional FileDownloader
* @throws {IllegalModelException} if the models fail validation
* @return {Promise} a promise when the download and update operation is completed.
*/
async updateExternalModels(options, modelFileDownloader) {
async updateExternalModels(options, fileDownloader) {

const NAME = 'updateExternalModels';
debug(NAME, 'updateExternalModels', options);

if(!modelFileDownloader) {
modelFileDownloader = new ModelFileDownloader(new DefaultModelFileLoader(this));
if(!fileDownloader) {
// How to create a modelfile from the external content
const processFile = (name, data) => {
const ast = Parser.parse(data);
return new ModelFile(this, ast, data, name);
};
// How to get external imports
const getExternalImports = (file) => {
return file.getExternalImports();
};
fileDownloader = new FileDownloader(new DefaultFileLoader(processFile), getExternalImports);
}

const externalModelFiles = await modelFileDownloader.downloadExternalDependencies(this.getModelFiles(), options)
const externalModelFiles = await fileDownloader.downloadExternalDependencies(this.getModelFiles(), options)
.catch(error => {
// If we're not able to download the latest dependencies, see whether the models all validate based on the available cached models.
if(error.code === 'MISSING_DEPENDENCY'){
Expand Down
2 changes: 1 addition & 1 deletion packages/concerto-core/lib/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

'use strict';

const TypedStack = require('@accordproject/concerto-util').TypedStack;
const DateTimeUtil = require('./datetimeutil');
const Globalize = require('./globalize');
const JSONGenerator = require('./serializer/jsongenerator');
const JSONPopulator = require('./serializer/jsonpopulator');
const Typed = require('./model/typed');
const ResourceValidator = require('./serializer/resourcevalidator');
const TypedStack = require('./serializer/typedstack');

const { utcOffset: defaultUtcOffset } = DateTimeUtil.setCurrentTime();
const baseDefaultOptions = {
Expand Down
5 changes: 2 additions & 3 deletions packages/concerto-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"repository": {
"type": "git",
"url": "https://github.com/accordproject/concerto.git",
"directory": "packages/concerto-core"
"directory": "packages/concerto-cto"
},
"keywords": [
"blockchain",
Expand Down Expand Up @@ -58,13 +58,12 @@
"yargs": "17.1.0"
},
"dependencies": {
"@accordproject/concerto-util": "1.2.1",
"@accordproject/concerto-cto": "1.2.1",
"@supercharge/promise-pool": "1.7.0",
"axios": "0.23.0",
"colors": "1.4.0",
"dayjs": "1.10.4",
"debug": "4.3.1",
"json-colorizer": "2.2.2",
"lorem-ipsum": "2.0.3",
"randexp": "0.5.3",
"semver": "7.3.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/concerto-core/scripts/api-changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
const child_process = require('child_process');
const fs = require('fs');
const path = require('path');
const Logger = require('../lib/logger');
const Logger = require('@accordproject/concerto-util').Logger;

const parentDirectory = path.resolve(__dirname, '..');
const apiSignatureFile = path.resolve(parentDirectory, 'api.txt');
Expand Down
2 changes: 1 addition & 1 deletion packages/concerto-core/scripts/apisignaturegenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
'use strict';

const fs = require('fs');
const Writer = require('../lib/writer');
const Writer = require('@accordproject/concerto-util').Writer;

/**
* Converts an array of parameter types to a string
Expand Down
2 changes: 1 addition & 1 deletion packages/concerto-core/scripts/changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

const fs = require('fs');
const yargs = require('yargs');
const Logger = require('../lib/logger');
const Logger = require('@accordproject/concerto-util').Logger;
const VersionChecker = require('./versionchecker');

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/concerto-core/scripts/parsejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const fs = require('fs-extra');
const klaw = require('klaw');
const path = require('path');
const program = require('commander');
const Logger = require('../lib/logger');
const Logger = require('@accordproject/concerto-util').Logger;
const APISignatureGenerator = require('./apisignaturegenerator');
const JavaScriptParser = require('./javascriptparser');
const JSONGenerator = require('./jsongenerator');
Expand Down
2 changes: 1 addition & 1 deletion packages/concerto-core/scripts/versionchecker.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

const crypto = require('crypto');
const semver = require('semver');
const Logger = require('../lib/logger');
const Logger = require('@accordproject/concerto-util').Logger;

/**
* Checks that a change log file takes into account
Expand Down
Loading

0 comments on commit 6977dd4

Please sign in to comment.