-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
1,256 additions
and
171 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
fn main() { | ||
cfg_aliases::cfg_aliases! { | ||
linux: { target_os = "linux" }, | ||
macos: { target_os = "macos" }, | ||
windows: { target_os = "windows" }, | ||
|
||
vfox: { any(feature = "vfox", target_os = "windows") }, | ||
asdf: { any(feature = "asdf", not(target_os = "windows")) }, | ||
} | ||
built::write_built_file().expect("Failed to acquire build-time information"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use crate::backend::asdf::AsdfBackend; | ||
use crate::cache::CacheManager; | ||
use crate::config::Config; | ||
use crate::dirs; | ||
use crate::hash::hash_to_str; | ||
use crate::tera::{get_tera, BASE_CONTEXT}; | ||
use crate::toolset::{ToolRequest, ToolVersion}; | ||
use eyre::{eyre, WrapErr}; | ||
use std::collections::{BTreeMap, HashMap}; | ||
use std::env; | ||
use std::sync::RwLock; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct ExternalPluginCache { | ||
list_bin_paths: RwLock<HashMap<ToolRequest, CacheManager<Vec<String>>>>, | ||
exec_env: RwLock<HashMap<ToolRequest, CacheManager<BTreeMap<String, String>>>>, | ||
} | ||
|
||
impl ExternalPluginCache { | ||
pub fn list_bin_paths<F>( | ||
&self, | ||
plugin: &AsdfBackend, | ||
tv: &ToolVersion, | ||
fetch: F, | ||
) -> eyre::Result<Vec<String>> | ||
where | ||
F: FnOnce() -> eyre::Result<Vec<String>>, | ||
{ | ||
let mut w = self.list_bin_paths.write().unwrap(); | ||
let cm = w.entry(tv.request.clone()).or_insert_with(|| { | ||
let list_bin_paths_filename = match &plugin.toml.list_bin_paths.cache_key { | ||
Some(key) => { | ||
let config = Config::get(); | ||
let key = render_cache_key(&config, tv, key); | ||
let filename = format!("{}-$KEY.msgpack.z", key); | ||
tv.cache_path().join("list_bin_paths").join(filename) | ||
} | ||
None => tv.cache_path().join("list_bin_paths-$KEY.msgpack.z"), | ||
}; | ||
CacheManager::new(list_bin_paths_filename) | ||
.with_fresh_file(dirs::DATA.to_path_buf()) | ||
.with_fresh_file(plugin.plugin_path.clone()) | ||
.with_fresh_file(tv.install_path()) | ||
}); | ||
cm.get_or_try_init(fetch).cloned() | ||
} | ||
|
||
pub fn exec_env<F>( | ||
&self, | ||
config: &Config, | ||
plugin: &AsdfBackend, | ||
tv: &ToolVersion, | ||
fetch: F, | ||
) -> eyre::Result<BTreeMap<String, String>> | ||
where | ||
F: FnOnce() -> eyre::Result<BTreeMap<String, String>>, | ||
{ | ||
let mut w = self.exec_env.write().unwrap(); | ||
let cm = w.entry(tv.request.clone()).or_insert_with(|| { | ||
let exec_env_filename = match &plugin.toml.exec_env.cache_key { | ||
Some(key) => { | ||
let key = render_cache_key(config, tv, key); | ||
let filename = format!("{}-$KEY.msgpack.z", key); | ||
tv.cache_path().join("exec_env").join(filename) | ||
} | ||
None => tv.cache_path().join("exec_env-$KEY.msgpack.z"), | ||
}; | ||
CacheManager::new(exec_env_filename) | ||
.with_fresh_file(dirs::DATA.to_path_buf()) | ||
.with_fresh_file(plugin.plugin_path.clone()) | ||
.with_fresh_file(tv.install_path()) | ||
}); | ||
cm.get_or_try_init(fetch).cloned() | ||
} | ||
} | ||
|
||
fn render_cache_key(config: &Config, tv: &ToolVersion, cache_key: &[String]) -> String { | ||
let elements = cache_key | ||
.iter() | ||
.map(|tmpl| { | ||
let s = parse_template(config, tv, tmpl).unwrap(); | ||
let s = s.trim().to_string(); | ||
trace!("cache key element: {} -> {}", tmpl.trim(), s); | ||
let mut s = hash_to_str(&s); | ||
s.truncate(10); | ||
s | ||
}) | ||
.collect::<Vec<String>>(); | ||
elements.join("-") | ||
} | ||
|
||
fn parse_template(config: &Config, tv: &ToolVersion, tmpl: &str) -> eyre::Result<String> { | ||
let mut ctx = BASE_CONTEXT.clone(); | ||
ctx.insert("project_root", &config.project_root); | ||
ctx.insert("opts", &tv.request.options()); | ||
get_tera( | ||
config | ||
.project_root | ||
.as_ref() | ||
.or(env::current_dir().as_ref().ok()) | ||
.map(|p| p.as_path()), | ||
) | ||
.render_str(tmpl, &ctx) | ||
.wrap_err_with(|| eyre!("failed to parse template: {tmpl}")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.