Skip to content

Commit

Permalink
Add target selector
Browse files Browse the repository at this point in the history
commit-id:2ec662b7
  • Loading branch information
maciektr committed Jun 11, 2024
1 parent 3e8b479 commit f0c052f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 26 deletions.
8 changes: 8 additions & 0 deletions scarb/src/bin/scarb/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ pub struct ExpandArgs {
#[command(flatten)]
pub features: FeaturesSpec,

/// Specify the target to expand by target kind.
#[arg(long)]
pub target_kind: Option<String>,

/// Specify the target to expand by target name.
#[arg(long)]
pub target_name: Option<String>,

/// Do not attempt formatting.
#[arg(long, default_value_t = false)]
pub ugly: bool,
Expand Down
5 changes: 4 additions & 1 deletion scarb/src/bin/scarb/commands/expand.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use anyhow::Result;
use smol_str::ToSmolStr;

use crate::args::ExpandArgs;
use scarb::core::Config;
use scarb::core::{Config, TargetKind};
use scarb::ops;
use scarb::ops::ExpandOpts;

Expand All @@ -12,6 +13,8 @@ pub fn run(args: ExpandArgs, config: &Config) -> Result<()> {
let opts = ExpandOpts {
features: args.features.try_into()?,
ugly: args.ugly,
target_name: args.target_name.map(|n| n.to_smolstr()),
target_kind: args.target_kind.map(TargetKind::try_new).transpose()?,
};
ops::expand(package, opts, &ws)
}
22 changes: 2 additions & 20 deletions scarb/src/ops/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::core::{
FeatureName, PackageId, PackageName, TargetKind, Utf8PathWorkspaceExt, Workspace,
};
use crate::ops;
use crate::ops::get_test_package_ids;

#[derive(Debug, Clone)]
pub enum FeaturesSelector {
Expand Down Expand Up @@ -81,26 +82,7 @@ where
let resolve = ops::resolve_workspace(ws)?;

// Add test compilation units to build
let packages = packages
.into_iter()
.flat_map(|package_id| {
let package = ws.members().find(|p| p.id == package_id).unwrap();
let mut result: Vec<PackageId> = package
.manifest
.targets
.iter()
.filter(|t| t.is_test())
.map(|t| {
package
.id
.for_test_target(t.group_id.clone().unwrap_or(t.name.clone()))
})
.collect();
result.push(package_id);
result
})
.collect::<Vec<PackageId>>();

let packages = get_test_package_ids(packages, ws);
let compilation_units = ops::generate_compilation_units(&resolve, &opts.features, ws)?
.into_iter()
.filter(|cu| {
Expand Down
40 changes: 35 additions & 5 deletions scarb/src/ops/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::compiler::helpers::{build_compiler_config, write_string};
use crate::compiler::{CairoCompilationUnit, CompilationUnit, CompilationUnitAttributes};
use crate::core::{Package, TargetKind, Workspace};
use crate::ops;
use crate::ops::FeaturesOpts;
use crate::ops::{get_test_package_ids, FeaturesOpts};
use anyhow::{anyhow, bail, Context, Result};
use cairo_lang_compiler::db::RootDatabase;
use cairo_lang_compiler::diagnostics::DiagnosticsError;
Expand All @@ -18,11 +18,14 @@ use cairo_lang_parser::db::ParserGroup;
use cairo_lang_syntax::node::helpers::UsePathEx;
use cairo_lang_syntax::node::{ast, TypedStablePtr, TypedSyntaxNode};
use cairo_lang_utils::Upcast;
use smol_str::SmolStr;
use std::collections::HashSet;

#[derive(Debug)]
pub struct ExpandOpts {
pub features: FeaturesOpts,
pub target_kind: Option<TargetKind>,
pub target_name: Option<SmolStr>,
pub ugly: bool,
}

Expand All @@ -38,10 +41,37 @@ pub fn expand(package: Package, opts: ExpandOpts, ws: &Workspace<'_>) -> Result<
.map(|unit| ops::compile::compile_unit(unit.clone(), ws))
.collect::<Result<Vec<_>>>()?;

let Some(compilation_unit) = compilation_units.into_iter().find(|unit| {
unit.main_package_id() == package.id
&& unit.main_component().target_kind() == TargetKind::LIB
}) else {
let Some(compilation_unit) = compilation_units
.into_iter()
// We rewrite group compilation units to single source paths ones. We value simplicity over
// performance here, as expand output will be read by people rather than tooling.
.flat_map(|unit| match unit {
CompilationUnit::Cairo(unit) => unit
.rewrite_to_single_source_paths()
.into_iter()
.map(CompilationUnit::Cairo)
.collect::<Vec<_>>(),
// We include non-cairo compilation units here, so we can show better error msg later.
_ => vec![unit],
})
.find(|unit| {
let target_kind = if opts.target_name.is_none() && opts.target_kind.is_none() {
// If no target specifier is used - default to lib.
Some(TargetKind::LIB)
} else {
opts.target_kind.clone()
};
// Includes test package ids.
get_test_package_ids(vec![package.id], ws).contains(&unit.main_package_id())
// We can use main_component below, as targets are not grouped.
&& target_kind.as_ref()
.map_or(true, |kind| unit.main_component().target_kind() == *kind)
&& opts
.target_name
.as_ref()
.map_or(true, |name| unit.main_component().first_target().name == *name)
})
else {
bail!("compilation unit not found for `{package_name}`")
};
let CompilationUnit::Cairo(compilation_unit) = compilation_unit else {
Expand Down
26 changes: 26 additions & 0 deletions scarb/src/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,3 +571,29 @@ fn generate_cairo_plugin_compilation_units(member: &Package) -> Result<Vec<Compi
)?],
})])
}

/// Generate package ids associated with test compilation units for the given packages.
/// This function will return input list along with generated test package ids.
pub fn get_test_package_ids(packages: Vec<PackageId>, ws: &Workspace<'_>) -> Vec<PackageId> {
packages
.into_iter()
.flat_map(|package_id| {
let Some(package) = ws.members().find(|p| p.id == package_id) else {
return Vec::new();
};
let mut result: Vec<PackageId> = package
.manifest
.targets
.iter()
.filter(|t| t.is_test())
.map(|t| {
package
.id
.for_test_target(t.group_id.clone().unwrap_or(t.name.clone()))
})
.collect();
result.push(package_id);
result
})
.collect::<Vec<PackageId>>()
}

0 comments on commit f0c052f

Please sign in to comment.