Skip to content

Commit

Permalink
Add group_id to target props
Browse files Browse the repository at this point in the history
commit-id:7964f21f
  • Loading branch information
maciektr committed May 22, 2024
1 parent ba4772b commit e7ebb48
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
7 changes: 6 additions & 1 deletion scarb/src/core/manifest/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct TargetInner {
pub kind: TargetKind,
pub name: SmolStr,
pub source_path: Utf8PathBuf,
pub group_id: Option<SmolStr>,
pub params: toml::Value,
}

Expand All @@ -36,13 +37,15 @@ impl Target {
kind: TargetKind,
name: impl Into<SmolStr>,
source_path: impl Into<Utf8PathBuf>,
group_id: Option<SmolStr>,
params: toml::Value,
) -> Self {
assert!(params.is_table(), "params must be a TOML table");
Self(Arc::new(TargetInner {
kind,
name: name.into(),
source_path: source_path.into(),
group_id,
params,
}))
}
Expand All @@ -56,6 +59,7 @@ impl Target {
kind,
name,
source_path,
None,
toml::Value::Table(toml::Table::new()),
)
}
Expand All @@ -64,10 +68,11 @@ impl Target {
kind: TargetKind,
name: impl Into<SmolStr>,
source_path: impl Into<Utf8PathBuf>,
group_id: Option<SmolStr>,
params: impl Serialize,
) -> Result<Self> {
let params = toml::Value::try_from(params)?;
Ok(Self::new(kind, name, source_path, params))
Ok(Self::new(kind, name, source_path, group_id, params))
}

pub fn is_lib(&self) -> bool {
Expand Down
11 changes: 10 additions & 1 deletion scarb/src/core/manifest/toml_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,13 +592,15 @@ impl TomlManifest {
self.lib.as_ref(),
&package_name,
root,
None,
)?);

targets.extend(Self::collect_target(
TargetKind::CAIRO_PLUGIN,
self.cairo_plugin.as_ref(),
&package_name,
root,
None,
)?);

for (kind, ext_toml) in self
Expand All @@ -612,6 +614,7 @@ impl TomlManifest {
Some(ext_toml),
&package_name,
root,
None,
)?);
}

Expand Down Expand Up @@ -645,6 +648,7 @@ impl TomlManifest {
Some(test_toml),
&package_name,
root,
None,
)?);
}
} else if auto_detect {
Expand All @@ -661,6 +665,7 @@ impl TomlManifest {
Some(&target_config),
&package_name,
root,
None,
)?);
// Auto-detect test targets from `tests` directory.
let tests_path = root.join(DEFAULT_TESTS_PATH);
Expand All @@ -679,6 +684,7 @@ impl TomlManifest {
Some(&target_config),
&package_name,
root,
None,
)?);
} else {
// Tests directory does not contain `lib.cairo` file.
Expand Down Expand Up @@ -712,6 +718,7 @@ impl TomlManifest {
Some(&target_config),
&package_name,
root,
Some("integration_tests".into()),
)?);
}
}
Expand All @@ -725,6 +732,7 @@ impl TomlManifest {
target: Option<&TomlTarget<T>>,
default_name: &SmolStr,
root: &Utf8Path,
group_id: Option<SmolStr>,
) -> Result<Option<Target>> {
let default_source_path = root.join(DEFAULT_SOURCE_PATH.as_path());
let Some(target) = target else {
Expand All @@ -747,7 +755,8 @@ impl TomlManifest {
.transpose()?
.unwrap_or(default_source_path.to_path_buf());

let target = Target::try_from_structured_params(kind, name, source_path, &target.params)?;
let target =
Target::try_from_structured_params(kind, name, source_path, group_id, &target.params)?;

Ok(Some(target))
}
Expand Down
17 changes: 17 additions & 0 deletions scarb/src/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,23 @@ fn check_unique_targets(targets: &Vec<&Target>) -> Result<()> {
)
}
}
for (kind, group_id) in targets
.iter()
.filter_map(|target| {
target
.group_id
.clone()
.map(|group_id| (target.kind.clone(), group_id))
})
.unique()
{
if used.contains(&(kind.as_str(), group_id.as_str())) {
bail!(
"the group id `{group_id}` of target `{kind}` duplicates target name\n\
help: use different group name to resolve the conflict",
)
}
}
Ok(())
}

Expand Down
9 changes: 8 additions & 1 deletion scarb/src/ops/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,18 @@ fn collect_dependency_kind(kind: &DepKind) -> Option<m::DepKind> {
}

fn collect_target_metadata(target: &Target) -> m::TargetMetadata {
let mut params = toml_to_json(&target.params);
if let Some(group) = target.group_id.as_ref() {
params.as_object_mut().unwrap().insert(
"group-id".to_string(),
serde_json::Value::String(group.to_string()),
);
}
m::TargetMetadataBuilder::default()
.kind(target.kind.to_string())
.name(target.name.to_string())
.source_path(target.source_path.clone())
.params(toml_to_json(&target.params))
.params(params)
.build()
.unwrap()
}
Expand Down

0 comments on commit e7ebb48

Please sign in to comment.