Skip to content

Commit

Permalink
Fmt & fix #6
Browse files Browse the repository at this point in the history
  • Loading branch information
AOx0 committed Oct 16, 2022
1 parent 3feb14a commit b7f9562
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 393 deletions.
272 changes: 90 additions & 182 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ license = "MIT"
[dependencies]
execute = "0.2.11"
async-recursion = "1.0.0"
clap = { version= "3.2.5", default-features = false, features=["env", "derive", "unicode", "suggestions", "std"] }
clap = { version= "4.0.10", features=["env", "derive", "unicode", "suggestions", "std"] }
tokio = { version = "1.19.2", default-features = false, features = ["full"] }
fs_extra = "1.2.0"
regex = "1.5.6"
Expand All @@ -34,4 +34,4 @@ rrm_scrap = {path="rrm_scrap", version="0.0.1-alpha.7"}
rrm_installer = { path="rrm_installer", version="0.0.1-alpha.7"}
colored = "2.0.0"
text_io = "0.1.10"
notify = "4.0.17"
notify = "5.0.0"
1 change: 1 addition & 0 deletions rrm_installer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ serde_json = "1.0.81"
include_dir = "0.7.2"
directories = "4.0.1"
dotenv = "0.15.0"
fs_extra = "1.2.0"
rrm_locals = {path="../rrm_locals", version="0.0.1-alpha.7"}
155 changes: 72 additions & 83 deletions rrm_installer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ use std::io::{BufReader, Read, Write};
use include_dir::{include_dir, Dir};
use std::path::{Path, PathBuf};
use std::process::exit;
use directories;
use fs_extra;

#[cfg(any(target_os = "macos", target_os = "linux"))]
use std::os::unix::fs::PermissionsExt;
use directories::{ProjectDirs, UserDirs};

#[cfg(target_os = "windows")]
static DEFAULT_PAGING_SOFTWARE: &str = r"C:\Windows\System32\more.com";
Expand All @@ -38,59 +41,66 @@ mod tests {
}
}

pub fn config_does_exists() -> bool {
let home = if let Some(home) = get_home() {
home
} else {
eprintln!("Something failed while getting the user's home dir");
exit(1);
};
pub fn get_or_create_config_dir() -> PathBuf {
if let Some(path) = env_var_config("XDG_CONFIG_HOME") {
return path;
}

let config = home.join(".rrm").join("config");
config.exists() && config.is_file()
}
if let Some(path) = env_var_config("RRM_CONFIG_DIR") {
return path;
}

fn config_exists(home: &Path) -> bool {
let config = home.join(".rrm").join("config");
config.exists() && config.is_file()
}
if let Some(path) = env_var_config("CONFIG_DIR") {
return path;
}

fn get_home() -> Option<PathBuf> {
#[cfg(feature = "dev")]
return Some(std::env::current_dir().unwrap());
if let Some(path) = env_var_config("CONFIG_HOME") {
return path;
}

#[cfg(not(feature = "dev"))]
if let Some(user_dirs) = directories::UserDirs::new() {
let home: &Path = user_dirs.home_dir();
Some(home.to_path_buf())
let config_dir = UserDirs::new().unwrap().home_dir().join(".config");
if config_dir.exists() {
let config_dir = config_dir.join("rrm");
if !config_dir.exists() {
fs::create_dir_all(&config_dir).unwrap();
}
return config_dir.to_path_buf();
}

let config_dir = UserDirs::new().unwrap().home_dir().join(".rrm");
if !config_dir.exists() {
fs::create_dir_all(&config_dir).unwrap();
return config_dir.to_path_buf();
}

let binding = ProjectDirs::from("com", "AOx0", "rrm").unwrap();
let config_dir = binding.preference_dir();
if !config_dir.exists() {
fs::create_dir_all(&config_dir).unwrap();
}

return config_dir.to_path_buf();
}

fn env_var_config(var: &'static str) -> Option<PathBuf> {
let env_config_dir = std::env::var(var);
if let Ok(env_config_dir) = env_config_dir {
if !Path::new(&env_config_dir).exists() {
fs::create_dir_all(&env_config_dir).unwrap();
}
Some(PathBuf::from(env_config_dir))
} else {
None
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Installer {
pub config: PathBuf,
pub home: PathBuf,
pub rim_install: Option<GamePath>,
pub use_more: bool,
pub with_paging: String,
}

fn create_config(at: &Path) {
fs::create_dir(at.parent().unwrap()).unwrap_or_else(|err| {
if !at.parent().unwrap().is_dir() {
panic!("{}", err);
}
});

File::create(at).unwrap_or_else(|err| {
eprintln!("Something happened while trying to create {}", at.display());
eprintln!("Error: {}", err);
exit(1);
});
}

pub fn run_steam_command(c: &str, config_path: &Path, count: usize) -> String {
let steam = get_steamcmd_path(config_path);

Expand Down Expand Up @@ -174,56 +184,35 @@ fn set_permissions_for_steamcmd(path: &Path) {

impl Installer {
fn init(path: Option<PathBuf>) -> Self {
let mut fresh_new = false;
let home = if let Some(home) = get_home() {
home
} else {
eprintln!("Something failed while getting the user's home dir");
exit(1);
};

let config_file = home.join(".rrm").join("config");
let config = if config_exists(&home) {
config_file
} else {
fresh_new = true;
create_config(&config_file);
if config_exists(&home) {
let steamcmd_path = home.join(".rrm").join("steamcmd");
fs::create_dir(&steamcmd_path).unwrap_or_else(|err| {
if !steamcmd_path.is_dir() {
panic!("{}", err);
}
});

PROJECT_DIR.extract(steamcmd_path.as_path()).unwrap();

#[cfg(any(target_os = "macos", target_os = "linux"))]
set_permissions_for_steamcmd(steamcmd_path.as_path());
let config_dir = get_or_create_config_dir();
let config_file = config_dir.join("config");
if !config_file.exists() {
File::create(&config_file).unwrap();

let steamcmd_path = config_dir.join("steamcmd");
fs::create_dir(&steamcmd_path).unwrap_or_else(|err| {
if !steamcmd_path.is_dir() {
panic!("{}", err);
}
});

config_file
} else {
exit(1);
}
};
PROJECT_DIR.extract(steamcmd_path.as_path()).unwrap();

let path = path.map(|path| GamePath::from(&path));
#[cfg(any(target_os = "macos", target_os = "linux"))]
set_permissions_for_steamcmd(steamcmd_path.as_path());

if fresh_new {
println!("Installing steamcmd...");
run_steam_command("", &current_dir().unwrap().as_path().join(".rrm"), 1);
run_steam_command("", &config_dir, 1);
println!("Done!");

std::env::set_current_dir(home.as_path()).unwrap();
} else {
std::env::set_current_dir(home.join(".rrm").as_path()).unwrap();
}

let path = path.map(|path| GamePath::from(&path));

std::env::set_current_dir(&config_dir).unwrap();

Installer {
with_paging: DEFAULT_PAGING_SOFTWARE.to_string(),
home,
rim_install: path,
config,
use_more: true,
}
}
Expand All @@ -242,9 +231,9 @@ impl Installer {
.write(true)
.truncate(true)
.read(false)
.open(&self.config)
.open(&get_or_create_config_dir().join("config"))
.unwrap_or_else(|err| {
eprintln!("Failed to open config file at {}", &self.config.display());
eprintln!("Failed to open config file at {}", &get_or_create_config_dir().join("config").display());
eprintln!("Error: {}", err);
exit(1);
});
Expand All @@ -270,7 +259,7 @@ impl Installer {
Installer::init_with_path(path)
} else {
let installer = Installer::init(None);
let old_config = Installer::load_config(&installer.config);
let old_config = Installer::load_config(&get_or_create_config_dir().join("config"));

if let Ok(mut i) = old_config {
if i.rim_install.as_ref().is_some() {
Expand Down Expand Up @@ -348,7 +337,7 @@ impl Installer {

pub fn install_sync(&self, c: &[&str]) -> (bool, String) {
let to_install = Self::gen_install_string(&c);
let a: String = run_steam_command(&to_install, &current_dir().unwrap(), 1);
let a: String = run_steam_command(&to_install, &get_or_create_config_dir(), 1);
(a.contains("Success. Downloaded item"), a)
}

Expand All @@ -359,10 +348,10 @@ impl Installer {
}

pub fn get_steamcmd_path(&self) -> PathBuf {
get_steamcmd_path(&self.config.parent().unwrap())
get_steamcmd_path(&get_or_create_config_dir())
}

pub fn run_steam_command(&self, c: &str, count: usize) -> String {
run_steam_command(c, &self.config, count)
run_steam_command(c, &get_or_create_config_dir(), count)
}
}
4 changes: 3 additions & 1 deletion rrm_locals/src/mod_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ fn get_mods(about_dir: &Path) -> Vec<ModPaths> {
} else {
None
},
manifest: if path.file_name().unwrap().to_str().unwrap().to_lowercase() == "manifest.xml" {
manifest: if path.file_name().unwrap().to_str().unwrap().to_lowercase()
== "manifest.xml"
{
Some(PathBuf::from(path))
} else {
None
Expand Down
2 changes: 1 addition & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub enum Options {
visible_alias = "use-paging"
)]
UsePager {
#[clap(required = true, possible_values= &["true", "false", "0", "1"])]
#[clap(required = true, value_parser(["true", "false", "0", "1"]))]
value: String,
},

Expand Down
Loading

0 comments on commit b7f9562

Please sign in to comment.