use std::fs;
use std::path::PathBuf;
use std::process::Command;
use dirs::home_dir;
use crate::constants::{JVR_CONFIGURE_FILE_NAME, JVR_HOME_DIR_NAME};
pub fn jvr_config_json_path() -> PathBuf {
let mut config_path = home_dir().expect("Failed to get user home directory");
config_path.push(JVR_HOME_DIR_NAME);
fs::create_dir_all(&config_path).expect("Failed to create jvr home directory");
config_path.push(JVR_CONFIGURE_FILE_NAME);
config_path
}
pub fn jvr_home_dir() -> PathBuf {
let mut user_home = home_dir().expect("Failed to get user home directory");
user_home.push(JVR_HOME_DIR_NAME);
user_home
}
pub fn create_symlink(target: &std::path::Path, link: &std::path::Path) -> std::io::Result<()> {
if link.exists() {
#[cfg(target_os = "windows")]
{
if link.is_dir() {
std::fs::remove_dir_all(link)?;
} else {
std::fs::remove_file(link)?;
}
}
#[cfg(not(target_os = "windows"))]
{
if link.is_symlink() {
std::fs::remove_file(link)?;
} else if link.is_dir() {
std::fs::remove_dir_all(link)?;
} else {
std::fs::remove_file(link)?;
}
}
}
if let Some(parent) = link.parent() {
std::fs::create_dir_all(parent)?;
}
#[cfg(target_os = "windows")]
{
junction::create(target, link)?;
}
#[cfg(not(target_os = "windows"))]
{
use std::os::unix::fs;
fs::symlink(target, link)?;
}
Ok(())
}
pub fn open_jvr_directory() {
let jvr_dir = jvr_home_dir();
if cfg!(target_os = "windows") {
Command::new("explorer")
.arg(jvr_dir)
.spawn()
.expect("Failed to open directory");
} else if cfg!(target_os = "macos") {
Command::new("open")
.arg(jvr_dir)
.spawn()
.expect("Failed to open directory");
} else if cfg!(target_os = "linux") {
Command::new("xdg-open")
.arg(jvr_dir)
.spawn()
.expect("Failed to open directory");
} else {
println!("Unsupported OS");
}
}