Skip to content

Add year to project template variables #3795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 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 @@ -43,6 +43,7 @@ shell-escape = "0.1"
tar = { version = "0.4", default-features = false }
tempdir = "0.3"
term = "0.4.4"
time = "0.1.36"
toml = "0.3"
url = "1.1"

Expand Down
1 change: 1 addition & 0 deletions src/cargo/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern crate shell_escape;
extern crate tar;
extern crate tempdir;
extern crate term;
extern crate time;
extern crate toml;
extern crate url;

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 @@ -10,6 +10,7 @@ use term::color::BLACK;

use handlebars::{Handlebars, no_escape};
use tempdir::TempDir;
use time;
use toml;

use core::Workspace;
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(), (time::now().tm_year + 1900).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
Expand Up @@ -2,6 +2,7 @@ extern crate cargo;
extern crate cargotest;
extern crate hamcrest;
extern crate tempdir;
extern crate time;

use std::fs::{self, File};
use std::io::prelude::*;
Expand Down Expand Up @@ -64,6 +65,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 +88,15 @@ 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();
let expected = format!("(c) {} {}", (time::now().tm_year + 1900).to_string(), "foo");
assert!(contents.contains(&expected));

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