用户自定义命令指南
用户定义的命令允许您向 Spring CLI 添加自定义命令。 命令的目录结构表示引入 shell 的命令和子命令。
例如,controller\new
转换为命令controller new
在 CLI 中。
位于子命令目录中的文件是:
-
名为
command.yaml
描述命令及其参数。 -
一个或多个作文件,用于描述向项目添加代码或配置时要执行的作。
使用以下命令将用户定义的命令注册到 CLI:
command add --from <repository-url>
该存储库的内容将复制到您的现有项目中。
例如,查看 github.com/rd-1-2022/udc-spring-controller 存储库的内容。
结构
所有用户定义的命令的目录结构都位于以下路径下:
.spring/commands
因此,对于用户定义的命令,controller new
,命令描述文件和作文件所在的完整目录结构为:
.spring/commands/controller/new
在此目录中,您可以定义:
-
这
command.yaml
描述命令的作用和命令参数的文件。 -
一个或多个作文件,用于定义要为此命令运行的作。
例如,github.com/rd-1-2022/udc-spring-controller 存储库的目录内容如下所示:
.
├── README.adoc
└── .spring
└── commands
└── controller
└── new
├── command.yaml
├── create-controller.yaml
└── RestController.java
描述命令
的command.yaml
文件controller new
命令如下:
command:
description: Generate a new Spring Controller
options:
#
- name: feature
description: name of the feature package
dataType: string
defaultValue: person
inputType: text
required: true
该文件包含命令的简要说明和一系列命令行选项。
这name
的选项是必需的。默认的dataType
是string
.
这dataType
可以是int
,integer
,bool
,boolean
,double
,float
,long
,short
或string
.
Spring CLI 在运行时合并这些命令,它们在请求一般帮助和特定于命令的帮助时出现。 下面的清单显示了一个示例:
$spring help
<output truncated>
User-defined Commands
controller new: Generate a new Spring Controller
下面的清单显示了第二个示例:
$ 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作文件。
作文件可以命名为您喜欢的任何名称。CLI 查找具有.yaml
和.yml
文件扩展名。
完成特定任务所需的作文件数量不限。作文件的运行顺序是深度优先,然后按字母顺序排列。
下面的清单显示了一个简单的示例:
actions:
- generate:
to: hello.txt
text: Hello at {{now}} on {{os-name}}.
此作会生成一个名为hello.txt
(如果尚不存在)位于当前工作目录中。
模板内容包含 kebab-case 变量名称。
这user-name
和os-name
变量来自 Java 系统属性,并自动注册到模板引擎中。
这now
variable 是new java.util.Date()
当命令运行时。
作为创建 Java 代码的更实际示例,以下三个清单显示了名为Controller.java
及其相关作和 github.com/rd-1-2022/udc-spring-controller 模板化 Java 文件。
这feature
variable 是一个命令选项。
-
命令文件
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 定义要生成的文件的位置。
如果要生成的文件已存在,则不会覆盖该文件,除非overwrite:
的添加级别与to:
田。
-
模板化 Java 文件
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.
One useful built-in variable is root-package-dir
, which is the directory where the class containing the @SpringApplication
annotation is located.
Template Engine
The template engine is Handlebars.
Several Handlebar helpers are registered by default
In the previous example, the {{capitalizeFirst feature}}
template variable is an example of using a Handlebars helper.
By default, several system variables are exposed to the template engine:
-
System.getProperties()
is available as {{system-properties}}
-
System.getenv()
is available as {{system-environment}}
-
The current time (defined by new Date().toString()
) is available as {{now}}
-
The java.io.tmpdir
system property is available as {{tmp-dir}}
-
The file.separator
system property is available as {{file-separator}}
* The os.name
system property is available as {{os-name}}
-
The user.name
system property is available as {\{user.name}}
The Java package name where the Spring Boot main application class resides is available as {{root-package}}
.
The directory where the Spring Boot main application class resides is available as {{root-package-dir}}
.
The Maven model also exposes several variables:
-
{{artifact-id}}
-
{{artifact-version}}
-
{{artifact-path}}
-
{{project-name}}
-
{{project-descriptoin}}
-
{{maven-model}}
- This is the org.apache.maven.model.Model class.
-
{{maven-properties}}
- This is a Java properties object that has, as keys, the values of each entry in the POM’s <properties>
section.
-
{{java-version}}
- This looks for a Maven Property named java.version
in the POM. If the value is 1.8
, it is converted to a value of 8
.
Creating a New User-defined Command
A simple way to get started is to run the following command:
spring command new hello create
This creates a user-defined command named hello
with a sub-command named create
.
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 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.
.
├── 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
.
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.txt
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.
...
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:
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).