Skip to content

Commit

Permalink
Use json output format in cargo if set in scarb_ui
Browse files Browse the repository at this point in the history
commit-id:fd15f5ab
  • Loading branch information
maciektr committed Feb 21, 2024
1 parent 128870b commit a2a7cd1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
30 changes: 30 additions & 0 deletions scarb/src/compiler/plugin/proc_macro/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::process::exec_piping;
use anyhow::Result;
use camino::Utf8PathBuf;
use libloading::library_filename;
use scarb_ui::OutputFormat;
use std::fmt::Display;
use std::process::Command;
use tracing::trace_span;

Expand Down Expand Up @@ -62,6 +64,7 @@ fn run_cargo(action: CargoAction, package: &Package, ws: &Workspace<'_>) -> Resu
let cmd = CargoCommand {
action,
current_dir: package.root().to_path_buf(),
output_format: ws.config().ui().output_format(),
target_dir: package
.target_path(ws.config())
.path_unchecked()
Expand All @@ -83,9 +86,33 @@ enum CargoAction {
struct CargoCommand {
current_dir: Utf8PathBuf,
target_dir: Utf8PathBuf,
output_format: OutputFormat,
action: CargoAction,
}

enum CargoOutputFormat {
Human,
Json,
}

impl Display for CargoOutputFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CargoOutputFormat::Human => write!(f, "human"),
CargoOutputFormat::Json => write!(f, "json"),
}
}
}

impl From<OutputFormat> for CargoOutputFormat {
fn from(format: OutputFormat) -> Self {
match format {
OutputFormat::Text => CargoOutputFormat::Human,
OutputFormat::Json => CargoOutputFormat::Json,
}
}
}

impl From<CargoCommand> for Command {
fn from(args: CargoCommand) -> Self {
let mut cmd = Command::new("cargo");
Expand All @@ -99,6 +126,9 @@ impl From<CargoCommand> for Command {
CargoAction::Fetch => (),
_ => {
cmd.arg("--release");
cmd.arg("--message-format");
let output_format: CargoOutputFormat = args.output_format.into();
cmd.arg(output_format.to_string());
cmd.arg("--target-dir");
cmd.arg(args.target_dir);
}
Expand Down
28 changes: 28 additions & 0 deletions scarb/tests/build_cairo_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,34 @@ fn resolve_fetched_plugins() {
assert!(t.child("Cargo.lock").exists())
}

#[test]
fn can_use_json_output() {
let t = TempDir::new().unwrap();
simple_project(&t);
let output = Scarb::quick_snapbox()
.arg("--json")
.arg("check")
.current_dir(&t)
.output()
.unwrap();
assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
let lines = stdout.lines().map(ToString::to_string).collect::<Vec<_>>();
let (first, lines) = lines.split_first().unwrap();
assert_matches(
r#"{"status":"checking","message":"hello v1.0.0 ([..]Scarb.toml)"}"#,
first,
);
let (last, lines) = lines.split_last().unwrap();
assert_matches(
r#"{"status":"finished","message":"checking release target(s) in [..]"}"#,
last,
);
// Line from Cargo.
let (last, _lines) = lines.split_last().unwrap();
assert_matches(r#"{"reason":"build-finished","success":true}"#, last);
}

#[test]
fn compile_cairo_plugin_with_lib_target() {
let t = TempDir::new().unwrap();
Expand Down

0 comments on commit a2a7cd1

Please sign in to comment.