Skip to content

Commit fcd6b60

Browse files
committed
Updated README and CHANGELOG.
1 parent 6099da4 commit fcd6b60

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
* Added support for expression script templates.
77

8+
* The built-in templates used for scripts can be overridden.
9+
810
**Fixed:**
911

1012
* Cargo script hanging on build failure on Windows.

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,40 @@ $ cat now.crs | cargo script --count --loop \
155155
5: }
156156
```
157157

158+
### Templates
159+
160+
You can use templates to avoid having to re-specify common code and dependencies. You can view a list of your templates by running `cargo-script templates list` (note the hyphen), or show the folder in which they should be stored by running `cargo-script templates show`. You can dump the contents of a template using `cargo-script templates dump NAME`.
161+
162+
Templates are Rust source files with two placeholders: `#{prelude}` for the auto-generated prelude (which should be placed at the top of the template), and `#{script}` for the contents of the script itself.
163+
164+
For example, a minimal expression template that adds a dependency and imports some additional symbols might be:
165+
166+
```rust
167+
// cargo-deps: itertools="0.6.2"
168+
#![allow(unused_imports)]
169+
#{prelude}
170+
extern crate itertools;
171+
use std::io::prelude::*;
172+
use std::mem;
173+
use itertools::Itertools;
174+
175+
fn main() {
176+
let result = {
177+
#{script}
178+
};
179+
println!("{:?}", result);
180+
}
181+
```
182+
183+
If stored in the templates folder as `grabbag.rs`, you can use it by passing the name `grabbag` via the `--template` option, like so:
184+
185+
```text
186+
$ cargo script -t grabbag -e "mem::size_of::<Box<Read>>()"
187+
16
188+
```
189+
190+
In addition, there are three built-in templates: `expr`, `loop`, and `loop-count`. These are used for the `--expr`, `--loop`, and `--loop --count` invocation forms. They can be overridden by placing templates with the same name in the template folder. If you have *not* overridden them, you can dump the contents of these built-in templates using the `templates dump` command noted above.
191+
158192
## Things That Should Probably Be Done
159193

160194
* Somehow convince the Cargo devs to add aggressive caching of dependencies so that compiling anything that has dependencies doesn't take an age.

0 commit comments

Comments
 (0)