用户自定义命令指南
用户定义的命令允许您向 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}}";
}
}
所有命令行参数都作为变量传递给模板引擎。在这种情况下,feature
选项。
一个有用的内置变量是root-package-dir
,该目录是包含@SpringApplication
annotation 的 Saved 已找到。
模板引擎
模板引擎是 Handlebars。 默认情况下,会注册多个 Handlebar 辅助对象
在前面的示例中,{{capitalizeFirst feature}}
template 变量是使用 Handlebars 帮助程序的一个示例。
默认情况下,几个系统变量会暴露给模板引擎:
-
System.getProperties()
可用作{{system-properties}}
-
System.getenv()
可用作{{system-environment}}
-
当前时间(由
new Date().toString()
) 的 ID 为{{now}}
-
这
java.io.tmpdir
system 属性以{{tmp-dir}}
-
这
file.separator
system 属性以{{file-separator}}
*这os.name
system 属性以{{os-name}}
-
这
user.name
system 属性以{\{user.name}}
Spring Boot 主应用程序类所在的 Java 包名称为{{root-package}}
.
Spring Boot 主应用程序类所在的目录为{{root-package-dir}}
.
Maven 模型还公开了几个变量:
-
{{artifact-id}}
-
{{artifact-version}}
-
{{artifact-path}}
-
{{project-name}}
-
{{project-descriptoin}}
-
{{maven-model}}
- 这是 org.apache.maven.model.Model 类。 -
{{maven-properties}}
- 这是一个 Java properties 对象,其键是 POM 的<properties>
部分。 -
{{java-version}}
- 这将查找名为java.version
在 POM 中。如果值为1.8
,它将转换为8
.
创建新的用户定义命令
一种简单的入门方法是运行以下命令:
spring command new hello create
这将创建一个名为hello
使用名为create
.
您可以查看spring command new
通过运行spring command new --help
.
以下清单显示输出为:
$ 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
生成以下目录结构和文件。
.
├── README.adoc
└── .spring
└── commands
└── hello
└── create
├── command.yaml
└── hello.yaml
以下清单显示了command.yaml
文件。它包含一个名为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
下面的清单显示了名为hello.yaml
.它会生成名为hello.txt
actions:
- generate:
to: hello.txt
text: Hello {{greeting}} at {{now}} on {{os-name}}.
该命令列在User-defined Commands
heading 时,运行spring help
命令。
...
User-defined Commands
hello create: Generate a new file with a hello message
运行spring hello create
命令会生成hello.txt
文件,其中包含以下内容:
Hello World at Mar 9, 2023 on Linux.
了解更多
Action Guide 介绍了可在作文件中使用的所有选项(用于向项目添加或修改代码和配置)。