用户自定义命令指南

用户定义的命令允许您向 Spring CLI 添加自定义命令。 命令的目录结构表示引入 shell 的命令和子命令。spring-doc.cadn.net.cn

例如,controller\new转换为命令controller new在 CLI 中。spring-doc.cadn.net.cn

位于子命令目录中的文件是:spring-doc.cadn.net.cn

使用以下命令将用户定义的命令注册到 CLI:spring-doc.cadn.net.cn

command add --from <repository-url>

该存储库的内容将复制到您的现有项目中。spring-doc.cadn.net.cn

结构

所有用户定义的命令的目录结构都位于以下路径下:spring-doc.cadn.net.cn

.spring/commands

因此,对于用户定义的命令,controller new,命令描述文件和作文件所在的完整目录结构为:spring-doc.cadn.net.cn

.spring/commands/controller/new

在此目录中,您可以定义:spring-doc.cadn.net.cn

例如,github.com/rd-1-2022/udc-spring-controller 存储库的目录内容如下所示:spring-doc.cadn.net.cn

.
├── README.adoc
└── .spring
    └── commands
        └── controller
            └── new
                ├── command.yaml
                ├── create-controller.yaml
                └── RestController.java

描述命令

command.yaml文件controller new命令如下:spring-doc.cadn.net.cn

command:
  description: Generate a new Spring Controller
  options:
    #
    - name: feature
      description: name of the feature package
      dataType: string
      defaultValue: person
      inputType: text
      required: true

该文件包含命令的简要说明和一系列命令行选项。spring-doc.cadn.net.cn

name的选项是必需的。默认的dataTypestring.spring-doc.cadn.net.cn

dataType可以是int,integer,bool,boolean,double,float,long,shortstring.spring-doc.cadn.net.cn

Spring CLI 在运行时合并这些命令,它们在请求一般帮助和特定于命令的帮助时出现。 下面的清单显示了一个示例:spring-doc.cadn.net.cn

$spring help

<output truncated>

User-defined Commands
       controller new: Generate a new Spring Controller

下面的清单显示了第二个示例:spring-doc.cadn.net.cn

$ spring help controller new
NAME
       controller new - Generate a new Spring Controller

SYNOPSIS
       controller new --feature String

OPTIONS
       --feature String
       name of the feature package
       [Optional, default = person]

作文件

作文件的结构类似于 GitHub作文件。spring-doc.cadn.net.cn

作文件可以命名为您喜欢的任何名称。CLI 查找具有.yaml.yml文件扩展名。spring-doc.cadn.net.cn

完成特定任务所需的作文件数量不限。作文件的运行顺序是深度优先,然后按字母顺序排列。spring-doc.cadn.net.cn

下面的清单显示了一个简单的示例:spring-doc.cadn.net.cn

actions:
  - generate:
      to: hello.txt
      text: Hello at {{now}} on {{os-name}}.

此作会生成一个名为hello.txt(如果尚不存在)位于当前工作目录中。 模板内容包含 kebab-case 变量名称。spring-doc.cadn.net.cn

user-nameos-name变量来自 Java 系统属性,并自动注册到模板引擎中。 这nowvariable 是new java.util.Date()当命令运行时。spring-doc.cadn.net.cn

作为创建 Java 代码的更实际示例,以下三个清单显示了名为Controller.java及其相关作和 github.com/rd-1-2022/udc-spring-controller 模板化 Java 文件。 这featurevariable 是一个命令选项。spring-doc.cadn.net.cn

command:
  description: Generate a new Spring Controller
  options:
    #
    - name: feature
      description: name of the feature package
      dataType: string
      defaultValue: person
      inputType: text
actions:
  - generate:
      to: src/main/java/{{root-package-dir}}/{{feature}}/{{capitalizeFirst feature}}Controller.java
      from: RestController.java

to:field 定义要生成的文件的位置。spring-doc.cadn.net.cn

如果要生成的文件已存在,则不会覆盖该文件,除非overwrite:的添加级别与to:田。spring-doc.cadn.net.cn

package {{root-package}}.{{feature}};

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class {{capitalizeFirst feature}}Controller {

	@GetMapping("/{{feature}}")
	public String greeting() {
		return "Hello {{feature}}";
	}
}

All command-line arguments are passed to the template engine as variables. In this case, the feature option is passed.spring-doc.cadn.net.cn

One useful built-in variable is root-package-dir, which is the directory where the class containing the @SpringApplication annotation is located.spring-doc.cadn.net.cn

Template Engine

The template engine is Handlebars. Several Handlebar helpers are registered by defaultspring-doc.cadn.net.cn

In the previous example, the {{capitalizeFirst feature}} template variable is an example of using a Handlebars helper.spring-doc.cadn.net.cn

By default, several system variables are exposed to the template engine:spring-doc.cadn.net.cn

The Java package name where the Spring Boot main application class resides is available as {{root-package}}.spring-doc.cadn.net.cn

The directory where the Spring Boot main application class resides is available as {{root-package-dir}}.spring-doc.cadn.net.cn

The Maven model also exposes several variables:spring-doc.cadn.net.cn

Creating a New User-defined Command

A simple way to get started is to run the following command:spring-doc.cadn.net.cn

spring command new hello create

This creates a user-defined command named hello with a sub-command named create.spring-doc.cadn.net.cn

You can view the full set of options for spring command new by running spring command new --help. The following listing shows the output is:spring-doc.cadn.net.cn

$ spring command new --help
NAME
       command new - Create a new user-defined command

SYNOPSIS
       command new --commandName String --subCommandName String --path String --help

OPTIONS
       --commandName String
       The name of the user-defined command to create
       [Optional, default = hello]

       --subCommandName String
       The name of the user-defined sub-command to create
       [Optional, default = new]

       --path String
       Path to execute command in
       [Optional]

       --help or -h
       help for command new
       [Optional]

Running spring command new hello create generates the following directory structure and files.spring-doc.cadn.net.cn

.
├── README.adoc
└── .spring
    └── commands
        └── hello
            └── create
                ├── command.yaml
                └── hello.yaml

The following listing shows the contents of the command.yaml file. It contains one command line argument, named greeting.spring-doc.cadn.net.cn

command:
  description: Generate a new file with a hello message
  options:
    #
    - name: greeting
      description: who or what to say hello to
      dataType: string
      defaultValue: World
      inputType: text     # TEXT

The following listing shows the action file named hello.yaml. It generates the file named hello.txtspring-doc.cadn.net.cn

actions:
  - generate:
      to: hello.txt
      text: Hello {{greeting}} at {{now}} on {{os-name}}.

The command is listed under the User-defined Commands heading when you run the spring help command.spring-doc.cadn.net.cn

...
User-defined Commands
       hello create: Generate a new file with a hello message

Running the spring hello create command generates the hello.txt file with the following contents:spring-doc.cadn.net.cn

Hello World at Mar 9, 2023 on Linux.

Learning more

The Action Guide describes all the options available for you to use in action files (to add or modify code and configuration to a project).spring-doc.cadn.net.cn