2020-11-22 05:25:24 +00:00
|
|
|
# tmpl templates
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
This documentation aims to cover FAQs and setup.
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
## Setting up a template
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
A "valid" tmpl template only requires two things
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2022-06-14 19:59:53 +00:00
|
|
|
1. A `tmpl.yaml` file in the root directory.
|
2020-11-22 05:25:24 +00:00
|
|
|
2. A `template` directory that serves as the "root" of the template.
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2022-06-14 19:59:53 +00:00
|
|
|
## tmpl.yaml
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2022-06-14 19:59:53 +00:00
|
|
|
**NOTE:** The tmpl.yaml file will be expanded, though not with the full power of the template itself.
|
|
|
|
The tmpl.yaml file will only expand environment variables with syntax `$USER` or `${USER}`.
|
2020-11-23 05:02:16 +00:00
|
|
|
For full documentation on the syntax, see [os.ExpandEnv](https://golang.org/pkg/os/#ExpandEnv).
|
|
|
|
|
2020-12-29 03:21:41 +00:00
|
|
|
When using the `--defaults` flag, no prompts will be shown and only default values will be used.
|
|
|
|
As another alternative, any environment variable that matches a key will bypass the prompt.
|
|
|
|
For example, `author` would have the corresponding environment variable `TMPL_VAR_AUTHOR`.
|
|
|
|
|
2022-06-14 19:59:53 +00:00
|
|
|
```yaml
|
|
|
|
# tmpl.yaml
|
|
|
|
# Write any template args here to prompt the user for, giving any defaults/options as applicable
|
|
|
|
|
|
|
|
prompts:
|
|
|
|
- id: project # The unique ID for the prompt
|
|
|
|
label: Project Name # The prompt message/label
|
|
|
|
help: The name to use in the project # Optional help message for the prompt
|
|
|
|
default: tmpl # Prompt default
|
2020-11-22 05:25:24 +00:00
|
|
|
```
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
## template directory
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
This directory contains any and all files that are part of the template.
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
Everything in this directory (including paths and file names!) will be executed as a [Go template](https://golang.org/pkg/text/template/).
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
See the [documentation](https://golang.org/pkg/text/template/) for every available possibility, but some basic examples are...
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2022-06-14 19:59:53 +00:00
|
|
|
* An id defined in `tmpl.yaml` (tmpl allows for keys to be called as a func or variable, whichever you prefer!)
|
2020-11-22 05:25:24 +00:00
|
|
|
* `{{project}}` or `{{.project}}`
|
|
|
|
* `{{author}}` or `{{.author}}`
|
|
|
|
* Conditionally including something
|
|
|
|
* `{{if eq project ""}} something... {{end}}`
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
### template helpers
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
For a full list, see [helper.go](registry/helper.go)
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2022-06-15 14:59:21 +00:00
|
|
|
| Helper | Example | Output |
|
|
|
|
|-------------|------------------------------------|-------------------------------------------------------------------------------------------------------|
|
|
|
|
| upper | `{{upper project}}` | `MY-PROJECT` |
|
|
|
|
| lower | `{{lower project}}` | `my-project` |
|
|
|
|
| title | `{{title project}}` | `My-Project` |
|
|
|
|
| snake | `{{snake project}}` | `my_project` |
|
|
|
|
| kebab | `{{kebab project}}` | `my-project` |
|
|
|
|
| pascal | `{{pascal project}}` | `MyProject` |
|
|
|
|
| camel | `{{camel project}}` | `myProject` |
|
|
|
|
| env | `{{env "USER"}}` | The current user |
|
|
|
|
| sep | `{{sep}}` | Filepath separator for current OS |
|
|
|
|
| time | `{{time "01/02/2006"}}` | `11/21/2020` - The time according to the given [format](https://flaviocopes.com/go-date-time-format/) |
|
|
|
|
| trim_prefix | `{{trim_prefix "foobar" "foo"}}` | `bar` |
|
|
|
|
| trim_suffix | `{{trim_suffix "foobar" "bar"}}` | `foo` |
|
|
|
|
| replace | `{{replace "foobar" "bar" "baz"}}` | `foobaz` |
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
## Sources
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
tmpl was designed to work with any local or git-based template. Unfortunately, in contrast to boilr, this means
|
|
|
|
it cannot be used with `user/repo` notation out of the box.
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
However, you _can_ set up a source (and subsequent env variable) to make it easier to use your preferred source while
|
|
|
|
still allowing for others.
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
### Setting up a source
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
Let's set up a source for [Gitea](https://gitea.com)
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
```
|
|
|
|
tmpl source add https://gitea.com gitea
|
|
|
|
```
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
To use it, either pass it in with the `--source` flag
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
```
|
|
|
|
tmpl --source gitea download jolheiser/tmpls tmpls
|
|
|
|
```
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
Or set it as the env variable `TMPL_SOURCE`
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
## Using a different branch
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
By default, tmpl will want to use a branch called `main` in your repository.
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
If you are using another branch as your default, you can set it as the env variable `TMPL_BRANCH`
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
Alternatively, you can specify on the command-line with the `--branch` flag of the `download` command
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
```
|
|
|
|
tmpl --source gitea download --branch license jolheiser/tmpls license
|
|
|
|
```
|
2021-11-02 03:22:27 +00:00
|
|
|
The above command would download the [license](https://git.jojodev.com/jolheiser/tmpls/src/branch/license) template from `jolheiser/tmpls`
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
## Putting it all together
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
I realize that many users will be using GitHub, and most will likely still be using the `master` branch.
|
2020-11-19 05:19:19 +00:00
|
|
|
|
2020-11-22 05:25:24 +00:00
|
|
|
1. Set up a source for GitHub
|
|
|
|
1. `tmpl source add https://github.com github`
|
|
|
|
2. Set the env variable `TMPL_SOURCE` to `github`
|
|
|
|
2. Set the env variable `TMPL_BRANCH` to `master`
|
2020-11-30 05:16:25 +00:00
|
|
|
3. Happy templating! `tmpl download user/repo repo`
|
|
|
|
|
|
|
|
## Backup and Restore
|
|
|
|
|
|
|
|
1. The simplest solution is to make a copy of your `registry.toml` (default: `~/.tmpl/registry.toml`).
|
|
|
|
* Once in the new location, you will need to use `tmpl restore`.
|
|
|
|
|
2021-01-03 03:29:37 +00:00
|
|
|
2. Alternatively, you can copy/paste the entire registry (default: `~/.tmpl`) and skip the restore step.
|
|
|
|
|
|
|
|
## `.tmplkeep`
|
|
|
|
|
|
|
|
Perhaps you are familiar with `.gitkeep` and its unofficial status in git. Git does not like empty directories, so usually
|
|
|
|
a `.gitkeep` (or just `.keep`) file is added to retain the directory while keeping it effectively empty.
|
|
|
|
|
|
|
|
tmpl instead uses `.tmplkeep` files for this purpose. The difference is, tmpl will **not** create the `.tmplkeep` file
|
|
|
|
when the template is executed. This allows you to set up directory structures (for staging, examples, etc.) that
|
|
|
|
will *actually* be empty after execution.
|