From: Aleksey Kladov Date: Sat, 10 Mar 2018 13:16:00 +0000 (+0300) Subject: Move command_prelude to a separate file X-Git-Tag: archive/raspbian/0.35.0-2+rpi1~3^2^2^2^2^2^2^2~22^2~2^2~47^2~16 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=7acd343baed3b629d6076bdbd09294565b0b97a0;p=cargo.git Move command_prelude to a separate file --- diff --git a/src/bin/cargo.rs b/src/bin/cargo.rs index 27c751d3a..b7aa9a412 100644 --- a/src/bin/cargo.rs +++ b/src/bin/cargo.rs @@ -19,6 +19,8 @@ use cargo::util::{self, CliResult, lev_distance, Config, CargoResult}; use cargo::util::{CliError, ProcessError}; mod cli; +mod command_prelude; + fn main() { env_logger::init(); diff --git a/src/bin/cli/bench.rs b/src/bin/cli/bench.rs index 58a73b4ed..df43b7801 100644 --- a/src/bin/cli/bench.rs +++ b/src/bin/cli/bench.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; use clap::AppSettings; pub fn cli() -> App { diff --git a/src/bin/cli/build.rs b/src/bin/cli/build.rs index 2d1bdd921..9eddd651b 100644 --- a/src/bin/cli/build.rs +++ b/src/bin/cli/build.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("build").alias("b") diff --git a/src/bin/cli/check.rs b/src/bin/cli/check.rs index 2777f2317..ecf7fe6ce 100644 --- a/src/bin/cli/check.rs +++ b/src/bin/cli/check.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("check") diff --git a/src/bin/cli/clean.rs b/src/bin/cli/clean.rs index e23eac7ad..7852c3c5c 100644 --- a/src/bin/cli/clean.rs +++ b/src/bin/cli/clean.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("clean") diff --git a/src/bin/cli/doc.rs b/src/bin/cli/doc.rs index ddf76081a..bcb709b29 100644 --- a/src/bin/cli/doc.rs +++ b/src/bin/cli/doc.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("doc") diff --git a/src/bin/cli/fetch.rs b/src/bin/cli/fetch.rs index 39b08ab93..bb4a946ab 100644 --- a/src/bin/cli/fetch.rs +++ b/src/bin/cli/fetch.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("fetch") diff --git a/src/bin/cli/generate_lockfile.rs b/src/bin/cli/generate_lockfile.rs index 447aa023e..56ab283f4 100644 --- a/src/bin/cli/generate_lockfile.rs +++ b/src/bin/cli/generate_lockfile.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("generate-lockfile") diff --git a/src/bin/cli/git_checkout.rs b/src/bin/cli/git_checkout.rs index 90719066e..4bbcba133 100644 --- a/src/bin/cli/git_checkout.rs +++ b/src/bin/cli/git_checkout.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("git-checkout") diff --git a/src/bin/cli/init.rs b/src/bin/cli/init.rs index a7038e0e0..e9d618b43 100644 --- a/src/bin/cli/init.rs +++ b/src/bin/cli/init.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("init") diff --git a/src/bin/cli/install.rs b/src/bin/cli/install.rs index 237e819ca..ce1834d9d 100644 --- a/src/bin/cli/install.rs +++ b/src/bin/cli/install.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("install") diff --git a/src/bin/cli/locate_project.rs b/src/bin/cli/locate_project.rs index ef18d30a6..1bddac4c2 100644 --- a/src/bin/cli/locate_project.rs +++ b/src/bin/cli/locate_project.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("locate-project") diff --git a/src/bin/cli/login.rs b/src/bin/cli/login.rs index c5d77b373..045770181 100644 --- a/src/bin/cli/login.rs +++ b/src/bin/cli/login.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("login") diff --git a/src/bin/cli/metadata.rs b/src/bin/cli/metadata.rs index 7b407a9f2..7f4904c14 100644 --- a/src/bin/cli/metadata.rs +++ b/src/bin/cli/metadata.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("metadata") diff --git a/src/bin/cli/mod.rs b/src/bin/cli/mod.rs index 78cf34184..8fb63d35f 100644 --- a/src/bin/cli/mod.rs +++ b/src/bin/cli/mod.rs @@ -17,12 +17,13 @@ use cargo::util::{ToUrl, CargoResultExt}; use cargo::ops::{self, CompileMode, OutputMetadataOptions}; use cargo::sources::{GitSource, RegistrySource}; -use self::command_prelude::*; use std::collections::BTreeSet; use std::env; use std::fs; + use search_directories; use is_executable; +use command_prelude::*; pub fn do_main(config: &mut Config) -> CliResult { let args = cli().get_matches_safe()?; @@ -727,329 +728,3 @@ mod update; mod verify_project; mod version; mod yank; - -mod command_prelude { - use std::path::PathBuf; - - use clap::{self, SubCommand, AppSettings, ArgMatches}; - use cargo::{Config, 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 type App = clap::App<'static, 'static>; - - pub trait CommonArgs: Sized { - fn _arg(self, arg: Arg<'static, 'static>) -> Self; - - fn arg_package(self, package: &'static str, all: &'static str, exclude: &'static str) -> Self { - self._arg(opt("package", package).short("p").value_name("SPEC").multiple(true)) - ._arg(opt("all", all)) - ._arg(opt("exclude", exclude).value_name("SPEC").multiple(true)) - } - - fn arg_single_package(self, package: &'static str) -> Self { - self._arg(opt("package", package).short("p").value_name("SPEC")) - } - - fn arg_jobs(self) -> Self { - self._arg( - opt("jobs", "Number of parallel jobs, defaults to # of CPUs") - .short("j").value_name("N") - ) - } - - fn arg_targets_all( - self, - lib: &'static str, - bin: &'static str, - bins: &'static str, - examle: &'static str, - examles: &'static str, - test: &'static str, - tests: &'static str, - bench: &'static str, - benchs: &'static str, - all: &'static str, - ) -> Self { - self.arg_targets_lib_bin(lib, bin, bins) - ._arg(opt("example", examle).value_name("NAME").multiple(true)) - ._arg(opt("examples", examles)) - ._arg(opt("test", test).value_name("NAME").multiple(true)) - ._arg(opt("tests", tests)) - ._arg(opt("bench", bench).value_name("NAME").multiple(true)) - ._arg(opt("benches", benchs)) - ._arg(opt("all-targets", all)) - } - - fn arg_targets_lib_bin( - self, - lib: &'static str, - bin: &'static str, - bins: &'static str, - ) -> Self { - self._arg(opt("lib", lib)) - ._arg(opt("bin", bin).value_name("NAME").multiple(true)) - ._arg(opt("bins", bins)) - } - - fn arg_targets_bins_examples( - self, - bin: &'static str, - bins: &'static str, - example: &'static str, - examples: &'static str, - ) -> Self { - self._arg(opt("bin", bin).value_name("NAME").multiple(true)) - ._arg(opt("bins", bins)) - ._arg(opt("example", example).value_name("NAME").multiple(true)) - ._arg(opt("examples", examples)) - } - - fn arg_targets_bin_example( - self, - bin: &'static str, - example: &'static str, - ) -> Self { - self._arg(opt("bin", bin).value_name("NAME").multiple(true)) - ._arg(opt("example", example).value_name("NAME").multiple(true)) - } - - fn arg_features(self) -> Self { - self - ._arg( - opt("features", "Space-separated list of features to activate") - .value_name("FEATURES") - ) - ._arg(opt("all-features", "Activate all available features")) - ._arg(opt("no-default-features", "Do not activate the `default` feature")) - } - - fn arg_release(self, release: &'static str) -> Self { - self._arg(opt("release", release)) - } - - fn arg_target_triple(self, target: &'static str) -> Self { - self._arg(opt("target", target).value_name("TRIPLE")) - } - - fn arg_manifest_path(self) -> Self { - self._arg(opt("manifest-path", "Path to Cargo.toml").value_name("PATH")) - } - - fn arg_message_format(self) -> Self { - self._arg( - opt("message-format", "Error format") - .value_name("FMT") - .case_insensitive(true) - .possible_values(&["human", "json"]).default_value("human") - ) - } - - fn arg_new_opts(self) -> Self { - self._arg( - opt("vcs", "\ -Initialize a new repository for the given version \ -control system (git, hg, pijul, or fossil) or do not \ -initialize any version control at all (none), overriding \ -a global configuration.") - .value_name("VCS") - .possible_values(&["git", "hg", "pijul", "fossil", "none"]) - ) - ._arg(opt("bin", "Use a binary (application) template [default]")) - ._arg(opt("lib", "Use a library template")) - ._arg( - opt("name", "Set the resulting package name, defaults to the directory name") - .value_name("NAME") - ) - } - - fn arg_index(self) -> Self { - self - ._arg( - opt("index", "Registry index to upload the package to") - .value_name("INDEX") - ) - ._arg( - opt("host", "DEPRECATED, renamed to '--index'") - .value_name("HOST") - .hidden(true) - ) - } - } - - impl CommonArgs for App { - fn _arg(self, arg: Arg<'static, 'static>) -> Self { - self.arg(arg) - } - } - - pub fn opt(name: &'static str, help: &'static str) -> Arg<'static, 'static> { - Arg::with_name(name).long(name).help(help) - } - - pub fn subcommand(name: &'static str) -> App { - SubCommand::with_name(name) - .settings(&[ - AppSettings::UnifiedHelpMessage, - AppSettings::DeriveDisplayOrder, - AppSettings::DontCollapseArgsInUsage, - ]) - } - - pub fn values(args: &ArgMatches, name: &str) -> Vec { - args.values_of(name).unwrap_or_default() - .map(|s| s.to_string()) - .collect() - } - - - pub fn config_from_args(config: &mut Config, args: &ArgMatches) -> CargoResult<()> { - let color = args.value_of("color").map(|s| s.to_string()); - config.configure( - args.occurrences_of("verbose") as u32, - if args.is_present("quite") { Some(true) } else { None }, - &color, - args.is_present("frozen"), - args.is_present("locked"), - &args.values_of_lossy("unstable-features").unwrap_or_default(), - ) - } - - pub fn root_manifest_from_args(config: &Config, args: &ArgMatches) -> CargoResult { - let manifest_path = args.value_of("manifest-path").map(|s| s.to_string()); - find_root_manifest_for_wd(manifest_path, config.cwd()) - } - - pub fn workspace_from_args<'a>(config: &'a Config, args: &ArgMatches) -> CargoResult> { - let root = root_manifest_from_args(config, args)?; - Workspace::new(&root, config) - } - - pub fn jobs_from_args(args: &ArgMatches) -> CargoResult> { //FIXME: validation - let jobs = match args.value_of("jobs") { - None => None, - Some(jobs) => Some(jobs.parse::().map_err(|_| { - clap::Error::value_validation_auto( - format!("could not parse `{}` as a number", jobs) - ) - })?) - }; - Ok(jobs) - } - - pub fn compile_options_from_args<'a>( - config: &'a Config, - args: &'a ArgMatches<'a>, - mode: CompileMode, - ) -> CargoResult> { - let spec = Packages::from_flags( - args.is_present("all"), - values(args, "exclude"), - values(args, "package"), - )?; - - let message_format = match args.value_of("message-format") { - None => MessageFormat::Human, - Some(f) => { - if f.eq_ignore_ascii_case("json") { - MessageFormat::Json - } else if f.eq_ignore_ascii_case("human") { - MessageFormat::Human - } else { - panic!("Impossible message format: {:?}", f) - } - } - }; - - let opts = CompileOptions { - config, - jobs: jobs_from_args(args)?, - target: args.value_of("target"), - features: values(args, "features"), - all_features: args.is_present("all-features"), - no_default_features: args.is_present("no-default-features"), - spec, - mode, - release: args.is_present("release"), - filter: CompileFilter::new(args.is_present("lib"), - values(args, "bin"), args.is_present("bins"), - values(args, "test"), args.is_present("tests"), - values(args, "example"), args.is_present("examples"), - values(args, "bench"), args.is_present("benches"), - args.is_present("all-targets")), - message_format, - target_rustdoc_args: None, - target_rustc_args: None, - }; - Ok(opts) - } - - pub fn compile_options_from_args_for_single_package<'a>( - config: &'a Config, - args: &'a ArgMatches<'a>, - mode: CompileMode, - ) -> CargoResult> { - let mut compile_opts = compile_options_from_args(config, args, mode)?; - compile_opts.spec = Packages::Packages(values(args, "package")); - Ok(compile_opts) - } - - pub fn new_opts_from_args<'a>(args: &'a ArgMatches<'a>, path: &'a str) -> CargoResult> { - let vcs = args.value_of("vcs").map(|vcs| match vcs { - "git" => VersionControl::Git, - "hg" => VersionControl::Hg, - "pijul" => VersionControl::Pijul, - "fossil" => VersionControl::Fossil, - "none" => VersionControl::NoVcs, - vcs => panic!("Impossible vcs: {:?}", vcs), - }); - NewOptions::new(vcs, - args.is_present("bin"), - args.is_present("lib"), - path, - args.value_of("name")) - } - - pub fn registry_from_args(config: &Config, args: &ArgMatches) -> CargoResult> { - match args.value_of("registry") { - Some(registry) => { - if !config.cli_unstable().unstable_options { - return Err(format_err!("registry option is an unstable feature and \ - requires -Zunstable-options to use.").into()); - } - Ok(Some(registry.to_string())) - } - None => Ok(None), - } - } - - pub fn index_from_args(config: &Config, args: &ArgMatches) -> CargoResult> { - // TODO: Deprecated - // remove once it has been decided --host can be removed - // We may instead want to repurpose the host flag, as - // mentioned in this issue - // https://github.com/rust-lang/cargo/issues/4208 - let msg = "The flag '--host' is no longer valid. - -Previous versions of Cargo accepted this flag, but it is being -deprecated. The flag is being renamed to 'index', as the flag -wants the location of the index. Please use '--index' instead. - -This will soon become a hard error, so it's either recommended -to update to a fixed version or contact the upstream maintainer -about this warning."; - - let index = match args.value_of("host") { - Some(host) => { - config.shell().warn(&msg)?; - Some(host.to_string()) - } - None => args.value_of("index").map(|s| s.to_string()) - }; - Ok(index) - } -} diff --git a/src/bin/cli/new.rs b/src/bin/cli/new.rs index f912c9581..eb03e37d5 100644 --- a/src/bin/cli/new.rs +++ b/src/bin/cli/new.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("new") diff --git a/src/bin/cli/owner.rs b/src/bin/cli/owner.rs index 4f1b87003..dd7c5adf1 100644 --- a/src/bin/cli/owner.rs +++ b/src/bin/cli/owner.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("owner") diff --git a/src/bin/cli/package.rs b/src/bin/cli/package.rs index e8215c77c..59ba384b7 100644 --- a/src/bin/cli/package.rs +++ b/src/bin/cli/package.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("package") diff --git a/src/bin/cli/pkgid.rs b/src/bin/cli/pkgid.rs index 764a5cb4b..26335735a 100644 --- a/src/bin/cli/pkgid.rs +++ b/src/bin/cli/pkgid.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("pkgid") diff --git a/src/bin/cli/publish.rs b/src/bin/cli/publish.rs index 0b0d08546..fc4ead218 100644 --- a/src/bin/cli/publish.rs +++ b/src/bin/cli/publish.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("publish") diff --git a/src/bin/cli/read_manifest.rs b/src/bin/cli/read_manifest.rs index c13e7e180..ec6ffec83 100644 --- a/src/bin/cli/read_manifest.rs +++ b/src/bin/cli/read_manifest.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("read-manifest") diff --git a/src/bin/cli/run.rs b/src/bin/cli/run.rs index 85ad63dbd..dae6c9de2 100644 --- a/src/bin/cli/run.rs +++ b/src/bin/cli/run.rs @@ -1,6 +1,6 @@ use clap::AppSettings; -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("run").alias("r") diff --git a/src/bin/cli/rustc.rs b/src/bin/cli/rustc.rs index bdc5aa185..8074a551e 100644 --- a/src/bin/cli/rustc.rs +++ b/src/bin/cli/rustc.rs @@ -1,6 +1,6 @@ use clap::AppSettings; -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("rustc") diff --git a/src/bin/cli/rustdoc.rs b/src/bin/cli/rustdoc.rs index 9f8d164bf..5db6ca9dd 100644 --- a/src/bin/cli/rustdoc.rs +++ b/src/bin/cli/rustdoc.rs @@ -1,6 +1,6 @@ use clap::AppSettings; -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("rustdoc") diff --git a/src/bin/cli/search.rs b/src/bin/cli/search.rs index 9e9f727f1..62d023b0f 100644 --- a/src/bin/cli/search.rs +++ b/src/bin/cli/search.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("search") diff --git a/src/bin/cli/test.rs b/src/bin/cli/test.rs index d4ae77fa9..9c0ae57b7 100644 --- a/src/bin/cli/test.rs +++ b/src/bin/cli/test.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; use clap::AppSettings; pub fn cli() -> App { diff --git a/src/bin/cli/uninstall.rs b/src/bin/cli/uninstall.rs index 0ad50d3f5..5ab7513e5 100644 --- a/src/bin/cli/uninstall.rs +++ b/src/bin/cli/uninstall.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("uninstall") diff --git a/src/bin/cli/update.rs b/src/bin/cli/update.rs index 2b2e82ed5..523051c25 100644 --- a/src/bin/cli/update.rs +++ b/src/bin/cli/update.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("update") diff --git a/src/bin/cli/verify_project.rs b/src/bin/cli/verify_project.rs index 890de1154..926d8014c 100644 --- a/src/bin/cli/verify_project.rs +++ b/src/bin/cli/verify_project.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("verify-project") diff --git a/src/bin/cli/version.rs b/src/bin/cli/version.rs index 75c2a9f06..0d7de9a6c 100644 --- a/src/bin/cli/version.rs +++ b/src/bin/cli/version.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("version") diff --git a/src/bin/cli/yank.rs b/src/bin/cli/yank.rs index 41a418554..54039a3b1 100644 --- a/src/bin/cli/yank.rs +++ b/src/bin/cli/yank.rs @@ -1,4 +1,4 @@ -use super::command_prelude::*; +use command_prelude::*; pub fn cli() -> App { subcommand("yank")