Skip to content

Commit 3bc8865

Browse files
committed
Auto merge of #3795 - jryans:template-year, r=alexcrichton
Add year to project template variables This adds the current year as a `year` variable for project templates. Some license files / headers include the year, so this should make it easier to include those in a template.
2 parents c54a2ad + 47221e9 commit 3bc8865

File tree

6 files changed

+38
-2
lines changed

6 files changed

+38
-2
lines changed

Cargo.lock

+19
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
@@ -43,6 +43,7 @@ shell-escape = "0.1"
4343
tar = { version = "0.4", default-features = false }
4444
tempdir = "0.3"
4545
term = "0.4.4"
46+
time = "0.1.36"
4647
toml = "0.3"
4748
url = "1.1"
4849

src/cargo/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extern crate shell_escape;
2626
extern crate tar;
2727
extern crate tempdir;
2828
extern crate term;
29+
extern crate time;
2930
extern crate toml;
3031
extern crate url;
3132

src/cargo/ops/cargo_new.rs

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use term::color::BLACK;
1010

1111
use handlebars::{Handlebars, no_escape};
1212
use tempdir::TempDir;
13+
use time;
1314
use toml;
1415

1516
use core::Workspace;
@@ -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(), (time::now().tm_year + 1900).to_string());
523525

524526
let template_set = try!(get_input_template(config, opts));
525527
for template in template_set.template_files.iter() {

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
@@ -2,6 +2,7 @@ extern crate cargo;
22
extern crate cargotest;
33
extern crate hamcrest;
44
extern crate tempdir;
5+
extern crate time;
56

67
use std::fs::{self, File};
78
use std::io::prelude::*;
@@ -75,6 +76,10 @@ fn simple_template() {
7576
name = "{{name}}"
7677
version = "0.0.1"
7778
authors = ["{{author}}"]
79+
"#).unwrap();
80+
File::create(&root.join("home/.cargo/templates/testtemplate/LICENSE"))
81+
.unwrap().write_all(br#"
82+
(c) {{year}} {{author}}
7883
"#).unwrap();
7984
File::create(&root.join("home/.cargo/templates/testtemplate/src/main.rs"))
8085
.unwrap().write_all(br#"
@@ -94,8 +99,15 @@ fn main () {
9499

95100
assert_that(&paths::root().join("foo"), existing_dir());
96101
assert_that(&paths::root().join("foo/Cargo.toml"), existing_file());
102+
assert_that(&paths::root().join("foo/LICENSE"), existing_file());
97103
assert_that(&paths::root().join("foo/src/main.rs"), existing_file());
98104

105+
let license = paths::root().join("foo/LICENSE");
106+
let mut contents = String::new();
107+
File::open(&license).unwrap().read_to_string(&mut contents).unwrap();
108+
let expected = format!("(c) {} {}", (time::now().tm_year + 1900).to_string(), "foo");
109+
assert!(contents.contains(&expected));
110+
99111
assert_that(cargo_process("build").cwd(&paths::root().join("foo")),
100112
execs().with_status(0));
101113
assert_that(&paths::root().join(&format!("foo/target/debug/foo{}",

0 commit comments

Comments
 (0)