Skip to content

Commit

Permalink
Moved checks into compile_options(), changed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DamianX committed Jan 3, 2019
1 parent f1d6a94 commit a51759c
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 197 deletions.
4 changes: 1 addition & 3 deletions src/bin/cargo/commands/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ 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)?;

args.check_optional_opts_all(&ws, &compile_opts)?;
let mut compile_opts = args.compile_options(config, CompileMode::Bench, Some(&ws))?;

compile_opts.build_config.release = true;

Expand Down
4 changes: 1 addition & 3 deletions src/bin/cargo/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ the --release flag will use the `release` profile instead.

pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let ws = args.workspace(config)?;
let mut compile_opts = args.compile_options(config, CompileMode::Build)?;

args.check_optional_opts_all(&ws, &compile_opts)?;
let mut compile_opts = args.compile_options(config, CompileMode::Build, Some(&ws))?;

compile_opts.export_dir = args.value_of_path("out-dir", config);
if compile_opts.export_dir.is_some() && !config.cli_unstable().unstable_options {
Expand Down
4 changes: 1 addition & 3 deletions src/bin/cargo/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
}
};
let mode = CompileMode::Check { test };
let compile_opts = args.compile_options(config, mode)?;

args.check_optional_opts_all(&ws, &compile_opts)?;
let compile_opts = args.compile_options(config, mode, Some(&ws))?;

ops::compile(&ws, &compile_opts)?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let mode = CompileMode::Doc {
deps: !args.is_present("no-deps"),
};
let mut compile_opts = args.compile_options(config, mode)?;
let mut compile_opts = args.compile_options(config, mode, Some(&ws))?;
compile_opts.local_rustdoc_args = if args.is_present("document-private-items") {
Some(vec!["--document-private-items".to_string()])
} else {
Expand Down
4 changes: 1 addition & 3 deletions src/bin/cargo/commands/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {

// Unlike other commands default `cargo fix` to all targets to fix as much
// code as we can.
let mut opts = args.compile_options(config, mode)?;

args.check_optional_opts_all(&ws, &opts)?;
let mut opts = args.compile_options(config, mode, Some(&ws))?;

if let CompileFilter::Default { .. } = opts.filter {
opts.filter = CompileFilter::Only {
Expand Down
8 changes: 3 additions & 5 deletions src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,9 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let registry = args.registry(config)?;

config.reload_rooted_at_cargo_home()?;
let mut compile_opts = args.compile_options(config, CompileMode::Build)?;

if let Ok(ws) = args.workspace(config) {
args.check_optional_opts_example_and_bin(&ws, &compile_opts)?;
}

let workspace = args.workspace(config).ok();
let mut compile_opts = args.compile_options(config, CompileMode::Build, workspace.as_ref())?;

compile_opts.build_config.release = !args.is_present("debug");

Expand Down
4 changes: 1 addition & 3 deletions src/bin/cargo/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ run. If you're passing arguments to both Cargo and the binary, the ones after
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let ws = args.workspace(config)?;

let mut compile_opts = args.compile_options(config, CompileMode::Build)?;

args.check_optional_opts_example_and_bin(&ws, &compile_opts)?;
let mut compile_opts = args.compile_options(config, CompileMode::Build, Some(&ws))?;

if !args.is_present("example") && !args.is_present("bin") {
let default_runs: Vec<_> = compile_opts
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
return Err(CliError::new(err, 101));
}
};
let mut compile_opts = args.compile_options_for_single_package(config, mode)?;
let mut compile_opts = args.compile_options_for_single_package(config, mode, Some(&ws))?;
let target_args = values(args, "args");
compile_opts.target_rustc_args = if target_args.is_empty() {
None
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ the `cargo help pkgid` command.
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let ws = args.workspace(config)?;
let mut compile_opts =
args.compile_options_for_single_package(config, CompileMode::Doc { deps: false })?;
args.compile_options_for_single_package(config, CompileMode::Doc { deps: false }, Some(&ws))?;
let target_args = values(args, "args");
compile_opts.target_rustdoc_args = if target_args.is_empty() {
None
Expand Down
4 changes: 1 addition & 3 deletions src/bin/cargo/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ To get the list of all options available for the test binaries use this:
pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let ws = args.workspace(config)?;

let mut compile_opts = args.compile_options(config, CompileMode::Test)?;

args.check_optional_opts_all(&ws, &compile_opts)?;
let mut compile_opts = args.compile_options(config, CompileMode::Test, Some(&ws))?;

let doc = args.is_present("doc");
if doc {
Expand Down
29 changes: 10 additions & 19 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ pub trait ArgMatchesExt {
&self,
config: &'a Config,
mode: CompileMode,
workspace: Option<&Workspace<'a>>,
) -> CargoResult<CompileOptions<'a>> {
let spec = Packages::from_flags(
self._is_present("all"),
Expand Down Expand Up @@ -344,15 +345,21 @@ pub trait ArgMatchesExt {
local_rustdoc_args: None,
export_dir: None,
};

if let Some(ws) = workspace {
self.check_optional_opts(ws, &opts)?;
}

Ok(opts)
}

fn compile_options_for_single_package<'a>(
&self,
config: &'a Config,
mode: CompileMode,
workspace: Option<&Workspace<'a>>,
) -> CargoResult<CompileOptions<'a>> {
let mut compile_opts = self.compile_options(config, mode)?;
let mut compile_opts = self.compile_options(config, mode, workspace)?;
compile_opts.spec = Packages::Packages(self._values_of("package"));
Ok(compile_opts)
}
Expand Down Expand Up @@ -429,27 +436,11 @@ about this warning.";
Ok(index)
}

fn check_optional_opts_example_and_bin(
&self,
workspace: &Workspace<'_>,
compile_opts: &CompileOptions<'_>,
) -> CliResult {
if self.is_present_with_zero_values("example") {
print_available_examples(&workspace, &compile_opts)?;
}

if self.is_present_with_zero_values("bin") {
print_available_binaries(&workspace, &compile_opts)?;
}

Ok(())
}

fn check_optional_opts_all(
fn check_optional_opts(
&self,
workspace: &Workspace<'_>,
compile_opts: &CompileOptions<'_>,
) -> CliResult {
) -> CargoResult<()> {
if self.is_present_with_zero_values("example") {
print_available_examples(&workspace, &compile_opts)?;
}
Expand Down
Loading

0 comments on commit a51759c

Please sign in to comment.