Fast.cli
一款令人难以置信的 Flutter 命令行界面。它允许您在不编写任何代码的情况下创建自己的 CLI。
通过此 CLI,您可以创建自定义模板并定义项目如何启动。
生成的目录和文件是从外向内定义的,也就是说,用户定义资源,CLI 为您生成。
您可以轻松地
- 创建
- 使用
- 编辑
- 分享
不要犹豫与社区分享您创建的插件,这非常简单。
功能
- 模板生成器
- 应用程序脚手架生成器
- Visual Studio Code 的代码片段生成器
- 自定义命令
- 标准命令行命令(CLI 已包含一些命令)
- install (将包添加到项目依赖项)
安装
首先,需要安装该包并配置缓存位置。
全局安装此包
为此,您需要 安装并配置 Dart SDK
如果您在全局安装包方面有任何疑问,请参阅 指南。
$ pub global activate fast
配置缓存位置
将 fast 缓存添加到您的 PATH 环境变量中。
$HOME/.fastcli/bin/
概述
- 插件系统 - 为您提供导入一组模板、资源和自定义命令的机制。
- 自定义命令 - 您定义的将在 cmd 中执行的命令。
- 脚手架 - 项目的初始结构(以已定义的结构开始您的项目)
- 模板 - (将为您的项目创建的代码,就像代码片段一样,但通过命令行)
快速入门
快速使用 CLI 功能。
安装您的第一个插件
插件系统是您创建、使用和共享自己资源的方式。将它们想象成 VS Code 的插件,但在这里是 CLI 的插件。
要使用 CLI,至少需要安装一个插件,所以请使用以下命令安装您的第一个插件。
不用担心,稍后您将学习如何创建自己的插件。现在只需安装这个已创建的插件。
fast plugin add <repository that has a plugin>
示例
$ fast plugin add git https://github.com/pbissonho/mvc_git_test.git
查看插件提供的功能
安装插件后,会创建一个同名的可执行文件,所以运行以下命令进行测试。
使用直接从存储库安装的“mvc”插件。
此命令将显示插件提供的功能。
$ mvc --help
使用插件脚手架
<plugin_name> create --name <flutter_projet_name> --scaffold <_scaffold_name>
创建一个名为“myapp”的 Flutter 应用程序,其结构定义在“mvc”插件的“sample”脚手架中。
示例
$ mvc create --name myapp --scaffold sample
运行插件自定义命令
mvc run <custom_command_name>
示例
$ mvc run build
使用插件模板
mvc <template_name> <template_arg1> <template_arg2> <template_arg3>...
$ mvc page --name myPage
插件系统
插件系统是您创建、使用和共享自己资源的方式。将它们想象成 VS Code 的插件,但在这里是 CLI 的插件。
此 CLI 旨在提供您创建自己的 CLI 的工具,默认情况下 CLI 不带任何资源,也就是说,它没有可用的插件、命令或脚手架。因此,要使用它,至少需要安装一个插件。
VS Code 中的插件可以提供代码片段、主题、图标等。
在此 CLI 中,插件提供
- 命令
- 脚手架
- 模板
插件结构
scaffolds/ <- 用于添加脚手架的文件夹。
templates/ <- 用于添加模板的文件夹。
commands.yaml <- 定义自定义命令的文件
plugin.yaml <- 定义插件名称和描述的文件。
在 GitHub 上查看示例,点击此处。
使用插件
首先安装插件。您可以直接从存储库安装。
添加插件时,会创建一个同名可执行文件,因此安装后可以通过插件本身的名称使用它。
安装
$ fast plugin add git https://github.com/pbissonho/mvc_git_test.git
使用
$ mvc --help
插件命令
add
添加插件
可以直接从 GitHub 存储库或本地路径添加插件。
直接从存储库安装。
$ fast plugin add git https://github.com/pbissonho/mvc_git_test.git
从本地路径安装。
$ fast plugin add path home/mypath/
remove
移除插件。
$ fast plugin remove --name <plugin_name>
list
显示所有已安装的插件。
$ fast plugin list
脚手架
脚手架是一个 yaml 文件,您可以在其中为 Flutter 项目定义文件夹结构和一组依赖项。
您不必创建空白的 Flutter 项目,而是可以使用已创建的文件夹结构和配置好的依赖项来启动项目。
定义一个 scaffold.yaml
要定义您的脚手架,只需遵循以下示例
文件: scaffold.yaml
# The scaffold name.
name: sample
description: A sample scaffold.
author: Pedro Bissonho <[email protected]>
# The folder structure that will be generated inside the 'lib' folder.
structure:
- ui:
- pages
- shared
- themes
- data:
- models
- repositorys
- blocs
# The folder structure that will be generated inside the 'test' folder.
test_structure:
- ui:
- data:
- fixtures:
# The standard dependencies that the project will have.
# Your project will start with all these dependencies.
# If the version is not informed, the version will be configured as the last version available in Dart Pub.
dependencies:
koin:
flutter_bloc:
# The standard dev_dependencies that the project will have.
# If the version is not informed, the version will be configured as the last version available in Dart Pub.
dev_dependencies:
koin_test:
使用脚手架
将您的 scaffold.yaml 文件放在插件内,然后使用它。
<plugin_name> create --name <app_name> --scaffold <scaffold_name>
示例
$ mvc create --name myapp --scaffold mvc1
结果
使用示例脚手架启动项目时,您将获得以下项目。

模板
模板是您用来生成代码的文件或一组文件。通过命令行生成代码,如同代码片段一样。
定义一个模板
首先,我们定义生成的文件。
在此示例中,当生成文件时,'@Name' 标签将被 '--name' 参数替换。
文件: @name_page.dart
import 'package:flutter/material.dart';
class @NamePage extends StatefulWidget {
@override
_@NamePageState createState() => _@NamePageState();
}
class _@NamePageState extends State<@NamePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Text("Hello"),
),
);
}
}
与上述文件在同一文件夹中,我们有一个 yaml 文件,它定义了模板的创建方式。
文件: template.yaml
#Yaml file for configuring the template.
#The template name
name: page
#The template description
description: Create_flutter_page.
#Path where files should be sent
to: lib/ui/pages/@name
#Arguments to be passed when executing this template
# You can define as many arguments as you want.
args:
- name
# - other1
# - other2
# - other3
使用模板
运行此命令以生成您的模板。
<plugin_name> <template_name> <templates_arg1, templates_arg2, templates_arg3...>
示例
$ mvc page --name home
结果
现在,您将在 lib/ui/pages/home 路径下拥有以下文件。
import 'package:flutter/material.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Text("Hello"),
),
);
}
}
代码片段生成器
一个用于根据模板文件为 VC Code 生成代码片段的命令。
重复使用模板文件中插入的代码来为 Visual Studio Code 生成代码片段。
定义您的代码片段
首先,我们创建模板文件和 template.yaml。
然后设置要生成的代码片段。
文件: @name_page.dart
import 'package:flutter/material.dart';
class @NamePage extends StatefulWidget {
@override
_@NamePageState createState() => _@NamePageState();
}
class _@NamePageState extends State<@NamePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Text("Hello"),
),
);
}
}
文件: template.yaml
name: page
description: Create_flutter_page.
to: lib/ui/pages/@name
# You can define as many arguments as you want.
args:
- name
# Set up a list of snippests.
snippets:
# The file containing the snippet code to be generated
# Reuse the code inserted in one of the template files to generate a VC Code snippets.
- file: '@name_page.dart'
prefix: page
#File lines that will not be included in the snippet.
excluded:
- 0
- 1
# It is possible to create snippests for each template file.
# - file: '@name_other1.dart'
# prefix: other1
# excluded:
# - 0
# - file: '@name_other2.dart'
# prefix: other2
# excluded:
# - 0
生成代码片段
代码片段仅为调用的插件生成,因此
如果安装了更多代码片段,它们也不会为它们生成。
运行以下命令来生成代码片段。
<plugin_name> snippets
示例
$ mvc snippets
结果
将可以生成代码片段。

自定义命令
记录并不得不输入诸如“flutter build_runner build”之类的命令并不是一件好事。
因此,自定义命令将解决此问题。只需编写一次,然后通过别名使用它们。
您只需在插件中创建一个 commands.yaml 文件并定义您的命令。
定义您的命令
文件: commands.yaml
commands:
runner: flutter build runner build
push: git push origin master
mkdir: mkdir myFolder
运行命令
<plugin_name> run <command_name>
示例
$ mvc run runner
迁移
v0.3 到 v0.4
已删除“config template”、“config scaffolds”和“config commands”命令。现在配置 CLI 并开始使用它要容易得多。只需创建您的插件并安装它。
脚手架、模板和命令保持不变,因此您将使用 0.4 版本而无需进行任何更改,只需将它们添加到您的插件中即可。