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 19, 2024
1 parent 9a43e94 commit 56686e0
Show file tree
Hide file tree
Showing 4 changed files with 42 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 @@ -40,8 +40,21 @@ 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())
Expand All @@ -50,7 +63,7 @@ pub fn compile_unit(unit: ProcMacroCompilationUnit, ws: &Workspace<'_>) -> Resul
};
let mut cmd: Command = cmd.into();
{
let _ = trace_span!("compile_proc_macro").enter();
let _ = trace_span!("proc_macro").enter();
let status = cmd
.status()
.with_context(|| format!("Failed to execute {:?}", cmd))?;
Expand All @@ -64,9 +77,15 @@ pub fn compile_unit(unit: ProcMacroCompilationUnit, ws: &Workspace<'_>) -> Resul
Ok(())
}

enum CargoAction {
Build,
Check,
}

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

impl From<CargoCommand> for Command {
Expand All @@ -75,7 +94,11 @@ impl From<CargoCommand> for Command {
cmd.stdout(Stdio::inherit());
cmd.stderr(Stdio::inherit());
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
15 changes: 15 additions & 0 deletions scarb/tests/build_cairo_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,21 @@ fn compile_cairo_plugin() {
"#});
}

#[test]
fn check_cairo_plugin() {
let t = TempDir::new().unwrap();
simple_project(&t);
Scarb::quick_snapbox()
.arg("check")
.current_dir(&t)
.assert()
.success()
.stdout_matches(indoc! {r#"
[..] Checking hello v1.0.0 ([..]Scarb.toml)
[..] Finished checking release target(s) in [..]
"#});
}

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

0 comments on commit 56686e0

Please sign in to comment.