用户自定义命令指南

用户定义的命令允许您向 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}}";
	}
}

所有命令行参数都作为变量传递给模板引擎。在这种情况下,feature选项。spring-doc.cadn.net.cn

一个有用的内置变量是root-package-dir,该目录是包含@SpringApplicationannotation 的 Saved 已找到。spring-doc.cadn.net.cn

模板引擎

模板引擎是 Handlebars。 默认情况下,会注册多个 Handlebar 辅助对象spring-doc.cadn.net.cn

在前面的示例中,{{capitalizeFirst feature}}template 变量是使用 Handlebars 帮助程序的一个示例。spring-doc.cadn.net.cn

默认情况下,几个系统变量会暴露给模板引擎:spring-doc.cadn.net.cn

Spring Boot 主应用程序类所在的 Java 包名称为{{root-package}}.spring-doc.cadn.net.cn

Spring Boot 主应用程序类所在的目录为{{root-package-dir}}.spring-doc.cadn.net.cn

Maven 模型还公开了几个变量:spring-doc.cadn.net.cn

创建新的用户定义命令

一种简单的入门方法是运行以下命令:spring-doc.cadn.net.cn

spring command new hello create

这将创建一个名为hello使用名为create.spring-doc.cadn.net.cn

您可以查看spring command new通过运行spring command new --help. 以下清单显示输出为: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]

运行spring command new hello create生成以下目录结构和文件。spring-doc.cadn.net.cn

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

以下清单显示了command.yaml文件。它包含一个名为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

下面的清单显示了名为hello.yaml.它会生成名为hello.txtspring-doc.cadn.net.cn

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

该命令列在User-defined Commandsheading 时,运行spring help命令。spring-doc.cadn.net.cn

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

运行spring hello create命令会生成hello.txt文件,其中包含以下内容:spring-doc.cadn.net.cn

Hello World at Mar 9, 2023 on Linux.

了解更多

Action Guide 介绍了可在作文件中使用的所有选项(用于向项目添加或修改代码和配置)。spring-doc.cadn.net.cn