From 456121ceff7c9b119f5d22ff4fd56263df7e5af4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sm=C3=B3=C5=82ka?= Date: Fri, 17 Jan 2025 19:04:42 +0100 Subject: [PATCH] Store plugins in the Crate structure commit-id:988401af --- src/project/crate_data.rs | 14 ++++++++++++-- src/project/scarb.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/project/crate_data.rs b/src/project/crate_data.rs index 73cccf72..503365be 100644 --- a/src/project/crate_data.rs +++ b/src/project/crate_data.rs @@ -7,13 +7,14 @@ use cairo_lang_filesystem::db::{ FilesGroupEx, }; use cairo_lang_filesystem::ids::{CrateId, CrateLongId, Directory}; +use cairo_lang_semantic::plugin::PluginSuite; use cairo_lang_utils::{Intern, LookupIntern}; use smol_str::SmolStr; use crate::lang::db::AnalysisDatabase; /// A complete set of information needed to set up a real crate in the analysis database. -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug)] pub struct Crate { /// Crate name. pub name: SmolStr, @@ -36,6 +37,10 @@ pub struct Crate { /// Crate settings. pub settings: CrateSettings, + + /// Plugins required by the crate. + #[allow(dead_code)] // TODO: remove. The field is used later in the stack + pub plugins: PluginSuite, } impl Crate { @@ -61,6 +66,8 @@ impl Crate { if let Some(file_stems) = &self.custom_main_file_stems { inject_virtual_wrapper_lib(db, crate_id, file_stems); } + + // TODO (later in the stack): Intern the plugin suite and set as override. } /// Construct a [`Crate`] from data already applied to the [`AnalysisDatabase`]. @@ -79,7 +86,10 @@ impl Crate { let custom_main_file_stems = extract_custom_file_stems(db, crate_id); - Some(Self { name, discriminator, root, custom_main_file_stems, settings }) + // TODO (later in the stack): Intern the suite and set as an override. + let plugins = PluginSuite::default(); + + Some(Self { name, discriminator, root, custom_main_file_stems, settings, plugins }) } /// States whether this is the `core` crate. diff --git a/src/project/scarb.rs b/src/project/scarb.rs index 07b746e6..546aded6 100644 --- a/src/project/scarb.rs +++ b/src/project/scarb.rs @@ -7,6 +7,8 @@ use cairo_lang_filesystem::cfg::CfgSet; use cairo_lang_filesystem::db::{ CrateSettings, DependencySettings, Edition, ExperimentalFeaturesConfig, }; +use cairo_lang_semantic::inline_macros::get_default_plugin_suite; +use cairo_lang_semantic::plugin::PluginSuite; use itertools::Itertools; use scarb_metadata::{ CompilationUnitComponentDependencyMetadata, CompilationUnitComponentId, Metadata, @@ -15,6 +17,7 @@ use scarb_metadata::{ use smol_str::ToSmolStr; use tracing::{debug, error, warn}; +use super::builtin_plugins::BuiltinPlugin; use super::manifest_registry::member_config::MemberConfig; use crate::lang::db::AnalysisDatabase; use crate::project::crate_data::Crate; @@ -64,6 +67,16 @@ pub fn extract_crates(metadata: &Metadata) -> Vec { } for component in &compilation_unit.components { + // Plugin components don't have crates associated with them. + if component + .id + .as_ref() + .and_then(|component_id| metadata.get_plugin(component_id)) + .is_some() + { + continue; + } + let crate_name = component.name.as_str(); let Some(component_id) = component.id.clone() else { error!("id of component {crate_name} was None in metadata"); @@ -180,12 +193,33 @@ pub fn extract_crates(metadata: &Metadata) -> Vec { let custom_main_file_stems = (file_stem != "lib").then_some(vec![file_stem.into()]); + let plugin_dependencies = package + .and_then(|package| metadata.get_package_plugin_dependencies(&package.id)) + .unwrap_or_default(); + + let plugins = plugin_dependencies + .into_iter() + .filter_map(|plugin_metadata| { + let builtin_plugin = + BuiltinPlugin::from_plugin_metadata(metadata, plugin_metadata)?; + + Some(builtin_plugin.suite()) + }) + .chain([get_default_plugin_suite()].into_iter()) + .fold(PluginSuite::default(), |mut acc, suite| { + acc.add(suite); + acc + }); + + debug!("Plugins for {}: {:#?}", package.unwrap().id.repr, plugins); + let cr = Crate { name: crate_name.into(), discriminator: component.discriminator.as_ref().map(ToSmolStr::to_smolstr), root: root.into(), custom_main_file_stems, settings, + plugins, }; if compilation_unit.package == component.package { @@ -237,6 +271,8 @@ pub fn extract_crates(metadata: &Metadata) -> Vec { let name = first_crate.name.clone(); let discriminator = first_crate.discriminator.clone(); + let plugins = first_crate.plugins.clone(); + let custom_main_file_stems = crs.into_iter().flat_map(|cr| cr.custom_main_file_stems.unwrap()).collect(); @@ -246,6 +282,7 @@ pub fn extract_crates(metadata: &Metadata) -> Vec { root, custom_main_file_stems: Some(custom_main_file_stems), settings, + plugins, }); }