-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(decorator): Added support for extracting decorators from model f…
…iles (#44) * feat(decorator): added support for extracting decorator from models Signed-off-by: Muskan Bararia <[email protected]> * feat(decorator): added support for extracting decorator from models Signed-off-by: Muskan Bararia <[email protected]> * chore: bumped up the concerto-core version Signed-off-by: Muskan Bararia <[email protected]> * chore: bumped up the concerto-core version Signed-off-by: Muskan Bararia <[email protected]> * chore: bumped up the concerto version Signed-off-by: Muskan Bararia <[email protected]> * feat: added default dir output for extract decorators and updated test cases Signed-off-by: Muskan Bararia <[email protected]> --------- Signed-off-by: Muskan Bararia <[email protected]> Co-authored-by: Muskan Bararia <[email protected]>
- Loading branch information
1 parent
3258869
commit 22b445a
Showing
8 changed files
with
643 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -687,6 +687,58 @@ class Commands { | |
throw new Error(e); | ||
} | ||
} | ||
|
||
/** | ||
* Extract the decorator command sets and vocabularies from a list of modelFiles | ||
* @param {string} modelFiles - the model from which vocabularies and decorators have to be extracted | ||
* @param {object} options - optional parameters | ||
* @param {string} options.locale - locale for extracted vocabularies | ||
* @param {string} options.removeDecoratorsFromSource - the flag to determine whether to remove decorators from source model files | ||
* @param {string} options.output - the output directory where the extracted models, decorators and vocabularies are to be written | ||
* @returns {Object} - Extracted models, decorators and vocabularies | ||
*/ | ||
static async extractDecorators(modelFiles, options) { | ||
try { | ||
const updatedModels = []; | ||
const allModelContent = modelFiles.map(file => fs.readFileSync(file, 'utf-8')); | ||
let modelManager = new ModelManager(); | ||
allModelContent.forEach(modelContent => { | ||
modelManager.addModel(modelContent); | ||
}); | ||
|
||
const resp = DecoratorManager.extractDecorators(modelManager, options); | ||
|
||
if (!fs.existsSync(options.output)) { | ||
// If it doesn't exist, create the directory | ||
fs.mkdirSync(options.output); | ||
} | ||
|
||
const namespaces = resp.modelManager.getNamespaces().filter(namespace=>namespace!=='[email protected]' && namespace!=='concerto'); | ||
namespaces.forEach(name=>{ | ||
let model = resp.modelManager.getModelFile(name); | ||
let modelAst=model.getAst(); | ||
let data = Printer.toCTO(modelAst); | ||
|
||
updatedModels.push(data); | ||
const filePath = path.join(options.output, model.namespace.split('@')[0]+'.cto'); | ||
fs.writeFileSync(filePath, data); | ||
}); | ||
|
||
resp.decoratorCommandSet.forEach((dcs, index) => { | ||
const fileName = 'dcs_'+ index.toString(); | ||
const filePath = path.join(options.output, fileName+'.json'); | ||
fs.writeFileSync(filePath, JSON.stringify(dcs)); | ||
}); | ||
resp.vocabularies.forEach((vocab, index) => { | ||
const fileName = 'vocabulary_'+ index.toString(); | ||
const filePath = path.join(options.output, fileName+'.yml'); | ||
fs.writeFileSync(filePath, vocab); | ||
}); | ||
return `Extracted Decorators and models in ${options.output}/.`; | ||
} catch (e) { | ||
throw new Error(e); | ||
} | ||
} | ||
} | ||
|
||
module.exports = Commands; |
Oops, something went wrong.