Copies a folder structure and if templating data is supplied then all .tpl
files will be converted using Handlebars and the .tpl
file extension will then be stripped.
It does not write over existing files unless the -f
or --force
flag is present.
It can be run dry
which will skip any file writes, but still logs what would it do. Use the -d
or --dry
flags.
Folder and file names also support Handlebars syntax. (Although you can't use \
and many others in folder names so you are limited). After applying the template into the file/folder names, \n
characters (since they invalid anyway) will be handled specially. At every line break the created folder structure branches off. The content of each of them will be identical.
The second line will be serialized as "file1.txt.tpl\nfile2.txt.tpl"
json
{
"dir": "dir1\ndir2",
"file": ["file1.txt.tpl", "file2.txt.tpl"]
}
from this folder
bash
./
./bar.txt.tpl
./{{dir}}/{{file}}
./{{dir}}/non-template.txt
these output files and folders will be produced
bash
./
./bar.txt
./dir1
./dir2
./dir1/non-template.txt
./dir2/non-template.txt
./dir1/file1.txt
./dir2/file1.txt
./dir1/file2.txt
./dir2/file2.txt
You can try this out with this command after downloading this repository (Given that you have Rust and Cargo installed):
bash
cargo run ./templates/example_tpl_dir ./templates/example_to --json='{ \"file2\": \"bar\nbare\", \"dir\": \"dir1\ndir2\", \"file\": [\"file1.txt.tpl\", \"file2.txt.tpl\"] }'
toml
[dependencies]
cpt = "0.4.1"
bash
cargo install cpt
Using shorthands
```rust use cpt::cpt;
fn main() -> Result<(), Box
cpt(from, to, data) // cp(from, to) to use without templating } ```
Using the builder
```rust use cpt::Cpt;
fn main() -> Result<(), Box
Cpt::new(from, to) .setforce(true) .setdry(false) .set_data(data) .execute() } ```
bash
cpt ./example ./exampletest --json='{ \"foo\": \"bar\" }'
Using help:
```bash cpt --help
cpt 0.4.1 AlexAegis Copies one folder structure to another place with files. Also formats templates!
USAGE:
cpt.exe [FLAGS] [OPTIONS]
FLAGS: -d, --dry If set, nothing will be written to the disk -f, --force If set, files can be overwritten in the target folder -h, --help Prints help information -q, --quiet Tarpaulin -V, --version Prints version information
OPTIONS:
-j, --json
ARGS:
The serializer only supports strings and arrays. A valid TypeScript type of the input would look like this:
ts
interface Input {
[key: string]: string | string[];
}
I made this for my Advent of Code project scaffolder which you can find in my AoC repo.
For this to be a little more than just a tiny toy project the next step would be to implement context-aware templating. If we think of a template as a tree where the leaves are the contents of a file, and their parents are the names of their files, then it would be nice to pass some context to these nodes about their parents and their positions.
This would allow automatic indexing for example.