diff --git a/Cargo.lock b/Cargo.lock index 38bb962b4..84b864069 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -388,6 +388,7 @@ version = "0.1.0" dependencies = [ "cargo-platform", "cargo_metadata", + "home", ] [[package]] diff --git a/crates/cargo-steel-lib/Cargo.toml b/crates/cargo-steel-lib/Cargo.toml index 8c605bae1..51b830f5a 100644 --- a/crates/cargo-steel-lib/Cargo.toml +++ b/crates/cargo-steel-lib/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] cargo_metadata = "0.15.3" +home = "0.5.9" # Keeps the MSRV at v1.70.0 cargo-platform = "=0.1.7" diff --git a/crates/cargo-steel-lib/src/lib.rs b/crates/cargo-steel-lib/src/lib.rs index 02cef11cd..eef625e93 100644 --- a/crates/cargo-steel-lib/src/lib.rs +++ b/crates/cargo-steel-lib/src/lib.rs @@ -13,8 +13,33 @@ TODO: - Specify target architecture */ +pub fn steel_home() -> Option { + std::env::var("STEEL_HOME") + .ok() + .map(PathBuf::from) + .or_else(|| { + let home = home::home_dir(); + + home.map(|mut x: PathBuf| { + x.push(".steel"); + + // Just go ahead and initialize the directory, even though + // this is probably not the best place to do this. This almost + // assuredly could be lifted out of this check since failing here + // could cause some annoyance. + if !x.exists() { + if let Err(_) = std::fs::create_dir(&x) { + eprintln!("Unable to create steel home directory {:?}", x) + } + } + + x + }) + }) +} + pub fn run() -> Result<(), Box> { - let mut steel_home = PathBuf::from(std::env::var("STEEL_HOME")?); + let mut steel_home = steel_home().expect("Unable to find STEEL_HOME"); steel_home.push("native");