Skip to content

Commit

Permalink
feat: Add Conan command verbosity control option
Browse files Browse the repository at this point in the history
Add a new `conan install` command builder method `verbosity()`
and a public enumeration defining the verbosity levels.

Resolves #4
  • Loading branch information
ravenexp committed Aug 28, 2024
1 parent 406e53b commit 6eeab71
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 8 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ can also be found in the project repository.

## Advanced usage

Using custom Conan profiles with names derived from the Cargo target information:
Using custom Conan profiles with names derived from the Cargo target information
and a reduced output verbosity level:

```rust
use conan2::ConanInstall;
use conan2::{ConanInstall, ConanVerbosity};

fn main() {
let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
Expand All @@ -64,6 +65,7 @@ fn main() {
ConanInstall::new()
.profile(&conan_profile)
.build("missing")
.verbosity(ConanVerbosity::Error) // Silence Conan warnings
.run()
.parse()
.emit();
Expand Down
9 changes: 7 additions & 2 deletions example-build-script/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use conan2::ConanInstall;
use conan2::{ConanInstall, ConanVerbosity};

fn main() {
ConanInstall::new().build("missing").run().parse().emit();
ConanInstall::new()
.build("missing")
.verbosity(ConanVerbosity::Error) // Silence Conan warnings
.run()
.parse()
.emit();
}
61 changes: 58 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@
//!
//! ## Advanced usage
//!
//! Using custom Conan profiles with names derived from the Cargo target information:
//! Using custom Conan profiles with names derived from the Cargo target information
//! and a reduced output verbosity level:
//!
//! ```no_run
//! use conan2::ConanInstall;
//! use conan2::{ConanInstall, ConanVerbosity};
//!
//! let target_os = std::env::var("CARGO_CFG_TARGET_OS").unwrap();
//! let target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap();
Expand All @@ -58,6 +59,7 @@
//! ConanInstall::new()
//! .profile(&conan_profile)
//! .build("missing")
//! .verbosity(ConanVerbosity::Error) // Silence Conan warnings
//! .run()
//! .parse()
//! .emit();
Expand Down Expand Up @@ -95,6 +97,34 @@ const CONAN_ENV: &str = "CONAN";
/// Default Conan binary name
const DEFAULT_CONAN: &str = "conan";

/// `conan` command verbosity level
///
/// Defines the level of detail of the Conan command output.
///
/// Enum variants correspond to the following options:
/// `-vquiet`, `-verror`, `-vwarning`, `-vnotice`, `-vstatus`,
/// `-v` or `-vverbose`, `-vv` or `-vdebug`, `-vvv` or `-vtrace`.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum ConanVerbosity {
/// `-vquiet`
Quiet,
/// `-verror`
Error,
/// `-vwarning`
#[default]
Warning,
/// `-vnotice`
Notice,
/// `-vstatus`
Status,
/// `-vverbose`
Verbose,
/// `-vdebug`
Debug,
/// `-vtrace`
Trace,
}

/// `conan install` command builder
///
/// This opaque type implements a command line builder for
Expand All @@ -109,6 +139,8 @@ pub struct ConanInstall {
profile: Option<String>,
/// Conan build policy
build: Option<String>,
/// Conan output verbosity level
verbosity: ConanVerbosity,
}

/// `conan install` command output data
Expand All @@ -125,6 +157,21 @@ pub struct CargoInstructions {
/// Conan dependency graph as a JSON-based tree structure
struct ConanDependencyGraph(Value);

impl std::fmt::Display for ConanVerbosity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ConanVerbosity::Quiet => f.write_str("quiet"),
ConanVerbosity::Error => f.write_str("error"),
ConanVerbosity::Warning => f.write_str("warning"),
ConanVerbosity::Notice => f.write_str("notice"),
ConanVerbosity::Status => f.write_str("status"),
ConanVerbosity::Verbose => f.write_str("verbose"),
ConanVerbosity::Debug => f.write_str("debug"),
ConanVerbosity::Trace => f.write_str("trace"),
}
}
}

impl ConanInstall {
/// Creates a new `conan install` command with the default recipe path (`.`).
#[must_use]
Expand Down Expand Up @@ -170,6 +217,14 @@ impl ConanInstall {
self
}

/// Sets the Conan command verbosity level.
///
/// Matches `-v` Conan executable option.
pub fn verbosity(&mut self, verbosity: ConanVerbosity) -> &mut ConanInstall {
self.verbosity = verbosity;
self
}

/// Runs the `conan install` command and captures its JSON-formatted output.
///
/// # Panics
Expand All @@ -191,7 +246,7 @@ impl ConanInstall {
command
.arg("install")
.arg(recipe)
.arg("-vwarning")
.arg(format!("-v{}", self.verbosity))
.arg("--format")
.arg("json")
.arg("--output-folder")
Expand Down
5 changes: 4 additions & 1 deletion tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
use std::{io::Write, path::Path};

use conan2::ConanInstall;
use conan2::{ConanInstall, ConanVerbosity};

#[test]
fn run_conan_install() {
let output = ConanInstall::with_recipe(Path::new("tests/conanfile.txt"))
.output_folder(Path::new(env!("CARGO_TARGET_TMPDIR")))
.verbosity(ConanVerbosity::Verbose)
.build("missing")
.run();

Expand All @@ -31,6 +32,7 @@ fn run_conan_install() {
fn fail_no_conanfile() {
let output = ConanInstall::new()
.output_folder(Path::new(env!("CARGO_TARGET_TMPDIR")))
.verbosity(ConanVerbosity::Status)
.run();

std::io::stderr().write_all(output.stderr()).unwrap();
Expand All @@ -48,6 +50,7 @@ fn fail_no_profile() {
let output = ConanInstall::with_recipe(Path::new("tests/conanfile.txt"))
.output_folder(Path::new(env!("CARGO_TARGET_TMPDIR")))
.profile("no-such-profile")
.verbosity(ConanVerbosity::Debug)
.run();

std::io::stderr().write_all(output.stderr()).unwrap();
Expand Down

0 comments on commit 6eeab71

Please sign in to comment.