Skip to content

Commit

Permalink
Merge pull request #9 from ink0rr/dev
Browse files Browse the repository at this point in the history
Various fixes
  • Loading branch information
ink0rr authored Nov 14, 2023
2 parents 758e332 + 89105b3 commit a3bd871
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 33 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.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[[bin]]
name = "rgl"

[package]
name = "rgl"
version = "0.1.7"
version = "0.1.8"
edition = "2021"

[[bin]]
name = "rgl"

[dependencies]
anyhow = "1.0.75"
clap = { version = "4.3.22", features = ["cargo"] }
Expand Down
13 changes: 7 additions & 6 deletions src/rgl/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,18 @@ fn copy_cached(from: &Path, to: &Path) -> Result<()> {
let entry = entry?;
let from = entry.path();
let to = to.join(entry.file_name());
if diff(&from, &to)? {
let from_is_dir = from.is_dir();
let to_is_dir = to.is_dir();
if !from_is_dir && !to_is_dir && diff(&from, &to)? {
return Ok(());
}
let to_dir = to.is_dir();
if from.is_dir() {
if !to_dir {
if from_is_dir {
if !to_is_dir {
fs::remove_file(&to)?;
}
return copy_cached(&from, &to);
}
if to_dir {
if to_is_dir {
fs::remove_dir_all(&to)?;
}
fs::copy(&from, &to)?;
Expand Down Expand Up @@ -87,7 +88,7 @@ fn cleanup(from: &Path, to: &Path) -> Result<()> {
}

/// Compare two file contents. Return true if they are identical.
fn diff(a: impl AsRef<Path>, b: impl AsRef<Path>) -> Result<bool> {
fn diff(a: &Path, b: &Path) -> Result<bool> {
let a = fs::File::open(a);
let b = fs::File::open(b);
if a.is_err() || b.is_err() {
Expand Down
2 changes: 1 addition & 1 deletion src/rgl/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl RunContext {
}

pub fn watch_project_files(&self) -> Result<()> {
let mut file_watcher = FileWatcher::new();
let mut file_watcher = FileWatcher::new()?;

file_watcher.watch(&self.data_path)?;
file_watcher.watch(&self.behavior_pack)?;
Expand Down
8 changes: 4 additions & 4 deletions src/rgl/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ pub fn run_or_watch(profile_name: &str, watch: bool, cached: bool) -> Result<()>
measure_time!("Export project", {
info!(
"Moving files to target location: \n\
\tBP: {} \n\
\tRP: {}",
\tBP: {} \n\
\tRP: {}",
bp.display(),
rp.display()
);
let export: Result<()> = {
let export = || -> Result<()> {
move_dir(temp_bp, bp)?;
move_dir(temp_rp, rp)
};
export.context("Failed to export project")?;
export().context("Failed to export project")?;
});

info!("Successfully ran the <b>{profile_name}</> profile");
Expand Down
2 changes: 1 addition & 1 deletion src/rgl/file_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rayon::prelude::*;
use std::{fs, io, path::Path};

fn copy_dir_impl(from: &Path, to: &Path) -> Result<()> {
fs::create_dir_all(&to)?;
fs::create_dir_all(to)?;
fs::read_dir(from)?
.par_bridge()
.map(|entry| -> Result<()> {
Expand Down
11 changes: 4 additions & 7 deletions src/rgl/file_watcher.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::error;
use anyhow::{Context, Result};
use notify::{Error, RecommendedWatcher, RecursiveMode};
use notify_debouncer_mini::{new_debouncer, DebouncedEvent, Debouncer};
Expand All @@ -10,13 +9,11 @@ pub struct FileWatcher {
}

impl FileWatcher {
pub fn new() -> Self {
pub fn new() -> Result<Self> {
let (tx, rx) = std::sync::mpsc::channel();
let debouncer = new_debouncer(Duration::from_millis(100), None, tx).unwrap_or_else(|err| {
error!("Failed to create file watcher: {}", err);
std::process::exit(1);
});
Self { rx, debouncer }
let debouncer = new_debouncer(Duration::from_millis(100), None, tx)
.context("Failed to create file watcher")?;
Ok(Self { rx, debouncer })
}

pub fn watch(&mut self, path: &str) -> Result<()> {
Expand Down
22 changes: 17 additions & 5 deletions src/rgl/filter_installer.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::{
copy_dir, empty_dir, get_filter_cache_dir, read_json, resolve_url, rimraf, write_json,
FilterRemote, Subprocess,
copy_dir, empty_dir, get_filter_cache_dir, move_dir, read_json, resolve_url, rimraf,
write_json, FilterRemote, Subprocess,
};
use crate::{info, warn};
use anyhow::{bail, Result};
use semver::Version;
use serde_json::Value;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

pub struct FilterInstaller {
pub name: String,
Expand Down Expand Up @@ -54,7 +54,7 @@ impl FilterInstaller {
Ok(Self { name, url, git_ref })
}

pub fn install(&self, force: bool) -> Result<bool> {
pub fn install(&self, data_path: &Path, force: bool) -> Result<bool> {
let filter_dir = PathBuf::from(".regolith")
.join("cache")
.join("filters")
Expand All @@ -73,7 +73,12 @@ impl FilterInstaller {
let https_url = format!("https://{}", self.url);
let cache_dir = get_filter_cache_dir(&https_url)?;

if !cache_dir.exists() {
if cache_dir.exists() {
Subprocess::new("git")
.args(vec!["fetch", "--all"])
.current_dir(&cache_dir)
.run_silent()?;
} else {
empty_dir(&cache_dir)?;
Subprocess::new("git")
.args(vec!["clone", &https_url, "."])
Expand All @@ -88,6 +93,13 @@ impl FilterInstaller {

copy_dir(cache_dir.join(&self.name), &filter_dir)?;

let filter_data = filter_dir.join("data");
let target_path = data_path.join(&self.name);
if filter_data.is_dir() && !target_path.exists() {
info!("Moving filter data to <b>{}</>", target_path.display());
move_dir(&filter_data, target_path)?;
}

let filter_config_path = filter_dir.join("filter.json");
let mut filter_config = read_json::<Value>(&filter_config_path)?;
filter_config["version"] = ref_to_version(&self.git_ref).into();
Expand Down
7 changes: 5 additions & 2 deletions src/rgl/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ use super::{ref_to_version, Config, FilterDefinition, FilterInstaller, RemoteFil
use crate::info;
use anyhow::Result;
use semver::Version;
use std::path::Path;

pub fn install_filters(force: bool) -> Result<()> {
let config = Config::load()?;
let data_path = Path::new(&config.regolith.data_path);
for (name, def) in config.regolith.filter_definitions {
if let FilterDefinition::Remote(def) = def {
info!("Installing filter <b>{}</>...", name);
let git_ref = Version::parse(&def.version)
.map(|version| format!("{name}-{version}"))
.unwrap_or(def.version);
let filter = FilterInstaller::new(name, def.url, git_ref)?;
filter.install(force)?;
filter.install(data_path, force)?;
}
}
info!("Successfully installed all filters");
Expand All @@ -21,10 +23,11 @@ pub fn install_filters(force: bool) -> Result<()> {

pub fn install_add(filters: Vec<&String>, force: bool) -> Result<()> {
let mut config = Config::load()?;
let data_path = Path::new(&config.regolith.data_path);
for arg in filters {
info!("Installing filter <b>{}</>...", arg);
let filter = FilterInstaller::from_arg(arg)?;
if filter.install(force)? {
if filter.install(data_path, force)? {
info!("Filter <b>{}</> successfully installed", filter.name);
let version = ref_to_version(&filter.git_ref);
config.regolith.filter_definitions.insert(
Expand Down
5 changes: 3 additions & 2 deletions src/rgl/profile.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::{find_mojang_dir, RunContext};
use crate::{info, measure_time};
use anyhow::{bail, Context, Result};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{collections::HashMap, fs, path::PathBuf};
use std::{fs, path::PathBuf};

#[derive(Serialize, Deserialize)]
pub struct Profile {
Expand All @@ -25,7 +26,7 @@ pub enum FilterRunner {
#[serde(skip_serializing_if = "Option::is_none")]
arguments: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
settings: Option<HashMap<String, Value>>,
settings: Option<IndexMap<String, Value>>,
},
ProfileFilter {
#[serde(rename = "profile")]
Expand Down

0 comments on commit a3bd871

Please sign in to comment.