Profile mods page (#119)

* Finish profile mods page

* commit missing

* finish pr

* Run lint
This commit is contained in:
Geometrically
2023-05-18 17:12:36 -07:00
committed by GitHub
parent 16407060f0
commit 4df7605b8d
9 changed files with 206 additions and 95 deletions

View File

@@ -125,7 +125,9 @@ pub async fn install(path: &Path) -> crate::Result<()> {
#[tracing::instrument]
#[theseus_macros::debug_pin]
pub async fn update_all(profile_path: &Path) -> crate::Result<()> {
pub async fn update_all(
profile_path: &Path,
) -> crate::Result<HashMap<PathBuf, PathBuf>> {
if let Some(profile) = get(profile_path, None).await? {
let loading_bar = init_loading(
LoadingBarType::ProfileUpdate {
@@ -137,24 +139,49 @@ pub async fn update_all(profile_path: &Path) -> crate::Result<()> {
)
.await?;
let keys = profile
.projects
.into_iter()
.filter(|(_, project)| {
matches!(
&project.metadata,
ProjectMetadata::Modrinth {
update_version: Some(_),
..
}
)
})
.map(|x| x.0)
.collect::<Vec<_>>();
let len = keys.len();
let map = Arc::new(RwLock::new(HashMap::new()));
use futures::StreamExt;
loading_try_for_each_concurrent(
futures::stream::iter(profile.projects.keys())
.map(Ok::<&PathBuf, crate::Error>),
futures::stream::iter(keys).map(Ok::<PathBuf, crate::Error>),
None,
Some(&loading_bar),
100.0,
profile.projects.keys().len(),
len,
None,
|project| async move {
let _ = update_project(profile_path, project).await?;
|project| async {
let map = map.clone();
Ok(())
async move {
let new_path =
update_project(profile_path, &project).await?;
map.write().await.insert(project, new_path);
Ok(())
}
.await
},
)
.await?;
Ok(())
Ok(Arc::try_unwrap(map).unwrap().into_inner())
} else {
Err(crate::ErrorKind::UnmanagedProfileError(
profile_path.display().to_string(),
@@ -271,11 +298,9 @@ pub async fn add_project_from_path(
pub async fn toggle_disable_project(
profile: &Path,
project: &Path,
) -> crate::Result<()> {
) -> crate::Result<PathBuf> {
if let Some(profile) = get(profile, None).await? {
profile.toggle_disable_project(project).await?;
Ok(())
Ok(profile.toggle_disable_project(project).await?)
} else {
Err(crate::ErrorKind::UnmanagedProfileError(
profile.display().to_string(),

View File

@@ -193,7 +193,7 @@ impl Profile {
let paths = profile.get_profile_project_paths()?;
let projects = crate::state::infer_data_from_files(
profile,
profile.clone(),
paths,
state.directories.caches_dir(),
&state.io_semaphore,
@@ -205,6 +205,14 @@ impl Profile {
if let Some(profile) = new_profiles.0.get_mut(&path) {
profile.projects = projects;
}
emit_profile(
profile.uuid,
profile.path,
&profile.metadata.name,
ProfilePayloadType::Synced,
)
.await?;
} else {
tracing::warn!(
"Unable to fetch single profile projects: path {path:?} invalid",
@@ -409,7 +417,7 @@ impl Profile {
pub async fn toggle_disable_project(
&self,
path: &Path,
) -> crate::Result<()> {
) -> crate::Result<PathBuf> {
let state = State::get().await?;
if let Some(mut project) = {
let mut profiles = state.profiles.write().await;
@@ -425,6 +433,12 @@ impl Profile {
if path.extension().map_or(false, |ext| ext == "disabled") {
project.disabled = false;
new_path.set_file_name(
path.file_name()
.unwrap_or_default()
.to_string_lossy()
.replace(".disabled", ""),
);
} else {
new_path.set_file_name(format!(
"{}.disabled",
@@ -437,17 +451,17 @@ impl Profile {
let mut profiles = state.profiles.write().await;
if let Some(profile) = profiles.0.get_mut(&self.path) {
profile.projects.insert(new_path, project);
profile.projects.insert(new_path.clone(), project);
}
Ok(new_path)
} else {
return Err(crate::ErrorKind::InputError(format!(
Err(crate::ErrorKind::InputError(format!(
"Project path does not exist: {:?}",
path
))
.into());
.into())
}
Ok(())
}
pub async fn remove_project(

View File

@@ -355,7 +355,7 @@ pub async fn infer_data_from_files(
return_projects.insert(
path,
Project {
disabled: false,
disabled: file_name.ends_with(".disabled"),
metadata: ProjectMetadata::Modrinth {
project: Box::new(project.clone()),
version: Box::new(version.clone()),
@@ -364,9 +364,17 @@ pub async fn infer_data_from_files(
.filter(|x| x.team_id == project.team)
.cloned()
.collect::<Vec<_>>(),
update_version: update_versions
.get(&hash)
.map(|val| Box::new(val.clone())),
update_version: if let Some(value) =
update_versions.get(&hash)
{
if value.id != version.id {
Some(Box::new(value.clone()))
} else {
None
}
} else {
None
},
incompatible: !version.loaders.contains(
&profile
.metadata
@@ -404,7 +412,7 @@ pub async fn infer_data_from_files(
path.clone(),
Project {
sha512: hash,
disabled: path.ends_with(".disabled"),
disabled: file_name.ends_with(".disabled"),
metadata: ProjectMetadata::Unknown,
file_name,
},
@@ -458,7 +466,7 @@ pub async fn infer_data_from_files(
path.clone(),
Project {
sha512: hash,
disabled: path.ends_with(".disabled"),
disabled: file_name.ends_with(".disabled"),
file_name,
metadata: ProjectMetadata::Inferred {
title: Some(
@@ -524,7 +532,7 @@ pub async fn infer_data_from_files(
path.clone(),
Project {
sha512: hash,
disabled: path.ends_with(".disabled"),
disabled: file_name.ends_with(".disabled"),
file_name,
metadata: ProjectMetadata::Inferred {
title: Some(if pack.name.is_empty() {
@@ -589,7 +597,7 @@ pub async fn infer_data_from_files(
path.clone(),
Project {
sha512: hash,
disabled: path.ends_with(".disabled"),
disabled: file_name.ends_with(".disabled"),
file_name,
metadata: ProjectMetadata::Inferred {
title: Some(pack.name.unwrap_or(pack.id)),
@@ -654,7 +662,7 @@ pub async fn infer_data_from_files(
path.clone(),
Project {
sha512: hash,
disabled: path.ends_with(".disabled"),
disabled: file_name.ends_with(".disabled"),
file_name,
metadata: ProjectMetadata::Inferred {
title: Some(
@@ -719,7 +727,7 @@ pub async fn infer_data_from_files(
path.clone(),
Project {
sha512: hash,
disabled: path.ends_with(".disabled"),
disabled: file_name.ends_with(".disabled"),
file_name,
metadata: ProjectMetadata::Inferred {
title: None,
@@ -739,7 +747,7 @@ pub async fn infer_data_from_files(
path.clone(),
Project {
sha512: hash,
disabled: path.ends_with(".disabled"),
disabled: file_name.ends_with(".disabled"),
file_name,
metadata: ProjectMetadata::Unknown,
},