Skip to content

Commit

Permalink
use rtx-versions.jdx.dev to fetch remote versions (#1136)
Browse files Browse the repository at this point in the history
This should improve performance a tad and will also make rtx work better in cases where it does not have GITHUB_API_TOKEN set and is rate-limited on GitHub
  • Loading branch information
jdx authored Dec 11, 2023
1 parent bac0036 commit b62fed7
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 10 deletions.
31 changes: 31 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions src/plugins/core/bun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::config::Settings;
use crate::github::GithubRelease;
use crate::install_context::InstallContext;
use crate::plugins::core::CorePlugin;
use crate::plugins::Plugin;
use crate::plugins::{Plugin, HTTP};
use crate::toolset::{ToolVersion, ToolVersionRequest};
use crate::ui::progress_report::ProgressReport;
use crate::{file, http};
Expand All @@ -27,9 +27,12 @@ impl BunPlugin {
}

fn fetch_remote_versions(&self) -> Result<Vec<String>> {
let http = http::Client::new()?;
match self.core.fetch_remote_versions_from_rtx() {
Ok(versions) => return Ok(versions),
Err(e) => warn!("failed to fetch remote versions: {}", e),
}
let releases: Vec<GithubRelease> =
http.json("https://api.github.com/repos/oven-sh/bun/releases?per_page=100")?;
HTTP.json("https://api.github.com/repos/oven-sh/bun/releases?per_page=100")?;
let versions = releases
.into_iter()
.map(|r| r.tag_name)
Expand Down
9 changes: 6 additions & 3 deletions src/plugins/core/deno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::config::{Config, Settings};
use crate::github::GithubRelease;
use crate::install_context::InstallContext;
use crate::plugins::core::CorePlugin;
use crate::plugins::Plugin;
use crate::plugins::{Plugin, HTTP};
use crate::toolset::{ToolVersion, ToolVersionRequest, Toolset};
use crate::ui::progress_report::ProgressReport;
use crate::{file, http};
Expand All @@ -28,9 +28,12 @@ impl DenoPlugin {
}

fn fetch_remote_versions(&self) -> Result<Vec<String>> {
let http = http::Client::new()?;
match self.core.fetch_remote_versions_from_rtx() {
Ok(versions) => return Ok(versions),
Err(e) => warn!("failed to fetch remote versions: {}", e),
}
let releases: Vec<GithubRelease> =
http.json("https://api.github.com/repos/denoland/deno/releases?per_page=100")?;
HTTP.json("https://api.github.com/repos/denoland/deno/releases?per_page=100")?;
let versions = releases
.into_iter()
.map(|r| r.name)
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/core/go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ impl GoPlugin {
}

fn fetch_remote_versions(&self) -> Result<Vec<String>> {
match self.core.fetch_remote_versions_from_rtx() {
Ok(versions) => return Ok(versions),
Err(e) => warn!("failed to fetch remote versions: {}", e),
}
CorePlugin::run_fetch_task_with_timeout(move || {
let repo = &*env::RTX_GO_REPO;
let output = cmd!("git", "ls-remote", "--tags", repo, "go*").read()?;
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/core/java.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ impl JavaPlugin {
}

fn fetch_remote_versions(&self) -> Result<Vec<String>> {
match self.core.fetch_remote_versions_from_rtx() {
Ok(versions) => return Ok(versions),
Err(e) => warn!("failed to fetch remote versions: {}", e),
}
let versions = self
.fetch_java_metadata("ga")?
.iter()
Expand Down
14 changes: 13 additions & 1 deletion src/plugins/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::plugins::core::java::JavaPlugin;
use crate::plugins::core::node::NodePlugin;
use crate::plugins::core::node_build::NodeBuildPlugin;
use crate::plugins::core::ruby::RubyPlugin;
use crate::plugins::{Plugin, PluginName};
use crate::plugins::{Plugin, PluginName, HTTP};
use crate::timeout::run_with_timeout;
use crate::toolset::ToolVersion;
use crate::{dirs, env};
Expand Down Expand Up @@ -63,6 +63,7 @@ pub static EXPERIMENTAL_CORE_PLUGINS: Lazy<PluginMap> = Lazy::new(|| {

#[derive(Debug)]
pub struct CorePlugin {
pub name: &'static str,
pub cache_path: PathBuf,
pub remote_version_cache: CacheManager<Vec<String>>,
}
Expand All @@ -71,6 +72,7 @@ impl CorePlugin {
pub fn new(name: &'static str) -> Self {
let cache_path = dirs::CACHE.join(name);
Self {
name,
remote_version_cache: CacheManager::new(cache_path.join("remote_versions.msgpack.z"))
.with_fresh_duration(*env::RTX_FETCH_REMOTE_VERSIONS_CACHE),
cache_path,
Expand All @@ -90,4 +92,14 @@ impl CorePlugin {
{
run_with_timeout(f, *env::RTX_FETCH_REMOTE_VERSIONS_TIMEOUT)
}

pub fn fetch_remote_versions_from_rtx(&self) -> Result<Vec<String>> {
let versions = HTTP
.get_text(format!("http://rtx-versions.jdx.dev/{}", &self.name))?
.lines()
.map(|v| v.trim().to_string())
.filter(|v| !v.is_empty())
.collect();
Ok(versions)
}
}
4 changes: 4 additions & 0 deletions src/plugins/core/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ impl PythonPlugin {
}

fn fetch_remote_versions(&self) -> Result<Vec<String>> {
match self.core.fetch_remote_versions_from_rtx() {
Ok(versions) => return Ok(versions),
Err(e) => warn!("failed to fetch remote versions: {}", e),
}
self.install_or_update_python_build()?;
let python_build_bin = self.python_build_bin();
CorePlugin::run_fetch_task_with_timeout(move || {
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/core/ruby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ impl RubyPlugin {
}

fn fetch_remote_versions(&self) -> Result<Vec<String>> {
match self.core.fetch_remote_versions_from_rtx() {
Ok(versions) => return Ok(versions),
Err(e) => warn!("failed to fetch remote versions: {}", e),
}
self.update_build_tool()?;
let ruby_build_bin = self.ruby_build_bin();
let versions = CorePlugin::run_fetch_task_with_timeout(move || {
Expand Down
39 changes: 37 additions & 2 deletions src/plugins/external_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use once_cell::sync::Lazy;

use crate::cache::CacheManager;
use crate::config::{Config, Settings};
use crate::default_shorthands::DEFAULT_SHORTHANDS;
use crate::env::RTX_FETCH_REMOTE_VERSIONS_TIMEOUT;
use crate::env_diff::{EnvDiff, EnvDiffOperation};
use crate::errors::Error::PluginNotInstalled;
Expand All @@ -24,13 +25,13 @@ use crate::install_context::InstallContext;
use crate::plugins::external_plugin_cache::ExternalPluginCache;
use crate::plugins::rtx_plugin_toml::RtxPluginToml;
use crate::plugins::Script::{Download, ExecEnv, Install, ParseLegacyFile};
use crate::plugins::{Plugin, PluginName, PluginType, Script, ScriptManager};
use crate::plugins::{Plugin, PluginName, PluginType, Script, ScriptManager, HTTP};
use crate::timeout::run_with_timeout;
use crate::toolset::{ToolVersion, ToolVersionRequest, Toolset};
use crate::ui::multi_progress_report::MultiProgressReport;
use crate::ui::progress_report::ProgressReport;
use crate::ui::prompt;
use crate::{dirs, env, file};
use crate::{dirs, env, file, http};

/// This represents a plugin installed to ~/.local/share/rtx/plugins
pub struct ExternalPlugin {
Expand Down Expand Up @@ -137,7 +138,41 @@ impl ExternalPlugin {
Ok(())
}

fn fetch_versions(&self) -> Result<Option<Vec<String>>> {
// ensure that we're using a default shorthand plugin
let git = Git::new(self.plugin_path.to_path_buf());
if git.get_remote_url()
!= DEFAULT_SHORTHANDS
.get(self.name.as_str())
.map(|s| s.to_string())
{
return Ok(None);
}
let versions = match HTTP.get_text(format!("http://rtx-versions.jdx.dev/{}", self.name)) {
Err(err) if http::error_code(&err) == Some(404) => return Ok(None),
res => res?,
};
let versions = versions
.lines()
.map(|v| v.trim().to_string())
.filter(|v| !v.is_empty())
.collect_vec();
match versions.is_empty() {
true => Ok(None),
false => Ok(Some(versions)),
}
}

fn fetch_remote_versions(&self, settings: &Settings) -> Result<Vec<String>> {
match self.fetch_versions() {
Ok(Some(versions)) => return Ok(versions),
Err(err) => warn!(
"Failed to fetch remote versions for plugin {}: {}",
style(&self.name).cyan().for_stderr(),
err
),
_ => {}
};
let cmd = self.script_man.cmd(settings, &Script::ListAll);
let result = run_with_timeout(
move || {
Expand Down
7 changes: 6 additions & 1 deletion src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@ use color_eyre::eyre::Result;
use console::style;
use eyre::WrapErr;
use itertools::Itertools;
use once_cell::sync::Lazy;
use regex::Regex;
use versions::Versioning;

pub use external_plugin::ExternalPlugin;
pub use script_manager::{Script, ScriptManager};

use crate::config::{Config, Settings};
use crate::env::RTX_FETCH_REMOTE_VERSIONS_TIMEOUT;
use crate::file::{display_path, remove_all, remove_all_with_warning};
use crate::install_context::InstallContext;
use crate::lock_file::LockFile;
use crate::runtime_symlinks::is_runtime_symlink;
use crate::toolset::{ToolVersion, ToolVersionRequest, Toolset};
use crate::ui::multi_progress_report::MultiProgressReport;
use crate::ui::progress_report::{ProgressReport, PROG_TEMPLATE};
use crate::{dirs, file};
use crate::{dirs, file, http};

pub mod core;
mod external_plugin;
Expand All @@ -33,6 +35,9 @@ mod script_manager;

pub type PluginName = String;

pub static HTTP: Lazy<http::Client> =
Lazy::new(|| http::Client::new_with_timeout(*RTX_FETCH_REMOTE_VERSIONS_TIMEOUT).unwrap());

pub trait Plugin: Debug + Send + Sync {
fn name(&self) -> &str;
fn get_type(&self) -> PluginType {
Expand Down

0 comments on commit b62fed7

Please sign in to comment.