Skip to content

Commit

Permalink
add SDK config support
Browse files Browse the repository at this point in the history
  • Loading branch information
boozook committed Mar 16, 2022
1 parent 2ce9289 commit 0ef8fd9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
46 changes: 46 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::collections::HashMap;
use std::path::PathBuf;
use std::str::FromStr;
use super::Error;

pub const CFG_DIR: &'static str = ".Playdate";
pub const CFG_FILENAME: &'static str = "config";
pub const CFG_KEY_SDK_ROOT: &'static str = "SDKRoot";

pub struct SdkCfg(HashMap<String, String>);

impl FromStr for SdkCfg {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(
s.trim()
.lines()
.filter_map(|line| {
line.split_once("\t")
.map(|(k, v)| (k.to_owned(), v.to_owned()))
})
.collect(),
))
}
}

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

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn parse() {
let path = "/path/PlaydateSDK-dir";
let cfg: SdkCfg = format!("{k}\t{v}\n", k = CFG_KEY_SDK_ROOT, v = path)
.parse()
.unwrap();
assert_eq!(cfg.sdk_path(), Some(PathBuf::from(path)));
}
}
25 changes: 24 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{anyhow, bail, Error};
use inflector::cases::titlecase::to_title_case;
use log::{info, debug};
use log::{debug, info};
use serde_derive::Deserialize;
use std::{
env,
Expand All @@ -16,6 +16,8 @@ use zip_extensions::zip_create_from_directory_with_options;
#[cfg(unix)]
use anyhow::Context;

mod config;

#[cfg(unix)]
const GCC_PATH_STR: &'static str = "/usr/local/bin/arm-none-eabi-gcc";
#[cfg(windows)]
Expand Down Expand Up @@ -44,7 +46,28 @@ const SDK_DIR: &'static str = "Developer";
#[cfg(windows)]
const SDK_DIR: &'static str = "Documents";

fn playdate_sdk_cfg() -> Result<config::SdkCfg, Error> {
let cfg_path = dirs::home_dir()
.ok_or(anyhow!("Can't find home dir"))?
.join(config::CFG_DIR)
.join(config::CFG_FILENAME);
fs::read_to_string(cfg_path)?.parse()
}

fn playdate_sdk_path() -> Result<PathBuf, Error> {
match playdate_sdk_cfg() {
Err(_) => {
debug!("Unable to read PlaydateSDK config from home dir, so using default.");
playdate_sdk_path_default()
}
Ok(cfg) => cfg.sdk_path().map(|p| Ok(p)).unwrap_or_else(|| {
debug!("Unable to determine PlaydateSDK path by config, so using default.");
playdate_sdk_path_default()
}),
}
}

fn playdate_sdk_path_default() -> Result<PathBuf, Error> {
let home_dir = dirs::home_dir().ok_or(anyhow!("Can't find home dir"))?;
Ok(home_dir.join(SDK_DIR).join("PlaydateSDK"))
}
Expand Down

0 comments on commit 0ef8fd9

Please sign in to comment.