|
1 | 1 | use crate::{THEME, TICK}; |
2 | | -use anyhow::{anyhow, Result}; |
| 2 | +use anyhow::{anyhow, bail, Result}; |
3 | 3 | use dialoguer::Confirm; |
4 | 4 | use ferinth::structures::version_structs::DependencyType; |
5 | 5 | use ferinth::Ferinth; |
6 | 6 | use furse::{structures::file_structs::FileRelationType, Furse}; |
7 | | -use libium::{add, config, upgrade}; |
| 7 | +use libium::{ |
| 8 | + add, |
| 9 | + config::{self, structs::ModIdentifier}, |
| 10 | + upgrade, |
| 11 | +}; |
8 | 12 | use octocrab::repos::RepoHandler; |
9 | 13 | use std::sync::Arc; |
10 | 14 |
|
@@ -68,28 +72,36 @@ pub async fn modrinth( |
68 | 72 | } else { |
69 | 73 | break; |
70 | 74 | }; |
71 | | - // Check if the dependency has already been added |
72 | | - if !profile.mods.iter().any(|mod_| { |
73 | | - config::structs::ModIdentifier::ModrinthProject(id.clone()) == mod_.identifier |
74 | | - }) { |
75 | | - // If it's required, add it without asking |
76 | | - if dependency.dependency_type == DependencyType::Required { |
77 | | - eprint!("Adding required dependency... "); |
| 75 | + // If it's required, add it without asking |
| 76 | + if dependency.dependency_type == DependencyType::Required { |
| 77 | + eprint!("Adding required dependency... "); |
| 78 | + match add::modrinth(modrinth.clone(), &id, profile, None, None).await { |
| 79 | + Ok(project) => println!("{} ({})", *TICK, project.title), |
| 80 | + Err(err) => { |
| 81 | + if matches!(err, add::Error::AlreadyAdded) { |
| 82 | + println!("{} Already added", *TICK); |
| 83 | + } else { |
| 84 | + bail!(err); |
| 85 | + } |
| 86 | + }, |
| 87 | + }; |
| 88 | + } else if dependency.dependency_type == DependencyType::Optional { |
| 89 | + let project = modrinth.get_project(&id).await?; |
| 90 | + // If it is not already added: |
| 91 | + if profile.mods.iter().any(|mod_| { |
| 92 | + mod_.name == project.title |
| 93 | + || ModIdentifier::ModrinthProject(id.clone()) == mod_.identifier |
| 94 | + // And the user wants to add it: |
| 95 | + }) && Confirm::with_theme(&*THEME) |
| 96 | + .with_prompt(format!( |
| 97 | + "Add optional dependency {} (https://modrinth.com/mod/{})?", |
| 98 | + project.title, project.slug |
| 99 | + )) |
| 100 | + .interact()? |
| 101 | + { |
| 102 | + eprint!("Adding optional dependency... "); |
78 | 103 | let project = add::modrinth(modrinth.clone(), &id, profile, None, None).await?; |
79 | 104 | println!("{} ({})", *TICK, project.title); |
80 | | - } else if dependency.dependency_type == DependencyType::Optional { |
81 | | - let project = modrinth.get_project(&id).await?; |
82 | | - let should_add = Confirm::with_theme(&*THEME) |
83 | | - .with_prompt(format!( |
84 | | - "Add optional dependency {} (https://modrinth.com/mod/{})?", |
85 | | - project.title, project.slug |
86 | | - )) |
87 | | - .interact()?; |
88 | | - if should_add { |
89 | | - eprint!("Adding optional dependency... "); |
90 | | - let project = add::modrinth(modrinth.clone(), &id, profile, None, None).await?; |
91 | | - println!("{} ({})", *TICK, project.title); |
92 | | - } |
93 | 105 | } |
94 | 106 | } |
95 | 107 | } |
@@ -125,31 +137,35 @@ pub async fn curseforge( |
125 | 137 | println!("{} ({})", *TICK, project.name); |
126 | 138 | for dependency in &latest_file.dependencies { |
127 | 139 | let id = dependency.mod_id; |
128 | | - // Check if the dependency has already been added |
129 | | - if !profile |
130 | | - .mods |
131 | | - .iter() |
132 | | - .any(|mod_| config::structs::ModIdentifier::CurseForgeProject(id) == mod_.identifier) |
133 | | - { |
134 | | - // If it's required, add it without asking |
135 | | - if dependency.relation_type == FileRelationType::RequiredDependency { |
136 | | - eprint!("Adding required dependency... "); |
| 140 | + // If it's required, add it without asking |
| 141 | + if dependency.relation_type == FileRelationType::RequiredDependency { |
| 142 | + eprint!("Adding required dependency... "); |
| 143 | + match add::curseforge(curseforge.clone(), id, profile, None, None).await { |
| 144 | + Ok(project) => println!("{} ({})", *TICK, project.name), |
| 145 | + Err(err) => { |
| 146 | + if matches!(err, add::Error::AlreadyAdded) { |
| 147 | + println!("{} Already added", *TICK); |
| 148 | + } else { |
| 149 | + bail!(err); |
| 150 | + } |
| 151 | + }, |
| 152 | + }; |
| 153 | + } else if dependency.relation_type == FileRelationType::OptionalDependency { |
| 154 | + let project = curseforge.get_mod(id).await?; |
| 155 | + // If it is not already added: |
| 156 | + if !profile.mods.iter().any(|mod_| { |
| 157 | + mod_.name == project.name || ModIdentifier::CurseForgeProject(id) == mod_.identifier |
| 158 | + // And the user wants to add it: |
| 159 | + }) && Confirm::with_theme(&*THEME) |
| 160 | + .with_prompt(format!( |
| 161 | + "Add optional dependency {} ({})?", |
| 162 | + project.name, project.links.website_url |
| 163 | + )) |
| 164 | + .interact()? |
| 165 | + { |
| 166 | + eprint!("Adding optional dependency... "); |
137 | 167 | let project = add::curseforge(curseforge.clone(), id, profile, None, None).await?; |
138 | 168 | println!("{} ({})", *TICK, project.name); |
139 | | - } else if dependency.relation_type == FileRelationType::OptionalDependency { |
140 | | - let project = curseforge.get_mod(id).await?; |
141 | | - let should_add = Confirm::with_theme(&*THEME) |
142 | | - .with_prompt(format!( |
143 | | - "Add optional dependency {} ({})?", |
144 | | - project.name, project.links.website_url |
145 | | - )) |
146 | | - .interact()?; |
147 | | - if should_add { |
148 | | - eprint!("Adding optional dependency... "); |
149 | | - let project = |
150 | | - add::curseforge(curseforge.clone(), id, profile, None, None).await?; |
151 | | - println!("{} ({})", *TICK, project.name); |
152 | | - } |
153 | 169 | } |
154 | 170 | } |
155 | 171 | } |
|
0 commit comments