最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Yeoman生成器

运维笔记admin13浏览0评论

Yeoman生成器

Yeoman生成器

我正在写一个生成器,用户将被提示一个参数,我们称之为option。根据其答案,我想更改其中一个输出文件:

some class.Java

public class SomeClass{

    //if option=x I don't want to include this attribute:
    private String field1;

    //if option=x I want to generate an attribute with the value of the promted attribute
    private String ${info};

我如何进行上述评论中描述的操作?

回答如下:

index.js

prompting()方法中,您将声明所有提示和包含名称的提示类型。然后在writing(),你将把那些传递给模板,这里是MyClass.java

module.exports = class extends Generator {
  prompting() {
    // Have Yeoman greet the user.
    this.log(yosay(
      'Welcome to the remarkable ' + chalk.red('generator-react-starter-kit-relay-container') + ' generator!'
    ));

    const prompts = [{
      type: 'input',
      name: 'info',
      message: 'INFO IN YOUR CLASS'
    }, {
      type: 'confirm',
      name: 'x',
      message: 'YOUR OPTION X'
    }];

    return this.prompt(prompts).then(props => {
      this.props = props;
    });
  }

  writing() {
    this.fs.copyTpl(
      this.templatePath('MyClass.js'),
      this.destinationPath(<YOUR DESTINATION PATH>),
      {
        info: this.props.info,
        x: this.props.x
      }
    );
  } 
};

模板/ MyClass.java

public class MyClass{

    <% if (x) { %>
    private String <%= info %>;
    <% } else { %>
    private String field1;
    <% } %>

}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论