Skip to content

Commit

Permalink
config: added rwlock around plugins map
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Dec 12, 2023
1 parent b0e668f commit 9dfb125
Show file tree
Hide file tree
Showing 28 changed files with 118 additions and 142 deletions.
10 changes: 4 additions & 6 deletions src/cli/current.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ impl Current {
match &self.plugin {
Some(plugin_name) => {
let plugin_name = unalias_plugin(plugin_name);
match config.plugins.get(plugin_name) {
Some(plugin) => self.one(&config, ts, out, plugin.clone()),
None => {
warn!("Plugin {} is not installed", plugin_name);
Ok(())
}
let plugin = config.get_or_create_plugin(plugin_name);
if !plugin.is_installed() {
bail!("Plugin {} is not installed", plugin_name);

Check warning on line 30 in src/cli/current.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/current.rs#L30

Added line #L30 was not covered by tests
}
self.one(&config, ts, out, plugin.clone())
}
None => self.all(&config, ts, out),
}
Expand Down
6 changes: 3 additions & 3 deletions src/cli/doctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Doctor {
);

let mut checks = Vec::new();
for plugin in config.plugins.values() {
for plugin in config.list_plugins() {

Check warning on line 48 in src/cli/doctor.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/doctor.rs#L48

Added line #L48 was not covered by tests
if !plugin.is_installed() {
checks.push(format!("plugin {} is not installed", &plugin.name()));
continue;
Expand Down Expand Up @@ -123,8 +123,8 @@ fn render_config_files(config: &Config) -> String {
fn render_plugins(config: &Config) -> String {
let mut s = style("plugins:\n").bold().to_string();
let plugins = config
.plugins
.values()
.list_plugins()
.into_iter()

Check warning on line 127 in src/cli/doctor.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/doctor.rs#L126-L127

Added lines #L126 - L127 were not covered by tests
.filter(|p| p.is_installed())
.collect::<Vec<_>>();
let max_plugin_name_len = plugins.iter().map(|p| p.name().len()).max().unwrap_or(0) + 2;
Expand Down
21 changes: 7 additions & 14 deletions src/cli/external.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
use clap::{ArgMatches, Command};
use color_eyre::eyre::Result;
use itertools::Itertools;
use rayon::prelude::*;

use crate::config::Config;

pub fn commands(config: &Config) -> Vec<Command> {
config
.plugins
.values()
.collect_vec()
.list_plugins()
.into_par_iter()
.flat_map(|p| match p.external_commands() {
Ok(commands) => commands,
Err(e) => {
warn!(
"failed to load external commands for plugin {}: {:#}",
p.name(),
e
);
.flat_map(|p| {
p.external_commands().unwrap_or_else(|e| {
let p = p.name();
warn!("failed to load external commands for plugin {p}: {e:#}");

Check warning on line 14 in src/cli/external.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/external.rs#L13-L14

Added lines #L13 - L14 were not covered by tests
vec![]
}
})
})
.collect()
}
Expand All @@ -36,7 +29,7 @@ pub fn execute(
.find(|c| c.get_name() == plugin)
{
if let Some((subcommand, matches)) = args.subcommand() {
let plugin = config.plugins.get(&plugin.to_string()).unwrap();
let plugin = config.get_or_create_plugin(plugin);

Check warning on line 32 in src/cli/external.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/external.rs#L32

Added line #L32 was not covered by tests
let args: Vec<String> = matches
.get_raw("args")
.unwrap_or_default()
Expand Down
10 changes: 5 additions & 5 deletions src/cli/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ impl Install {
warn!("specify a version with `rtx install <PLUGIN>@<VERSION>`");
return Ok(());
}
ts.install_versions(&mut config, tool_versions, &mpr, self.force)
ts.install_versions(&config, tool_versions, &mpr, self.force)
}

fn get_requested_tool_versions(
&self,
config: &mut Config,
config: &Config,
ts: &Toolset,
runtimes: &[ToolArg],
mpr: &MultiProgressReport,
Expand Down Expand Up @@ -102,10 +102,10 @@ impl Install {
Ok(tool_versions)
}

fn install_missing_runtimes(&self, mut config: Config) -> Result<()> {
fn install_missing_runtimes(&self, config: Config) -> Result<()> {
let mut ts = ToolsetBuilder::new()
.with_latest_versions()
.build(&mut config)?;
.build(&config)?;
let versions = ts
.list_missing_versions(&config)
.into_iter()
Expand All @@ -116,7 +116,7 @@ impl Install {
return Ok(());
}
let mpr = MultiProgressReport::new(&config.settings);
ts.install_versions(&mut config, versions, &mpr, self.force)?;
ts.install_versions(&config, versions, &mpr, self.force)?;
Ok(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct Link {
}

impl Link {
pub fn run(self, mut config: Config, _out: &mut Output) -> Result<()> {
pub fn run(self, config: Config, _out: &mut Output) -> Result<()> {
let version = match self.tool.tvr {
Some(ref tvr) => tvr.version(),
None => {
Expand Down
11 changes: 7 additions & 4 deletions src/cli/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ impl Ls {
fn verify_plugin(&self, config: &Config) -> Result<()> {
match &self.plugin {
Some(plugin_name) => {
let plugin = config.plugins.get(plugin_name);
if plugin.is_none() || !plugin.unwrap().is_installed() {
let plugin = config.get_or_create_plugin(plugin_name);
if !plugin.is_installed() {
return Err(PluginNotInstalled(plugin_name.clone()))?;
}
}
Expand Down Expand Up @@ -209,12 +209,11 @@ impl Ls {
Ok(())
}

fn get_runtime_list(&self, config: &mut Config) -> Result<Vec<RuntimeRow>> {
fn get_runtime_list(&self, config: &Config) -> Result<Vec<RuntimeRow>> {
let mut tsb = ToolsetBuilder::new().with_global_only(self.global);

if let Some(plugin) = &self.plugin {
tsb = tsb.with_tools(&[plugin]);
config.plugins.retain(|p, _| p == plugin);
}
let ts = tsb.build(config)?;
let mut versions: HashMap<(String, String), (Arc<dyn Plugin>, ToolVersion)> = ts
Expand All @@ -233,6 +232,10 @@ impl Ls {

let rvs: Vec<RuntimeRow> = versions
.into_iter()
.filter(|((plugin_name, _), _)| match &self.plugin {
Some(p) => p.eq(plugin_name),
None => true,
})
.sorted_by_cached_key(|((plugin_name, version), _)| {
(
plugin_name.clone(),
Expand Down
7 changes: 3 additions & 4 deletions src/cli/ls_remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ impl LsRemote {

fn run_all(self, config: Config, out: &mut Output) -> Result<()> {
let versions = config
.plugins
.values()
.par_bridge()
.list_plugins()
.into_par_iter()

Check warning on line 72 in src/cli/ls_remote.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/ls_remote.rs#L71-L72

Added lines #L71 - L72 were not covered by tests
.map(|p| {
let versions = p.list_remote_versions(&config.settings)?;
Ok((p, versions))
Expand All @@ -87,7 +86,7 @@ impl LsRemote {
Ok(())
}

fn get_plugin(&self, config: &mut Config) -> Result<Option<Arc<dyn Plugin>>> {
fn get_plugin(&self, config: &Config) -> Result<Option<Arc<dyn Plugin>>> {
match &self.plugin {
Some(tool_arg) => {
let plugin = config.get_or_create_plugin(&tool_arg.plugin);
Expand Down
2 changes: 1 addition & 1 deletion src/cli/plugins/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl PluginsInstall {

fn install_one(
&self,
config: &mut Config,
config: &Config,
name: PluginName,
git_url: Option<String>,
mpr: &MultiProgressReport,
Expand Down
2 changes: 1 addition & 1 deletion src/cli/plugins/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub struct PluginsLs {

impl PluginsLs {
pub fn run(self, config: Config, out: &mut Output) -> Result<()> {
let mut tools = config.plugins.values().cloned().collect::<BTreeSet<_>>();
let mut tools = config.list_plugins().into_iter().collect::<BTreeSet<_>>();

if self.all {
for (plugin, url) in config.get_shorthands() {
Expand Down
6 changes: 3 additions & 3 deletions src/cli/plugins/ls_remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ pub struct PluginsLsRemote {
impl PluginsLsRemote {
pub fn run(self, config: Config, out: &mut Output) -> Result<()> {
let installed_plugins = config
.plugins
.values()
.list_plugins()
.into_iter()
.filter(|p| p.is_installed())
.map(|p| p.name())
.map(|p| p.name().to_string())
.collect::<HashSet<_>>();

let shorthands = config.get_shorthands().iter().sorted().collect_vec();
Expand Down
10 changes: 7 additions & 3 deletions src/cli/plugins/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ impl PluginsUninstall {
let mpr = MultiProgressReport::new(&config.settings);

let plugins = match self.all {
true => config.plugins.keys().cloned().collect(),
true => config
.list_plugins()
.into_iter()
.map(|p| p.name().to_string())
.collect(),

Check warning on line 35 in src/cli/plugins/uninstall.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/plugins/uninstall.rs#L31-L35

Added lines #L31 - L35 were not covered by tests
false => self.plugin.clone(),
};

Expand All @@ -45,8 +49,8 @@ impl PluginsUninstall {
plugin_name: &str,
mpr: &MultiProgressReport,
) -> Result<()> {
match config.plugins.get(plugin_name) {
Some(plugin) if plugin.is_installed() => {
match config.get_or_create_plugin(plugin_name) {
plugin if plugin.is_installed() => {
let mut pr = mpr.add();
plugin.decorate_progress_bar(&mut pr, None);
plugin.uninstall(&pr)?;
Expand Down
7 changes: 2 additions & 5 deletions src/cli/plugins/update.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use color_eyre::eyre::{eyre, Result};
use console::style;
use color_eyre::eyre::Result;

use crate::config::Config;
use crate::output::Output;
Expand Down Expand Up @@ -31,9 +30,7 @@ impl Update {
None => (p.as_str(), None),
};
let p = unalias_plugin(p);
let plugin = config.plugins.get(p).ok_or_else(|| {
eyre!("plugin {} not found", style(p).cyan().for_stderr())
})?;
let plugin = config.get_or_create_plugin(p);
Ok((plugin.clone(), ref_))
})
.collect::<Result<_>>()?,
Expand Down
2 changes: 1 addition & 1 deletion src/cli/prune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Prune {

for cf in config.get_tracked_config_files()?.values() {
let mut ts = cf.to_toolset().clone();
ts.resolve(&mut config);
ts.resolve(&config);
for (_, tv) in ts.list_current_versions(&config) {
to_delete.remove(&tv.to_string());
}
Expand Down
6 changes: 3 additions & 3 deletions src/cli/sync/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl SyncNode {
Ok(())
}

fn run_brew(self, mut config: Config, out: &mut Output) -> Result<()> {
fn run_brew(self, config: Config, out: &mut Output) -> Result<()> {

Check warning on line 51 in src/cli/sync/node.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/sync/node.rs#L51

Added line #L51 was not covered by tests
let tool = config.get_or_create_plugin(&PluginName::from("node"));

let brew_prefix = PathBuf::from(cmd!("brew", "--prefix").read()?).join("opt");
Expand All @@ -69,7 +69,7 @@ impl SyncNode {
config.rebuild_shims_and_runtime_symlinks()
}

fn run_nvm(self, mut config: Config, out: &mut Output) -> Result<()> {
fn run_nvm(self, config: Config, out: &mut Output) -> Result<()> {

Check warning on line 72 in src/cli/sync/node.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/sync/node.rs#L72

Added line #L72 was not covered by tests
let tool = config.get_or_create_plugin(&PluginName::from("node"));

let nvm_versions_path = NVM_DIR.join("versions").join("node");
Expand All @@ -87,7 +87,7 @@ impl SyncNode {
config.rebuild_shims_and_runtime_symlinks()
}

fn run_nodenv(self, mut config: Config, out: &mut Output) -> Result<()> {
fn run_nodenv(self, config: Config, out: &mut Output) -> Result<()> {

Check warning on line 90 in src/cli/sync/node.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/sync/node.rs#L90

Added line #L90 was not covered by tests
let tool = config.get_or_create_plugin(&PluginName::from("node"));

let nodenv_versions_path = NODENV_ROOT.join("versions");
Expand Down
2 changes: 1 addition & 1 deletion src/cli/sync/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct SyncPython {
}

impl SyncPython {
pub fn run(self, mut config: Config, out: &mut Output) -> Result<()> {
pub fn run(self, config: Config, out: &mut Output) -> Result<()> {
let python = config.get_or_create_plugin(&PluginName::from("python"));

let pyenv_versions_path = PYENV_ROOT.join("versions");
Expand Down
2 changes: 1 addition & 1 deletion src/cli/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Upgrade {
Ok(())
}

fn upgrade(&self, config: &mut Config, outdated: OutputVec, out: &mut Output) -> Result<()> {
fn upgrade(&self, config: &Config, outdated: OutputVec, out: &mut Output) -> Result<()> {
let mpr = MultiProgressReport::new(&config.settings);
let mut ts = ToolsetBuilder::new().with_args(&self.tool).build(config)?;

Expand Down
7 changes: 2 additions & 5 deletions src/cli/where.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use color_eyre::eyre::Result;

use crate::cli::args::tool::{ToolArg, ToolArgParser};
use crate::config::Config;
use crate::errors::Error::{PluginNotInstalled, VersionNotInstalled};
use crate::errors::Error::VersionNotInstalled;
use crate::output::Output;
use crate::toolset::ToolsetBuilder;

Expand Down Expand Up @@ -47,10 +47,7 @@ impl Where {
_ => self.tool,
};

let plugin = match config.plugins.get(&runtime.plugin) {
Some(plugin) => plugin,
None => Err(PluginNotInstalled(runtime.plugin.clone()))?,
};
let plugin = config.get_or_create_plugin(&runtime.plugin);

match runtime
.tvr
Expand Down
2 changes: 1 addition & 1 deletion src/cli/which.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Which {
None => Err(eyre!("{} not found", self.bin_name)),
}
}
fn get_toolset(&self, config: &mut Config) -> Result<Toolset> {
fn get_toolset(&self, config: &Config) -> Result<Toolset> {
let mut tsb = ToolsetBuilder::new();
if let Some(tool) = &self.tool {
tsb = tsb.with_args(&[tool.clone()]);
Expand Down
9 changes: 2 additions & 7 deletions src/config/config_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,7 @@ pub trait ConfigFile: Debug + Display + Send + Sync {
}

impl dyn ConfigFile {
pub fn add_runtimes(
&mut self,
config: &mut Config,
runtimes: &[ToolArg],
pin: bool,
) -> Result<()> {
pub fn add_runtimes(&mut self, config: &Config, runtimes: &[ToolArg], pin: bool) -> Result<()> {
// TODO: this has become a complete mess and could probably be greatly simplified
let mpr = MultiProgressReport::new(&config.settings);
let mut ts = self.to_toolset().to_owned();
Expand Down Expand Up @@ -104,7 +99,7 @@ impl dyn ConfigFile {
.into_iter()
.map(|tvr| {
if pin {
let plugin = config.plugins.get(&plugin).unwrap();
let plugin = config.get_or_create_plugin(&plugin);
let tv = tvr.resolve(
config,
plugin.clone(),
Expand Down
Loading

0 comments on commit 9dfb125

Please sign in to comment.