Skip to content

Commit 875a8ab

Browse files
Ewan Higgsehiggs
authored andcommitted
Cargo templating for new and init
PR #3004 This is a resubmission of the PR #1747 (from scratch) which adds support for templating in Cargo. The templates are implemented using the handlebars crate (where the original PR used mustache). Examples: cargo new --template https://url/to/template somedir foo cargo new --template https://url/to/templates --template-subdir somedir foo cargo new --template ../path/to/template somedir foo
1 parent 546212c commit 875a8ab

File tree

16 files changed

+713
-84
lines changed

16 files changed

+713
-84
lines changed

Cargo.lock

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ fs2 = "0.3"
2828
git2 = "0.6"
2929
git2-curl = "0.7"
3030
glob = "0.2"
31+
handlebars = "0.20"
3132
libc = "0.2"
3233
libgit2-sys = "0.6"
3334
log = "0.3"
35+
miow = "0.1"
3436
num_cpus = "1.0"
3537
regex = "0.1"
3638
rustc-serialize = "0.3"

src/bin/init.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub struct Options {
1212
flag_lib: bool,
1313
arg_path: Option<String>,
1414
flag_name: Option<String>,
15+
flag_template_subdir: Option<String>,
16+
flag_template: Option<String>,
1517
flag_vcs: Option<ops::VersionControl>,
1618
flag_frozen: bool,
1719
flag_locked: bool,
@@ -32,6 +34,8 @@ Options:
3234
--bin Use a binary (application) template
3335
--lib Use a library template
3436
--name NAME Set the resulting package name
37+
--template <repository> Use a specified template repository
38+
--template-subdir <template> Use a specified template within a template repository
3539
-v, --verbose ... Use verbose output (-vv very verbose/build.rs output)
3640
-q, --quiet No output printed to stdout
3741
--color WHEN Coloring: auto, always, never
@@ -47,14 +51,22 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
4751
options.flag_frozen,
4852
options.flag_locked)?;
4953

50-
let Options { flag_bin, flag_lib, arg_path, flag_name, flag_vcs, .. } = options;
54+
let Options {
55+
flag_bin, flag_lib,
56+
arg_path, flag_name,
57+
flag_vcs,
58+
flag_template_subdir, flag_template,
59+
..
60+
} = options;
5161

5262
let tmp = &arg_path.unwrap_or(format!("."));
5363
let opts = ops::NewOptions::new(flag_vcs,
5464
flag_bin,
5565
flag_lib,
5666
tmp,
57-
flag_name.as_ref().map(|s| s.as_ref()));
67+
flag_name.as_ref().map(|s| s.as_ref()),
68+
flag_template_subdir.as_ref().map(|s| s.as_ref()),
69+
flag_template.as_ref().map(|s| s.as_ref()));
5870

5971
let opts_lib = opts.lib;
6072
ops::init(opts, config)?;

src/bin/new.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ pub struct Options {
1212
flag_lib: bool,
1313
arg_path: String,
1414
flag_name: Option<String>,
15+
flag_template_subdir: Option<String>,
16+
flag_template: Option<String>,
1517
flag_vcs: Option<ops::VersionControl>,
1618
flag_frozen: bool,
1719
flag_locked: bool,
@@ -32,6 +34,8 @@ Options:
3234
--bin Use a binary (application) template
3335
--lib Use a library template
3436
--name NAME Set the resulting package name, defaults to the value of <path>
37+
--template <repository> Use a specified template repository
38+
--template-subdir <template-subdir> Use a specified template within a template repository
3539
-v, --verbose ... Use verbose output (-vv very verbose/build.rs output)
3640
-q, --quiet No output printed to stdout
3741
--color WHEN Coloring: auto, always, never
@@ -47,13 +51,21 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
4751
options.flag_frozen,
4852
options.flag_locked)?;
4953

50-
let Options { flag_bin, flag_lib, arg_path, flag_name, flag_vcs, .. } = options;
54+
let Options {
55+
flag_bin, flag_lib,
56+
arg_path, flag_name,
57+
flag_vcs,
58+
flag_template_subdir, flag_template,
59+
..
60+
} = options;
5161

5262
let opts = ops::NewOptions::new(flag_vcs,
5363
flag_bin,
5464
flag_lib,
5565
&arg_path,
56-
flag_name.as_ref().map(|s| s.as_ref()));
66+
flag_name.as_ref().map(|s| s.as_ref()),
67+
flag_template_subdir.as_ref().map(|s| s.as_ref()),
68+
flag_template.as_ref().map(|s| s.as_ref()));
5769

5870
let opts_lib = opts.lib;
5971
ops::new(opts, config)?;

src/cargo/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ extern crate flate2;
1212
extern crate fs2;
1313
extern crate git2;
1414
extern crate glob;
15+
extern crate handlebars;
1516
extern crate libc;
1617
extern crate libgit2_sys;
1718
extern crate num_cpus;

0 commit comments

Comments
 (0)