Skip to content

Commit

Permalink
Merge pull request #12 from auyer/feature/manage_installed_versions
Browse files Browse the repository at this point in the history
Feature/manage installed versions
  • Loading branch information
auyer committed May 16, 2023
2 parents 50389ea + 0f4845d commit 91cccb6
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 167 deletions.
226 changes: 121 additions & 105 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion libprotonup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ tar = "0.4"
xz2 = "0.1"

[dev-dependencies]
tokio = { version = "1.27", features = ["macros", "rt"] }
tokio = { version = "1.28", features = ["macros", "rt"] }
58 changes: 58 additions & 0 deletions libprotonup/src/apps.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use crate::files::list_folders_in_path;
use std::fmt;

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum App {
Steam,
SteamFlatpak,
Lutris,
LutrisFlatpak,
DetectAll,
}

impl fmt::Display for App {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Self::Steam => write!(f, "Steam"),
Self::SteamFlatpak => write!(f, "Steam Flatpak"),
Self::Lutris => write!(f, "Lutris"),
Self::LutrisFlatpak => write!(f, "Lutris Flatpak"),
Self::DetectAll => write!(f, "Detect All"),
}
}
}

impl App {
pub fn default_install_dir(&self) -> &'static str {
match *self {
Self::Steam => "~/.steam/steam/compatibilitytools.d/",
Self::SteamFlatpak => {
"~/.var/app/com.valvesoftware.Steam/data/Steam/compatibilitytools.d/"
}
Self::Lutris => "~/.local/share/lutris/runners/wine/",
Self::LutrisFlatpak => "~/.var/app/net.lutris.Lutris/data/lutris/runners/wine/",
Self::DetectAll => "",
}
}

pub fn list_installed_versions(&self) -> Result<Vec<String>, anyhow::Error> {
list_folders_in_path(self.default_install_dir())
}
}

// APP_VARIANTS contains the subset of variants of the App enum that are actual apps
pub static APP_VARIANTS: &[App] = &[
App::Steam,
App::SteamFlatpak,
App::Lutris,
App::LutrisFlatpak,
];

// APP_VARIANTS_WITH_DETECT contains all variants of the App enum including the DetectAll variant
pub static APP_VARIANTS_WITH_DETECT: &[App] = &[
App::DetectAll,
App::Steam,
App::SteamFlatpak,
App::Lutris,
App::LutrisFlatpak,
];
6 changes: 0 additions & 6 deletions libprotonup/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

pub const DEFAULT_INSTALL_DIR: &str = "~/.steam/steam/compatibilitytools.d/";
pub const DEFAULT_INSTALL_DIR_FLATPAK: &str =
"~/.var/app/com.valvesoftware.Steam/data/Steam/compatibilitytools.d/";
pub const DEFAULT_LUTRIS_INSTALL_DIR: &str = "~/.local/share/lutris/runners/wine/";
pub const DEFAULT_LUTRIS_INSTALL_DIR_FLATPAK: &str =
"~/.var/app/net.lutris.Lutris/data/lutris/runners/wine/";
pub const TEMP_DIR: &str = "/tmp/";

pub const GITHUB_URL: &str = "https://api.github.com/repos";
Expand Down
31 changes: 30 additions & 1 deletion libprotonup/src/file.rs → libprotonup/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ fn path_result(path: &Path) -> String {
}
}

// decompress will detect the extension and decompress the file with the appropriate function
pub fn decompress(from_path: &Path, destination_path: &Path) -> Result<()> {
let path_str = from_path.as_os_str().to_string_lossy();

Expand Down Expand Up @@ -84,12 +85,40 @@ pub fn create_progress_trackers() -> (Arc<AtomicUsize>, Arc<AtomicBool>) {
)
}

// check_if_exists checks if a folder exists in a path
pub fn check_if_exists(path: &str, tag: &str) -> bool {
let f_path = utils::expand_tilde(format!("{path}/{tag}")).unwrap();
let p = std::path::Path::new(&f_path);
let p = f_path.as_path();
p.is_dir()
}

// list_folders_in_path returns a vector of strings of the folders in a path
pub fn list_folders_in_path(path: &str) -> Result<Vec<String>, anyhow::Error> {
let f_path = utils::expand_tilde(path).unwrap();
let p = f_path.as_path();
let paths: Vec<String> = p
.read_dir()
.with_context(|| format!("Failed to read directory : {}", path_result(p)))?
.filter_map(|e| e.ok())
.filter(|e| e.path().is_dir())
.map(|e| {
let path = e.path();
let name = path.file_name().unwrap();
name.to_str().unwrap().to_string()
})
.collect();
Ok(paths)
}

// removes a directory and all its contents
pub fn remove_dir_all(path: &str) -> Result<()> {
let f_path = utils::expand_tilde(path).unwrap();
let p = f_path.as_path();
std::fs::remove_dir_all(p)
.with_context(|| format!("[Remove] Failed to remove directory : {}", path_result(p)))?;
Ok(())
}

/// requires pointers to store the progress, and another to store "done" status
/// Create them with `create_progress_trackers`
pub async fn download_file_progress(
Expand Down
3 changes: 2 additions & 1 deletion libprotonup/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod apps;
pub mod constants;
pub mod file;
pub mod files;
pub mod github;
pub mod parameters;
pub mod utils;
2 changes: 1 addition & 1 deletion libprotonup/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl VariantParameters {

///
pub fn variant_type(&self) -> &Variant {
return &self.variant_ref;
&self.variant_ref
}
}

Expand Down
8 changes: 3 additions & 5 deletions protonup-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ description = "TUI Program for Custom Proton Download and installation written i
[dependencies]

# Use this to use the local changes in libprotonup.
# libprotonup = { path = "../libprotonup" }
libprotonup = { path = "../libprotonup" }
# This is necessary to publish to crates.io
libprotonup = { version = "0.5.0" }
# libprotonup = { version = "0.5.0" }
inquire = { version = "0.6", default-features = false, features = ["termion"] }
indicatif = { version = "0.17", features = [
"improved_unicode",
"unicode-segmentation",
] }
tokio = { version = "1.27", features = ["macros", "rt-multi-thread"] }
tokio = { version = "1.28", features = ["macros", "rt-multi-thread"] }
clap = { version = "4.2.1", features = ["derive"] }


Loading

0 comments on commit 91cccb6

Please sign in to comment.