Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 37 additions & 23 deletions src/cli/remove.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{fs, path::Path};
use std::{
fs,
io::ErrorKind,
path::Path,
};

use crate::{branch::DiscordBranch, error, info, path as dvm_path, success, Res};

Expand All @@ -14,47 +18,57 @@ pub async fn remove(release_type: DiscordBranch, verbose: bool) -> Res<()> {
error!("{} not installed", release_type);
}

let version = fs::read_to_string(dvm_path::version_file(release_type)?)
.expect("could not read version file: malformed installation detected");
let version = fs::read_to_string(dvm_path::version_file(release_type)?)?;
if verbose {
info!("reading version file")
}

info!("removing version {}:{}", release_type, version);

// remove all {release type} associated files
fs::remove_dir_all(dvm_path::install_dir(release_type)?)
.expect("error when removing data dirs");
if let Err(e) = fs::remove_dir_all(dvm_path::install_dir(release_type)?) {
if e.kind() != ErrorKind::NotFound {
return Err(e.into());
}
}
if verbose {
info!("removed data dirs")
}

fs::remove_file(dvm_path::dvm_bin_dir()?.join(dvm_path::pkg_name(release_type)))
.expect("error when removing bin file");
let bin_file = dvm_path::dvm_bin_dir()?.join(dvm_path::pkg_name(release_type));
if let Err(e) = fs::remove_file(&bin_file) {
if e.kind() != ErrorKind::NotFound {
return Err(e.into());
}
}
if verbose {
info!("removed bin file")
}

fs::remove_file(
dvm_path::home_dir()?
.join(".local")
.join("share")
.join("applications")
.join(format!("{}.desktop", dvm_path::pkg_name(release_type))),
)
.expect("error when removing desktop file");
let desktop_file = dvm_path::home_dir()?
.join(".local")
.join("share")
.join("applications")
.join(format!("{}.desktop", dvm_path::pkg_name(release_type)));
if let Err(e) = fs::remove_file(&desktop_file) {
if e.kind() != ErrorKind::NotFound {
return Err(e.into());
}
}
if verbose {
info!("removed desktop file")
}

fs::remove_file(
dvm_path::home_dir()?
.join(".local")
.join("share")
.join("icons")
.join(format!("{}.png", dvm_path::pkg_name(release_type))),
)
.expect("error when removing icon");
let icon_file = dvm_path::home_dir()?
.join(".local")
.join("share")
.join("icons")
.join(format!("{}.png", dvm_path::pkg_name(release_type)));
if let Err(e) = fs::remove_file(&icon_file) {
if e.kind() != ErrorKind::NotFound {
return Err(e.into());
}
}
if verbose {
info!("removed icon")
}
Expand Down
27 changes: 25 additions & 2 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,39 @@ pub fn app_dir(branch: DiscordBranch) -> Res<PathBuf> {
return Ok(install);
}

let mut best_match: Option<(Vec<u32>, PathBuf)> = None;
for entry in fs::read_dir(&install)? {
let entry = entry?;
let path = entry.path();
if !path.is_dir() {
continue;
}

let Some(name) = path.file_name().and_then(|n| n.to_str()) else {
continue;
};
let Some(version) = name.strip_prefix("app-") else {
continue;
};

let candidate = path.join("resources").join("app.asar");
if candidate.exists() {
return Ok(path);
if !candidate.exists() {
continue;
}

let parsed = version
.split('.')
.map(|part| part.parse::<u32>().unwrap_or(0))
.collect::<Vec<_>>();
Comment on lines +81 to +84

match &best_match {
Some((best_version, _)) if &parsed <= best_version => {}
_ => best_match = Some((parsed, path)),
}
}

if let Some((_, path)) = best_match {
return Ok(path);
}

Ok(install)
Expand Down
48 changes: 24 additions & 24 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, fs, path::Path};
use std::{collections::HashMap, fs};

use tokio::process::Command;

Expand Down Expand Up @@ -124,20 +124,19 @@ pub async fn install_version(
}
}

let app_dir = dvm_path::app_dir(release_type)?;
// patch desktop entry to point to dvm launcher bin
let desktop_source = install_dir.join(format!("{}.desktop", pkg_name));

let desktop_file = app_dir.join(format!("{}.desktop", pkg_name));
if Path::new(&desktop_file).exists() {
if desktop_source.exists() {
Command::new("sed")
.arg("-i")
.arg(format!(
"s#/usr/share/{}/{}#{}/{}#",
"s#/usr/bin/{}#{}/{}#",
pkg_name,
pascal_pkg,
dvm_path::dvm_bin_dir()?.display(),
pkg_name
))
.arg(&desktop_file)
.arg(&desktop_source)
.spawn()?
.wait()
.await?;
Comment on lines +130 to 142
Expand All @@ -158,7 +157,15 @@ if [[ -f $USER_FLAGS_FILE ]]; then
USER_FLAGS="$(cat $USER_FLAGS_FILE | sed 's/#.*//')"
fi

exec "{}/{}" "$@" $USER_FLAGS
INSTALL_DIR="{}"
BIN_NAME="{}"
APP_DIR="$(find "$INSTALL_DIR" -maxdepth 1 -type d -name 'app-*' 2>/dev/null | sort -V | tail -n 1)"

if [[ -n "$APP_DIR" && -x "$APP_DIR/$BIN_NAME" ]]; then
exec "$APP_DIR/$BIN_NAME" "$@" $USER_FLAGS
fi
Comment on lines +160 to +166

exec "$INSTALL_DIR/$BIN_NAME" "$@" $USER_FLAGS
"#,
pkg_name,
install_dir.display(),
Expand All @@ -183,28 +190,21 @@ exec "{}/{}" "$@" $USER_FLAGS

// copy desktop file to .local/share/applications
let local_apps_dir = dvm_path::home_dir()?.join(".local").join("share").join("applications");
if Path::new(&desktop_file).exists() {
Command::new("install")
.arg("-Dm644")
.arg(&desktop_file)
.arg(&local_apps_dir)
.spawn()?
.wait()
.await?;
if desktop_source.exists() {
fs::create_dir_all(&local_apps_dir)?;
fs::copy(
&desktop_source,
local_apps_dir.join(format!("{}.desktop", pkg_name)),
)?;
info!("installing desktop file");
}

// copy icon to .local/share/icons
let local_icons_dir = dvm_path::home_dir()?.join(".local").join("share").join("icons");
fs::create_dir_all(&local_icons_dir)?;
let icon_file = app_dir.join("discord.png");
if Path::new(&icon_file).exists() {
Command::new("cp")
.arg(&icon_file)
.arg(local_icons_dir.join(format!("{}.png", pkg_name)))
.spawn()?
.wait()
.await?;
let icon_file = install_dir.join("discord.png");
if icon_file.exists() {
fs::copy(&icon_file, local_icons_dir.join(format!("{}.png", pkg_name)))?;
info!("installing icons");
}

Expand Down
Loading