Skip to content

Commit 69ffd99

Browse files
committed
Add year to project template variables
1 parent 19ea423 commit 69ffd99

File tree

6 files changed

+49
-3
lines changed

6 files changed

+49
-3
lines changed

Cargo.lock

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ name = "cargo"
1717
path = "src/cargo/lib.rs"
1818

1919
[dependencies]
20+
chrono = "0.2.25"
2021
crates-io = { path = "src/crates-io", version = "0.7" }
2122
crossbeam = "0.2"
2223
curl = "0.4.6"

src/cargo/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#[macro_use] extern crate log;
66
#[macro_use] extern crate serde_derive;
77
#[macro_use] extern crate serde_json;
8+
extern crate chrono;
89
extern crate crates_io as registry;
910
extern crate crossbeam;
1011
extern crate curl;

src/cargo/ops/cargo_new.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use git2::Config as GitConfig;
88

99
use term::color::BLACK;
1010

11+
use chrono::{Datelike,Local};
1112
use handlebars::{Handlebars, no_escape};
1213
use tempdir::TempDir;
1314
use toml;
@@ -520,6 +521,7 @@ fn mk(config: &Config, opts: &MkOptions) -> CargoResult<()> {
520521
let mut data = BTreeMap::new();
521522
data.insert("name".to_owned(), name.to_owned());
522523
data.insert("author".to_owned(), author);
524+
data.insert("year".to_owned(), Local::now().year().to_string());
523525

524526
let template_set = try!(get_input_template(config, opts));
525527
for template in template_set.template_files.iter() {
@@ -582,7 +584,7 @@ fn collect_template_dir(template_path: &PathBuf, _: &Path) -> CargoResult<Vec<Bo
582584
human(format!("entry is somehow not a subpath \
583585
of the directory being walked."))
584586
})));
585-
templates.push(Box::new(InputFileTemplateFile::new(entry_path,
587+
templates.push(Box::new(InputFileTemplateFile::new(entry_path,
586588
dest_file_name.to_path_buf())));
587589
Ok(())
588590
}));

src/doc/guide.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ repository by default. If you don't want it to do that, pass `--vcs none`.
2929

3030
You can also use your own template to scaffold cargo projects! See the
3131
[Templates](#templates) section for more details.
32-
32+
3333
Let’s check out what Cargo has generated for us:
3434

3535
```shell
@@ -480,7 +480,8 @@ $ cargo new proj --template http://your/project/repo
480480
The variables available for use are:
481481

482482
- `name`: the name of the project
483-
- `authors`: the toml formatted name of the project author
483+
- `author`: the toml formatted name of the project author
484+
- `year`: the current year
484485

485486
In the future, more variables may be added. Suggestions welcome!
486487

tests/new.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
extern crate cargo;
22
extern crate cargotest;
3+
extern crate chrono;
34
extern crate hamcrest;
45
extern crate tempdir;
56

@@ -10,6 +11,7 @@ use std::env;
1011
use cargo::util::ProcessBuilder;
1112
use cargotest::process;
1213
use cargotest::support::{execs, git, paths};
14+
use chrono::{Datelike,Local};
1315
use hamcrest::{assert_that, existing_file, existing_dir, is_not};
1416
use tempdir::TempDir;
1517

@@ -64,6 +66,10 @@ fn simple_template() {
6466
name = "{{name}}"
6567
version = "0.0.1"
6668
authors = ["{{author}}"]
69+
"#).unwrap();
70+
File::create(&root.join("home/.cargo/templates/testtemplate/LICENSE"))
71+
.unwrap().write_all(br#"
72+
(c) {{year}} {{author}}
6773
"#).unwrap();
6874
File::create(&root.join("home/.cargo/templates/testtemplate/src/main.rs"))
6975
.unwrap().write_all(br#"
@@ -83,8 +89,14 @@ fn main () {
8389

8490
assert_that(&paths::root().join("foo"), existing_dir());
8591
assert_that(&paths::root().join("foo/Cargo.toml"), existing_file());
92+
assert_that(&paths::root().join("foo/LICENSE"), existing_file());
8693
assert_that(&paths::root().join("foo/src/main.rs"), existing_file());
8794

95+
let license = paths::root().join("foo/LICENSE");
96+
let mut contents = String::new();
97+
File::open(&license).unwrap().read_to_string(&mut contents).unwrap();
98+
assert!(contents.contains(&format!("(c) {} {}", Local::now().year(), "foo")));
99+
88100
assert_that(cargo_process("build").cwd(&paths::root().join("foo")),
89101
execs().with_status(0));
90102
assert_that(&paths::root().join(&format!("foo/target/debug/foo{}",

0 commit comments

Comments
 (0)