UNPKG

generator-liferay

Version:
184 lines (160 loc) 4.84 kB
// Portlet "use strict"; const ejs = require("ejs"); ejs.delimiter = "?"; const Generator = require("yeoman-generator"); const utils = require("../utils"); // const portlet = require("./portlet"); module.exports = class extends Generator { initializing() { this.success = false; this.argument("name", { type: String, required: false, description: "Project name is required" }); this.argument("portlet", { type: String, required: false, description: "Portlet Name is required" }); this.argument("package", { type: String, required: false, description: "Package is required" }); } async prompting() { const answers = await this.prompt([ { name: "name", message: "Project name?", type: "input", default: "my-project", when: !this.options.name }, { name: "portlet", message: "Portlet name?", type: "input", default: "MyPortlet", when: !this.options.portlet }, { name: "package", message: "Package name?", type: "input", default: "com.package.docs", when: !this.options.package } ]); if (answers.name === undefined) { answers.name = this.options.name; } if (answers.portlet === undefined) { answers.portlet = this.options.portlet; } if (answers.package === undefined) { answers.package = this.options.package; } this.projectName = answers.name; this.portletName = answers.portlet; this.packageName = answers.package; this.packagePath = this.packageName.replace(/\./g, "/").toLowerCase(); this.rootPath = this.destinationPath("modules/" + this.projectName); // Check if this generator is used in a Liferay workspace if (!utils.isWorkspace(this.destinationPath())) { this.log.error("This generator must be used in a Liferay workspace"); process.exit(1); } // Check if project already exists if (utils.isFolderExists(this.rootPath)) { this.log.error(`Project already exists... (${this.projectName})`); process.exit(1); } } async writing() { this.resourcePath = this.rootPath + "/src/main/resources"; this.packagePath = this.rootPath + "/src/main/java/" + this.packagePath; // Create java root folder utils.createFolders(this.packagePath); const copyOpts = { globOptions: { ignore: [] } }; const opts = { projectName: this.projectName, portletName: this.portletName, packageName: this.packageName, packageFullName: this.packageName + "." + this.portletName, packageSurName: this.portletName.replace(/portlet/gi, "") }; try { await this.fs.copyTpl( this.templatePath("source"), this.destinationPath(this.rootPath), opts, copyOpts, { globOptions: { dot: true } } ); await this.fs.copyTpl( this.templatePath("portlet"), this.destinationPath(this.packagePath), opts, copyOpts, { globOptions: { dot: true } } ); await this.fs.commit([], async () => { // Rename Keys const fromKeys = this.packagePath + "/constants/Keys.java"; const toKeys = this.packagePath + `/constants/${this.portletName}Keys.java`; utils.doRename(fromKeys, toKeys); // Rename portlet const fromPortlet = this.packagePath + "/portlet/Portlet.java"; const toPortlet = this.packagePath + `/portlet/${this.portletName}.java`; utils.doRename(fromPortlet, toPortlet); }); this.success = true; } catch (error) { this.success = false; console.log(error); } // portlet.createLanguagePropertiesFile( // this.resourcePath, // this.portletName, // this.packageName // ); // portlet.createViewJSP(this.resourcePath, this.portletName); // portlet.createPortletClass( // this.packagePath, // this.portletName, // this.packageName // ); // portlet.createPortletConstantsClass( // this.packagePath, // this.portletName, // this.packageName // ); // try { // this.fs.copy( // this.templatePath("source"), // this.destinationPath(this.rootPath), // { globOptions: { dot: true } } // ); // this.success = true; // } catch (err) { // this.success = false; // console.log(err); // } } end() { if (this.success) { utils.dotGitIgnore(this.rootPath + "/_gitignore"); this.log(`Project created... (${this.projectName})`); } } }; // blade create -t mvc-portlet -p com.liferay.docs.guestbook -c GuestbookPortlet my-guestbook-project