summaryrefslogtreecommitdiff
path: root/src/environment.rs
blob: 33c316d60e4460d62449aff61566293e90858747 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// (c) 2023-2025 Cory Forsstrom, Casper Rogild Storm
// (c) 2024-2025 Polesznyák Márk László

use std::env;
use std::path::PathBuf;

pub const CONFIG_FILE_NAME: &str = "config.toml";
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const GIT_HASH: Option<&str> = option_env!("GIT_HASH");

pub fn formatted_version() -> String {
    let hash = GIT_HASH
        .map(|hash| format!(" ({hash})"))
        .unwrap_or_default();

    format!("{}{hash}", VERSION)
}

pub fn config_dir() -> PathBuf {
    portable_dir().unwrap_or_else(platform_specific_config_dir)
}

fn portable_dir() -> Option<PathBuf> {
    let exe = env::current_exe().ok()?;
    let dir = exe.parent()?;

    dir.join(CONFIG_FILE_NAME)
        .is_file()
        .then(|| dir.to_path_buf())
}

fn platform_specific_config_dir() -> PathBuf {
    #[cfg(target_os = "macos")]
    {
        xdg_config_dir().unwrap_or_else(|| {
            dirs_next::config_dir()
                .expect("expected valid config dir")
                .join("iced-builder")
        })
    }
    #[cfg(not(target_os = "macos"))]
    {
        dirs_next::config_dir()
            .expect("expected valid config dir")
            .join("iced-builder")
    }
}

#[cfg(target_os = "macos")]
#[inline(always)]
fn xdg_config_dir() -> Option<PathBuf> {
    let config_path = xdg::BaseDirectories::new().config_home?;
    let config_dir = config_path.join("iced-builder");

    config_dir
        .join(CONFIG_FILE_NAME)
        .is_file()
        .then_some(config_dir)
}