Skip to content

Commit

Permalink
Implement check command for cairo plugins
Browse files Browse the repository at this point in the history
commit-id:1299f0be
  • Loading branch information
maciektr committed Feb 21, 2024
1 parent da0b48b commit 051be9b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
27 changes: 25 additions & 2 deletions scarb/src/compiler/plugin/proc_macro/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,54 @@ impl SharedLibraryProvider for Package {
}

pub fn compile_unit(unit: ProcMacroCompilationUnit, ws: &Workspace<'_>) -> Result<()> {
run_cargo(CargoAction::Build, unit, ws)
}

pub fn check_unit(unit: ProcMacroCompilationUnit, ws: &Workspace<'_>) -> Result<()> {
run_cargo(CargoAction::Check, unit, ws)
}

fn run_cargo(
action: CargoAction,
unit: ProcMacroCompilationUnit,
ws: &Workspace<'_>,
) -> Result<()> {
let main_package = unit.components.first().unwrap().package.clone();
let cmd = CargoCommand {
action,
current_dir: main_package.root().to_path_buf(),
target_dir: main_package
.target_path(ws.config())
.path_unchecked()
.to_path_buf(),
};
{
let _ = trace_span!("compile_proc_macro").enter();
let _ = trace_span!("proc_macro").enter();
exec(&mut cmd.into(), ws.config())?;
}
Ok(())
}

enum CargoAction {
Build,
Check,
}

struct CargoCommand {
current_dir: Utf8PathBuf,
target_dir: Utf8PathBuf,
action: CargoAction,
}

impl From<CargoCommand> for Command {
fn from(args: CargoCommand) -> Self {
let mut cmd = Command::new("cargo");
cmd.current_dir(args.current_dir);
cmd.args(["build", "--release"]);
match args.action {
CargoAction::Build => cmd.arg("build"),
CargoAction::Check => cmd.arg("check"),
};
cmd.arg("--release");
cmd.arg("--target-dir");
cmd.arg(args.target_dir);
cmd
Expand Down
2 changes: 1 addition & 1 deletion scarb/src/compiler/plugin/proc_macro/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ pub mod compilation;
mod ffi;
mod host;

pub use compilation::compile_unit;
pub use compilation::{check_unit, compile_unit};
pub use ffi::*;
pub use host::*;
2 changes: 1 addition & 1 deletion scarb/src/ops/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn check_unit(unit: CompilationUnit, ws: &Workspace<'_>) -> Result<()> {
.print(Status::new("Checking", &unit.name()));

match unit {
CompilationUnit::ProcMacro(_unit) => (),
CompilationUnit::ProcMacro(unit) => proc_macro::check_unit(unit, ws)?,
CompilationUnit::Cairo(unit) => {
let db = build_scarb_root_database(&unit, ws)?;

Expand Down
27 changes: 27 additions & 0 deletions scarb/tests/build_cairo_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,33 @@ fn compile_cairo_plugin() {
);
}

#[test]
fn check_cairo_plugin() {
let t = TempDir::new().unwrap();
simple_project(&t);
let output = Scarb::quick_snapbox()
.arg("check")
.current_dir(&t)
.output()
.unwrap();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
assert!(stdout.contains("Checking hello v1.0.0"));
let lines = stdout.lines().map(ToString::to_string).collect::<Vec<_>>();
let (last, lines) = lines.split_last().unwrap();
assert_matches(r#"[..] Finished checking release target(s) in [..]"#, last);
let (last, _lines) = lines.split_last().unwrap();
// Line from Cargo output
assert_matches(
r#"[..] Finished release [optimized] target(s) in [..]"#,
last,
);
}

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

0 comments on commit 051be9b

Please sign in to comment.