Skip to content

Commit

Permalink
➕ Add syncing feature (closes #32)
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenixr-codes committed Apr 22, 2024
1 parent 0ce60bb commit 0264fce
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
66 changes: 64 additions & 2 deletions src/cli/sync.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
use super::build;
use super::prelude::*;
use crate::paths;
use crate::Project;
use clap::{ArgMatches, Command};
use libuuid::Uuid;
use std::fs;
use std::str::FromStr;
use std::{env, path::PathBuf, process::ExitCode};

const DEV_BP: &'static str = "development_behavior_packs";
Expand Down Expand Up @@ -48,17 +55,72 @@ mod location {
}
}

fn update(dev_dir: &PathBuf, projet_id: &Uuid) -> bool {
for pack in dev_dir.read_dir().expect("failed to read dir") {
match pack {
Ok(pack) => {
let path = pack.path();
if !path.is_dir() {
continue;
}
let fingerprint = path.join(paths::FINGERPRINT);
match fs::read_to_string(fingerprint) {
Ok(value) => {
return Uuid::from_str(&value).is_ok_and(|value| &value == projet_id);
}
Err(e) => log::error!("failed to read fingerprint file: {}", e),
}
}
Err(e) => log::error!("failed to read entry of pack direcrory: {}", e),
};
}
false
}

pub fn cmd() -> Command {
Command::new("sync").about("Update the associated packs in the Minecraft directories")
Command::new("sync")
.about("Update the associated packs in the Minecraft directories")
.arg_build_opts()
}

pub fn run(matches: &ArgMatches) -> ExitCode {
build::run(matches);
let id = Project::current().unwrap().id;

let com_mojang: PathBuf = match env::var_os("COM_MOJANG") {
Some(var) => PathBuf::from(var),
None => match location::get() {
Some(loc) => loc,
None => return ExitCode::FAILURE,
},
};
todo!("find packs with same id as current project and update them");

let mut updated_any = false;
for entry in com_mojang
.read_dir()
.expect("cannot read com_mojang directory")
{
match entry {
Ok(entry) => {
for dir in [DEV_BP, DEV_RP, DEV_SP] {
let p = entry.path().join(dir);
if p.is_dir() {
let updated = update(&p, &id);
if updated {
updated_any = true;
}
}
}
}
Err(e) => {
log::error!("Failed to read entry of com_mojang directory: {}", e);
}
}
}

if updated_any {
ExitCode::SUCCESS
} else {
ExitCode::FAILURE
}
}
3 changes: 3 additions & 0 deletions src/paths.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::env::current_dir;
use std::path::PathBuf;

/// The name of the file which contains the ID of the Allay project.
pub const FINGERPRINT: &'static str = ".allay-fingerprint";

/// Returns the path of the project root or [`None`] if an `allay.toml` file cannot be found.
pub fn try_root() -> Option<PathBuf> {
let mut now = current_dir().ok()?;
Expand Down
2 changes: 1 addition & 1 deletion src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl Project {
)?;

log::debug!("Adding fingerprint");
if let Err(e) = fs::write(&dest.join(".allay-fingerprint"), self.id) {
if let Err(e) = fs::write(&dest.join(paths::FINGERPRINT), self.id) {
log::error!("Failed to add fingerprint: {}", e);
};

Expand Down

0 comments on commit 0264fce

Please sign in to comment.