Skip to content

Commit

Permalink
Merge pull request #137 from sigmaSd/idirs
Browse files Browse the repository at this point in the history
allow configuring allow irust directories
  • Loading branch information
sigmaSd committed May 5, 2024
2 parents a8658b0 + cea2ac0 commit eb4a549
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 12 deletions.
2 changes: 1 addition & 1 deletion crates/irust/src/irust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl IRust {
options.main_result,
options.edition,
// prelude dir
(|| Some(dirs::data_dir()?.join("irust")))(),
(|| Some(crate::utils::irust_dirs::data_dir()?.join("irust")))(),
)
.expect("Could not create repl");

Expand Down
4 changes: 2 additions & 2 deletions crates/irust/src/irust/highlight/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crossterm::style::Color;
use serde::{Deserialize, Serialize};

fn themes_path() -> Result<std::path::PathBuf> {
Ok(dirs::config_dir()
Ok(crate::utils::irust_dirs::config_dir()
.ok_or("Error accessing config_dir")?
.join("irust")
.join("themes"))
Expand Down Expand Up @@ -38,7 +38,7 @@ pub fn theme_or_create_default(name: String) -> Theme {

pub fn installed_themes() -> Result<Vec<DirEntry>> {
Ok(std::fs::read_dir(
dirs::config_dir()
crate::utils::irust_dirs::config_dir()
.ok_or("Error accessing config_dir")?
.join("irust")
.join("themes"),
Expand Down
2 changes: 1 addition & 1 deletion crates/irust/src/irust/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct History {

impl History {
pub fn new(irust_dir: PathBuf) -> Result<Self> {
let history_file_path = if let Some(cache_dir) = dirs::cache_dir() {
let history_file_path = if let Some(cache_dir) = crate::utils::irust_dirs::cache_dir() {
let irust_cache = cache_dir.join("irust");
let _ = std::fs::create_dir_all(&irust_cache);
irust_cache.join("history")
Expand Down
2 changes: 1 addition & 1 deletion crates/irust/src/irust/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl Options {
}

pub fn config_path() -> Option<std::path::PathBuf> {
let config_dir = match dirs::config_dir() {
let config_dir = match crate::utils::irust_dirs::config_dir() {
Some(dir) => dir.join("irust"),
None => return None,
};
Expand Down
4 changes: 2 additions & 2 deletions crates/irust/src/irust/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ impl IRust {
.to_string();
match buffer.as_str() {
"" => {
if let Some(dir) = dirs::home_dir() {
if let Some(dir) = crate::utils::irust_dirs::home_dir() {
set_current_dir(dir)?;
}
}
Expand Down Expand Up @@ -940,7 +940,7 @@ impl IRust {
return Err("Failed to execute expression".into());
}

let dbg_cmds_path = env::temp_dir().join("irust_dbg_cmds");
let dbg_cmds_path = crate::utils::irust_dirs::temp_dir().join("irust_dbg_cmds");
{
let mut dbg_cmds = std::fs::File::create(&dbg_cmds_path)?;
writeln!(&mut dbg_cmds, "b {expr_line_num}")?;
Expand Down
10 changes: 7 additions & 3 deletions crates/irust/src/irust/script/script_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ macro_rules! mtry {
impl ScriptManager {
pub fn new() -> Option<Self> {
let mut sm = rscript::ScriptManager::default();
let script_path = dirs::config_dir()?.join("irust").join("script");
let script_path = crate::utils::irust_dirs::config_dir()?
.join("irust")
.join("script");
sm.add_scripts_by_path(
&script_path,
rscript::Version::parse(crate::args::VERSION).expect("correct version"),
Expand All @@ -35,7 +37,9 @@ impl ScriptManager {
}

// read conf if available
let script_conf_path = dirs::config_dir()?.join("irust").join(SCRIPT_CONFIG_NAME);
let script_conf_path = crate::utils::irust_dirs::config_dir()?
.join("irust")
.join(SCRIPT_CONFIG_NAME);

// ignore any error that happens while trying to read conf
// If an error happens, a new configuration will be written anyway when ScriptManager is
Expand Down Expand Up @@ -75,7 +79,7 @@ impl Drop for ScriptManager {
}
// Ignore errors on drop
let _ = mtry!({
let script_conf_path = dirs::config_dir()
let script_conf_path = crate::utils::irust_dirs::config_dir()
.ok_or("could not find config directory")?
.join("irust")
.join(SCRIPT_CONFIG_NAME);
Expand Down
33 changes: 33 additions & 0 deletions crates/irust/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,36 @@ fn visit_dirs(
}
Ok(())
}

pub mod irust_dirs {
pub fn config_dir() -> Option<std::path::PathBuf> {
if let Ok(dir) = std::env::var("IRUST_CONFIG_DIR") {
return Some(dir.into());
}
dirs::config_dir()
}
pub fn data_dir() -> Option<std::path::PathBuf> {
if let Ok(dir) = std::env::var("IRUST_DATA_DIR") {
return Some(dir.into());
}
dirs::data_dir()
}
pub fn home_dir() -> Option<std::path::PathBuf> {
if let Ok(dir) = std::env::var("IRUST_HOME_DIR") {
return Some(dir.into());
}
dirs::home_dir()
}
pub fn cache_dir() -> Option<std::path::PathBuf> {
if let Ok(dir) = std::env::var("IRUST_CACHE_DIR") {
return Some(dir.into());
}
dirs::cache_dir()
}
pub fn temp_dir() -> std::path::PathBuf {
if let Ok(dir) = std::env::var("IRUST_TEMP_DIR") {
return dir.into();
}
std::env::temp_dir()
}
}
8 changes: 6 additions & 2 deletions crates/irust_repl/src/cargo_cmds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use crate::{
use std::io;
use std::io::prelude::*;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::process::{Command, ExitStatus};
use std::sync::OnceLock;
use std::{env::temp_dir, process::Stdio};
use std::{fs, process};

static NO_COLOR: OnceLock<bool> = OnceLock::new();
Expand Down Expand Up @@ -54,7 +54,11 @@ pub struct CargoPaths {

impl CargoPaths {
fn new(name: &str) -> Self {
let tmp_dir = temp_dir();
let tmp_dir = if let Ok(dir) = std::env::var("IRUST_TEMP_DIR") {
dir.into()
} else {
std::env::temp_dir()
};
let common_root = tmp_dir.join("irust_repls");
let irust_dir = common_root.join(name);
let irust_target_dir = (|| {
Expand Down

0 comments on commit eb4a549

Please sign in to comment.