Skip to content

Commit

Permalink
Merge pull request #341 from boozook/assets/misc
Browse files Browse the repository at this point in the history
Add todos, docs, remove garbage
  • Loading branch information
boozook authored May 5, 2024
2 parents 182ea6d + 8d88754 commit 28d376f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 21 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 support/build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "playdate-build"
version = "0.2.6"
version = "0.2.7"
readme = "README.md"
description = "Utils that help to build package for Playdate"
keywords = ["playdate", "package", "encoding", "manifest", "assets"]
Expand Down
2 changes: 0 additions & 2 deletions support/build/src/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ pub fn apply_build_plan<'l, 'r, P: AsRef<Path>>(plan: BuildPlan<'l, 'r>,
};

let (mut plan, crate_root) = plan.into_parts();
// let mut results = HashMap::with_capacity(plan.as_inner().len());
let mut results = HashMap::with_capacity(plan.len());
// for entry in plan.into_inner().drain(..) {
for entry in plan.drain(..) {
let current: Vec<_> = match &entry {
Mapping::AsIs(inc, ..) => {
Expand Down
24 changes: 15 additions & 9 deletions support/build/src/assets/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,15 +200,16 @@ pub fn build_plan<'l, 'r, 'c: 'l, V>(env: &'c Env,
}


// TODO: tests for `abs_or_rel_crate_existing`
/// Make path relative to `crate_root` if it isn't absolute, checking existence.
/// Returns `None` if path doesn't exist.
pub fn abs_or_rel_crate_existing<'t, P1, P2>(p: P1, root: P2) -> std::io::Result<Option<Cow<'t, Path>>>
pub fn abs_or_rel_crate_existing<'t, P1, P2>(path: P1, root: P2) -> std::io::Result<Option<Cow<'t, Path>>>
where P1: 't + AsRef<Path> + Into<Cow<'t, Path>>,
P2: AsRef<Path> {
let p = if p.as_ref().is_absolute() && p.as_ref().try_exists()? {
Some(p.into())
let p = if path.as_ref().is_absolute() && path.as_ref().try_exists()? {
Some(path.into())
} else {
let abs = root.as_ref().join(p);
let abs = root.as_ref().join(path);
if abs.try_exists()? {
Some(Cow::Owned(abs))
} else {
Expand All @@ -218,14 +219,14 @@ pub fn abs_or_rel_crate_existing<'t, P1, P2>(p: P1, root: P2) -> std::io::Result
Ok(p)
}

/// Same as [`abs_or_rel_crate_existing`], but returns given `p` as fallback.
/// Same as [`abs_or_rel_crate_existing`], but returns given `path` as fallback.
#[inline]
pub fn abs_or_rel_crate_any<'t, P1, P2>(p: P1, root: P2) -> Cow<'t, Path>
pub fn abs_or_rel_crate_any<'t, P1, P2>(path: P1, root: P2) -> Cow<'t, Path>
where P1: 't + AsRef<Path> + Into<Cow<'t, Path>> + Clone,
P2: AsRef<Path> {
abs_or_rel_crate_existing(p.clone(), root).ok()
.flatten()
.unwrap_or(p.into())
abs_or_rel_crate_existing(path.clone(), root).ok()
.flatten()
.unwrap_or(path.into())
}


Expand All @@ -245,6 +246,7 @@ fn possibly_matching_any<P: Into<PathBuf>, I: IntoIterator<Item = P>>(path: &Pat
}


// TODO: tests for `possibly_matching`
/// Check that filter (possibly) pattern `expr` matches the `path`.
fn possibly_matching<P: Into<PathBuf>>(path: &Path, expr: P) -> bool {
// TODO: remove {crate_root} part if it is from filter (or both?).
Expand All @@ -262,9 +264,12 @@ fn possibly_matching<P: Into<PathBuf>>(path: &Path, expr: P) -> bool {
}


/// Assets Build Plan for a crate.
#[derive(Debug, PartialEq, Eq, Hash, serde::Serialize)]
pub struct BuildPlan<'left, 'right> {
/// Instructions - what file where to put
plan: Vec<Mapping<'left, 'right>>,
/// Root directory of associated crate
crate_root: &'left Path,
}

Expand Down Expand Up @@ -386,6 +391,7 @@ pub enum Mapping<'left, 'right>
}

impl Mapping<'_, '_> {
// TODO: tests for `Mapping::eq_ignore_expr`
pub fn eq_ignore_expr(&self, other: &Self) -> bool {
match (self, other) {
(Mapping::AsIs(a, _), Mapping::AsIs(b, _)) | (Mapping::Into(a, _), Mapping::Into(b, _)) => a.eq(b),
Expand Down
7 changes: 4 additions & 3 deletions support/build/src/assets/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ pub fn resolve_includes<S: AsRef<str>, Excl: AsRef<str>>(expr: S,
links: LinkBehavior)
-> Result<Vec<Match>, Error> {
let expr = unixish_path_pattern(expr.as_ref());
let crate_root = crate_root.to_string_lossy();

// let crate_root = crate_root.to_string_lossy();
// #[cfg(windows)]
// let crate_root = unixish_path_pattern(crate_root.as_ref());
// let crate_root = Path::new(crate_root.as_ref());

let crate_root = Path::new(crate_root.as_ref());
let glob = Glob::new(expr.as_ref()).map_err(|err| {
// According wax's issue https://github.com/olson-sean-k/wax/issues/34
// we doesn't support Windows absolute paths and hope to partially relative paths.
Expand Down Expand Up @@ -233,7 +233,7 @@ impl Match {
}
}

// TODO: test it
// TODO: tests for `Match::set_target`
fn set_target<P: Into<PathBuf>>(&mut self, path: P) {
trace!("old target: {}", self.target().display());
match self {
Expand Down Expand Up @@ -339,6 +339,7 @@ impl Expr<'_> {
}

impl<'e> Expr<'e> {
// TODO: tests for `Expr::set`
fn set<'s, S: Into<Cow<'s, str>>>(&mut self, actual: S)
where 's: 'e {
let original = match self {
Expand Down
10 changes: 5 additions & 5 deletions support/build/src/assets/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ mod plan {

#[test]
#[cfg_attr(windows, should_panic)]
fn resolve_external() {
fn resolve_exact_external_abs() {
let (temp, sub, _files, env) = prepared_tmp("as_is-resolve_external");

let opts = AssetsOptions::default();
Expand Down Expand Up @@ -224,7 +224,7 @@ mod plan {

#[test]
#[cfg_attr(windows, should_panic)]
fn resolve_external_many() {
fn resolve_glob_external_many() {
let (_, _, files, env) = prepared_tmp("as_is-resolve_external_many");

let opts = AssetsOptions::default();
Expand Down Expand Up @@ -357,15 +357,15 @@ mod plan {


#[test]
fn local_exact() {
fn local_exact_target() {
let env = Env::default().unwrap();
let opts = AssetsOptions::default();

let root = crate_root();
let root = Some(root.as_path());

// left hand of rule:
let targets = ["trg/", "trg//", "/trg/", "//trg/"]; // XXX: " too
let targets = ["trg/", "trg//", "/trg/", "//trg/"];
let targets_rel = ["trg/", "trg//"]; // non-abs targets
// right hand of rule:
let tests: HashSet<_> = vec!["Cargo.toml", "src/lib.rs"].into_iter().collect();
Expand Down Expand Up @@ -402,7 +402,7 @@ mod plan {

#[test]
#[cfg_attr(windows, should_panic)]
fn resolve_local_target() {
fn glob_local_target() {
let env = Env::default().unwrap();
let opts = AssetsOptions::default();

Expand Down

0 comments on commit 28d376f

Please sign in to comment.