feat: more breaking changes
This commit is contained in:
@ -1,26 +1,63 @@
|
||||
use anyhow::{anyhow, Result};
|
||||
use directories::ProjectDirs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tauri::{AppHandle, Manager};
|
||||
use std::{path::{Path, PathBuf}, sync::OnceLock};
|
||||
|
||||
pub fn get_dirs() -> ProjectDirs {
|
||||
ProjectDirs::from("org", "7EVENDAYSHOLIDAYS", "STARTLINER")
|
||||
.expect("Unable to set up config directories")
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
static NAME: &str = "startliner";
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
static NAME: &str = "STARTLINER";
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
pub struct Dirs {
|
||||
config_dir: PathBuf,
|
||||
data_dir: PathBuf,
|
||||
cache_dir: PathBuf,
|
||||
}
|
||||
|
||||
pub fn config_dir() -> PathBuf {
|
||||
get_dirs().config_dir().to_owned()
|
||||
static DIRS: OnceLock<Dirs> = OnceLock::new();
|
||||
|
||||
pub fn init_dirs(apph: &AppHandle) {
|
||||
DIRS.get_or_init(|| {
|
||||
if cfg!(windows) {
|
||||
Dirs {
|
||||
config_dir: apph.path().data_dir().expect("Unable to set project directories").join(NAME).join("cfg"),
|
||||
data_dir: apph.path().data_dir().expect("Unable to set project directories").join(NAME).join("data"),
|
||||
cache_dir: apph.path().cache_dir().expect("Unable to set project directories").join(NAME),
|
||||
}
|
||||
} else {
|
||||
Dirs {
|
||||
config_dir: apph.path().config_dir().expect("Unable to set project directories").join(NAME),
|
||||
data_dir: apph.path().data_dir().expect("Unable to set project directories").join(NAME),
|
||||
cache_dir: apph.path().cache_dir().expect("Unable to set project directories").join(NAME),
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn all_dirs() -> &'static Dirs {
|
||||
&DIRS.get().expect("Directories uninitialized")
|
||||
}
|
||||
|
||||
pub fn config_dir() -> &'static Path {
|
||||
&DIRS.get().expect("Directories uninitialized").config_dir
|
||||
}
|
||||
|
||||
pub fn data_dir() -> &'static Path {
|
||||
&DIRS.get().expect("Directories uninitialized").data_dir
|
||||
}
|
||||
|
||||
pub fn cache_dir() -> &'static Path {
|
||||
&DIRS.get().expect("Directories uninitialized").cache_dir
|
||||
}
|
||||
|
||||
pub fn pkg_dir() -> PathBuf {
|
||||
get_dirs().data_dir().join("pkg").to_owned()
|
||||
data_dir().join("pkg")
|
||||
}
|
||||
|
||||
pub fn pkg_dir_of(namespace: &str, name: &str) -> PathBuf {
|
||||
pkg_dir().join(format!("{}-{}", namespace, name)).to_owned()
|
||||
}
|
||||
|
||||
pub fn cache_dir() -> PathBuf {
|
||||
get_dirs().cache_dir().to_owned()
|
||||
pkg_dir().join(format!("{}-{}", namespace, name))
|
||||
}
|
||||
|
||||
pub fn path_to_str(p: impl AsRef<Path>) -> Result<String> {
|
||||
@ -40,4 +77,4 @@ pub fn copy_recursive(src: &Path, dst: &Path) -> std::io::Result<()> {
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user