Skip to content

Commit ca4c1d9

Browse files
Store plugins in the Crate structure
commit-id:988401af
1 parent a1a6506 commit ca4c1d9

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/project/crate_data.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ use cairo_lang_filesystem::db::{
77
FilesGroupEx,
88
};
99
use cairo_lang_filesystem::ids::{CrateId, CrateLongId, Directory};
10+
use cairo_lang_semantic::plugin::PluginSuite;
1011
use cairo_lang_utils::{Intern, LookupIntern};
1112
use smol_str::SmolStr;
1213

1314
use crate::lang::db::AnalysisDatabase;
1415

1516
/// A complete set of information needed to set up a real crate in the analysis database.
16-
#[derive(Debug, PartialEq, Eq)]
17+
#[derive(Debug)]
1718
pub struct Crate {
1819
/// Crate name.
1920
pub name: SmolStr,
@@ -36,6 +37,10 @@ pub struct Crate {
3637

3738
/// Crate settings.
3839
pub settings: CrateSettings,
40+
41+
/// Plugins required by the crate.
42+
#[allow(dead_code)] // TODO: remove. The field is used later in the stack
43+
pub plugins: PluginSuite,
3944
}
4045

4146
impl Crate {
@@ -61,6 +66,8 @@ impl Crate {
6166
if let Some(file_stems) = &self.custom_main_file_stems {
6267
inject_virtual_wrapper_lib(db, crate_id, file_stems);
6368
}
69+
70+
// TODO (later in the stack): Intern the plugin suite and set as override.
6471
}
6572

6673
/// Construct a [`Crate`] from data already applied to the [`AnalysisDatabase`].
@@ -79,7 +86,10 @@ impl Crate {
7986

8087
let custom_main_file_stems = extract_custom_file_stems(db, crate_id);
8188

82-
Some(Self { name, discriminator, root, custom_main_file_stems, settings })
89+
// TODO (later in the stack): Intern the suite and set as an override.
90+
let plugins = PluginSuite::default();
91+
92+
Some(Self { name, discriminator, root, custom_main_file_stems, settings, plugins })
8393
}
8494

8595
/// States whether this is the `core` crate.

src/project/scarb.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use cairo_lang_filesystem::cfg::CfgSet;
77
use cairo_lang_filesystem::db::{
88
CrateSettings, DependencySettings, Edition, ExperimentalFeaturesConfig,
99
};
10+
use cairo_lang_semantic::inline_macros::get_default_plugin_suite;
11+
use cairo_lang_semantic::plugin::PluginSuite;
1012
use itertools::Itertools;
1113
use scarb_metadata::{
1214
CompilationUnitComponentDependencyMetadata, CompilationUnitComponentId, Metadata,
@@ -15,6 +17,7 @@ use scarb_metadata::{
1517
use smol_str::ToSmolStr;
1618
use tracing::{debug, error, warn};
1719

20+
use super::builtin_plugins::BuiltinPlugin;
1821
use super::manifest_registry::member_config::MemberConfig;
1922
use crate::lang::db::AnalysisDatabase;
2023
use crate::project::crate_data::Crate;
@@ -64,6 +67,16 @@ pub fn extract_crates(metadata: &Metadata) -> Vec<Crate> {
6467
}
6568

6669
for component in &compilation_unit.components {
70+
// Plugin components don't have crates associated with them.
71+
if component
72+
.id
73+
.as_ref()
74+
.and_then(|component_id| metadata.get_plugin(component_id))
75+
.is_some()
76+
{
77+
continue;
78+
}
79+
6780
let crate_name = component.name.as_str();
6881
let Some(component_id) = component.id.clone() else {
6982
error!("id of component {crate_name} was None in metadata");
@@ -180,12 +193,36 @@ pub fn extract_crates(metadata: &Metadata) -> Vec<Crate> {
180193

181194
let custom_main_file_stems = (file_stem != "lib").then_some(vec![file_stem.into()]);
182195

196+
let plugin_dependencies = package
197+
.and_then(|package| metadata.get_package_plugin_dependencies(&package.id))
198+
.unwrap_or_default();
199+
200+
let plugins = plugin_dependencies
201+
.into_iter()
202+
.filter_map(|plugin_metadata| {
203+
let is_builtin = metadata.is_builtin(plugin_metadata);
204+
if !is_builtin.unwrap_or_default() {
205+
return None;
206+
}
207+
208+
let builtin_plugin = BuiltinPlugin::try_new(plugin_metadata);
209+
Some(builtin_plugin?.suite())
210+
})
211+
.chain([get_default_plugin_suite()].into_iter())
212+
.fold(PluginSuite::default(), |mut acc, suite| {
213+
acc.add(suite);
214+
acc
215+
});
216+
217+
debug!("Plugins for {}: {:#?}", package.unwrap().id.repr, plugins);
218+
183219
let cr = Crate {
184220
name: crate_name.into(),
185221
discriminator: component.discriminator.as_ref().map(ToSmolStr::to_smolstr),
186222
root: root.into(),
187223
custom_main_file_stems,
188224
settings,
225+
plugins,
189226
};
190227

191228
if compilation_unit.package == component.package {
@@ -237,6 +274,8 @@ pub fn extract_crates(metadata: &Metadata) -> Vec<Crate> {
237274
let name = first_crate.name.clone();
238275
let discriminator = first_crate.discriminator.clone();
239276

277+
let plugins = first_crate.plugins.clone();
278+
240279
let custom_main_file_stems =
241280
crs.into_iter().flat_map(|cr| cr.custom_main_file_stems.unwrap()).collect();
242281

@@ -246,6 +285,7 @@ pub fn extract_crates(metadata: &Metadata) -> Vec<Crate> {
246285
root,
247286
custom_main_file_stems: Some(custom_main_file_stems),
248287
settings,
288+
plugins,
249289
});
250290
}
251291

0 commit comments

Comments
 (0)