Skip to content

Commit

Permalink
Implement recursive, resolution-time dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
theRookieCoder committed Nov 17, 2024
1 parent c6c7a1d commit 5ce2df9
Show file tree
Hide file tree
Showing 11 changed files with 605 additions and 241 deletions.
556 changes: 414 additions & 142 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,21 @@ rfd = { version = "0.14", optional = true, default-features = false, features =
reqwest = { version = "0.12", default-features = false, features = [
"rustls-tls",
] }
tokio = { version = "1.40", default-features = false, features = [
tokio = { version = "1.41", default-features = false, features = [
"rt-multi-thread",
"macros",
] }
clap = { version = "4.5", features = ["derive"] }
clap_complete = "4.5"
serde_json = "1.0"
indicatif = "0.17"
octocrab = "0.41"
octocrab = "0.42"
fs_extra = "1.3"
ferinth = "2.11"
colored = "2.1"
futures = "0.3"
inquire = "0.7"
libium = { git = "https://github.com/gorilla-devs/libium", rev = "b7d2cd767b840313bc71dca643e4252c4e16dd3e" }
# libium = { path = "../libium" }
# libium = { git = "https://github.com/gorilla-devs/libium", rev = "b7d2cd767b840313bc71dca643e4252c4e16dd3e" }
libium = { path = "../libium" }
# libium = "1.32"
anyhow = "1.0"
furse = "1.5"
Expand Down
9 changes: 4 additions & 5 deletions src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use fs_extra::{
dir::{copy as copy_dir, CopyOptions as DirCopyOptions},
file::{move_file, CopyOptions as FileCopyOptions},
};
use futures::{stream::FuturesUnordered, StreamExt as _};
use indicatif::ProgressBar;
use libium::{iter_ext::IterExt as _, upgrade::DownloadData};
use std::{
Expand All @@ -17,7 +16,7 @@ use std::{
sync::{Arc, Mutex},
time::Duration,
};
use tokio::sync::Semaphore;
use tokio::{sync::Semaphore, task::JoinSet};

/// Check the given `directory`
///
Expand Down Expand Up @@ -112,7 +111,7 @@ pub async fn download(
.lock()
.expect("Mutex poisoned")
.enable_steady_tick(Duration::from_millis(100));
let mut tasks = FuturesUnordered::new();
let mut tasks = JoinSet::new();
let semaphore = Arc::new(Semaphore::new(
*PARALLEL_NETWORK.get_or_init(|| DEFAULT_PARALLEL_NETWORK),
));
Expand All @@ -124,7 +123,7 @@ pub async fn download(
let client = client.clone();
let output_dir = output_dir.clone();

tasks.push(async move {
tasks.spawn(async move {
let _permit = semaphore.acquire_owned().await?;

let (length, filename) = downloadable
Expand All @@ -150,7 +149,7 @@ pub async fn download(
Ok::<(), Error>(())
});
}
while let Some(res) = tasks.next().await {
for res in tasks.join_all().await {
res?;
}
Arc::try_unwrap(progress_bar)
Expand Down
15 changes: 10 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const CROSS: &str = "×";
static TICK: LazyLock<ColoredString> = LazyLock::new(|| "✓".green());

pub static PARALLEL_NETWORK: OnceLock<usize> = OnceLock::new();
// Bump up the default to ~50
pub const DEFAULT_PARALLEL_NETWORK: usize = 10;

/// Indicatif themes
Expand Down Expand Up @@ -184,7 +185,9 @@ async fn actual_main(mut cli_app: Ferium) -> Result<()> {
(filename, None, None) => {
println!("{} {}", "Unknown file:".yellow(), filename.dimmed());
}
(_, Some(mr_id), None) => send_ids.push(ModIdentifier::ModrinthProject(mr_id)),
(_, Some(mr_id), None) => {
send_ids.push(ModIdentifier::ModrinthProject(mr_id));
}
(_, None, Some(cf_id)) => {
send_ids.push(ModIdentifier::CurseForgeProject(cf_id));
}
Expand Down Expand Up @@ -265,13 +268,15 @@ async fn actual_main(mut cli_app: Ferium) -> Result<()> {
format!("{} {:8}", "CF".red(), id.to_string().dimmed()),
ModIdentifier::ModrinthProject(id) =>
format!("{} {:8}", "MR".green(), id.dimmed()),
ModIdentifier::GitHubRepository(_) => "GH".purple().to_string(),
ModIdentifier::GitHubRepository(..) => "GH".purple().to_string(),
_ => todo!(),
},
match &mod_.identifier {
ModIdentifier::ModrinthProject(_)
| ModIdentifier::CurseForgeProject(_) => mod_.name.bold().to_string(),
ModIdentifier::GitHubRepository(id) =>
format!("{}/{}", id.0.dimmed(), id.1.bold()),
ModIdentifier::GitHubRepository(owner, repo) =>
format!("{}/{}", owner.dimmed(), repo.bold()),
_ => todo!(),
},
);
}
Expand Down Expand Up @@ -458,7 +463,7 @@ fn get_active_profile(config: &mut Config) -> Result<&mut Profile> {
bail!("There are no profiles configured, add a profile using `ferium profile create`")
}
1 => config.active_profile = 0,
n if n <= config.active_profile => {
n if config.active_profile >= n => {
println!(
"{}",
"Active profile specified incorrectly, please pick a profile to use"
Expand Down
23 changes: 10 additions & 13 deletions src/subcommands/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use anyhow::{Context as _, Result};
use colored::Colorize as _;
use ferinth::structures::{project::Project, user::TeamMember};
use furse::structures::mod_structs::Mod;
use futures::{stream::FuturesUnordered, StreamExt as _};
use libium::{
config::structs::{ModIdentifier, Profile},
iter_ext::IterExt as _,
CURSEFORGE_API, GITHUB_API, MODRINTH_API,
};
use octocrab::models::{repos::Release, Repository};
use tokio::task::JoinSet;

enum Metadata {
CF(Mod),
Expand All @@ -31,7 +31,7 @@ impl Metadata {
Metadata::CF(p) => ModIdentifier::CurseForgeProject(p.id),
Metadata::MD(p, _) => ModIdentifier::ModrinthProject(p.id.clone()),
Metadata::GH(p, _) => {
ModIdentifier::GitHubRepository((p.owner.clone().unwrap().login, p.name.clone()))
ModIdentifier::GitHubRepository(p.owner.clone().unwrap().login, p.name.clone())
}
}
}
Expand All @@ -42,26 +42,23 @@ pub async fn verbose(profile: &mut Profile, markdown: bool) -> Result<()> {
eprint!("Querying metadata... ");
}

let mut tasks = FuturesUnordered::new();
let mut tasks = JoinSet::new();
let mut mr_ids = Vec::new();
let mut cf_ids = Vec::new();
for mod_ in &profile.mods {
match mod_.identifier.clone() {
ModIdentifier::CurseForgeProject(project_id) => cf_ids.push(project_id),
ModIdentifier::ModrinthProject(project_id) => mr_ids.push(project_id),
ModIdentifier::GitHubRepository((owner, repo)) => {
tasks.push(async {
ModIdentifier::GitHubRepository(owner, repo) => {
let repo = GITHUB_API.repos(owner, repo);
tasks.spawn(async move {
Ok::<_, anyhow::Error>((
GITHUB_API.repos(&owner, &repo).get().await?,
GITHUB_API
.repos(owner, repo)
.releases()
.list()
.send()
.await?,
repo.get().await?,
repo.releases().list().send().await?,
))
});
}
_ => todo!(),
}
}

Expand Down Expand Up @@ -93,7 +90,7 @@ pub async fn verbose(profile: &mut Profile, markdown: bool) -> Result<()> {
for project in cf_projects {
metadata.push(Metadata::CF(project));
}
while let Some(res) = tasks.next().await {
for res in tasks.join_all().await {
let (repo, releases) = res?;
metadata.push(Metadata::GH(repo, releases.items));
}
Expand Down
9 changes: 5 additions & 4 deletions src/subcommands/modpack/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ pub fn switch(config: &mut Config, modpack_name: Option<String>) -> Result<()> {
})
.collect_vec();

if let Ok(selection) = Select::new("Select which modpack to switch to", modpack_info)
.with_starting_cursor(config.active_modpack)
.raw_prompt()
{
let mut select = Select::new("Select which modpack to switch to", modpack_info);
if config.active_modpack < config.modpacks.len() {
select.starting_cursor = config.active_modpack;
}
if let Ok(selection) = select.raw_prompt() {
config.active_modpack = selection.index;
}
Ok(())
Expand Down
8 changes: 4 additions & 4 deletions src/subcommands/modpack/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::{
};
use anyhow::{Context as _, Result};
use colored::Colorize as _;
use futures::{stream::FuturesUnordered, StreamExt as _};
use indicatif::ProgressBar;
use libium::{
config::structs::{Modpack, ModpackIdentifier},
Expand All @@ -22,6 +21,7 @@ use std::{
path::{Path, PathBuf},
time::Duration,
};
use tokio::task::JoinSet;

pub async fn upgrade(modpack: &'_ Modpack) -> Result<()> {
let mut to_download: Vec<DownloadData> = Vec::new();
Expand Down Expand Up @@ -58,7 +58,7 @@ pub async fn upgrade(modpack: &'_ Modpack) -> Result<()> {
let files = CURSEFORGE_API.get_files(file_ids).await?;
println!("{} Fetched {} mods", &*TICK, files.len());

let mut tasks = FuturesUnordered::new();
let mut tasks = JoinSet::new();
let mut msg_shown = false;
for file in files {
match try_from_cf_file(file) {
Expand All @@ -81,7 +81,7 @@ pub async fn upgrade(modpack: &'_ Modpack) -> Result<()> {
println!("\n{}", "The following mod(s) have denied 3rd parties such as Ferium from downloading it".red().bold());
}
msg_shown = true;
tasks.push(async move {
tasks.spawn(async move {
let project = CURSEFORGE_API.get_mod(mod_id).await?;
eprintln!(
"- {}
Expand All @@ -97,7 +97,7 @@ pub async fn upgrade(modpack: &'_ Modpack) -> Result<()> {
}
}

while let Some(res) = tasks.next().await {
for res in tasks.join_all().await {
res?;
}

Expand Down
9 changes: 5 additions & 4 deletions src/subcommands/profile/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ pub fn switch(config: &mut Config, profile_name: Option<String>) -> Result<()> {
})
.collect_vec();

if let Ok(selection) = Select::new("Select which profile to switch to", profile_info)
.with_starting_cursor(config.active_profile)
.raw_prompt()
{
let mut select = Select::new("Select which profile to switch to", profile_info);
if config.active_profile < config.profiles.len() {
select.starting_cursor = config.active_profile;
}
if let Ok(selection) = select.raw_prompt() {
config.active_profile = selection.index;
}
Ok(())
Expand Down
9 changes: 6 additions & 3 deletions src/subcommands/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ pub fn remove(profile: &mut Profile, to_remove: Vec<String>) -> Result<()> {
match &mod_.identifier {
ModIdentifier::CurseForgeProject(id) => format!("CF {:8}", id.to_string()),
ModIdentifier::ModrinthProject(id) => format!("MR {id:8}"),
ModIdentifier::GitHubRepository(_) => "GH".to_string(),
ModIdentifier::GitHubRepository(..) => "GH".to_string(),
_ => todo!(),
},
match &mod_.identifier {
ModIdentifier::ModrinthProject(_) | ModIdentifier::CurseForgeProject(_) =>
mod_.name.clone(),
ModIdentifier::GitHubRepository(id) => format!("{}/{}", id.0, id.1),
ModIdentifier::GitHubRepository(owner, repo) => format!("{owner}/{repo}"),
_ => todo!(),
},
)
})
Expand All @@ -44,9 +46,10 @@ pub fn remove(profile: &mut Profile, to_remove: Vec<String>) -> Result<()> {
|| match &mod_.identifier {
ModIdentifier::CurseForgeProject(id) => id.to_string() == to_remove,
ModIdentifier::ModrinthProject(id) => id == &to_remove,
ModIdentifier::GitHubRepository((owner, name)) => {
ModIdentifier::GitHubRepository(owner, name) => {
format!("{owner}/{name}").eq_ignore_ascii_case(&to_remove)
}
_ => todo!(),
}
}) {
items_to_remove.push(index);
Expand Down
Loading

0 comments on commit 5ce2df9

Please sign in to comment.