feat(content): manage installed versions
This commit is contained in:
1 parent
062a9234ee
commit
aa03dac34d
6 files changed
+277
-12
No files matched your search
@@ -22,6 +22,7 @@ pub struct InstallRoot {
|
||||
pub installed_path: Option<PathBuf>,
|
||||
pub kind: ContentKind,
|
||||
pub target_world: Option<PathBuf>,
|
||||
pub force_reinstall: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -574,6 +575,7 @@ pub async fn resolve(
|
||||
&nodes,
|
||||
minecraft_dir,
|
||||
root.target_world.as_deref(),
|
||||
root.force_reinstall && key == &root_key,
|
||||
)
|
||||
})
|
||||
})
|
||||
@@ -1042,14 +1044,17 @@ fn planned_install(
|
||||
nodes: &HashMap<ProjectKey, Node>,
|
||||
minecraft_dir: &Path,
|
||||
target_world: Option<&Path>,
|
||||
force_reinstall: bool,
|
||||
) -> PlannedInstall {
|
||||
let installed_path = node
|
||||
.installed
|
||||
.as_ref()
|
||||
.map(|installed| minecraft_dir.join(&installed.relative_path));
|
||||
let replacement = node.installed.as_ref().is_some_and(|installed| {
|
||||
installed.identity.version_id.is_empty() || installed.identity.version_id != node.version.id
|
||||
});
|
||||
let replacement = force_reinstall
|
||||
|| node.installed.as_ref().is_some_and(|installed| {
|
||||
installed.identity.version_id.is_empty()
|
||||
|| installed.identity.version_id != node.version.id
|
||||
});
|
||||
PlannedInstall {
|
||||
provider: key.provider.clone(),
|
||||
project_id: key.project_id.clone(),
|
||||
|
||||
@@ -213,6 +213,7 @@ fn root(version: VersionInfo) -> InstallRoot {
|
||||
installed_path: None,
|
||||
kind: ContentKind::Mod,
|
||||
target_world: None,
|
||||
force_reinstall: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+101
-3
@@ -261,8 +261,12 @@ impl App {
|
||||
|
||||
// content area delegates to whichever tab is active.
|
||||
// worlds use the same list navigation without the toggle
|
||||
let discovery_popup_open = self
|
||||
.active_discovery_state()
|
||||
.is_some_and(|state| state.version_popup.is_some() || state.project_page_open());
|
||||
if self.focused == FocusedArea::Content
|
||||
&& self.content_mode == widgets::content::ContentMode::Discover
|
||||
&& (self.content_mode == widgets::content::ContentMode::Discover
|
||||
|| discovery_popup_open)
|
||||
{
|
||||
let (search_active, popup_open, project_page_open) = self
|
||||
.active_discovery_state_mut()
|
||||
@@ -343,7 +347,13 @@ impl App {
|
||||
if let Some(state) = self.active_discovery_state_mut() {
|
||||
state.begin_world_selection(worlds);
|
||||
}
|
||||
} else if kind == Some(crate::instance::ContentKind::Mod) {
|
||||
} else if matches!(
|
||||
kind,
|
||||
Some(
|
||||
crate::instance::ContentKind::Mod
|
||||
| crate::instance::ContentKind::DataPack
|
||||
)
|
||||
) {
|
||||
self.spawn_active_discovery_dependencies();
|
||||
} else if let Some(state) = self.active_discovery_state_mut() {
|
||||
state.begin_confirmation();
|
||||
@@ -394,6 +404,12 @@ impl App {
|
||||
}
|
||||
} else if self.content_tab == widgets::content::ContentTab::Worlds {
|
||||
if self.open_world_datapacks.is_some() {
|
||||
if key_event.code == KeyCode::Char('v')
|
||||
&& !self.world_datapacks_state.search.active
|
||||
{
|
||||
self.spawn_installed_versions();
|
||||
return Ok(());
|
||||
}
|
||||
if matches!(key_event.code, KeyCode::Esc | KeyCode::Char('h'))
|
||||
&& !self.world_datapacks_state.search.active
|
||||
{
|
||||
@@ -458,6 +474,10 @@ impl App {
|
||||
_ => None,
|
||||
};
|
||||
if let Some(state) = state {
|
||||
if key_event.code == KeyCode::Char('v') && !state.search.active {
|
||||
self.spawn_installed_versions();
|
||||
return Ok(());
|
||||
}
|
||||
if key_event.code == KeyCode::Char('d') && !state.search.active {
|
||||
if let Some(pending) = state.pending_delete() {
|
||||
self.confirm_content_delete(pending);
|
||||
@@ -798,10 +818,82 @@ impl App {
|
||||
}
|
||||
widgets::content::ContentTab::Shaders => Some(&mut self.shaders_discovery_state),
|
||||
widgets::content::ContentTab::DataPacks => Some(&mut self.datapacks_discovery_state),
|
||||
widgets::content::ContentTab::Worlds if self.open_world_datapacks.is_some() => {
|
||||
Some(&mut self.datapacks_discovery_state)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn active_discovery_state(&self) -> Option<&widgets::content::discovery::DiscoveryState> {
|
||||
match self.content_tab {
|
||||
widgets::content::ContentTab::Mods => Some(&self.mods_discovery_state),
|
||||
widgets::content::ContentTab::ResourcePacks => {
|
||||
Some(&self.resource_packs_discovery_state)
|
||||
}
|
||||
widgets::content::ContentTab::Shaders => Some(&self.shaders_discovery_state),
|
||||
widgets::content::ContentTab::DataPacks => Some(&self.datapacks_discovery_state),
|
||||
widgets::content::ContentTab::Worlds if self.open_world_datapacks.is_some() => {
|
||||
Some(&self.datapacks_discovery_state)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_installed_versions(&mut self) {
|
||||
let Some(instance) = self.instances_state.selected_instance().cloned() else {
|
||||
return;
|
||||
};
|
||||
let (entry, kind, target_world) = match self.content_tab {
|
||||
widgets::content::ContentTab::Mods => (
|
||||
self.mods_state.selected_entry().cloned(),
|
||||
crate::instance::ContentKind::Mod,
|
||||
None,
|
||||
),
|
||||
widgets::content::ContentTab::ResourcePacks => (
|
||||
self.resource_packs_state.selected_entry().cloned(),
|
||||
crate::instance::ContentKind::ResourcePack,
|
||||
None,
|
||||
),
|
||||
widgets::content::ContentTab::Shaders => (
|
||||
self.shaders_state.selected_entry().cloned(),
|
||||
crate::instance::ContentKind::Shader,
|
||||
None,
|
||||
),
|
||||
widgets::content::ContentTab::Worlds => (
|
||||
self.world_datapacks_state.selected_entry().cloned(),
|
||||
crate::instance::ContentKind::DataPack,
|
||||
self.open_world_datapacks.clone(),
|
||||
),
|
||||
_ => return,
|
||||
};
|
||||
let Some(entry) = entry else {
|
||||
return;
|
||||
};
|
||||
let paths = crate::storage::InstancePaths::new(
|
||||
self.instance_manager.instances_dir.join(&instance.name),
|
||||
);
|
||||
let Ok(relative_path) = entry.path.strip_prefix(paths.minecraft()) else {
|
||||
return;
|
||||
};
|
||||
let Some(record) = self
|
||||
.content_manifest
|
||||
.as_ref()
|
||||
.filter(|(name, _)| name == &instance.name)
|
||||
.and_then(|(_, manifest)| manifest.record(relative_path))
|
||||
.cloned()
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let Some(state) = self.active_discovery_state_mut() else {
|
||||
return;
|
||||
};
|
||||
let Some(request) = state.begin_installed_versions(&entry, &record, target_world) else {
|
||||
return;
|
||||
};
|
||||
self.spawn_discovery_versions_request(instance, kind, request);
|
||||
}
|
||||
|
||||
fn spawn_active_discovery_versions(&mut self) {
|
||||
let Some(instance) = self.instances_state.selected_instance().cloned() else {
|
||||
return;
|
||||
@@ -900,7 +992,13 @@ impl App {
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(versions) => {
|
||||
Ok(mut versions) => {
|
||||
if let Some(current) = request.current_version_id.as_deref()
|
||||
&& !versions.iter().any(|version| version.id == current)
|
||||
&& let Ok(version) = provider.version(current).await
|
||||
{
|
||||
versions.push(version);
|
||||
}
|
||||
if let Ok(bytes) = serde_json::to_vec_pretty(&versions) {
|
||||
let _ = crate::storage::write_atomic(&version_cache, &bytes);
|
||||
}
|
||||
|
||||
@@ -395,6 +395,68 @@ fn install_and_change_version_popups_only_differ_in_title() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn installed_version_popup_tracks_current_version_and_reinstalls_it() {
|
||||
let mut state = DiscoveryState::new(ContentKind::Mod);
|
||||
let entry = crate::instance::content::entry::ContentEntry {
|
||||
file_stem: "project".to_owned(),
|
||||
name: "Project".to_owned(),
|
||||
source_slug: None,
|
||||
installed_path: None,
|
||||
provider_project: None,
|
||||
world_details: None,
|
||||
title_suffix: None,
|
||||
footer_label: None,
|
||||
description: String::new(),
|
||||
enabled: true,
|
||||
icon_bytes: None,
|
||||
provider_icon: false,
|
||||
provider_description: false,
|
||||
path: PathBuf::from("mods/project.jar"),
|
||||
icon_lines: None,
|
||||
};
|
||||
let record = crate::instance::ContentFileRecord {
|
||||
relative_path: entry.path.clone(),
|
||||
kind: ContentKind::Mod,
|
||||
enabled: true,
|
||||
fingerprint: crate::instance::FileFingerprint {
|
||||
size: 1,
|
||||
modified_ns: 1,
|
||||
hashes: Default::default(),
|
||||
},
|
||||
resolution: crate::instance::Resolution::Resolved {
|
||||
project: crate::instance::ProviderProject {
|
||||
provider: "modrinth".to_owned(),
|
||||
project_id: "project".to_owned(),
|
||||
version_id: "current".to_owned(),
|
||||
},
|
||||
},
|
||||
provider_aliases: vec![crate::instance::ProviderProject {
|
||||
provider: "curseforge".to_owned(),
|
||||
project_id: "42".to_owned(),
|
||||
version_id: "84".to_owned(),
|
||||
}],
|
||||
required_dependencies: Vec::new(),
|
||||
automatic_dependency: false,
|
||||
cleanup_eligible: false,
|
||||
};
|
||||
|
||||
let request = state
|
||||
.begin_installed_versions(&entry, &record, None)
|
||||
.unwrap();
|
||||
assert_eq!(request.current_version_id.as_deref(), Some("current"));
|
||||
assert_eq!(state.version_popup.as_ref().unwrap().sources.len(), 2);
|
||||
state.version_popup.as_mut().unwrap().loading = false;
|
||||
state.version_popup.as_mut().unwrap().versions = vec![version("current")];
|
||||
assert_eq!(
|
||||
state.version_popup.as_ref().unwrap().title(),
|
||||
"Reinstall Project"
|
||||
);
|
||||
|
||||
let request = state.begin_dependency_resolution().unwrap();
|
||||
assert!(request.root.force_reinstall);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compatible_versions_populate_the_open_popup() {
|
||||
let project = DiscoveryProject {
|
||||
|
||||
@@ -373,6 +373,7 @@ pub struct VersionPopupState {
|
||||
pub sources: Vec<crate::instance::ProviderProject>,
|
||||
pub source_index: usize,
|
||||
pub installed_path: Option<PathBuf>,
|
||||
pub current_version_id: Option<String>,
|
||||
pub minecraft_versions: Vec<String>,
|
||||
pub selected_minecraft_version: Option<String>,
|
||||
pub selecting_minecraft_version: bool,
|
||||
@@ -391,7 +392,12 @@ pub struct VersionPopupState {
|
||||
|
||||
impl VersionPopupState {
|
||||
pub fn title(&self) -> String {
|
||||
if self.installed_path.is_some() {
|
||||
if self.installed_path.is_some()
|
||||
&& self.current_version_id.as_deref()
|
||||
== self.selected_version().map(|version| version.id.as_str())
|
||||
{
|
||||
format!("Reinstall {}", self.project_title)
|
||||
} else if self.installed_path.is_some() {
|
||||
format!("Change {} version", self.project_title)
|
||||
} else {
|
||||
format!("Install {}", self.project_title)
|
||||
@@ -437,6 +443,7 @@ pub struct VersionsRequest {
|
||||
pub request_id: u64,
|
||||
pub project_id: String,
|
||||
pub provider: String,
|
||||
pub current_version_id: Option<String>,
|
||||
pub pending: PendingActions,
|
||||
}
|
||||
|
||||
@@ -740,6 +747,7 @@ impl DiscoveryState {
|
||||
sources,
|
||||
source_index: 0,
|
||||
installed_path,
|
||||
current_version_id: None,
|
||||
minecraft_versions: Vec::new(),
|
||||
selected_minecraft_version: None,
|
||||
selecting_minecraft_version: self.modpacks,
|
||||
@@ -759,6 +767,57 @@ impl DiscoveryState {
|
||||
request_id,
|
||||
project_id,
|
||||
provider: source.provider,
|
||||
current_version_id: None,
|
||||
pending: self.pending_actions.clone(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn begin_installed_versions(
|
||||
&mut self,
|
||||
entry: &ContentEntry,
|
||||
record: &crate::instance::ContentFileRecord,
|
||||
target_world: Option<(String, PathBuf)>,
|
||||
) -> Option<VersionsRequest> {
|
||||
let current = record.resolved_project()?.clone();
|
||||
let mut sources = vec![current.clone()];
|
||||
for alias in &record.provider_aliases {
|
||||
if !sources.iter().any(|source| {
|
||||
source.provider == alias.provider && source.project_id == alias.project_id
|
||||
}) {
|
||||
sources.push(alias.clone());
|
||||
}
|
||||
}
|
||||
self.next_action_request_id = self.next_action_request_id.wrapping_add(1);
|
||||
let request_id = self.next_action_request_id;
|
||||
self.version_popup = Some(VersionPopupState {
|
||||
request_id,
|
||||
project_id: current.project_id.clone(),
|
||||
provider: current.provider.clone(),
|
||||
project_title: entry.name.clone(),
|
||||
sources,
|
||||
source_index: 0,
|
||||
installed_path: Some(entry.path.clone()),
|
||||
current_version_id: Some(current.version_id.clone()),
|
||||
minecraft_versions: Vec::new(),
|
||||
selected_minecraft_version: None,
|
||||
selecting_minecraft_version: false,
|
||||
selecting_world: false,
|
||||
worlds: ContentListState::default(),
|
||||
target_world,
|
||||
versions: Vec::new(),
|
||||
selected: 0,
|
||||
loading: true,
|
||||
resolving_dependencies: false,
|
||||
confirming: false,
|
||||
installing: false,
|
||||
dependency_plan: None,
|
||||
error: None,
|
||||
});
|
||||
Some(VersionsRequest {
|
||||
request_id,
|
||||
project_id: current.project_id,
|
||||
provider: current.provider,
|
||||
current_version_id: Some(current.version_id),
|
||||
pending: self.pending_actions.clone(),
|
||||
})
|
||||
}
|
||||
@@ -780,7 +839,11 @@ impl DiscoveryState {
|
||||
popup.selecting_minecraft_version = selecting_minecraft_version;
|
||||
popup.selecting_world = false;
|
||||
popup.worlds = ContentListState::default();
|
||||
popup.target_world = None;
|
||||
if self.kind != ContentKind::DataPack || popup.current_version_id.is_none() {
|
||||
popup.target_world = None;
|
||||
}
|
||||
popup.current_version_id =
|
||||
(!source.version_id.is_empty()).then(|| source.version_id.clone());
|
||||
popup.versions.clear();
|
||||
popup.selected = 0;
|
||||
popup.loading = true;
|
||||
@@ -792,6 +855,7 @@ impl DiscoveryState {
|
||||
request_id: popup.request_id,
|
||||
project_id: source.project_id,
|
||||
provider: source.provider,
|
||||
current_version_id: popup.current_version_id.clone(),
|
||||
pending: self.pending_actions.clone(),
|
||||
})
|
||||
}
|
||||
@@ -1052,6 +1116,7 @@ impl DiscoveryState {
|
||||
return None;
|
||||
}
|
||||
let version = popup.selected_version()?.clone();
|
||||
let force_reinstall = popup.current_version_id.as_deref() == Some(version.id.as_str());
|
||||
popup.loading = true;
|
||||
popup.resolving_dependencies = true;
|
||||
popup.error = None;
|
||||
@@ -1066,6 +1131,7 @@ impl DiscoveryState {
|
||||
installed_path: popup.installed_path.clone(),
|
||||
kind,
|
||||
target_world: popup.target_world.as_ref().map(|(_, path)| path.clone()),
|
||||
force_reinstall,
|
||||
},
|
||||
pending: self.pending_actions.clone(),
|
||||
})
|
||||
@@ -1407,7 +1473,7 @@ pub fn handle_key(key_event: &KeyEvent, state: &mut DiscoveryState) -> bool {
|
||||
&& !popup.installing
|
||||
{
|
||||
popup.confirming = false;
|
||||
if popup.target_world.is_some() {
|
||||
if popup.target_world.is_some() && popup.current_version_id.is_none() {
|
||||
popup.selecting_world = true;
|
||||
}
|
||||
popup.error = None;
|
||||
|
||||
@@ -329,6 +329,8 @@ pub fn render(
|
||||
| (ContentMode::Installed, ContentTab::Shaders) => &[
|
||||
("j/k", " navigate"),
|
||||
("⏎", " toggle"),
|
||||
("v", " versions"),
|
||||
("u", " update all"),
|
||||
("d", " delete"),
|
||||
("Shift+⏎", " open dir"),
|
||||
("h/l", " tabs"),
|
||||
@@ -337,6 +339,8 @@ pub fn render(
|
||||
],
|
||||
(ContentMode::Installed, ContentTab::Worlds) if open_world_datapacks.is_some() => &[
|
||||
("j/k", " navigate"),
|
||||
("v", " versions"),
|
||||
("u", " update all"),
|
||||
("d", " delete"),
|
||||
("Shift+Enter", " open dir"),
|
||||
("h/Esc", " back"),
|
||||
@@ -550,6 +554,14 @@ pub fn render(
|
||||
false,
|
||||
false,
|
||||
);
|
||||
if datapacks_discovery_state.version_popup.is_some() {
|
||||
render_version_popup(
|
||||
frame,
|
||||
content_area,
|
||||
datapacks_discovery_state,
|
||||
picker,
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if worlds_state.loaded_for.as_deref() != Some(instance.name.as_str()) {
|
||||
@@ -642,6 +654,9 @@ fn render_downloadable(
|
||||
false,
|
||||
false,
|
||||
);
|
||||
if discovery_state.version_popup.is_some() {
|
||||
render_version_popup(frame, area, discovery_state, picker);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -776,6 +791,8 @@ fn render_version_popup(
|
||||
.map(|version| confirmation_release_date(&version.date_published))
|
||||
.unwrap_or_else(|| "Unknown".to_owned());
|
||||
let replacing = popup.installed_path.is_some();
|
||||
let reinstalling = popup.current_version_id.as_deref()
|
||||
== popup.selected_version().map(|version| version.id.as_str());
|
||||
let provider_label = popup.provider_label().to_owned();
|
||||
let target_world = popup.target_world.as_ref().map(|(name, _)| name.clone());
|
||||
let dependency_installs = popup
|
||||
@@ -804,6 +821,7 @@ fn render_version_popup(
|
||||
.map(|plan| plan.optional_dependencies)
|
||||
.unwrap_or_default();
|
||||
let can_switch_provider = popup.sources.len() > 1;
|
||||
let current_version_id = popup.current_version_id.clone();
|
||||
let items = if selecting_minecraft_version {
|
||||
popup
|
||||
.minecraft_versions
|
||||
@@ -814,15 +832,30 @@ fn render_version_popup(
|
||||
popup
|
||||
.visible_versions()
|
||||
.map(|version| {
|
||||
ListItem::new(discovery_version_label(version))
|
||||
.style(Style::default().fg(theme.text()))
|
||||
let style = if current_version_id.as_deref() == Some(version.id.as_str()) {
|
||||
Style::default()
|
||||
.fg(theme.text())
|
||||
.add_modifier(Modifier::ITALIC)
|
||||
} else {
|
||||
Style::default().fg(theme.text())
|
||||
};
|
||||
ListItem::new(discovery_version_label(version)).style(style)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
let keybinds = if confirming {
|
||||
crate::tui::widgets::popups::keybind_line(&[
|
||||
("h", " back"),
|
||||
("Enter", if replacing { " change" } else { " install" }),
|
||||
(
|
||||
"Enter",
|
||||
if reinstalling {
|
||||
" reinstall"
|
||||
} else if replacing {
|
||||
" change"
|
||||
} else {
|
||||
" install"
|
||||
},
|
||||
),
|
||||
])
|
||||
} else {
|
||||
let mut keybinds = vec![("j/k", " navigate")];
|
||||
|
||||
Reference in new issue
Block a user