The fx create
command generates scaffolding for new projects. See fx create --help
for usage details.
//tools/create/templates/
..tmpl
extension or .tmpl-<lang>
for language-specific template files.$
in a file/directory name to substitute the user's PROJECT_NAME
.copy_templates
target in //tools/create/BUILD.gn
to include all your new template files.CreateArgs
in //tools/create/src/main.rs
.Each top-level directory in //tools/create/templates
corresponds to a project type of the same name.
Files with the .tmpl
and .tmpl-*
extensions in these directories are handlebars templates. Template expansion is performed on the templates and the directory structure is replicated in the new project directory.
Templates with the .tmpl
extension are language agnostic.
Templates with the .tmpl-<lang>
extension are expanded only when the user supplies the --lang=<lang>
flag.
Multiple languages can be supported in the same template directory. For instance, the component-v2
command supports cpp
and rust
languages by having both a BUILD.gn.tmpl-cpp
and BUILD.gn.tmpl-rust
file.
The template expansion uses handlebars syntax. Expand a variable with the syntax {{VAR_NAME}}
.
The available variable names are:
COPYRIGHT_YEAR
: Today's year, eg. 2020, for use in copyright headers,PROJECT_NAME
: The user-specified project name,PROJECT_PATH
: The path from the fuchsia root directory to the new project,PROJECT_TYPE
: The project-type as specified on the command line, e.g: ‘component-v2’.Path names are treated differently. If a $
character is encountered in a template path, it is substituted with the PROJECT_NAME
variable expansion.
Eg. component/meta/$.cml.tmpl
with PROJECT_NAME="foo"
expands to meta/foo.cml
.
To make a new variable accessible to templates, add it to TemplateArgs
in //tools/create/src/main.rs
.
Helper functions can be invoked with the syntax {{helper VAR_NAME}}
.
The available helper functions are:
pascal_case
: Converts a string argument into PascalCase,snake_case
: Converts a string argument into snake*_*case,screaming_snake_case
: Converts a string argument into SCREAMING_SNAKE_CASE,To make a new helper function accessible to templates, follow instructions in //tools/create/src/tmpl_helpers.rs
.