UNPKG

ern-api-gen

Version:

Electrode Native API generator

158 lines 6.65 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /* tslint:disable:max-classes-per-file */ const File_1 = __importDefault(require("../java/File")); const Mustache_1 = __importDefault(require("../java/Mustache")); const DefaultGenerator_1 = __importDefault(require("../DefaultGenerator")); const SupportingFile_1 = __importDefault(require("../SupportingFile")); const LoggerFactory_1 = __importDefault(require("../java/LoggerFactory")); const camelCase_1 = __importDefault(require("lodash/camelCase")); const javaUtil_1 = require("../java/javaUtil"); const FileUtils_1 = __importDefault(require("../java/FileUtils")); const cli_1 = require("../java/cli"); const beanUtils_1 = require("../java/beanUtils"); class Meta { constructor(values) { this.outputFolder = ''; this.name = 'default'; this.targetPackage = 'io.swagger.codegen'; beanUtils_1.apply(this, values); } /** * Converter method to process supporting files: execute with mustache, * or simply copy to destination directory * * @param targetDir - destination directory * @param data - map with additional params needed to process templates * @return converter object to pass to lambdaj */ static processFiles(targetDir, data) { return new Writer(targetDir, data); } /** * Creates mustache loader for template using classpath loader * * @param generator - class with reader getter * @return loader for template */ static loader(generator) { return new Reader(generator); } /** * Converts package name to path on file system * * @param packageName - package name to convert * @return relative path */ static asPath(packageName) { return packageName.split('.').join(File_1.default.separator); } getImplementationVersion() { return (this.swaggerVersion = '2.1.3'); } run() { const targetDir = new File_1.default(this.outputFolder); Log.info('writing to folder [{}]', targetDir.getAbsolutePath()); const mainClass = camelCase_1.default(this.name) + 'Generator'; const supportingFiles = [ new SupportingFile_1.default('pom.mustache', '', 'pom.xml'), new SupportingFile_1.default('generatorClass.mustache', ['src/main/java', Meta.asPath(this.targetPackage)].join(File_1.default.separator), mainClass + '.java'), new SupportingFile_1.default('README.mustache', '', 'README.md'), new SupportingFile_1.default('api.template', 'src/main/resources' + File_1.default.separator + this.name, 'api.mustache'), new SupportingFile_1.default('model.template', 'src/main/resources' + File_1.default.separator + this.name, 'model.mustache'), new SupportingFile_1.default('services.mustache', 'src/main/resources/META-INF/services', 'io.swagger.codegen.CodegenConfig'), ]; let swaggerVersion = this.getImplementationVersion(); if (swaggerVersion == null) { swaggerVersion = '2.1.3'; } const data = javaUtil_1.newHashMap(['generatorPackage', this.targetPackage], ['generatorClass', mainClass], ['name', this.name], ['fullyQualifiedGeneratorClass', this.targetPackage + '.' + mainClass], ['swaggerCodegenVersion', swaggerVersion]); supportingFiles.forEach(Meta.processFiles(targetDir, data).convert); } } Meta.Usage = new cli_1.Command({ description: 'MetaGenerator. Generator for creating a new template set ' + 'and configuration for Codegen. The output will be based on the language you ' + 'specify, and includes default templates to include.', name: 'meta', }, [ { description: 'where to write the generated files ({current dir by default},', hasArg: true, name: ['-o', '--output'], property: 'outputFolder', required: true, title: 'output directory', }, { description: 'the human-readable name of the generator', hasArg: true, name: ['-n', '--name'], title: 'name', }, { description: 'the swagger version to use', hasArg: true, name: ['-s', '--swaggerVersion'], title: 'swagger version', }, { description: 'the package to put the main class into ({defaults to io.swagger.codegen},', hasArg: true, name: ['-p', '--package'], property: 'targetPackage', title: 'package', }, ]); Meta.TEMPLATE_DIR_CLASSPATH = 'codegen'; Meta.MUSTACHE_EXTENSION = '.mustache'; exports.default = Meta; class Writer { constructor(targetDir, data) { this.convert = support => { try { const destinationFolder = new File_1.default(new File_1.default(this.targetDir.getAbsolutePath()), support.folder); const outputFile = new File_1.default(destinationFolder, support.destinationFilename); const template = this.generator.readTemplate(new File_1.default(Meta.TEMPLATE_DIR_CLASSPATH, support.templateFile).getPath()); let formatted = template; if (support.templateFile.endsWith(Meta.MUSTACHE_EXTENSION)) { Log.info('writing file to {}', outputFile.getAbsolutePath()); formatted = Mustache_1.default.compiler() .withLoader(Meta.loader(this.generator)) .defaultValue('') .compile(template) .execute(this.data); } else { Log.info('copying file to {}', outputFile.getAbsolutePath()); } FileUtils_1.default.writeStringToFile(outputFile, formatted); return outputFile; } catch (e) { throw new Error("Can't generate project"); } }; this.targetDir = targetDir; this.data = data; this.generator = new DefaultGenerator_1.default(); } } exports.Writer = Writer; class Reader { constructor(generator) { this.generator = generator; } getTemplate(name) { return this.generator.getTemplateReader(Meta.TEMPLATE_DIR_CLASSPATH + File_1.default.separator + name + Meta.MUSTACHE_EXTENSION); } } exports.Reader = Reader; const Log = LoggerFactory_1.default.getLogger(Meta); //# sourceMappingURL=Meta.js.map