Skip to content

Commit

Permalink
fix assets-plan index - wrong for dev; some little improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
boozook committed Jun 12, 2024
1 parent 7eb4e5a commit 809d56c
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 73 deletions.
2 changes: 1 addition & 1 deletion 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/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-playdate"
version = "0.5.0-beta.4"
version = "0.5.0-beta.5"
readme = "README.md"
description = "Build tool for neat yellow console."
keywords = ["playdate", "build", "cargo", "plugin", "cargo-subcommand"]
Expand Down
11 changes: 9 additions & 2 deletions cargo/src/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,15 @@ pub mod proto {
pub struct AssetsArtifactsNew<'t, 'cfg> {
artifacts: Vec<AssetsArtifact>,
index: BTreeMap<RootKey, Vec<usize>>,
tree: &'t MetaDeps<'cfg>,
pub tree: &'t MetaDeps<'cfg>,
}


impl AssetsArtifactsNew<'_, '_> {
pub fn len(&self) -> usize { self.artifacts.len() }
pub fn artifacts(&self) -> &[AssetsArtifact] { &self.artifacts }
pub fn index(&self) -> &BTreeMap<RootKey, Vec<usize>> { &self.index }

pub fn iter(&self) -> impl Iterator<Item = (&RootNode, impl Iterator<Item = &AssetsArtifact>)> {
self.index
.iter()
Expand Down Expand Up @@ -119,7 +123,10 @@ pub mod proto {

// checking cache, apply each plan:
for (index, plan) in plans.plans.into_iter().enumerate() {
let key = plans.index.iter().find(|(_, v)| **v == index).expect("key").0;
let key = plans.index
.iter()
.find_map(|(k, i)| (*i == index).then_some(k))
.ok_or_else(|| anyhow::anyhow!("No assets-plan key in plan #{index}"))?;

log::debug!("#{index} build (dev:{}) {}", key.dev, key.id);

Expand Down
111 changes: 55 additions & 56 deletions cargo/src/assets/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,57 +216,56 @@ pub mod proto {
let dep_key = Key::from(dep).with_dev(with_dev);


let plan_for =
|plans: &mut AssetsPlans, indices: &mut Vec<usize>, key: Key, dev: bool| -> anyhow::Result<()> {
let source = dep.as_source();
let name_log = dev.then_some("dev-").unwrap_or_default();
if let Some(assets) = source.metadata()
.map(|m| if dev { m.dev_assets() } else { m.assets() }) &&
!assets.is_empty()
{
match assets_build_plan(&env, assets, &options, Some(crate_root.into())) {
Ok(plan) => {
let pid = key.id;
let is_dev = key.dev;
let dev_index = plans.plans.len();
let compile_target_agnostic = plan.compile_target_agnostic();
plans.index.insert(key, dev_index);
plans.plans.push(plan);
indices.push(dev_index);

log::debug!(" done: +#{dev_index} (dev:{is_dev})");
cfg.log().verbose(|mut log| {
log.status("Plan", format_args!("{name_log}assets for {pid} planned",))
});

if !compile_target_agnostic {
cfg.log()
.error("Assets is not compile-target-agnostic, this is not supported");
}
},
Err(err) => {
let plan_for = |plans: &mut AssetsPlans, indices: &mut Vec<usize>, key: Key| -> anyhow::Result<()> {
let source = dep.as_source();
let dev_prefix = key.dev.then_some("dev-").unwrap_or_default();
if let Some(assets) = source.metadata()
.map(|m| if key.dev { m.dev_assets() } else { m.assets() }) &&
!assets.is_empty()
{
match assets_build_plan(&env, assets, &options, Some(crate_root.into())) {
Ok(plan) => {
let pid = key.id;
let is_dev = key.dev;
let dev_index = plans.plans.len();
let compile_target_agnostic = plan.compile_target_agnostic();
plans.index.insert(key, dev_index);
plans.plans.push(plan);
indices.push(dev_index);

log::debug!(" done: +#{dev_index} (dev:{is_dev})");
cfg.log().verbose(|mut log| {
log.status("Plan", format_args!("{dev_prefix}assets for {pid} planned"))
});

if !compile_target_agnostic {
cfg.log()
.error(format_args!("{err}, caused when planning {name_log}assets for {}", key.id));
return cfg.compile_options
.build_config
.keep_going
.then_some(())
.ok_or(err.into());
},
}
} else {
cfg.log().verbose(|mut log| {
log.status(
"Skip",
format_args!(
"{name_log}assets for {} without plan, reason: empty",
key.id
),
)
})
.error("Assets is not compile-target-agnostic, this is not supported");
}
},
Err(err) => {
cfg.log()
.error(format_args!("{err}, caused when planning {dev_prefix}assets for {}", key.id));
return cfg.compile_options
.build_config
.keep_going
.then_some(())
.ok_or(err.into());
},
}
Ok(())
};
} else {
cfg.log().verbose(|mut log| {
log.status(
"Skip",
format_args!(
"{dev_prefix}assets for {} without plan, reason: empty",
key.id
),
)
})
}
Ok(())
};

if let Some(i) = plans.index.get(&dep_key) {
// we already have plan for this dep
Expand All @@ -275,16 +274,16 @@ pub mod proto {
} else if with_dev && let Some(base_index) = plans.index.get(&dep_key.with_dev(false)).copied() {
// we already have plan for this dep, but not for dev part
indices.push(base_index);
log::debug!(" done (~#{base_index}) (dev:{})", dep_key.dev);
log::debug!(" done (~#{base_index}) (dev:{})", false);

plan_for(&mut plans, &mut indices, dep_key, true)?;
plan_for(&mut plans, &mut indices, dep_key.with_dev(true))?;
} else {
// else just build a plan
plan_for(&mut plans, &mut indices, dep_key, false)?;
plan_for(&mut plans, &mut indices, dep_key.with_dev(false))?;

// also for dev targets if needed
if with_dev {
plan_for(&mut plans, &mut indices, dep_key, true)?;
plan_for(&mut plans, &mut indices, dep_key.with_dev(true))?;
}
}
}
Expand Down Expand Up @@ -424,13 +423,13 @@ pub mod proto {

let name = this.id.name();
let root_name = root_id.name();
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}");
let msg = format!("{name}'s `{dev}assets.{target:?}` overrides {others}");

if options.overwrite() {
cfg.log().warn(msg)
} else {
cfg.log().error(&msg);
let why = format!("but that's not allowed by the top-level crate {root_name}");
cfg.log().error(format_args!("{msg}, {why}"));
overrides.push(msg);
}
}
Expand Down
13 changes: 10 additions & 3 deletions cargo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,21 @@ fn execute(config: &Config) -> CargoResult<()> {
return Err(anyhow::anyhow!("build-plan in not implemented yet"));
}

let deps_tree = crate::utils::cargo::meta_deps::meta_deps(config)?;
build::build(config)?;
},

cli::cmd::Cmd::Package => {
let deps_tree = crate::utils::cargo::meta_deps::meta_deps(config)?;
let assets_new = assets::proto::build_all(config, &deps_tree)?;

let assets = assets::build(config)?;
let products = build::build(config)?;

log::debug!("assets artifacts: {}", assets.len());
log::debug!("assets artifacts: old:{} / new:{}", assets.len(), assets_new.len());
log::debug!("build artifacts: {}", products.len());

package::build_all(config, assets, products)?;
package::build_all(config, assets, assets_new, products)?;
},

cli::cmd::Cmd::Run => {
Expand Down Expand Up @@ -145,6 +149,9 @@ fn execute(config: &Config) -> CargoResult<()> {
bail!("Nothing found to run");
}

let deps_tree = crate::utils::cargo::meta_deps::meta_deps(config)?;
let assets_new = assets::proto::build_all(config, &deps_tree)?;

// build requested package(s):
let assets = assets::build(config)?;
let mut products = build::build(config)?;
Expand All @@ -171,7 +178,7 @@ fn execute(config: &Config) -> CargoResult<()> {
})
.count();

let packages = package::build_all(config, assets, products)?;
let packages = package::build_all(config, assets, assets_new, products)?;
match packages.len() {
1 => (),
0 => bail!("No packages have been produced, nothing to run."),
Expand Down
34 changes: 34 additions & 0 deletions cargo/src/package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use playdate::metadata::format::Metadata;
use playdate::metadata::validation::Validate;
use playdate::metadata::validation::ValidateCrate;

use crate::assets::proto::AssetsArtifactsNew;
use crate::assets::AssetsArtifact;
use crate::assets::AssetsArtifacts;
use crate::assets::playdate_metadata;
Expand Down Expand Up @@ -51,6 +52,7 @@ pub struct Product<'p> {

pub fn build_all<'b>(config: &'_ Config,
assets: AssetsArtifacts<'_>,
assets_new: AssetsArtifactsNew<'_, '_>,
products: Vec<BuildProduct<'b>>)
-> CargoResult<Vec<Product<'b>>> {
let products: Vec<SuccessfulBuildProduct> = products.into_iter().flat_map(TryInto::try_into).collect();
Expand All @@ -68,6 +70,38 @@ pub fn build_all<'b>(config: &'_ Config,
let mut results = Vec::new();

for ((package, _), mut products) in targets {
log::debug!(
"Looking for assets artifacts for ({}) {}::{} for {}:",
&products[0].src_ct,
package.package_id(),
products[0].name,
match &products[0].ck {
CompileKind::Host => "host",
CompileKind::Target(ref kind) => kind.short_name(),
}
);

let (root, assets_ng) = assets_new.iter()
.find(|(r, _)| {
let unit = r.node().unit();
unit.package_id == package.package_id() &&
unit.platform == products[0].ck &&
unit.target.crate_types.contains(&products[0].src_ct) &&
unit.target.name == products[0].name
})
.ok_or_else(|| {
let ck: Cow<_> = match products[0].ck {
CompileKind::Host => "host".into(),
CompileKind::Target(ref kind) => kind.short_name().into(),
};
anyhow!(
"No assets artifacts for ({}) {}::{} for {ck}",
&products[0].src_ct,
package.package_id(),
products[0].name,
)
})?;

match products.len() {
0 => unreachable!("impossible len=0"),
1 => {
Expand Down
Loading

0 comments on commit 809d56c

Please sign in to comment.