Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat matching pattern assets #14

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ serde = { version = "1.0.203", features = ["derive"] }
semver = { version = "1.0.23", features = ["serde"] }
sha2 = "0.10.8"
plotters = "0.3.6"
open = "5.2.0"
open = "5.2.0"
glob = "0.3.1"
1 change: 1 addition & 0 deletions src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl SubCommandTrait for Init {
authors: Vec::new(),
use_autoplay_for_multimedia: None,
rdp_epsilon: None,
assets: Some(vec![]),
plugins: Some(vec![]),
};

Expand Down
42 changes: 19 additions & 23 deletions src/cli/prebuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use cazan_common::{image::ImageEdgesParser, triangulation::triangulate};

use argh::FromArgs;
use cprint::{ceprintln, cformat, cprintln};
use glob::glob;
use image::GenericImageView;
use plotters::prelude::*;
use serde_json::{json, Value};
Expand All @@ -31,8 +32,12 @@ const DEFAULT_EPSILON: f64 = 3.0;
description = "pre-build the assets of your project"
)]
pub struct PreBuild {
#[argh(option, short = 'a', description = "asset directories")]
pub asset_dirs: Vec<String>,
#[argh(
option,
short = 'a',
description = "the assets of your project (ex: assets/sprite-*.png"
)]
pub assets: Vec<String>,

#[argh(
option,
Expand All @@ -55,20 +60,6 @@ pub struct PreBuild {
pub open: bool,
}

fn read_dir_recursive(dir: &std::path::Path) -> Vec<std::path::PathBuf> {
let mut files = Vec::new();
for entry in fs::read_dir(dir).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path.is_dir() {
files.extend(read_dir_recursive(&path));
} else {
files.push(path);
}
}
files
}

impl SubCommandTrait for PreBuild {
fn run(&self) -> ExitCode {
if self.open && !self.preview {
Expand All @@ -92,18 +83,23 @@ impl SubCommandTrait for PreBuild {
cprintln!("Warning lock file is not up-to-date with cazan.json. To update it use `cazan lock`" => Yellow);
}

let files = self
.asset_dirs
.iter()
.flat_map(|dir| read_dir_recursive(dir.as_ref()))
.collect::<Vec<PathBuf>>();
let assets = if self.assets.is_empty() {
config.assets.unwrap_or_default()
} else {
self.assets.iter().map(|s| s.as_str()).collect()
};

let files: Vec<PathBuf> = files
let files: Vec<PathBuf> = assets
.iter()
.flat_map(|pattern| glob(pattern).expect("Failed to read pattern"))
.map(|entry| entry.unwrap_or_else(|_| PathBuf::new()))
.filter(|file| file.extension().map_or(false, |ext| ext == "png"))
.cloned()
.collect();

if files.is_empty() {
return ExitCode::SUCCESS;
}

let mut map = serde_json::Map::<String, Value>::new();
let terminal: Arc<Mutex<SubTerminal>> =
Arc::new(Mutex::new(SubTerminal::new(files.len() as u16)));
Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Config<'a> {
pub use_autoplay_for_multimedia: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub rdp_epsilon: Option<f64>,
pub assets: Option<Vec<&'a str>>,
pub plugins: Option<Vec<PluginConfig<'a>>>,
}

Expand Down
Loading