From 4f3927cb4362f925aec325557fc13c8db127025b Mon Sep 17 00:00:00 2001 From: Piotr Figiela <77412592+Draggu@users.noreply.github.com> Date: Wed, 23 Oct 2024 17:46:04 +0200 Subject: [PATCH] Proc macro server: Load `ProcMacroHost` commit-id:819109f5 --- .../bin/scarb/commands/proc_macro_server.rs | 53 +++++++++++++++++-- scarb/src/ops/mod.rs | 2 + scarb/src/ops/proc_macro_server/mod.rs | 6 +++ 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 scarb/src/ops/proc_macro_server/mod.rs diff --git a/scarb/src/bin/scarb/commands/proc_macro_server.rs b/scarb/src/bin/scarb/commands/proc_macro_server.rs index 398c50e76..b61e02c2c 100644 --- a/scarb/src/bin/scarb/commands/proc_macro_server.rs +++ b/scarb/src/bin/scarb/commands/proc_macro_server.rs @@ -1,7 +1,54 @@ use anyhow::Result; -use scarb::core::Config; +use scarb::{ + compiler::{plugin::proc_macro::ProcMacroHost, CairoCompilationUnit, CompilationUnit}, + core::{Config, Workspace}, + ops::{self, FeaturesOpts, FeaturesSelector}, +}; #[tracing::instrument(skip_all, level = "info")] -pub fn run(_config: &Config) -> Result<()> { - unimplemented!() +pub fn run(config: &mut Config) -> Result<()> { + let ws = ops::read_workspace(config.manifest_path(), config)?; + let resolve = ops::resolve_workspace(&ws)?; + let compilation_units = ops::generate_compilation_units( + &resolve, + &FeaturesOpts { + features: FeaturesSelector::AllFeatures, + no_default_features: false, + }, + &ws, + )?; + + // Compile procedural macros only. + for unit in &compilation_units { + if let CompilationUnit::ProcMacro(_) = unit { + ops::compile_unit(unit.clone(), &ws)?; + } + } + + let mut proc_macros = ProcMacroHost::default(); + + // Load previously compiled procedural macros. + for unit in compilation_units { + if let CompilationUnit::Cairo(unit) = unit { + load_plugins(unit, &ws, &mut proc_macros)?; + } + } + + ops::start_proc_macro_server(proc_macros) +} + +fn load_plugins( + unit: CairoCompilationUnit, + ws: &Workspace<'_>, + proc_macros: &mut ProcMacroHost, +) -> Result<()> { + for plugin_info in unit + .cairo_plugins + .into_iter() + .filter(|plugin_info| !plugin_info.builtin) + { + proc_macros.register(plugin_info.package, ws.config())?; + } + + Ok(()) } diff --git a/scarb/src/ops/mod.rs b/scarb/src/ops/mod.rs index 7dbab6b74..df3d5ac7c 100644 --- a/scarb/src/ops/mod.rs +++ b/scarb/src/ops/mod.rs @@ -11,6 +11,7 @@ pub use manifest::*; pub use metadata::*; pub use new::*; pub use package::*; +pub use proc_macro_server::*; pub use publish::*; pub use resolve::*; pub use scripts::*; @@ -27,6 +28,7 @@ mod manifest; mod metadata; mod new; mod package; +mod proc_macro_server; mod publish; mod resolve; mod scripts; diff --git a/scarb/src/ops/proc_macro_server/mod.rs b/scarb/src/ops/proc_macro_server/mod.rs new file mode 100644 index 000000000..9f1fb9d5e --- /dev/null +++ b/scarb/src/ops/proc_macro_server/mod.rs @@ -0,0 +1,6 @@ +use crate::compiler::plugin::proc_macro::ProcMacroHost; +use anyhow::Result; + +pub fn start_proc_macro_server(proc_macros: ProcMacroHost) -> Result<()> { + unimplemented!() +}