Skip to main content

conduit_cli/core/installer/
sync.rs

1use crate::core::error::CoreResult;
2use crate::core::events::{CoreCallbacks, CoreEvent};
3use crate::core::installer::download::download_to_path;
4use crate::core::paths::CorePaths;
5use crate::lock::LockedMod;
6use std::path::Path;
7
8pub async fn sync_from_lock(
9    paths: &CorePaths,
10    locked_mods: impl IntoIterator<Item = &'_ LockedMod>,
11    callbacks: &mut dyn CoreCallbacks,
12) -> CoreResult<()> {
13    crate::core::installer::resolve::ensure_dirs(paths)?;
14
15    for locked_mod in locked_mods {
16        install_from_lock(locked_mod, paths.cache_dir(), paths.mods_dir(), callbacks).await?;
17    }
18
19    Ok(())
20}
21
22async fn install_from_lock(
23    mod_data: &LockedMod,
24    cache_dir: &Path,
25    mods_dir: &Path,
26    callbacks: &mut dyn CoreCallbacks,
27) -> CoreResult<()> {
28    let cached_path = cache_dir.join(format!("{}.jar", mod_data.hash));
29    let dest_path = mods_dir.join(&mod_data.filename);
30
31    if dest_path.exists() {
32        return Ok(());
33    }
34
35    if !cached_path.exists() {
36        download_to_path(&mod_data.url, &cached_path, &mod_data.filename, callbacks).await?;
37    }
38
39    crate::core::installer::resolve::hard_link_jar(&cached_path, &dest_path)?;
40
41    callbacks.on_event(CoreEvent::LinkedFile {
42        filename: mod_data.filename.clone(),
43    });
44
45    Ok(())
46}