Skip to content

Commit

Permalink
🐛⬆️ Fix a few bugs & bump dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
aksiome committed Apr 11, 2024
1 parent 1b93ef5 commit 3d1f03e
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 106 deletions.
221 changes: 129 additions & 92 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ edition = "2021"

[dependencies]
anyhow = "1.0"
clap = { version = "4.4", features = ["derive", "cargo"] }
clap = { version = "4.5", features = ["derive", "cargo"] }
console = "0.15"
derive_more = "0.99"
edit = "0.1"
env_logger = { version = "0.10", default-features = false }
env_logger = { version = "0.11", default-features = false }
fastanvil = "0"
fastnbt = "2"
flate2 = "1.0"
globset = "0.4"
ignore = "0.4"
indicatif = { version = "0.17", features = ["rayon"] }
inquire = { version = "0.6", features = ["console"], default-features = false }
inquire = { version = "0.7", features = ["console"], default-features = false }
log = "0.4"
path-absolutize = "3.1"
rayon = "1.7"
rayon = "1.10"
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.9"
zip = { version = "0.6", features = ["deflate", "time"], default-features = false }
Expand Down
10 changes: 10 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::path::{Path, PathBuf};

use globset::{Glob, GlobSet, GlobSetBuilder};
use ignore::overrides::{Override, OverrideBuilder};
use path_absolutize::Absolutize;
use serde::{Deserialize, Deserializer};

use crate::entries::ExtraEntry;
Expand Down Expand Up @@ -85,6 +86,15 @@ where

impl Config {
pub fn load(path: &Path, noprompt: bool) -> Option<Self> {
let conf_path = path.absolutize().unwrap();
let current_dir = std::env::current_dir().expect("could not get working dir");
std::env::set_current_dir(path.parent()?).expect("could not set working dir");
let config = Self::try_load(&conf_path, noprompt);
std::env::set_current_dir(current_dir).expect("could not set working dir");
config
}

fn try_load(path: &Path, noprompt: bool) -> Option<Self> {
std::fs::read_to_string(path).map_or_else(|err| {
log::error!("could not read the config file ({err})");
Self::try_edit(path, include_str!("../config.yaml"), noprompt)
Expand Down
2 changes: 1 addition & 1 deletion src/entries/datapack.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::PrefixPath;
use crate::utils::PathUtils;
use crate::utils;
use super::*;

Expand Down
1 change: 1 addition & 0 deletions src/entries/extra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde::Deserialize;
use super::*;

#[derive(Clone, Debug, Deserialize, From)]
#[serde(untagged)]
pub enum ExtraEntry {
Short(PathBuf),
Full(PathBuf, PathBuf),
Expand Down
2 changes: 1 addition & 1 deletion src/entries/file.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::PrefixPath;
use crate::utils::PathUtils;
use super::*;

#[derive(Clone, Debug, Deref, From)]
Expand Down
2 changes: 1 addition & 1 deletion src/entries/level.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::formats::{Level, NbtFormat};
use crate::utils::PrefixPath;
use crate::utils::PathUtils;
use super::*;

#[derive(Clone, Debug, Deref, From)]
Expand Down
2 changes: 1 addition & 1 deletion src/entries/region.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::formats::Region;
use crate::utils::PrefixPath;
use crate::utils::PathUtils;
use super::*;

#[derive(Clone, Debug, Deref, From)]
Expand Down
2 changes: 1 addition & 1 deletion src/entries/resourcepack.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::PrefixPath;
use crate::utils::PathUtils;
use crate::utils;
use super::*;

Expand Down
2 changes: 1 addition & 1 deletion src/entries/scoreboard.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::formats::{NbtFormat, Scoreboard};
use crate::utils::PrefixPath;
use crate::utils::PathUtils;
use super::*;

#[derive(Clone, Debug, Deref, From)]
Expand Down
4 changes: 4 additions & 0 deletions src/storage/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ impl InMemoryStorage for ZipStorage<Cursor<Vec<u8>>> {

impl FilesystemStorage for ZipStorage<File> {
fn new(path: &Path) -> Self {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).unwrap();
}

Self {
path: Some(path.to_owned()),
writer: Mutex::new(ZipWriter::new(File::create(path).unwrap())),
Expand Down
32 changes: 28 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::{Path, PathBuf};
use std::path::{Component, Path, PathBuf};
use std::str::FromStr;
use std::time::Duration;

Expand All @@ -9,13 +9,37 @@ use inquire::{Confirm, CustomUserError, Text};

use crate::storage::{InMemoryStorage, Storage, ZipStorage};

pub trait PrefixPath {
pub trait PathUtils {
fn prefix<P: AsRef<Path>>(&self, prefix: Option<P>) -> PathBuf;
fn normalize(&self) -> PathBuf;
}

impl PrefixPath for PathBuf {
impl PathUtils for PathBuf {
fn prefix<P: AsRef<Path>>(&self, prefix: Option<P>) -> PathBuf {
prefix.map_or_else(|| self.to_owned(), |p| p.as_ref().join(self))
match prefix {
Some(p) => p.as_ref().join(self),
None => self.to_owned(),
}.normalize()
}

fn normalize(&self) -> PathBuf {
let mut components = self.components().peekable();
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
components.next();
PathBuf::from(c.as_os_str())
} else {
PathBuf::new()
};
for component in components {
match component {
Component::Prefix(..) => unreachable!(),
Component::RootDir => ret.push(component.as_os_str()),
Component::CurDir => {}
Component::ParentDir => {ret.pop();},
Component::Normal(c) => ret.push(c),
}
}
ret
}
}

Expand Down

0 comments on commit 3d1f03e

Please sign in to comment.