Skip to content

Commit 6b9c063

Browse files
committed
File per command
1 parent f2f062c commit 6b9c063

32 files changed

+656
-525
lines changed

src/bin/cli.rs

Lines changed: 19 additions & 485 deletions
Large diffs are not rendered by default.

src/bin/command_prelude.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use std::path::PathBuf;
22

3-
use clap::{self, SubCommand, AppSettings, ArgMatches};
4-
use cargo::{Config, CargoResult};
3+
use clap::{self, SubCommand};
4+
use cargo::CargoResult;
55
use cargo::core::Workspace;
66
use cargo::ops::{CompileMode, CompileOptions, CompileFilter, Packages, MessageFormat,
77
VersionControl, NewOptions};
88
use cargo::util::important_paths::find_root_manifest_for_wd;
99

10-
pub use clap::Arg;
10+
pub use clap::{Arg, ArgMatches, AppSettings};
11+
pub use cargo::{Config, CliResult, CliError};
1112

1213
pub type App = clap::App<'static, 'static>;
1314

src/bin/commands/bench.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use command_prelude::*;
2-
use clap::AppSettings;
2+
3+
use cargo::ops::{self, CompileMode, TestOptions};
34

45
pub fn cli() -> App {
56
subcommand("bench")
@@ -65,3 +66,31 @@ not affect how many jobs are used when running the benchmarks.
6566
Compilation can be customized with the `bench` profile in the manifest.
6667
")
6768
}
69+
70+
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
71+
let ws = args.workspace(config)?;
72+
let mut compile_opts = args.compile_options(config, CompileMode::Bench)?;
73+
compile_opts.release = true;
74+
75+
let ops = TestOptions {
76+
no_run: args.is_present("no-run"),
77+
no_fail_fast: args.is_present("no-fail-fast"),
78+
only_doc: false,
79+
compile_opts,
80+
};
81+
82+
let mut bench_args = vec![];
83+
bench_args.extend(args.value_of("BENCHNAME").into_iter().map(|s| s.to_string()));
84+
bench_args.extend(args.values_of("args").unwrap_or_default().map(|s| s.to_string()));
85+
86+
let err = ops::run_benches(&ws, &ops, &bench_args)?;
87+
match err {
88+
None => Ok(()),
89+
Some(err) => {
90+
Err(match err.exit.as_ref().and_then(|e| e.code()) {
91+
Some(i) => CliError::new(format_err!("bench failed"), i),
92+
None => CliError::new(err.into(), 101)
93+
})
94+
}
95+
}
96+
}

src/bin/commands/build.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use command_prelude::*;
22

3+
use cargo::ops::{self, CompileMode};
4+
35
pub fn cli() -> App {
46
subcommand("build").alias("b")
57
.about("Compile a local package and all of its dependencies")
@@ -42,3 +44,10 @@ the --release flag will use the `release` profile instead.
4244
")
4345

4446
}
47+
48+
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
49+
let ws = args.workspace(config)?;
50+
let compile_opts = args.compile_options(config, CompileMode::Build)?;
51+
ops::compile(&ws, &compile_opts)?;
52+
Ok(())
53+
}

src/bin/commands/check.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use command_prelude::*;
22

3+
use cargo::ops::{self, CompileMode};
4+
35
pub fn cli() -> App {
46
subcommand("check")
57
.about("Check a local package and all of its dependencies for errors")
@@ -48,3 +50,20 @@ The `--profile test` flag can be used to check unit tests with the
4850
`#[cfg(test)]` attribute.
4951
")
5052
}
53+
54+
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
55+
let ws = args.workspace(config)?;
56+
let test = match args.value_of("profile") {
57+
Some("test") => true,
58+
None => false,
59+
Some(profile) => {
60+
let err = format_err!("unknown profile: `{}`, only `test` is \
61+
currently supported", profile);
62+
return Err(CliError::new(err, 101));
63+
}
64+
};
65+
let mode = CompileMode::Check { test };
66+
let compile_opts = args.compile_options(config, mode)?;
67+
ops::compile(&ws, &compile_opts)?;
68+
Ok(())
69+
}

src/bin/commands/clean.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use command_prelude::*;
22

3+
use cargo::ops::{self, CleanOptions};
4+
35
pub fn cli() -> App {
46
subcommand("clean")
57
.about("Remove artifacts that cargo has generated in the past")
@@ -17,3 +19,15 @@ given, then all packages' artifacts are removed. For more information on SPEC
1719
and its format, see the `cargo help pkgid` command.
1820
")
1921
}
22+
23+
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
24+
let ws = args.workspace(config)?;
25+
let opts = CleanOptions {
26+
config,
27+
spec: values(args, "package"),
28+
target: args.target(),
29+
release: args.is_present("release"),
30+
};
31+
ops::clean(&ws, &opts)?;
32+
Ok(())
33+
}

src/bin/commands/doc.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use command_prelude::*;
22

3+
use cargo::ops::{self, CompileMode, DocOptions};
4+
35
pub fn cli() -> App {
46
subcommand("doc")
57
.about("Build a package's documentation")
@@ -39,3 +41,15 @@ current package is documented. For more information on SPEC and its format, see
3941
the `cargo help pkgid` command.
4042
")
4143
}
44+
45+
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
46+
let ws = args.workspace(config)?;
47+
let mode = CompileMode::Doc { deps: !args.is_present("no-deps") };
48+
let compile_opts = args.compile_options(config, mode)?;
49+
let doc_opts = DocOptions {
50+
open_result: args.is_present("open"),
51+
compile_opts,
52+
};
53+
ops::doc(&ws, &doc_opts)?;
54+
Ok(())
55+
}

src/bin/commands/fetch.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use command_prelude::*;
22

3+
use cargo::ops;
4+
35
pub fn cli() -> App {
46
subcommand("fetch")
57
.about("Fetch dependencies of a package from the network")
@@ -15,3 +17,9 @@ If the lockfile is not available, then this is the equivalent of
1517
all updated.
1618
")
1719
}
20+
21+
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
22+
let ws = args.workspace(config)?;
23+
ops::fetch(&ws)?;
24+
Ok(())
25+
}

src/bin/commands/generate_lockfile.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use command_prelude::*;
22

3+
use cargo::ops;
4+
35
pub fn cli() -> App {
46
subcommand("generate-lockfile")
57
.about("Generate the lockfile for a project")
@@ -15,3 +17,9 @@ If the lockfile is not available, then this is the equivalent of
1517
all updated.
1618
")
1719
}
20+
21+
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
22+
let ws = args.workspace(config)?;
23+
ops::generate_lockfile(&ws)?;
24+
Ok(())
25+
}

src/bin/commands/git_checkout.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
use command_prelude::*;
22

3+
use cargo::core::{GitReference, SourceId, Source};
4+
use cargo::sources::GitSource;
5+
use cargo::util::ToUrl;
6+
37
pub fn cli() -> App {
48
subcommand("git-checkout")
59
.about("Checkout a copy of a Git repository")
610
.arg(Arg::with_name("url").long("url").value_name("URL").required(true))
711
.arg(Arg::with_name("reference").long("reference").value_name("REF").required(true))
812
}
13+
14+
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
15+
let url = args.value_of("url").unwrap().to_url()?;
16+
let reference = args.value_of("reference").unwrap();
17+
18+
let reference = GitReference::Branch(reference.to_string());
19+
let source_id = SourceId::for_git(&url, reference)?;
20+
21+
let mut source = GitSource::new(&source_id, config)?;
22+
23+
source.update()?;
24+
25+
Ok(())
26+
}

src/bin/commands/init.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
use command_prelude::*;
22

3+
use cargo::ops;
4+
35
pub fn cli() -> App {
46
subcommand("init")
57
.about("Create a new cargo package in an existing directory")
68
.arg(Arg::with_name("path").default_value("."))
79
.arg_new_opts()
810
}
11+
12+
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
13+
let opts = args.new_options()?;
14+
ops::init(&opts, config)?;
15+
config.shell().status("Created", format!("{} project", opts.kind))?;
16+
Ok(())
17+
}

src/bin/commands/install.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
use command_prelude::*;
22

3+
use cargo::core::{GitReference, SourceId};
4+
use cargo::ops::{self, CompileMode};
5+
use cargo::util::ToUrl;
6+
37
pub fn cli() -> App {
48
subcommand("install")
59
.about("Create a new cargo package in an existing directory")
@@ -84,3 +88,40 @@ specified by setting the `CARGO_TARGET_DIR` environment variable to a relative
8488
path. In particular, this can be useful for caching build artifacts on
8589
continuous integration systems.")
8690
}
91+
92+
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
93+
let mut compile_opts = args.compile_options(config, CompileMode::Build)?;
94+
compile_opts.release = !args.is_present("debug");
95+
96+
let krates = args.values_of("crate").unwrap_or_default().collect::<Vec<_>>();
97+
98+
let source = if let Some(url) = args.value_of("git") {
99+
let url = url.to_url()?;
100+
let gitref = if let Some(branch) = args.value_of("branch") {
101+
GitReference::Branch(branch.to_string())
102+
} else if let Some(tag) = args.value_of("tag") {
103+
GitReference::Tag(tag.to_string())
104+
} else if let Some(rev) = args.value_of("rev") {
105+
GitReference::Rev(rev.to_string())
106+
} else {
107+
GitReference::Branch("master".to_string())
108+
};
109+
SourceId::for_git(&url, gitref)?
110+
} else if let Some(path) = args.value_of("path") {
111+
SourceId::for_path(&config.cwd().join(path))?
112+
} else if krates.is_empty() {
113+
SourceId::for_path(config.cwd())?
114+
} else {
115+
SourceId::crates_io(config)?
116+
};
117+
118+
let version = args.value_of("version");
119+
let root = args.value_of("root");
120+
121+
if args.is_present("list") {
122+
ops::install_list(root, config)?;
123+
} else {
124+
ops::install(root, krates, &source, version, &compile_opts, args.is_present("force"))?;
125+
}
126+
Ok(())
127+
}

src/bin/commands/locate_project.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
use command_prelude::*;
22

3+
use cargo::print_json;
4+
35
pub fn cli() -> App {
46
subcommand("locate-project")
57
.about("Checkout a copy of a Git repository")
68
.arg_manifest_path()
79
}
10+
11+
#[derive(Serialize)]
12+
pub struct ProjectLocation {
13+
root: String
14+
}
15+
16+
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
17+
let root = args.root_manifest(config)?;
18+
19+
let root = root.to_str()
20+
.ok_or_else(|| format_err!("your project path contains characters \
21+
not representable in Unicode"))
22+
.map_err(|e| CliError::new(e, 1))?
23+
.to_string();
24+
25+
let location = ProjectLocation { root };
26+
27+
print_json(&location);
28+
Ok(())
29+
}

src/bin/commands/login.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
use command_prelude::*;
22

3+
use std::io::{self, BufRead};
4+
5+
use cargo::core::{SourceId, Source};
6+
use cargo::sources::RegistrySource;
7+
use cargo::util::{CargoError, CargoResultExt};
8+
use cargo::ops;
9+
310
pub fn cli() -> App {
411
subcommand("login")
512
.about("Save an api token from the registry locally. \
@@ -8,3 +15,37 @@ pub fn cli() -> App {
815
.arg(opt("host", "Host to set the token for").value_name("HOST"))
916
.arg(opt("registry", "Registry to use").value_name("REGISTRY"))
1017
}
18+
19+
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
20+
let registry = args.registry(config)?;
21+
22+
let token = match args.value_of("token") {
23+
Some(token) => token.to_string(),
24+
None => {
25+
let host = match registry {
26+
Some(ref _registry) => {
27+
return Err(format_err!("token must be provided when \
28+
--registry is provided.").into());
29+
}
30+
None => {
31+
let src = SourceId::crates_io(config)?;
32+
let mut src = RegistrySource::remote(&src, config);
33+
src.update()?;
34+
let config = src.config()?.unwrap();
35+
args.value_of("host").map(|s| s.to_string())
36+
.unwrap_or(config.api.unwrap())
37+
}
38+
};
39+
println!("please visit {}me and paste the API Token below", host);
40+
let mut line = String::new();
41+
let input = io::stdin();
42+
input.lock().read_line(&mut line).chain_err(|| {
43+
"failed to read stdin"
44+
}).map_err(CargoError::from)?;
45+
line.trim().to_string()
46+
}
47+
};
48+
49+
ops::registry_login(config, token, registry)?;
50+
Ok(())
51+
}

0 commit comments

Comments
 (0)