Skip to content

Commit

Permalink
impl structural inheritance for package.metadata.playdate.options b…
Browse files Browse the repository at this point in the history
…y `workspace.metadata.playdate.options`
  • Loading branch information
boozook committed Jun 9, 2024
1 parent aaa92ab commit 279d0c1
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 78 deletions.
28 changes: 14 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cargo/src/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ fn deps_tree_metadata<'cfg: 'r, 't: 'r, 'r>(package: &'cfg Package,
let mut packages = HashMap::new();
if let Some(metadata) = playdate_metadata(package) {
// if explicitly allowed collect deps => scan deps-tree
if metadata.assets_options().dependencies {
if metadata.assets_options().dependencies() {
log::debug!("inspecting deps-tree of {}", package.package_id());

packages.insert(package, metadata);
Expand Down
4 changes: 2 additions & 2 deletions cargo/src/assets/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ pub mod proto {
root.package_id(),
root.node().target().kind().description()
);
log::debug!(" dependencies are allowed: {}", options.dependencies);
log::debug!(" dependencies are allowed: {}", options.dependencies());


let plan_key = MultiKey::from(root);
Expand Down Expand Up @@ -420,7 +420,7 @@ pub mod proto {
let why = format!("but that's not allowed by the top-level crate {root_name}");
let msg = format!("{name}'s `{dev}assets.{target:?}` overrides {others}, {why}");

if options.overwrite {
if options.overwrite() {
cfg.log().warn(msg)
} else {
cfg.log().error(&msg);
Expand Down
57 changes: 47 additions & 10 deletions cargo/src/utils/cargo/meta_deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use cargo::core::{PackageId, PackageIdSpecQuery};
use cargo::util::interning::InternedString;
use cargo::CargoResult;
use playdate::manifest::PackageSource;
use playdate::metadata::format::ws::WorkspaceMetadata;
use playdate::metadata::source::MetadataSource;
use playdate::metadata::format::Metadata as MainMetadata;
use serde::Deserialize;
Expand Down Expand Up @@ -39,6 +40,8 @@ pub struct MetaDeps<'cfg> {
pub struct RootNode<'cfg> {
node: Node<'cfg>,
deps: Vec<Node<'cfg>>,

ws: Option<&'cfg WorkspaceMetadata>,
}

impl<'t> RootNode<'t> {
Expand Down Expand Up @@ -81,19 +84,20 @@ impl<'t> MetaDeps<'t> {
};
let is_sub_tk = |u: &&Unit| matches!(u.target.kind, TargetKind::Lib(_));

let ws = meta.workspace_metadata.as_ref();

let mut roots = units.roots
.iter()
.map(|i| &units.units[*i])
.filter(mode_is_build)
.filter(is_norm_tk)
.map(|u| {
// let m = meta.packages.iter().find(|p| p.id == u.package_id);
let m = meta.packages.iter().find(|p| p.id.matches(u.package_id));
Node::<'t> { meta: m, unit: u }
})
.map(|node| {
RootNode::<'t> { node,
RootNode::<'t> { ws,
node,
deps: Vec::with_capacity(0) }
})
.collect::<Vec<_>>();
Expand All @@ -107,7 +111,6 @@ impl<'t> MetaDeps<'t> {
.filter(mode_is_build)
.filter(is_sub_tk)
.map(|u| {
// let m = meta.packages.iter().find(|p| p.id == u.package_id);
let m = meta.packages.iter().find(|p| p.id.matches(u.package_id));
Node::<'t> { meta: m, unit: u }
})
Expand Down Expand Up @@ -272,7 +275,7 @@ impl<'t> MetaDeps<'t> {
.map(|m| m.assets_options())
})
.unwrap_or_default()
.dependencies
.dependencies()
}
}

Expand All @@ -283,7 +286,7 @@ pub trait DependenciesAllowed {


impl DependenciesAllowed for RootNode<'_> {
fn deps_allowed(&self) -> bool { self.node.deps_allowed() }
fn deps_allowed(&self) -> bool { self.node.deps_allowed() || self.as_source().assets_options().dependencies() }
}

impl DependenciesAllowed for Node<'_> {
Expand All @@ -294,7 +297,7 @@ impl DependenciesAllowed for Node<'_> {
.and_then(|m| m.inner.as_ref())
.map(|m| m.assets_options())
.unwrap_or_default()
.dependencies
.dependencies()
}
}

Expand All @@ -308,7 +311,7 @@ impl DependenciesAllowed for cargo::core::Package {
.ok()
})
.and_then(|m| m.inner)
.map(|m| m.assets_options().dependencies)
.map(|m| m.assets_options().dependencies())
.unwrap_or_default()
}
}
Expand All @@ -325,10 +328,10 @@ impl<'t> Node<'t> {

impl<'t> RootNode<'t> {
pub fn into_source(self) -> impl PackageSource<Metadata = MainMetadata<InternedString>> + 't {
CrateNode::from(self.node)
CrateNode::from(&self)
}
pub fn as_source(&self) -> impl PackageSource<Metadata = MainMetadata<InternedString>> + 't {
self.to_owned().into_source()
CrateNode::from(self)
}
}

Expand All @@ -337,6 +340,8 @@ struct CrateNode<'t> {
node: Node<'t>,
bins: Vec<&'t str>,
examples: Vec<&'t str>,

ws: Option<&'t WorkspaceMetadata>,
}

impl<'t> From<Node<'t>> for CrateNode<'t> {
Expand All @@ -355,7 +360,30 @@ impl<'t> From<Node<'t>> for CrateNode<'t> {
.flat_map(|m| m.targets.iter())
.filter(|t| t.kind == TargetKind::Example)
.map(|t| t.name.as_str())
.collect() }
.collect(),
ws: None }
}
}

impl<'t> From<&RootNode<'t>> for CrateNode<'t> {
fn from(root: &RootNode<'t>) -> Self {
let node = root.node;
Self { node,
bins: node.meta
.as_ref()
.into_iter()
.flat_map(|m| m.targets.iter())
.filter(|t| t.kind == TargetKind::Bin)
.map(|t| t.name.as_str())
.collect(),
examples: node.meta
.as_ref()
.into_iter()
.flat_map(|m| m.targets.iter())
.filter(|t| t.kind == TargetKind::Example)
.map(|t| t.name.as_str())
.collect(),
ws: root.ws }
}
}

Expand Down Expand Up @@ -408,4 +436,13 @@ impl PackageSource for CrateNode<'_> {
.map(|m| m.manifest_path.as_path().into())
.unwrap_or_default()
}


// from ws metadata:
fn default_options(&self) -> Option<&playdate::metadata::format::ws::OptionsDefault> {
self.ws
.and_then(|m| m.inner.as_ref())
.as_ref()
.and_then(|m| m.options.as_ref())
}
}
13 changes: 10 additions & 3 deletions support/build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,25 @@ Package build options, instruction for Playdate Package Build System such as car

```toml
[package.metadata.playdate.options]
workspace = true # use `workspace.metadata.playdate.options` as defaults (default is `false`)
assets.dependencies = true # just set or override corresponding value from `workspace.metadata`
```

Field `workspace` works like the cargo's feature [inheriting a dependency from a workspace][cargo-inheriting-dep-ws], turning on structural inheritance of `package.metadata.playdate.options` by `workspace.metadata.playdate.options`.


Available options is `assets`, see [Assets Options](#assets-options).

_Currently there is no more options, it's just reserved for future use._

This configuration is used for primary packages only. Primary packages are the ones the user selected on the command-line, either with `-p` flags or the defaults based on the current directory and the default workspace members.
So, `options` from top-level package are applying to entire dependency tree ignoring `options` of dependencies. Thus, only the end user controls how the assets will be collected & built.

Note: this is depends on implementation, above is how it works in the reference impl `cargo-playdate`.
_Note: this is depends on implementation, above is how it works in the reference impl `cargo-playdate`._


[cargo-inheriting-dep-ws]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#inheriting-a-dependency-from-a-workspace


#### Assets Options

Expand All @@ -164,12 +171,12 @@ This is how assets will be collected for your package.
```toml
[package.metadata.playdate.options.assets]
dependencies = true # allow to build assets for dependencies (default is `false`)
overwrite = true # overwrite existing assets in build dir (default is `true`)
overwrite = true # overwrite existing assets in build dir (default is `true`, alias: `override`)
method = "link" # "copy" or "link" (default is `link`) - how assets should be collected, make symlinks or copy files
follow-symlinks = true # follow symlinks (default is `true`)
```


Field `overwrite` also allows higher dependencies to overwrite assets of deeper dependency.


- - -
Expand Down
6 changes: 3 additions & 3 deletions support/build/src/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub fn apply_build_plan<'l, 'r, P: AsRef<Path>>(plan: BuildPlan<'l, 'r>,
use crate::fs::ensure_dir_exists;

let target_root = target_root.as_ref();
let build_method = assets_options.method;
let overwrite = assets_options.overwrite;
let build_method = assets_options.method();
let overwrite = assets_options.overwrite();
info!("collecting assets:");
debug!("assets build method: {build_method:?}, overwrite: {overwrite}");

Expand Down Expand Up @@ -171,7 +171,7 @@ pub enum OpRes {

impl AssetsOptions {
fn link_behavior(&self) -> LinkBehavior {
if self.follow_symlinks {
if self.follow_symlinks() {
LinkBehavior::ReadTarget
} else {
LinkBehavior::ReadFile
Expand Down
Loading

0 comments on commit 279d0c1

Please sign in to comment.