UNPKG

jmms

Version:

Jmms cli tools, Jmms is a java meta-micro-service framework

196 lines (183 loc) 8.65 kB
const _ = require('lodash'); const log = require('../../log'); const fs = require('fs'); const path = require('path'); const shelljs = require('shelljs'); const cheerio = require('cheerio') const formatString = require('string-format') const pd = require('pretty-data').pd const mvn = require('maven').create(); const DEP_RPC_FEIGN = { groupId: "jmms.modules", artifactId: "modules-rpc-feign", version: "0.1.0-SNAPSHOT" } const DEP_SWAGGER_CODE_GEN = { groupId: "io.swagger", artifactId: "swagger-codegen-maven-plugin", version: "2.2.2" } var BaseGenerator = require('../../base'); let addPluginToPomFile = (pomFile, name, options) => { let language = options.language || "java"; let library = options.library; let templateDirectory = options.templateDirectory; return new Promise((resolve, reject) => { if (fs.existsSync(pomFile)) { fs.readFile(pomFile, "utf-8", (err, xml) => { if (err) { reject(err); } else { let $ = cheerio.load(xml, { xmlMode: true }); let project = $("project"); if (project.length != 1) { reject("Illegal pom file format, project element not found."); return; } let build = project.find("> build"); if (build.length <= 0) { $("<build />").appendTo(project); build = project.children().last(); } let plugins = build.find("> plugins"); if (plugins.length <= 0) { $("<plugins />").appendTo(build); plugins = build.children().last(); } let plugin = null; plugins.find("> plugin").each((index, it) => { if ($(it).find("> groupId").text() == DEP_SWAGGER_CODE_GEN.groupId && $(it).find("> artifactId").text() == DEP_SWAGGER_CODE_GEN.artifactId) { plugin = $(it); } }); if (plugin) { let versionEl = plugin.find("> version"); if (versionEl.length > 0) { versionEl.text("2.2.2"); } else { plugin.append(formatString("<version>{}</version>", DEP_SWAGGER_CODE_GEN.version)); } } else { plugin = $(formatString("<plugin><groupId>{}</groupId><artifactId>{}</artifactId><version>{}</version></plugin>", DEP_SWAGGER_CODE_GEN.groupId, DEP_SWAGGER_CODE_GEN.artifactId, DEP_SWAGGER_CODE_GEN.version)).appendTo(plugins); plugin = plugins.children().last(); } let executions = plugin.find("> executions"); if (executions.length <= 0) { $("<executions />").appendTo(plugin); executions = plugin.children().last(); } let execution = null; executions.find("> execution").each((index, it) => { if ($(it).find("> id").text() == name) { execution = $(it); } }); let configuration = null; if (execution) { configuration = execution.find("> configuration"); } else { $(formatString("<execution><id>{}</id><phase>generate-sources</phase><goals><goal>generate</goal></goals></execution>", name)).appendTo(executions); execution = executions.children().last(); } if (!configuration || configuration.length <= 0) { $("<configuration />").appendTo(execution); configuration = execution.children().last(); } let inputSpec = configuration.find("> inputSpec"); if (inputSpec.length <= 0) { if (!options.location) { reject("Please enter swagger file location option: --location=./xxx/swagger.json") return; } $("<inputSpec />").appendTo(configuration); inputSpec = configuration.children().last(); } inputSpec.text(options.location); let language = configuration.find("> language"); if (language.length <= 0) { $("<language />").appendTo(configuration); language = configuration.children().last(); } language.text(options.language); if (!options.library && options.language == "java") { options.library = "feign" } if (options.library) { let library = configuration.find("> library"); if (library.length <= 0) { $("<library />").appendTo(configuration); library = configuration.children().last(); } library.text(options.library); } if (options["api-package"]) { let apiPackage = configuration.find("> apiPackage"); if (apiPackage.length <= 0) { $("<apiPackage />").appendTo(configuration); apiPackage = configuration.children().last(); } apiPackage.text(options["api-package"]); } if (options["model-package"]) { let modelPackage = configuration.find("> modelPackage"); if (modelPackage.length <= 0) { $("<modelPackage />").appendTo(configuration); modelPackage = configuration.children().last(); } modelPackage.text(options["model-package"]); } let dependencies = project.find("> dependencies"); if (dependencies.length <= 0) { $("<dependencies />").appendTo(project); dependencies = project.children().last(); } let dependency = null; dependencies.find("> dependency").each((index, it) => { if ($(it).find("> groupId").text() == DEP_RPC_FEIGN.groupId && $(it).find("> artifactId").text() == DEP_RPC_FEIGN.artifactId) { dependency = $(it); } }); if (!dependency) { dependencies.append(formatString("<dependency><groupId>{}</groupId><artifactId>{}</artifactId><version>{}</version></dependency>", DEP_RPC_FEIGN.groupId, DEP_RPC_FEIGN.artifactId, DEP_RPC_FEIGN.version)); } pd.step = ' ' fs.writeFile(pomFile, pd.xml($.xml()), (err) => { if (err) { reject(err); } else { resolve(); } }); } }); } else { reject("./pom.xml file not exists."); } }); } module.exports = class extends BaseGenerator { constructor(args, opts) { super(args, opts); } prompting () { let name = this.args[0]; if (!name || this.options.help) { shelljs.echo(this._usageHelp()); shelljs.exit(1); } else { addPluginToPomFile("./pom.xml", name, Object.assign({language: "java"}, this.options)).then(() => { mvn.execute("compile").then(() => { shelljs.exit(0); }).catch(() => { shelljs.exit(1); }); }).catch(err => { shelljs.echo(err); shelljs.exit(1); }); } } };