Skip to content

Commit f052d99

Browse files
committedJan 24, 2016
Auto merge of #2081 - vi:cargo_init, r=alexcrichton
Implement `cargo init` command and appropriate tests ( #21). Features: * Working like `cargo new` if there are no files in current directory * Auto-detection of `--bin` * Auto-detection of already existing VSC and appending to respecive ignore file * Appending of appropriate `[lib]` or `[[bin]]` section to `Cargo.toml` in case of some non-standard source locations Concerns: * I'm not experienced in Rust + lazy => code looks poorer compared to the rest Cargo code * The test don't cover 100% of functions * Project consisting of both binary and library is not handled * Many deviations from [previously proposed algorithm](#2008 (comment))
2 parents 1a929a1 + 800172f commit f052d99

File tree

8 files changed

+688
-46
lines changed

8 files changed

+688
-46
lines changed
 

‎src/bin/cargo.rs

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ macro_rules! each_subcommand{
6969
$mac!(generate_lockfile);
7070
$mac!(git_checkout);
7171
$mac!(help);
72+
$mac!(init);
7273
$mac!(install);
7374
$mac!(locate_project);
7475
$mac!(login);

‎src/bin/init.rs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use std::env;
2+
3+
use cargo::ops;
4+
use cargo::util::{CliResult, Config};
5+
6+
#[derive(RustcDecodable)]
7+
struct Options {
8+
flag_verbose: bool,
9+
flag_quiet: bool,
10+
flag_color: Option<String>,
11+
flag_bin: bool,
12+
arg_path: Option<String>,
13+
flag_name: Option<String>,
14+
flag_vcs: Option<ops::VersionControl>,
15+
}
16+
17+
pub const USAGE: &'static str = "
18+
Create a new cargo package in current directory
19+
20+
Usage:
21+
cargo init [options] [<path>]
22+
cargo init -h | --help
23+
24+
Options:
25+
-h, --help Print this message
26+
--vcs VCS Initialize a new repository for the given version
27+
control system (git or hg) or do not initialize any version
28+
control at all (none) overriding a global configuration.
29+
--bin Use a binary instead of a library template
30+
--name NAME Set the resulting package name
31+
-v, --verbose Use verbose output
32+
-q, --quiet No output printed to stdout
33+
--color WHEN Coloring: auto, always, never
34+
";
35+
36+
pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
37+
debug!("executing; cmd=cargo-init; args={:?}", env::args().collect::<Vec<_>>());
38+
try!(config.shell().set_verbosity(options.flag_verbose, options.flag_quiet));
39+
try!(config.shell().set_color_config(options.flag_color.as_ref().map(|s| &s[..])));
40+
41+
let Options { flag_bin, arg_path, flag_name, flag_vcs, .. } = options;
42+
43+
let opts = ops::NewOptions {
44+
version_control: flag_vcs,
45+
bin: flag_bin,
46+
path: &arg_path.unwrap_or(format!(".")),
47+
name: flag_name.as_ref().map(|s| s.as_ref()),
48+
};
49+
50+
try!(ops::init(opts, config));
51+
Ok(None)
52+
}
53+

0 commit comments

Comments
 (0)