Skip to content

Commit

Permalink
Use XDG paths to search for Playdate SDK
Browse files Browse the repository at this point in the history
* $XDG_CONFIG_HOME/.Playdate/config, then .Playdate/config
* $XDG_DATA_HOME/playdate_sdk then .local/share/playdate-sdk
  • Loading branch information
eirnym committed Apr 1, 2024
1 parent 16ebf30 commit 597dc0c
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions support/utils/src/toolchain/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ impl Sdk {
result
};

try_with(Self::try_from_default_env).or_else(|_| try_with(Self::try_from_default_config))
.or_else(|_| try_with(Self::try_from_default_path))
let res = try_with(Self::try_from_default_env).or_else(|_| try_with(Self::try_from_default_config));
#[cfg(unix)]
let res = res.or_else(|_| try_with(Self::try_xdg_unix_path));

res.or_else(|_| try_with(Self::try_from_default_path))
}

/// Create new `Sdk` with exact passed SDK path
Expand Down Expand Up @@ -107,6 +110,18 @@ impl Sdk {
let home = utils::home_dir()?;
Self::try_new_exact(home.join(SDK_HOME_DIR).join("PlaydateSDK"))
}

#[cfg(unix)]
pub fn try_xdg_unix_path() -> Result<Self, Error> {
const XDG_CONFIG_DATA_ENV: &'static str = "XDG_CONFIG_DATA";

let xdg_data_path = match std::env::var(XDG_CONFIG_DATA_ENV) {
Ok(ref variable) => PathBuf::from(variable),
Err(_) => utils::home_dir()?.join(".local").join("share"),
};

Self::try_new_exact(xdg_data_path.join("playdate-sdk"))
}
}


Expand Down Expand Up @@ -148,6 +163,10 @@ mod config {
use std::path::PathBuf;
use std::str::FromStr;

#[cfg(unix)]
const DEFAULT_XDG_CONFIG_DIR: &'static str = ".config";
#[cfg(unix)]
const XDG_CONFIG_HOME_ENV: &'static str = "XDG_CONFIG_HOME";
const CFG_DIR: &'static str = ".Playdate";
const CFG_FILENAME: &'static str = "config";
const CFG_KEY_SDK_ROOT: &'static str = "SDKRoot";
Expand All @@ -156,8 +175,31 @@ mod config {

impl Cfg {
pub fn try_default() -> Result<Self, Error> {
let cfg_path = utils::home_dir()?.join(CFG_DIR).join(CFG_FILENAME);
std::fs::read_to_string(cfg_path)?.parse()
fn find_config_folder() -> Result<PathBuf, Error> {
#[cfg(unix)]
{
let xdg_cfg_path = match std::env::var(XDG_CONFIG_HOME_ENV) {
Ok(ref variable) => PathBuf::from(variable),
Err(_) => utils::home_dir()?.join(DEFAULT_XDG_CONFIG_DIR),
}.join(CFG_DIR)
.join(CFG_FILENAME);

if xdg_cfg_path.exists() {
return Ok(xdg_cfg_path);
}
// Fallback to ~/.Playdate/config
}

let cfg_path = utils::home_dir()?.join(CFG_DIR).join(CFG_FILENAME);

if cfg_path.exists() {
return Ok(cfg_path);
}

return Err(Error::new(std::io::ErrorKind::NotFound, "Config file not found"));
}

std::fs::read_to_string(find_config_folder()?)?.parse()
}

pub fn sdk_path(&self) -> Option<PathBuf> { self.0.get(CFG_KEY_SDK_ROOT).map(PathBuf::from) }
Expand Down

0 comments on commit 597dc0c

Please sign in to comment.