UNPKG

@tripsnek/tmf

Version:

TypeScript Modeling Framework - A TypeScript port of the Eclipse Modeling Framework (EMF)

143 lines (139 loc) 5.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TGeneratorFactory = void 0; const tgen_utils_1 = require("./tgen-utils"); const eclass_impl_1 = require("../metamodel/eclass-impl"); const tgenerator_package_initializer_1 = require("./tgenerator-package-initializer"); /** * Generates *-factory.ts files, which provide static access to * methods for instantiating data objects for a TMF model. */ class TGeneratorFactory { rootPackage; /** * Single public static method for generating a XXXFactory class. * @param pkg */ generateFactoryContents(pkg) { //build path to root package initializer const pathToRoot = tgen_utils_1.TGenUtils.getPathToRoot(pkg); this.rootPackage = pkg; while (this.rootPackage.getESuperPackage()) this.rootPackage = this.rootPackage.getESuperPackage(); return `${this.genGenericImports(pkg)} ${this.genSpecificImports(pkg)} import { ${tgenerator_package_initializer_1.TGeneratorPackageInitializer.generateClassName(this.rootPackage)}} from '${pathToRoot}${tgenerator_package_initializer_1.TGeneratorPackageInitializer.generateFileName(this.rootPackage)}'; export class ${tgen_utils_1.TGenUtils.genFactoryClassName(pkg)} extends EFactory { ${this.genSingleton(pkg)} ${this.genCreateSwitch(pkg)} ${this.genClassCreators(pkg)} } `; } //====================================================================== // Private helper methods /** * Generate a basic set of imports for eCore classes that we expect every * Factory is going to need * @param pkg */ genGenericImports(pkg) { const pkgClassName = tgen_utils_1.TGenUtils.genPackageClassName(pkg); return `${tgen_utils_1.TGenUtils.DEFAULT_IMPORTS} import { EReference } from '@tripsnek/tmf'; import { EAttribute } from '@tripsnek/tmf'; import { EFactory } from '@tripsnek/tmf'; import { ${pkgClassName} } from './${tgen_utils_1.TGenUtils.genPackageFileName(pkg)}';`; } /** * Generate a specific set of imports for the interfaces and Impl classes in * this model. * @param pkg */ genSpecificImports(pkg) { const pkgClassName = tgen_utils_1.TGenUtils.genPackageClassName(pkg); let result = ''; if (!(pkgClassName === 'EcorePackage')) { for (const eClass of pkg.getEClassifiers()) { if (eClass instanceof eclass_impl_1.EClassImpl && !eClass.isAbstract()) { result += ` import { ${eClass.getName()} } from './api/${tgen_utils_1.TGenUtils.genClassApiName(eClass)}'; import { ${eClass.getName()}Impl } from './impl/${tgen_utils_1.TGenUtils.genClassImplName(eClass)}';`; } } } return result; } /** * Generate the singleton eINSTANCE for this model's factory class. * @param pkg */ genSingleton(pkg) { const className = tgen_utils_1.TGenUtils.genFactoryClassName(pkg); const pkgClassName = tgen_utils_1.TGenUtils.genPackageClassName(pkg); return ` /* Singleton */ public static _eINSTANCE: ${className} = ${className}.init(); public static init(): ${className} { if (!${className}._eINSTANCE) { ${className}._eINSTANCE = new ${className}(); } return ${className}._eINSTANCE; } static get eINSTANCE(): ${className} { ${tgenerator_package_initializer_1.TGeneratorPackageInitializer.generateClassName(this.rootPackage)}.registerAll(); return this._eINSTANCE; } `; } /** * Generate the generic create() method for this factory that switches on * EClass to call the right specific creator method. * @param pkg */ genCreateSwitch(pkg) { const pkgClassName = tgen_utils_1.TGenUtils.genPackageClassName(pkg); let result = ''; if (!(pkgClassName === 'EcorePackage')) { result += ` public override create(eClass: EClass): any { switch (eClass.getClassifierId()) {`; for (const eClass of pkg.getEClassifiers()) { if (eClass instanceof eclass_impl_1.EClassImpl && !eClass.isAbstract()) { const classCreatorName = 'create' + eClass.getName(); result += ` case ${pkgClassName}.${tgen_utils_1.TGenUtils.genClassIdFieldName(eClass)}: return this.${classCreatorName}();`; } } result += ` default: throw new Error( "The class '" + eClass.getName() + "' is not a valid classifier" ); } }`; } return result; } /** * Generate all the class-specific createXXX() methods. * @param pkg */ genClassCreators(pkg) { const pkgClassName = tgen_utils_1.TGenUtils.genPackageClassName(pkg); let result = ''; if (!(pkgClassName === 'EcorePackage')) { for (const eClass of pkg.getEClassifiers()) { if (eClass instanceof eclass_impl_1.EClassImpl && !eClass.isAbstract()) { result += ` public create${eClass.getName()}(): ${eClass.getName()} { return new ${eClass.getName()}Impl(); }`; } } } return result; } } exports.TGeneratorFactory = TGeneratorFactory; //# sourceMappingURL=tgenerator-factory.js.map