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 16, 2024
1 parent 80e60c5 commit 1153df8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
23 changes: 21 additions & 2 deletions scarb/src/compiler/plugin/proc_macro/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,18 @@ impl SharedLibraryProvider for Package {
}

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

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

fn run_cargo(action: CargoAction, unit: CompilationUnit, ws: &Workspace<'_>) -> Result<()> {
let main_package = unit.components.first().unwrap().package.clone();

let mut cmd = CargoCommand::builder()
.action(action)
.current_dir(main_package.root().to_path_buf())
.target_dir(
main_package
Expand All @@ -47,7 +56,7 @@ pub fn compile_unit(unit: CompilationUnit, ws: &Workspace<'_>) -> Result<()> {
.build();

{
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 @@ -62,11 +71,17 @@ pub fn compile_unit(unit: CompilationUnit, ws: &Workspace<'_>) -> Result<()> {
Ok(())
}

enum CargoAction {
Build,
Check,
}

#[derive(TypedBuilder)]
#[builder(build_method(into = Command))]
struct CargoCommand {
current_dir: Utf8PathBuf,
target_dir: Utf8PathBuf,
action: CargoAction,
}

impl From<CargoCommand> for Command {
Expand All @@ -75,7 +90,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::*;
30 changes: 17 additions & 13 deletions scarb/src/ops/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,23 +125,27 @@ fn check_unit(unit: CompilationUnit, ws: &Workspace<'_>) -> Result<()> {
.ui()
.print(Status::new("Checking", &unit.name()));

let db = build_scarb_root_database(&unit, ws)?;
if unit.is_cairo_plugin() {
proc_macro::check_unit(unit, ws)?;
} else {
let db = build_scarb_root_database(&unit, ws)?;

check_starknet_dependency(&unit, ws, &db, &package_name);
check_starknet_dependency(&unit, ws, &db, &package_name);

let mut compiler_config = build_compiler_config(&unit, ws);
let mut compiler_config = build_compiler_config(&unit, ws);

compiler_config
.diagnostics_reporter
.ensure(&db)
.map_err(|err| {
let valid_error = err.into();
if !suppress_error(&valid_error) {
ws.config().ui().anyhow(&valid_error);
}
compiler_config
.diagnostics_reporter
.ensure(&db)
.map_err(|err| {
let valid_error = err.into();
if !suppress_error(&valid_error) {
ws.config().ui().anyhow(&valid_error);
}

anyhow!("could not check `{package_name}` due to previous error")
})?;
anyhow!("could not check `{package_name}` due to previous error")
})?;
};

Ok(())
}
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 1153df8

Please sign in to comment.