Skip to content

Commit

Permalink
Store plugins in the Crate structure
Browse files Browse the repository at this point in the history
commit-id:988401af
  • Loading branch information
integraledelebesgue committed Jan 22, 2025
1 parent 98cc352 commit 456121c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/project/crate_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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`].
Expand All @@ -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.
Expand Down
37 changes: 37 additions & 0 deletions src/project/scarb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -64,6 +67,16 @@ pub fn extract_crates(metadata: &Metadata) -> Vec<Crate> {
}

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");
Expand Down Expand Up @@ -180,12 +193,33 @@ pub fn extract_crates(metadata: &Metadata) -> Vec<Crate> {

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 {
Expand Down Expand Up @@ -237,6 +271,8 @@ pub fn extract_crates(metadata: &Metadata) -> Vec<Crate> {
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();

Expand All @@ -246,6 +282,7 @@ pub fn extract_crates(metadata: &Metadata) -> Vec<Crate> {
root,
custom_main_file_stems: Some(custom_main_file_stems),
settings,
plugins,
});
}

Expand Down

0 comments on commit 456121c

Please sign in to comment.