Skip to content

Commit

Permalink
Add year to project template variables
Browse files Browse the repository at this point in the history
  • Loading branch information
jryans committed Mar 3, 2017
1 parent 19ea423 commit 69ffd99
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 3 deletions.
29 changes: 29 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ name = "cargo"
path = "src/cargo/lib.rs"

[dependencies]
chrono = "0.2.25"
crates-io = { path = "src/crates-io", version = "0.7" }
crossbeam = "0.2"
curl = "0.4.6"
Expand Down
1 change: 1 addition & 0 deletions src/cargo/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#[macro_use] extern crate log;
#[macro_use] extern crate serde_derive;
#[macro_use] extern crate serde_json;
extern crate chrono;
extern crate crates_io as registry;
extern crate crossbeam;
extern crate curl;
Expand Down
4 changes: 3 additions & 1 deletion src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use git2::Config as GitConfig;

use term::color::BLACK;

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

let template_set = try!(get_input_template(config, opts));
for template in template_set.template_files.iter() {
Expand Down Expand Up @@ -582,7 +584,7 @@ fn collect_template_dir(template_path: &PathBuf, _: &Path) -> CargoResult<Vec<Bo
human(format!("entry is somehow not a subpath \
of the directory being walked."))
})));
templates.push(Box::new(InputFileTemplateFile::new(entry_path,
templates.push(Box::new(InputFileTemplateFile::new(entry_path,
dest_file_name.to_path_buf())));
Ok(())
}));
Expand Down
5 changes: 3 additions & 2 deletions src/doc/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ repository by default. If you don't want it to do that, pass `--vcs none`.

You can also use your own template to scaffold cargo projects! See the
[Templates](#templates) section for more details.

Let’s check out what Cargo has generated for us:

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

- `name`: the name of the project
- `authors`: the toml formatted name of the project author
- `author`: the toml formatted name of the project author
- `year`: the current year

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

Expand Down
12 changes: 12 additions & 0 deletions tests/new.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extern crate cargo;
extern crate cargotest;
extern crate chrono;
extern crate hamcrest;
extern crate tempdir;

Expand All @@ -10,6 +11,7 @@ use std::env;
use cargo::util::ProcessBuilder;
use cargotest::process;
use cargotest::support::{execs, git, paths};
use chrono::{Datelike,Local};
use hamcrest::{assert_that, existing_file, existing_dir, is_not};
use tempdir::TempDir;

Expand Down Expand Up @@ -64,6 +66,10 @@ fn simple_template() {
name = "{{name}}"
version = "0.0.1"
authors = ["{{author}}"]
"#).unwrap();
File::create(&root.join("home/.cargo/templates/testtemplate/LICENSE"))
.unwrap().write_all(br#"
(c) {{year}} {{author}}
"#).unwrap();
File::create(&root.join("home/.cargo/templates/testtemplate/src/main.rs"))
.unwrap().write_all(br#"
Expand All @@ -83,8 +89,14 @@ fn main () {

assert_that(&paths::root().join("foo"), existing_dir());
assert_that(&paths::root().join("foo/Cargo.toml"), existing_file());
assert_that(&paths::root().join("foo/LICENSE"), existing_file());
assert_that(&paths::root().join("foo/src/main.rs"), existing_file());

let license = paths::root().join("foo/LICENSE");
let mut contents = String::new();
File::open(&license).unwrap().read_to_string(&mut contents).unwrap();
assert!(contents.contains(&format!("(c) {} {}", Local::now().year(), "foo")));

assert_that(cargo_process("build").cwd(&paths::root().join("foo")),
execs().with_status(0));
assert_that(&paths::root().join(&format!("foo/target/debug/foo{}",
Expand Down

0 comments on commit 69ffd99

Please sign in to comment.