1717const fs = require ( 'fs' ) ;
1818const mkdirp = require ( 'mkdirp' ) ;
1919
20- const { ModelManager , Factory , Serializer } = require ( '@accordproject/concerto-core' ) ;
21- const ModelFile = require ( '@accordproject/concerto-core' ) . ModelFile ;
22- const DefaultModelFileLoader = require ( '@accordproject/concerto-core' ) . DefaultModelFileLoader ;
20+ const ModelLoader = require ( '@accordproject/concerto-core' ) . ModelLoader ;
21+ const Factory = require ( '@accordproject/concerto-core' ) . Factory ;
22+ const Serializer = require ( '@accordproject/concerto-core' ) . Serializer ;
2323const FileWriter = require ( '@accordproject/concerto-tools' ) . FileWriter ;
2424const CodeGen = require ( '@accordproject/concerto-tools' ) . CodeGen ;
2525
@@ -30,77 +30,12 @@ const PlantUMLVisitor = CodeGen.PlantUMLVisitor;
3030const TypescriptVisitor = CodeGen . TypescriptVisitor ;
3131const XmlSchemaVisitor = CodeGen . XmlSchemaVisitor ;
3232
33- const defaultSystemContent = `namespace org.accordproject.base
34- abstract asset Asset { }
35- abstract participant Participant { }
36- abstract transaction Transaction identified by transactionId {
37- o String transactionId
38- }
39- abstract event Event identified by eventId {
40- o String eventId
41- }` ;
42- const defaultSystemName = '@org.accordproject.base' ;
43-
4433/**
4534 * Utility class that implements the commands exposed by the CLI.
4635 * @class
4736 * @memberof module:concerto-cli
4837 */
4938class Commands {
50-
51- /**
52- * Add model file
53- *
54- * @param {object } modelFileLoader - the model loader
55- * @param {object } modelManager - the model manager
56- * @param {string } ctoFile - the model file
57- * @param {boolean } system - whether this is a system model
58- * @return {object } the model manager
59- */
60- static async addModel ( modelFileLoader , modelManager , ctoFile , system ) {
61- let modelFile = null ;
62- if ( system && ! ctoFile ) {
63- modelFile = new ModelFile ( modelManager , defaultSystemContent , defaultSystemName , true ) ;
64- } else if ( modelFileLoader . accepts ( ctoFile ) ) {
65- modelFile = await modelFileLoader . load ( ctoFile ) ;
66- } else {
67- const content = fs . readFileSync ( ctoFile , 'utf8' ) ;
68- modelFile = new ModelFile ( modelManager , content , ctoFile ) ;
69- }
70-
71- if ( system ) {
72- modelManager . addModelFile ( modelFile , modelFile . getName ( ) , false , true ) ;
73- } else {
74- modelManager . addModelFile ( modelFile , modelFile . getName ( ) , true , false ) ;
75- }
76-
77- return modelManager ;
78- }
79-
80- /**
81- * Load system and models in a new model manager
82- *
83- * @param {string } ctoSystemFile - the system model file
84- * @param {string[] } ctoFiles - the CTO files (can be local file paths or URLs)
85- * @return {object } the model manager
86- */
87- static async loadModelManager ( ctoSystemFile , ctoFiles ) {
88- let modelManager = new ModelManager ( ) ;
89- const modelFileLoader = new DefaultModelFileLoader ( modelManager ) ;
90-
91- // Load system model
92- modelManager = await Commands . addModel ( modelFileLoader , modelManager , ctoSystemFile , true ) ;
93-
94- // Load user models
95- for ( let ctoFile of ctoFiles ) {
96- modelManager = await Commands . addModel ( modelFileLoader , modelManager , ctoFile , false ) ;
97- }
98-
99- // Validate update models
100- await modelManager . updateExternalModels ( ) ;
101- return modelManager ;
102- }
103-
10439 /**
10540 * Validate a sample JSON against the model
10641 *
@@ -112,7 +47,7 @@ class Commands {
11247 static async validate ( sample , ctoSystemFile , ctoFiles ) {
11348 const json = JSON . parse ( fs . readFileSync ( sample , 'utf8' ) ) ;
11449
115- const modelManager = await Commands . loadModelManager ( ctoSystemFile , ctoFiles ) ;
50+ const modelManager = await ModelLoader . loadModelManager ( ctoSystemFile , ctoFiles ) ;
11651 const factory = new Factory ( modelManager ) ;
11752 const serializer = new Serializer ( factory , modelManager ) ;
11853
@@ -129,7 +64,7 @@ class Commands {
12964 * @param {string } output the output directory
13065 */
13166 static async compile ( target , ctoSystemFile , ctoFiles , output ) {
132- const modelManager = await Commands . loadModelManager ( ctoSystemFile , ctoFiles ) ;
67+ const modelManager = await ModelLoader . loadModelManager ( ctoSystemFile , ctoFiles ) ;
13368
13469 let visitor = null ;
13570
@@ -174,7 +109,7 @@ class Commands {
174109 * @param {string } output the output directory
175110 */
176111 static async get ( ctoSystemFile , ctoFiles , output ) {
177- const modelManager = await Commands . loadModelManager ( ctoSystemFile , ctoFiles ) ;
112+ const modelManager = await ModelLoader . loadModelManager ( ctoSystemFile , ctoFiles ) ;
178113 mkdirp . sync ( output ) ;
179114 modelManager . writeModelsToFileSystem ( output ) ;
180115 return `Loaded external models in '${ output } '.` ;
0 commit comments