Skip to content

Commit

Permalink
Bump to 0.20; Read config from file
Browse files Browse the repository at this point in the history
  • Loading branch information
szeweq committed May 14, 2024
1 parent ba316f7 commit 96620cc
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 11 deletions.
10 changes: 6 additions & 4 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mc-repack"
version = "0.18.1"
version = "0.20.0"
edition = "2021"
authors = ["Szeweq"]
license = "MIT"
Expand All @@ -17,6 +17,7 @@ members = ["lib-core"]

[workspace.dependencies]
zip = {version = "^1.2.3", default-features = false}
toml = "^0.8.0"
serde = "1.0"
crossbeam-channel = "^0.5.8"
anyhow = "1.0"
Expand All @@ -29,9 +30,11 @@ doc = false
anyhow = ["dep:anyhow"]

[dependencies]
mc-repack-core = {version = "0.18.1", path = "lib-core"}
mc-repack-core = {version = "0.20.0", path = "lib-core"}
crossbeam-channel = {workspace = true}
anyhow = {optional = true, workspace = true}
serde = {workspace = true}
toml = {workspace = true, features = ["preserve_order"]}

# CLI Dependencies
clap = {version = "4.5", features = ["derive", "cargo"]}
Expand Down
4 changes: 2 additions & 2 deletions lib-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mc-repack-core"
version = "0.18.1"
version = "0.20.0"
edition = "2021"
authors = ["Szeweq"]
license = "MIT"
Expand Down Expand Up @@ -32,7 +32,7 @@ serde_json = {version = "^1.0.93", features = ["preserve_order"]}
oxipng = {optional = true, version = "^9.1.0", default-features = false, features = ["parallel"]}
flate2 = "^1.0.26"
json_comments = "0.2"
toml = {optional = true, version = "^0.8.0", features = ["preserve_order"]}
toml = {optional = true, workspace = true, features = ["preserve_order"]}
zopfli = {version = "^0.8.0", optional = true}
state = "0.6"

Expand Down
35 changes: 32 additions & 3 deletions src/bin/mc-repack/cli_args.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::path::PathBuf;
use std::{io, path::PathBuf};

use mc_repack_core::min;

use crate::config;


#[derive(Debug, clap::Parser)]
Expand All @@ -17,7 +21,11 @@ pub struct Args {

/// Use built-in blacklist for files
#[arg(short = 'b', long)]
pub use_blacklist: bool
pub use_blacklist: bool,

/// (Optional) Use custom .toml config file. If no path is provided, it will use `mc-repack.toml`
#[arg(short = 'c', long)]
pub config: Option<PathBuf>
}
pub struct RepackOpts {
pub silent: bool,
Expand All @@ -26,10 +34,31 @@ pub struct RepackOpts {
}
impl RepackOpts {
pub fn from_args(args: &Args) -> Self {
let cfgmap = mc_repack_core::cfg::ConfigMap::default();
match config::read_config(args.config.clone()) {
Ok(c) => {
if let Some(x) = c.json {
cfgmap.set::<min::json::MinifierJSON>(x);
}
if let Some(x) = c.nbt {
cfgmap.set::<min::nbt::MinifierNBT>(x);
}
if let Some(x) = c.png {
cfgmap.set::<min::png::MinifierPNG>(x);
}
if let Some(x) = c.toml {
cfgmap.set::<min::toml::MinifierTOML>(x);
}
}
Err(e) if e.kind() != io::ErrorKind::NotFound => {
eprintln!("Failed to read config: {e}");
}
_ => {}
}
Self {
silent: args.silent,
use_blacklist: args.use_blacklist,
cfgmap: mc_repack_core::cfg::ConfigMap::default()
cfgmap
}
}
}
28 changes: 28 additions & 0 deletions src/bin/mc-repack/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::{io, path::PathBuf};

use mc_repack_core::min;
use crate::Result_;

#[derive(serde::Deserialize)]
pub struct Config {
pub json: Option<min::json::JSONConfig>,
pub nbt: Option<min::nbt::NBTConfig>,
pub png: Option<min::png::PNGConfig>,
pub toml: Option<min::toml::TOMLConfig>
}

pub fn read_config(path: Option<PathBuf>) -> Result_<Config> {
let path = match path {
Some(p) => {
let meta = std::fs::metadata(&p)?;
if meta.is_dir() {
p.join("mc-repack.toml")
} else {
p
}
}
None => PathBuf::from("mc-repack.toml")
};
let f = std::fs::read_to_string(path)?;
toml::from_str(&f).map_err(io::Error::other)
}
1 change: 1 addition & 0 deletions src/bin/mc-repack/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use indicatif::{ProgressBar, ProgressStyle, MultiProgress};
use mc_repack_core::{entry::{self, ZipEntryReader, ZipEntrySaver}, errors::{EntryRepackError, ErrorCollector}, fop::FileType, optimizer::{optimize_with, ProgressState}};

mod cli_args;
mod config;

#[cfg(not(feature = "anyhow"))]
type Error_ = io::Error;
Expand Down

0 comments on commit 96620cc

Please sign in to comment.