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 21, 2024
1 parent a5e4c11 commit f3d8b1b
Show file tree
Hide file tree
Showing 5 changed files with 64 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
12 changes: 11 additions & 1 deletion scarb/src/core/manifest/toml_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ pub struct DetailedTomlDependency {
pub struct TomlTarget<P> {
pub name: Option<SmolStr>,
pub source_path: Option<Utf8PathBuf>,
pub group_id: Option<SmolStr>,

#[serde(flatten)]
pub params: P,
Expand Down Expand Up @@ -654,6 +655,7 @@ impl TomlManifest {
let target_config = TomlTarget::<TomlExternalTargetParams> {
name: Some(target_name),
source_path,
group_id: None,
params: TestTargetProps::default().try_into()?,
};
targets.extend(Self::collect_target::<TomlExternalTargetParams>(
Expand All @@ -672,6 +674,7 @@ impl TomlManifest {
let target_config = TomlTarget::<TomlExternalTargetParams> {
name: Some(target_name),
source_path: Some(source_path),
group_id: None,
params: TestTargetProps::new(TestTargetType::Integration).try_into()?,
};
targets.extend(Self::collect_target::<TomlExternalTargetParams>(
Expand Down Expand Up @@ -705,6 +708,7 @@ impl TomlManifest {
let target_config = TomlTarget::<TomlExternalTargetParams> {
name: Some(target_name),
source_path: Some(source_path),
group_id: Some("integration_tests".into()),
params: TestTargetProps::new(TestTargetType::Integration).try_into()?,
};
targets.extend(Self::collect_target(
Expand Down Expand Up @@ -747,7 +751,13 @@ 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,
target.group_id.clone(),
&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
22 changes: 22 additions & 0 deletions scarb/tests/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,25 @@ fn target_name_duplicate() {
help: use different target names to resolve the conflict
"#});
}

#[test]
fn target_group_id_duplicates_target_name() {
let t = TempDir::new().unwrap();
ProjectBuilder::start()
.name("first")
.manifest_extra(indoc! {r#"
[[target.starknet-contract]]
name = "hello"
group-id = "hello"
"#})
.build(&t);
Scarb::quick_snapbox()
.arg("fetch")
.current_dir(&t)
.assert()
.failure()
.stdout_matches(indoc! {r#"
error: the group id `hello` of target `starknet-contract` duplicates target name
help: use different group name to resolve the conflict
"#});
}

0 comments on commit f3d8b1b

Please sign in to comment.