Skip to content

Commit

Permalink
Treat Cairo plugins as package dependencies
Browse files Browse the repository at this point in the history
commit-id:52f11b96
  • Loading branch information
integraledelebesgue committed Jan 16, 2025
1 parent 5e2567b commit 5ecc4f8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
3 changes: 3 additions & 0 deletions scarb-metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,9 @@ pub struct CompilationUnitCairoPluginMetadata {
/// Package ID.
pub package: PackageId,

/// An ID which uniquely identifies the plugin in scope of the Compilation Unit.
pub discriminator: Option<CompilationUnitComponentId>,

/// Whether Scarb will attempt to load prebuilt binaries associated with this plugin.
pub prebuilt_allowed: Option<bool>,

Expand Down
2 changes: 2 additions & 0 deletions scarb/src/compiler/compilation_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ pub struct CompilationUnitComponent {
#[derive(Clone, Debug, TypedBuilder)]
#[non_exhaustive]
pub struct CompilationUnitCairoPlugin {
/// The ID of the [`CompilationUnitComponent`] the plugin is represented by in the [`CairoCompilationUnit`].
pub discriminator: CompilationUnitComponentId,
/// The Scarb plugin [`Package`] to load.
pub package: Package,
/// Indicate whether the plugin is built into Scarb, or compiled from source.
Expand Down
14 changes: 11 additions & 3 deletions scarb/src/compiler/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,23 @@ fn build_project_config(unit: &CairoCompilationUnit) -> Result<ProjectConfig> {
let dependencies = component
.dependencies
.iter()
.map(|compilation_unit_component_id| {
.filter_map(|compilation_unit_component_id| {
if unit.cairo_plugins
.iter()
.find(|plugin| plugin.discriminator == *compilation_unit_component_id)
.map(|plugin| (plugin.package.id.name.to_string(), plugin.discriminator.clone()))
.is_some() {
return None;
}

let compilation_unit_component = unit.components.iter().find(|component| component.id == *compilation_unit_component_id)
.expect("dependency of a component is guaranteed to exist in compilation unit components");
(
Some((
compilation_unit_component.cairo_package_name().to_string(),
DependencySettings {
discriminator: compilation_unit_component.id.to_discriminator()
},
)
))
})
.collect();

Expand Down
3 changes: 3 additions & 0 deletions scarb/src/ops/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ fn collect_cairo_compilation_unit_metadata(
.map(|c| {
m::CompilationUnitCairoPluginMetadataBuilder::default()
.package(wrap_package_id(c.package.id))
.discriminator(m::CompilationUnitComponentId {
repr: c.discriminator.package_id.to_string(),
})
.prebuilt_allowed(c.prebuilt_allowed)
.build()
.unwrap()
Expand Down
31 changes: 30 additions & 1 deletion scarb/src/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,19 @@ fn cairo_compilation_unit_for_target(
component.dependencies = dependencies;
}

let member_plugin_dependencies = cairo_plugins
.iter()
.map(|plugin| plugin.discriminator.clone());

let member_component = components
.iter_mut()
.find(|component| component.package.id == member.id)
.unwrap();

member_component
.dependencies
.extend(member_plugin_dependencies);

Ok(CairoCompilationUnit {
main_package_id,
components,
Expand Down Expand Up @@ -706,6 +719,9 @@ impl<'a> PackageSolutionCollector<'a> {
let target = package.target(&TargetKind::CAIRO_PLUGIN).unwrap();
let props: CairoPluginProps = target.props()?;
Ok(CompilationUnitCairoPlugin::builder()
.discriminator(CompilationUnitComponentId {
package_id: package.id,
})
.package(package)
.builtin(props.builtin)
.prebuilt_allowed(prebuilt_allowed)
Expand All @@ -725,10 +741,23 @@ impl<'a> PackageSolutionCollector<'a> {
let package_id = component.id.package_id;

// Those are direct dependencies of the component.
let dependencies_packages = self
let mut dependencies_packages = self
.resolve
.package_dependencies(package_id, self.target_kind.as_ref().unwrap());

let mut plugin_dependencies = if self
.target_kind
.as_ref()
.is_some_and(|kind| kind == &TargetKind::CAIRO_PLUGIN)
{
vec![]
} else {
self.resolve
.package_dependencies(package_id, &TargetKind::CAIRO_PLUGIN)
};

dependencies_packages.append(&mut plugin_dependencies);

// We iterate over all the compilation unit components to get dependency's version.
let mut dependencies: Vec<CompilationUnitComponentId> = components
.iter()
Expand Down

0 comments on commit 5ecc4f8

Please sign in to comment.