Skip to content

Commit

Permalink
File per command
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad committed Mar 12, 2018
1 parent f2f062c commit 6b9c063
Show file tree
Hide file tree
Showing 32 changed files with 656 additions and 525 deletions.
504 changes: 19 additions & 485 deletions src/bin/cli.rs

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/bin/command_prelude.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::path::PathBuf;

use clap::{self, SubCommand, AppSettings, ArgMatches};
use cargo::{Config, CargoResult};
use clap::{self, SubCommand};
use cargo::CargoResult;
use cargo::core::Workspace;
use cargo::ops::{CompileMode, CompileOptions, CompileFilter, Packages, MessageFormat,
VersionControl, NewOptions};
use cargo::util::important_paths::find_root_manifest_for_wd;

pub use clap::Arg;
pub use clap::{Arg, ArgMatches, AppSettings};
pub use cargo::{Config, CliResult, CliError};

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

Expand Down
31 changes: 30 additions & 1 deletion src/bin/commands/bench.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use command_prelude::*;
use clap::AppSettings;

use cargo::ops::{self, CompileMode, TestOptions};

pub fn cli() -> App {
subcommand("bench")
Expand Down Expand Up @@ -65,3 +66,31 @@ not affect how many jobs are used when running the benchmarks.
Compilation can be customized with the `bench` profile in the manifest.
")
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
let ws = args.workspace(config)?;
let mut compile_opts = args.compile_options(config, CompileMode::Bench)?;
compile_opts.release = true;

let ops = TestOptions {
no_run: args.is_present("no-run"),
no_fail_fast: args.is_present("no-fail-fast"),
only_doc: false,
compile_opts,
};

let mut bench_args = vec![];
bench_args.extend(args.value_of("BENCHNAME").into_iter().map(|s| s.to_string()));
bench_args.extend(args.values_of("args").unwrap_or_default().map(|s| s.to_string()));

let err = ops::run_benches(&ws, &ops, &bench_args)?;
match err {
None => Ok(()),
Some(err) => {
Err(match err.exit.as_ref().and_then(|e| e.code()) {
Some(i) => CliError::new(format_err!("bench failed"), i),
None => CliError::new(err.into(), 101)
})
}
}
}
9 changes: 9 additions & 0 deletions src/bin/commands/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use command_prelude::*;

use cargo::ops::{self, CompileMode};

pub fn cli() -> App {
subcommand("build").alias("b")
.about("Compile a local package and all of its dependencies")
Expand Down Expand Up @@ -42,3 +44,10 @@ the --release flag will use the `release` profile instead.
")

}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
let ws = args.workspace(config)?;
let compile_opts = args.compile_options(config, CompileMode::Build)?;
ops::compile(&ws, &compile_opts)?;
Ok(())
}
19 changes: 19 additions & 0 deletions src/bin/commands/check.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use command_prelude::*;

use cargo::ops::{self, CompileMode};

pub fn cli() -> App {
subcommand("check")
.about("Check a local package and all of its dependencies for errors")
Expand Down Expand Up @@ -48,3 +50,20 @@ The `--profile test` flag can be used to check unit tests with the
`#[cfg(test)]` attribute.
")
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
let ws = args.workspace(config)?;
let test = match args.value_of("profile") {
Some("test") => true,
None => false,
Some(profile) => {
let err = format_err!("unknown profile: `{}`, only `test` is \
currently supported", profile);
return Err(CliError::new(err, 101));
}
};
let mode = CompileMode::Check { test };
let compile_opts = args.compile_options(config, mode)?;
ops::compile(&ws, &compile_opts)?;
Ok(())
}
14 changes: 14 additions & 0 deletions src/bin/commands/clean.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use command_prelude::*;

use cargo::ops::{self, CleanOptions};

pub fn cli() -> App {
subcommand("clean")
.about("Remove artifacts that cargo has generated in the past")
Expand All @@ -17,3 +19,15 @@ given, then all packages' artifacts are removed. For more information on SPEC
and its format, see the `cargo help pkgid` command.
")
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
let ws = args.workspace(config)?;
let opts = CleanOptions {
config,
spec: values(args, "package"),
target: args.target(),
release: args.is_present("release"),
};
ops::clean(&ws, &opts)?;
Ok(())
}
14 changes: 14 additions & 0 deletions src/bin/commands/doc.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use command_prelude::*;

use cargo::ops::{self, CompileMode, DocOptions};

pub fn cli() -> App {
subcommand("doc")
.about("Build a package's documentation")
Expand Down Expand Up @@ -39,3 +41,15 @@ current package is documented. For more information on SPEC and its format, see
the `cargo help pkgid` command.
")
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
let ws = args.workspace(config)?;
let mode = CompileMode::Doc { deps: !args.is_present("no-deps") };
let compile_opts = args.compile_options(config, mode)?;
let doc_opts = DocOptions {
open_result: args.is_present("open"),
compile_opts,
};
ops::doc(&ws, &doc_opts)?;
Ok(())
}
8 changes: 8 additions & 0 deletions src/bin/commands/fetch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use command_prelude::*;

use cargo::ops;

pub fn cli() -> App {
subcommand("fetch")
.about("Fetch dependencies of a package from the network")
Expand All @@ -15,3 +17,9 @@ If the lockfile is not available, then this is the equivalent of
all updated.
")
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
let ws = args.workspace(config)?;
ops::fetch(&ws)?;
Ok(())
}
8 changes: 8 additions & 0 deletions src/bin/commands/generate_lockfile.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use command_prelude::*;

use cargo::ops;

pub fn cli() -> App {
subcommand("generate-lockfile")
.about("Generate the lockfile for a project")
Expand All @@ -15,3 +17,9 @@ If the lockfile is not available, then this is the equivalent of
all updated.
")
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
let ws = args.workspace(config)?;
ops::generate_lockfile(&ws)?;
Ok(())
}
18 changes: 18 additions & 0 deletions src/bin/commands/git_checkout.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
use command_prelude::*;

use cargo::core::{GitReference, SourceId, Source};
use cargo::sources::GitSource;
use cargo::util::ToUrl;

pub fn cli() -> App {
subcommand("git-checkout")
.about("Checkout a copy of a Git repository")
.arg(Arg::with_name("url").long("url").value_name("URL").required(true))
.arg(Arg::with_name("reference").long("reference").value_name("REF").required(true))
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
let url = args.value_of("url").unwrap().to_url()?;
let reference = args.value_of("reference").unwrap();

let reference = GitReference::Branch(reference.to_string());
let source_id = SourceId::for_git(&url, reference)?;

let mut source = GitSource::new(&source_id, config)?;

source.update()?;

Ok(())
}
9 changes: 9 additions & 0 deletions src/bin/commands/init.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
use command_prelude::*;

use cargo::ops;

pub fn cli() -> App {
subcommand("init")
.about("Create a new cargo package in an existing directory")
.arg(Arg::with_name("path").default_value("."))
.arg_new_opts()
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
let opts = args.new_options()?;
ops::init(&opts, config)?;
config.shell().status("Created", format!("{} project", opts.kind))?;
Ok(())
}
41 changes: 41 additions & 0 deletions src/bin/commands/install.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use command_prelude::*;

use cargo::core::{GitReference, SourceId};
use cargo::ops::{self, CompileMode};
use cargo::util::ToUrl;

pub fn cli() -> App {
subcommand("install")
.about("Create a new cargo package in an existing directory")
Expand Down Expand Up @@ -84,3 +88,40 @@ specified by setting the `CARGO_TARGET_DIR` environment variable to a relative
path. In particular, this can be useful for caching build artifacts on
continuous integration systems.")
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
let mut compile_opts = args.compile_options(config, CompileMode::Build)?;
compile_opts.release = !args.is_present("debug");

let krates = args.values_of("crate").unwrap_or_default().collect::<Vec<_>>();

let source = if let Some(url) = args.value_of("git") {
let url = url.to_url()?;
let gitref = if let Some(branch) = args.value_of("branch") {
GitReference::Branch(branch.to_string())
} else if let Some(tag) = args.value_of("tag") {
GitReference::Tag(tag.to_string())
} else if let Some(rev) = args.value_of("rev") {
GitReference::Rev(rev.to_string())
} else {
GitReference::Branch("master".to_string())
};
SourceId::for_git(&url, gitref)?
} else if let Some(path) = args.value_of("path") {
SourceId::for_path(&config.cwd().join(path))?
} else if krates.is_empty() {
SourceId::for_path(config.cwd())?
} else {
SourceId::crates_io(config)?
};

let version = args.value_of("version");
let root = args.value_of("root");

if args.is_present("list") {
ops::install_list(root, config)?;
} else {
ops::install(root, krates, &source, version, &compile_opts, args.is_present("force"))?;
}
Ok(())
}
22 changes: 22 additions & 0 deletions src/bin/commands/locate_project.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
use command_prelude::*;

use cargo::print_json;

pub fn cli() -> App {
subcommand("locate-project")
.about("Checkout a copy of a Git repository")
.arg_manifest_path()
}

#[derive(Serialize)]
pub struct ProjectLocation {
root: String
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
let root = args.root_manifest(config)?;

let root = root.to_str()
.ok_or_else(|| format_err!("your project path contains characters \
not representable in Unicode"))
.map_err(|e| CliError::new(e, 1))?
.to_string();

let location = ProjectLocation { root };

print_json(&location);
Ok(())
}
41 changes: 41 additions & 0 deletions src/bin/commands/login.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
use command_prelude::*;

use std::io::{self, BufRead};

use cargo::core::{SourceId, Source};
use cargo::sources::RegistrySource;
use cargo::util::{CargoError, CargoResultExt};
use cargo::ops;

pub fn cli() -> App {
subcommand("login")
.about("Save an api token from the registry locally. \
Expand All @@ -8,3 +15,37 @@ pub fn cli() -> App {
.arg(opt("host", "Host to set the token for").value_name("HOST"))
.arg(opt("registry", "Registry to use").value_name("REGISTRY"))
}

pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
let registry = args.registry(config)?;

let token = match args.value_of("token") {
Some(token) => token.to_string(),
None => {
let host = match registry {
Some(ref _registry) => {
return Err(format_err!("token must be provided when \
--registry is provided.").into());
}
None => {
let src = SourceId::crates_io(config)?;
let mut src = RegistrySource::remote(&src, config);
src.update()?;
let config = src.config()?.unwrap();
args.value_of("host").map(|s| s.to_string())
.unwrap_or(config.api.unwrap())
}
};
println!("please visit {}me and paste the API Token below", host);
let mut line = String::new();
let input = io::stdin();
input.lock().read_line(&mut line).chain_err(|| {
"failed to read stdin"
}).map_err(CargoError::from)?;
line.trim().to_string()
}
};

ops::registry_login(config, token, registry)?;
Ok(())
}
Loading

0 comments on commit 6b9c063

Please sign in to comment.