Skip to content

Commit

Permalink
Add --lib to init/new. Add status message for completed creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Turner committed Jul 29, 2016
1 parent 5157040 commit a882abe
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 35 deletions.
24 changes: 16 additions & 8 deletions src/bin/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct Options {
flag_quiet: Option<bool>,
flag_color: Option<String>,
flag_bin: bool,
flag_lib: bool,
arg_path: Option<String>,
flag_name: Option<String>,
flag_vcs: Option<ops::VersionControl>,
Expand All @@ -28,7 +29,8 @@ Options:
--vcs VCS Initialize a new repository for the given version
control system (git or hg) or do not initialize any version
control at all (none) overriding a global configuration.
--bin Use a binary instead of a library template
--bin Use a binary (application) template
--lib Use a library template
--name NAME Set the resulting package name
-v, --verbose ... Use verbose output
-q, --quiet No output printed to stdout
Expand All @@ -45,16 +47,22 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
options.flag_frozen,
options.flag_locked));

let Options { flag_bin, arg_path, flag_name, flag_vcs, .. } = options;
let Options { flag_bin, flag_lib, arg_path, flag_name, flag_vcs, .. } = options;

let opts = ops::NewOptions {
version_control: flag_vcs,
bin: flag_bin,
path: &arg_path.unwrap_or(format!(".")),
name: flag_name.as_ref().map(|s| s.as_ref()),
};
let tmp = &arg_path.unwrap_or(format!("."));
let opts = ops::NewOptions::new(flag_vcs,
flag_bin,
flag_lib,
tmp,
flag_name.as_ref().map(|s| s.as_ref()));

let opts_lib = opts.lib;
try!(ops::init(opts, config));

try!(config.shell().status("Created", format!("{} project",
if opts_lib { "library" }
else {"binary (application)"})));

Ok(None)
}

24 changes: 16 additions & 8 deletions src/bin/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct Options {
flag_quiet: Option<bool>,
flag_color: Option<String>,
flag_bin: bool,
flag_lib: bool,
arg_path: String,
flag_name: Option<String>,
flag_vcs: Option<ops::VersionControl>,
Expand All @@ -28,7 +29,8 @@ Options:
--vcs VCS Initialize a new repository for the given version
control system (git or hg) or do not initialize any version
control at all (none) overriding a global configuration.
--bin Use a binary instead of a library template
--bin Use a binary (application) template
--lib Use a library template
--name NAME Set the resulting package name
-v, --verbose ... Use verbose output
-q, --quiet No output printed to stdout
Expand All @@ -45,16 +47,22 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
options.flag_frozen,
options.flag_locked));

let Options { flag_bin, arg_path, flag_name, flag_vcs, .. } = options;
let Options { flag_bin, flag_lib, arg_path, flag_name, flag_vcs, .. } = options;

let opts = ops::NewOptions {
version_control: flag_vcs,
bin: flag_bin,
path: &arg_path,
name: flag_name.as_ref().map(|s| s.as_ref()),
};
let opts = ops::NewOptions::new(flag_vcs,
flag_bin,
flag_lib,
&arg_path,
flag_name.as_ref().map(|s| s.as_ref()));

let opts_lib = opts.lib;
try!(ops::new(opts, config));

try!(config.shell().status("Created", format!("{} `{}` project",
if opts_lib { "library" }
else {"binary (application)"},
arg_path)));

Ok(None)
}

34 changes: 34 additions & 0 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum VersionControl { Git, Hg, NoVcs }
pub struct NewOptions<'a> {
pub version_control: Option<VersionControl>,
pub bin: bool,
pub lib: bool,
pub path: &'a str,
pub name: Option<&'a str>,
}
Expand Down Expand Up @@ -53,6 +54,31 @@ impl Decodable for VersionControl {
}
}

impl<'a> NewOptions<'a> {
pub fn new(version_control: Option<VersionControl>,
bin: bool,
lib: bool,
path: &'a str,
name: Option<&'a str>) -> NewOptions<'a> {

// default to lib
let is_lib = if !bin {
true
}
else {
lib
};

NewOptions {
version_control: version_control,
bin: bin,
lib: is_lib,
path: path,
name: name,
}
}
}

struct CargoNewConfig {
name: Option<String>,
email: Option<String>,
Expand Down Expand Up @@ -235,6 +261,10 @@ pub fn new(opts: NewOptions, config: &Config) -> CargoResult<()> {
path.display())
}

if opts.lib && opts.bin {
bail!("can't specify both lib and binary outputs");
}

let name = try!(get_name(&path, &opts, config));
try!(check_name(name));

Expand All @@ -260,6 +290,10 @@ pub fn init(opts: NewOptions, config: &Config) -> CargoResult<()> {
bail!("`cargo init` cannot be run on existing Cargo projects")
}

if opts.lib && opts.bin {
bail!("can't specify both lib and binary outputs");
}

let name = try!(get_name(&path, &opts, config));
try!(check_name(name));

Expand Down
1 change: 1 addition & 0 deletions tests/cargotest/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ fn substitute_macros(input: &str) -> String {
let macros = [
("[RUNNING]", " Running"),
("[COMPILING]", " Compiling"),
("[CREATED]", " Created"),
("[FINISHED]", " Finished"),
("[ERROR]", "error:"),
("[WARNING]", "warning:"),
Expand Down
31 changes: 19 additions & 12 deletions tests/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ fn cargo_process(s: &str) -> ProcessBuilder {

#[test]
fn simple_lib() {
assert_that(cargo_process("init").arg("--vcs").arg("none")
assert_that(cargo_process("init").arg("--lib").arg("--vcs").arg("none")
.env("USER", "foo"),
execs().with_status(0));
execs().with_status(0).with_stderr("\
[CREATED] library project
"));

assert_that(&paths::root().join("Cargo.toml"), existing_file());
assert_that(&paths::root().join("src/lib.rs"), existing_file());
Expand All @@ -38,7 +40,9 @@ fn simple_bin() {
fs::create_dir(&path).unwrap();
assert_that(cargo_process("init").arg("--bin").arg("--vcs").arg("none")
.env("USER", "foo").cwd(&path),
execs().with_status(0));
execs().with_status(0).with_stderr("\
[CREATED] binary (application) project
"));

assert_that(&paths::root().join("foo/Cargo.toml"), existing_file());
assert_that(&paths::root().join("foo/src/main.rs"), existing_file());
Expand Down Expand Up @@ -165,7 +169,7 @@ fn multibin_project_name_clash() {
}
"#).unwrap();

assert_that(cargo_process("init").arg("--vcs").arg("none")
assert_that(cargo_process("init").arg("--lib").arg("--vcs").arg("none")
.env("USER", "foo").cwd(&path),
execs().with_status(101).with_stderr("\
[ERROR] multiple possible binary sources found:
Expand Down Expand Up @@ -214,8 +218,10 @@ fn lib_already_exists_nosrc() {

#[test]
fn simple_git() {
assert_that(cargo_process("init").arg("--vcs").arg("git")
.env("USER", "foo"),
assert_that(cargo_process("init").arg("--lib")
.arg("--vcs")
.arg("git")
.env("USER", "foo"),
execs().with_status(0));

assert_that(&paths::root().join("Cargo.toml"), existing_file());
Expand All @@ -229,7 +235,8 @@ fn auto_git() {
let td = TempDir::new("cargo").unwrap();
let foo = &td.path().join("foo");
fs::create_dir_all(&foo).unwrap();
assert_that(cargo_process("init").cwd(foo.clone())
assert_that(cargo_process("init").arg("--lib")
.cwd(foo.clone())
.env("USER", "foo"),
execs().with_status(0));

Expand Down Expand Up @@ -271,7 +278,7 @@ use --name to override crate name
fn git_autodetect() {
fs::create_dir(&paths::root().join(".git")).unwrap();

assert_that(cargo_process("init")
assert_that(cargo_process("init").arg("--lib")
.env("USER", "foo"),
execs().with_status(0));

Expand All @@ -287,7 +294,7 @@ fn git_autodetect() {
fn mercurial_autodetect() {
fs::create_dir(&paths::root().join(".hg")).unwrap();

assert_that(cargo_process("init")
assert_that(cargo_process("init").arg("--lib")
.env("USER", "foo"),
execs().with_status(0));

Expand All @@ -304,8 +311,8 @@ fn gitignore_appended_not_replaced() {

File::create(&paths::root().join(".gitignore")).unwrap().write_all(b"qqqqqq\n").unwrap();

assert_that(cargo_process("init")
.env("USER", "foo"),
assert_that(cargo_process("init").arg("--lib")
.env("USER", "foo"),
execs().with_status(0));


Expand All @@ -323,7 +330,7 @@ fn gitignore_appended_not_replaced() {
fn cargo_lock_gitignored_if_lib1() {
fs::create_dir(&paths::root().join(".git")).unwrap();

assert_that(cargo_process("init").arg("--vcs").arg("git")
assert_that(cargo_process("init").arg("--lib").arg("--vcs").arg("git")
.env("USER", "foo"),
execs().with_status(0));

Expand Down
16 changes: 10 additions & 6 deletions tests/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ fn cargo_process(s: &str) -> ProcessBuilder {

#[test]
fn simple_lib() {
assert_that(cargo_process("new").arg("foo").arg("--vcs").arg("none")
assert_that(cargo_process("new").arg("--lib").arg("foo").arg("--vcs").arg("none")
.env("USER", "foo"),
execs().with_status(0));
execs().with_status(0).with_stderr("\
[CREATED] library `foo` project
"));

assert_that(&paths::root().join("foo"), existing_dir());
assert_that(&paths::root().join("foo/Cargo.toml"), existing_file());
Expand All @@ -36,9 +38,11 @@ fn simple_lib() {

#[test]
fn simple_bin() {
assert_that(cargo_process("new").arg("foo").arg("--bin")
assert_that(cargo_process("new").arg("--bin").arg("foo")
.env("USER", "foo"),
execs().with_status(0));
execs().with_status(0).with_stderr("\
[CREATED] binary (application) `foo` project
"));

assert_that(&paths::root().join("foo"), existing_dir());
assert_that(&paths::root().join("foo/Cargo.toml"), existing_file());
Expand All @@ -54,7 +58,7 @@ fn simple_bin() {
#[test]
fn simple_git() {
let td = TempDir::new("cargo").unwrap();
assert_that(cargo_process("new").arg("foo").cwd(td.path().clone())
assert_that(cargo_process("new").arg("--lib").arg("foo").cwd(td.path().clone())
.env("USER", "foo"),
execs().with_status(0));

Expand Down Expand Up @@ -120,7 +124,7 @@ use --name to override crate name"));

#[test]
fn rust_prefix_stripped() {
assert_that(cargo_process("new").arg("rust-foo").env("USER", "foo"),
assert_that(cargo_process("new").arg("--lib").arg("rust-foo").env("USER", "foo"),
execs().with_status(0)
.with_stdout("note: package will be named `foo`; use --name to override"));
let toml = paths::root().join("rust-foo/Cargo.toml");
Expand Down
3 changes: 2 additions & 1 deletion tests/workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ fn new_warns_you_this_will_not_work() {
.file("src/lib.rs", "");
p.build();

assert_that(p.cargo("new").arg("bar").env("USER", "foo"),
assert_that(p.cargo("new").arg("--lib").arg("bar").env("USER", "foo"),
execs().with_status(0)
.with_stderr("\
warning: compiling this new crate may not work due to invalid workspace \
Expand All @@ -780,6 +780,7 @@ workspace: [..]
this may be fixable by ensuring that this crate is depended on by the workspace \
root: [..]
[CREATED] library `bar` project
"));
}

Expand Down

0 comments on commit a882abe

Please sign in to comment.