Skip to content

Commit

Permalink
refactor: clean up arcs
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Nov 29, 2024
1 parent 169338a commit f49d330
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 43 deletions.
26 changes: 12 additions & 14 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fs::File;
use std::hash::Hash;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, MutexGuard};
use std::sync::{Arc, Mutex};

use crate::cache::{CacheManager, CacheManagerBuilder};
use crate::cli::args::{BackendArg, ToolVersionType};
Expand Down Expand Up @@ -48,9 +48,8 @@ pub type VersionCacheManager = CacheManager<Vec<String>>;

static TOOLS: Mutex<Option<Arc<BackendMap>>> = Mutex::new(None);

fn load_tools() -> MutexGuard<'static, Option<Arc<BackendMap>>> {
let mut memo_tools = TOOLS.lock().unwrap();
if memo_tools.is_some() {
fn load_tools() -> Arc<BackendMap> {
if let Some(memo_tools) = TOOLS.lock().unwrap().clone() {
return memo_tools;
}
time!("load_tools start");
Expand Down Expand Up @@ -79,35 +78,34 @@ fn load_tools() -> MutexGuard<'static, Option<Arc<BackendMap>>> {
.into_iter()
.map(|backend| (backend.ba().short.clone(), backend))
.collect();
*memo_tools = Some(Arc::new(tools.clone()));
let tools = Arc::new(tools);
*TOOLS.lock().unwrap() = Some(tools.clone());
time!("load_tools done");
memo_tools
tools
}

pub fn list() -> BackendList {
load_tools().as_ref().unwrap().values().cloned().collect()
load_tools().values().cloned().collect()
}

pub fn get(ba: &BackendArg) -> Option<ABackend> {
let mut m = load_tools();
let backends = m.as_mut().unwrap();
let backends = load_tools();
if let Some(backend) = backends.get(&ba.short) {
Some(backend.clone())
} else if let Some(backend) = arg_to_backend(ba.clone()) {
let mut backends = (**backends).clone();
let mut backends = backends.deref().clone();
backends.insert(ba.short.clone(), backend.clone());
*m = Some(Arc::new(backends));
*TOOLS.lock().unwrap() = Some(Arc::new(backends));
Some(backend)
} else {
None
}
}

pub fn remove(short: &str) {
let mut t = load_tools();
let mut backends = t.clone().unwrap().deref().clone();
let mut backends = load_tools().deref().clone();
backends.remove(short);
*t = Some(Arc::new(backends));
*TOOLS.lock().unwrap() = Some(Arc::new(backends));
}

pub fn arg_to_backend(ba: BackendArg) -> Option<ABackend> {
Expand Down
49 changes: 20 additions & 29 deletions src/toolset/install_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ use rayon::prelude::*;
use std::collections::BTreeMap;
use std::ops::Deref;
use std::path::PathBuf;
use std::sync::{Arc, Mutex, MutexGuard};
use std::sync::{Arc, Mutex};
use versions::Versioning;

type InstallStatePlugins = BTreeMap<String, PluginType>;
type InstallStateTools = BTreeMap<String, InstallStateTool>;
type MutexResult<T> = Result<MutexGuard<'static, Option<Arc<T>>>>;
type MutexResult<T> = Result<Arc<T>>;

#[derive(Debug, Clone)]
pub struct InstallStateTool {
Expand Down Expand Up @@ -46,12 +46,11 @@ pub(crate) fn init() -> Result<()> {
}

fn init_plugins() -> MutexResult<InstallStatePlugins> {
let mut mu = INSTALL_STATE_PLUGINS.lock().unwrap();
if mu.is_some() {
return Ok(mu);
if let Some(plugins) = INSTALL_STATE_PLUGINS.lock().unwrap().clone() {
return Ok(plugins);
}
let dirs = file::dir_subdirs(&dirs::PLUGINS)?;
let plugins = dirs
let plugins: InstallStatePlugins = dirs
.into_iter()
.filter_map(|d| {
time!("init_plugins {d}");
Expand All @@ -65,14 +64,14 @@ fn init_plugins() -> MutexResult<InstallStatePlugins> {
}
})
.collect();
*mu = Some(Arc::new(plugins));
Ok(mu)
let plugins = Arc::new(plugins);
*INSTALL_STATE_PLUGINS.lock().unwrap() = Some(plugins.clone());
Ok(plugins)
}

fn init_tools() -> MutexResult<InstallStateTools> {
let mut mu = INSTALL_STATE_TOOLS.lock().unwrap();
if mu.is_some() {
return Ok(mu);
if let Some(tools) = INSTALL_STATE_TOOLS.lock().unwrap().clone() {
return Ok(tools);
}
let mut tools = file::dir_subdirs(&dirs::INSTALLS)?
.into_par_iter()
Expand Down Expand Up @@ -105,7 +104,7 @@ fn init_tools() -> MutexResult<InstallStateTools> {
.flatten()
.filter(|(_, tool)| !tool.versions.is_empty())
.collect::<BTreeMap<_, _>>();
for (short, pt) in init_plugins()?.as_ref().unwrap().iter() {
for (short, pt) in init_plugins()?.iter() {
let full = match pt {
PluginType::Asdf => format!("asdf:{short}"),
PluginType::Vfox => format!("vfox:{short}"),
Expand All @@ -119,39 +118,34 @@ fn init_tools() -> MutexResult<InstallStateTools> {
});
tool.full = Some(full);
}
*mu = Some(Arc::new(tools));
Ok(mu)
let tools = Arc::new(tools);
*INSTALL_STATE_TOOLS.lock().unwrap() = Some(tools.clone());
Ok(tools)
}

pub fn list_plugins() -> Result<Arc<BTreeMap<String, PluginType>>> {
let plugins = init_plugins()?;
Ok(plugins.as_ref().unwrap().clone())
Ok(plugins)
}

pub fn get_tool_full(short: &str) -> Result<Option<String>> {
let tools = init_tools()?;
Ok(tools
.as_ref()
.unwrap()
.get(short)
.and_then(|t| t.full.clone()))
Ok(tools.get(short).and_then(|t| t.full.clone()))
}

pub fn get_plugin_type(short: &str) -> Result<Option<PluginType>> {
let plugins = init_plugins()?;
Ok(plugins.as_ref().unwrap().get(short).cloned())
Ok(plugins.get(short).cloned())
}

pub fn list_tools() -> Result<Arc<BTreeMap<String, InstallStateTool>>> {
let tools = init_tools()?;
Ok(tools.as_ref().unwrap().clone())
Ok(tools)
}

pub fn backend_type(short: &str) -> Result<Option<BackendType>> {
let tools = init_tools()?;
let backend_type = tools
.as_ref()
.unwrap()
.get(short)
.and_then(|ist| ist.full.as_ref())
.map(|full| BackendType::guess(full));
Expand All @@ -161,18 +155,15 @@ pub fn backend_type(short: &str) -> Result<Option<BackendType>> {
pub fn list_versions(short: &str) -> Result<Vec<String>> {
let tools = init_tools()?;
Ok(tools
.as_ref()
.unwrap()
.get(short)
.map(|tool| tool.versions.clone())
.unwrap_or_default())
}

pub fn add_plugin(short: &str, plugin_type: PluginType) -> Result<()> {
let mut p = init_plugins()?;
let mut plugins = p.take().unwrap().deref().clone();
let mut plugins = init_plugins()?.deref().clone();
plugins.insert(short.to_string(), plugin_type);
*p = Some(Arc::new(plugins));
*INSTALL_STATE_PLUGINS.lock().unwrap() = Some(Arc::new(plugins));
Ok(())
}

Expand Down

0 comments on commit f49d330

Please sign in to comment.