Skip to content

Commit

Permalink
refactor: remove duplicate remote_versions_caches
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Nov 26, 2024
1 parent 6882f53 commit 1d04a5d
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 216 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ jobs:
- name: Run e2e tests
uses: nick-fields/retry@v3
env:
GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_API_TOKEN: ${{ secrets.RTX_GITHUB_TOKEN || secrets.GITHUB_TOKEN }}
TEST_TRANCHE: ${{matrix.tranche}}
TEST_TRANCHE_COUNT: 8
TEST_ALL: 1
Expand Down
89 changes: 38 additions & 51 deletions src/backend/aqua.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::aqua::aqua_registry::{AquaChecksumType, AquaPackage, AquaPackageType, AQUA_REGISTRY};
use crate::backend::backend_type::BackendType;
use crate::backend::Backend;
use crate::cache::{CacheManager, CacheManagerBuilder};
use crate::cli::args::BackendArg;
use crate::cli::version::{ARCH, OS};
use crate::cmd::CmdLineRunner;
Expand All @@ -12,7 +11,7 @@ use crate::install_context::InstallContext;
use crate::plugins::VERSION_REGEX;
use crate::registry::REGISTRY;
use crate::toolset::ToolVersion;
use crate::{dirs, file, github};
use crate::{file, github};
use eyre::{bail, ContextCompat, Result};
use indexmap::IndexSet;
use itertools::Itertools;
Expand All @@ -25,7 +24,6 @@ use std::path::{Path, PathBuf};
pub struct AquaBackend {
ba: BackendArg,
id: String,
remote_version_cache: CacheManager<Vec<String>>,
}

impl Backend for AquaBackend {
Expand All @@ -42,47 +40,43 @@ impl Backend for AquaBackend {
}

fn _list_remote_versions(&self) -> eyre::Result<Vec<String>> {
self.remote_version_cache
.get_or_try_init(|| {
let pkg = AQUA_REGISTRY.package(&self.id)?;
if !pkg.repo_owner.is_empty() && !pkg.repo_name.is_empty() {
let versions = if let Some("github_tag") = pkg.version_source.as_deref() {
github::list_tags(&format!("{}/{}", pkg.repo_owner, pkg.repo_name))?
} else {
github::list_releases(&format!("{}/{}", pkg.repo_owner, pkg.repo_name))?
.into_iter()
.map(|r| r.tag_name)
.collect_vec()
};
Ok(versions
.into_iter()
.filter_map(|v| {
let mut v = v.as_str();
match pkg.version_filter_ok(v) {
Ok(true) => {}
Ok(false) => return None,
Err(e) => {
warn!("[{}] aqua version filter error: {e}", self.ba);
}
}
if let Some(prefix) = &pkg.version_prefix {
if let Some(_v) = v.strip_prefix(prefix) {
v = _v
} else {
return None;
}
}
v = v.strip_prefix('v').unwrap_or(v);
Some(v.to_string())
})
.rev()
.collect())
} else {
warn!("no aqua registry found for {}", self.ba);
Ok(vec![])
}
})
.cloned()
let pkg = AQUA_REGISTRY.package(&self.id)?;
if !pkg.repo_owner.is_empty() && !pkg.repo_name.is_empty() {
let versions = if let Some("github_tag") = pkg.version_source.as_deref() {
github::list_tags(&format!("{}/{}", pkg.repo_owner, pkg.repo_name))?
} else {
github::list_releases(&format!("{}/{}", pkg.repo_owner, pkg.repo_name))?
.into_iter()
.map(|r| r.tag_name)
.collect_vec()
};
Ok(versions
.into_iter()
.filter_map(|v| {
let mut v = v.as_str();
match pkg.version_filter_ok(v) {
Ok(true) => {}
Ok(false) => return None,
Err(e) => {
warn!("[{}] aqua version filter error: {e}", self.ba);
}
}
if let Some(prefix) = &pkg.version_prefix {
if let Some(_v) = v.strip_prefix(prefix) {
v = _v
} else {
return None;
}
}
v = v.strip_prefix('v').unwrap_or(v);
Some(v.to_string())
})
.rev()
.collect())
} else {
warn!("no aqua registry found for {}", self.ba);
Ok(vec![])
}
}

fn install_version_impl(
Expand Down Expand Up @@ -168,13 +162,6 @@ impl AquaBackend {
});
}
Self {
remote_version_cache: CacheManagerBuilder::new(
ba.cache_path.join("remote_versions.msgpack.z"),
)
.with_fresh_duration(SETTINGS.fetch_remote_versions_cache())
.with_fresh_file(dirs::DATA.to_path_buf())
.with_fresh_file(ba.installs_path.to_path_buf())
.build(),
id: id.to_string(),
ba,
}
Expand Down
18 changes: 1 addition & 17 deletions src/backend/asdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ pub struct AsdfBackend {
pub toml: MisePluginToml,
plugin: Box<AsdfPlugin>,
cache: ExternalPluginCache,
remote_version_cache: CacheManager<Vec<String>>,
latest_stable_cache: CacheManager<Option<String>>,
alias_cache: CacheManager<Vec<(String, String)>>,
legacy_filename_cache: CacheManager<Vec<String>>,
Expand All @@ -51,13 +50,6 @@ impl AsdfBackend {
let toml = MisePluginToml::from_file(&toml_path).unwrap();
Self {
cache: ExternalPluginCache::default(),
remote_version_cache: CacheManagerBuilder::new(
ba.cache_path.join("remote_versions.msgpack.z"),
)
.with_fresh_duration(SETTINGS.fetch_remote_versions_cache())
.with_fresh_file(plugin_path.clone())
.with_fresh_file(plugin_path.join("bin/list-all"))
.build(),
latest_stable_cache: CacheManagerBuilder::new(
ba.cache_path.join("latest_stable.msgpack.z"),
)
Expand Down Expand Up @@ -240,15 +232,7 @@ impl Backend for AsdfBackend {
}

fn _list_remote_versions(&self) -> Result<Vec<String>> {
self.remote_version_cache
.get_or_try_init(|| self.plugin.fetch_remote_versions())
.wrap_err_with(|| {
eyre!(
"Failed listing remote versions for asdf tool {}",
style(&self.name).blue().for_stderr(),
)
})
.cloned()
self.plugin.fetch_remote_versions()
}

fn latest_stable_version(&self) -> Result<Option<String>> {
Expand Down
35 changes: 11 additions & 24 deletions src/backend/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use url::Url;

use crate::backend::backend_type::BackendType;
use crate::backend::Backend;
use crate::cache::{CacheManager, CacheManagerBuilder};
use crate::cli::args::BackendArg;
use crate::cmd::CmdLineRunner;
use crate::config::{Config, SETTINGS};
Expand All @@ -20,7 +19,6 @@ use crate::{env, file};
#[derive(Debug)]
pub struct CargoBackend {
ba: BackendArg,
remote_version_cache: CacheManager<Vec<String>>,
}

impl Backend for CargoBackend {
Expand All @@ -45,20 +43,16 @@ impl Backend for CargoBackend {
// TODO: maybe fetch tags/branches from git?
return Ok(vec!["HEAD".into()]);
}
self.remote_version_cache
.get_or_try_init(|| {
let raw = HTTP_FETCH.get_text(get_crate_url(&self.tool_name())?)?;
let stream = Deserializer::from_str(&raw).into_iter::<CrateVersion>();
let mut versions = vec![];
for v in stream {
let v = v?;
if !v.yanked {
versions.push(v.vers);
}
}
Ok(versions)
})
.cloned()
let raw = HTTP_FETCH.get_text(get_crate_url(&self.tool_name())?)?;
let stream = Deserializer::from_str(&raw).into_iter::<CrateVersion>();
let mut versions = vec![];
for v in stream {
let v = v?;
if !v.yanked {
versions.push(v.vers);
}
}
Ok(versions)
}

fn install_version_impl(
Expand Down Expand Up @@ -134,14 +128,7 @@ impl Backend for CargoBackend {

impl CargoBackend {
pub fn from_arg(ba: BackendArg) -> Self {
Self {
remote_version_cache: CacheManagerBuilder::new(
ba.cache_path.join("remote_versions.msgpack.z"),
)
.with_fresh_duration(SETTINGS.fetch_remote_versions_cache())
.build(),
ba,
}
Self { ba }
}

fn is_binstall_enabled(&self, tv: &ToolVersion) -> bool {
Expand Down
20 changes: 5 additions & 15 deletions src/backend/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::toolset::ToolVersion;
#[derive(Debug)]
pub struct NPMBackend {
ba: BackendArg,
remote_version_cache: CacheManager<Vec<String>>,
latest_version_cache: CacheManager<Option<String>>,
}

Expand All @@ -33,15 +32,11 @@ impl Backend for NPMBackend {
}

fn _list_remote_versions(&self) -> eyre::Result<Vec<String>> {
self.remote_version_cache
.get_or_try_init(|| {
let raw = cmd!(NPM_PROGRAM, "view", self.tool_name(), "versions", "--json")
.full_env(self.dependency_env()?)
.read()?;
let versions: Vec<String> = serde_json::from_str(&raw)?;
Ok(versions)
})
.cloned()
let raw = cmd!(NPM_PROGRAM, "view", self.tool_name(), "versions", "--json")
.full_env(self.dependency_env()?)
.read()?;
let versions: Vec<String> = serde_json::from_str(&raw)?;
Ok(versions)
}

fn latest_stable_version(&self) -> eyre::Result<Option<String>> {
Expand Down Expand Up @@ -110,11 +105,6 @@ impl Backend for NPMBackend {
impl NPMBackend {
pub fn from_arg(ba: BackendArg) -> Self {
Self {
remote_version_cache: CacheManagerBuilder::new(
ba.cache_path.join("remote_versions.msgpack.z"),
)
.with_fresh_duration(SETTINGS.fetch_remote_versions_cache())
.build(),
latest_version_cache: CacheManagerBuilder::new(
ba.cache_path.join("latest_version.msgpack.z"),
)
Expand Down
46 changes: 19 additions & 27 deletions src/backend/pipx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use xx::regex;
#[derive(Debug)]
pub struct PIPXBackend {
ba: BackendArg,
remote_version_cache: CacheManager<Vec<String>>,
latest_version_cache: CacheManager<Option<String>>,
}

Expand All @@ -47,27 +46,25 @@ impl Backend for PIPXBackend {
* we return a single version.
*/
fn _list_remote_versions(&self) -> eyre::Result<Vec<String>> {
self.remote_version_cache
.get_or_try_init(|| match self.tool_name().parse()? {
PipxRequest::Pypi(package) => {
let url = format!("https://pypi.org/pypi/{}/json", package);
let data: PypiPackage = HTTP_FETCH.json(url)?;
let versions = data
.releases
.keys()
.map(|v| v.to_string())
.sorted_by_cached_key(|v| Versioning::new(v))
.collect();
Ok(versions)
}
PipxRequest::Git(url) if url.starts_with("https://github.com/") => {
let repo = url.strip_prefix("https://github.com/").unwrap();
let data = github::list_releases(repo)?;
Ok(data.into_iter().rev().map(|r| r.tag_name).collect())
}
PipxRequest::Git { .. } => Ok(vec!["latest".to_string()]),
})
.cloned()
match self.tool_name().parse()? {
PipxRequest::Pypi(package) => {
let url = format!("https://pypi.org/pypi/{}/json", package);
let data: PypiPackage = HTTP_FETCH.json(url)?;
let versions = data
.releases
.keys()
.map(|v| v.to_string())
.sorted_by_cached_key(|v| Versioning::new(v))
.collect();
Ok(versions)
}
PipxRequest::Git(url) if url.starts_with("https://github.com/") => {
let repo = url.strip_prefix("https://github.com/").unwrap();
let data = github::list_releases(repo)?;
Ok(data.into_iter().rev().map(|r| r.tag_name).collect())
}
PipxRequest::Git { .. } => Ok(vec!["latest".to_string()]),
}
}

fn latest_stable_version(&self) -> eyre::Result<Option<String>> {
Expand Down Expand Up @@ -132,11 +129,6 @@ impl Backend for PIPXBackend {
impl PIPXBackend {
pub fn from_arg(ba: BackendArg) -> Self {
Self {
remote_version_cache: CacheManagerBuilder::new(
ba.cache_path.join("remote_versions.msgpack.z"),
)
.with_fresh_duration(SETTINGS.fetch_remote_versions_cache())
.build(),
latest_version_cache: CacheManagerBuilder::new(
ba.cache_path.join("latest_version.msgpack.z"),
)
Expand Down
27 changes: 7 additions & 20 deletions src/backend/spm.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::backend::backend_type::BackendType;
use crate::backend::Backend;
use crate::cache::{CacheManager, CacheManagerBuilder};
use crate::cli::args::BackendArg;
use crate::cmd::CmdLineRunner;
use crate::config::{Settings, SETTINGS};
use crate::config::Settings;
use crate::install_context::InstallContext;
use crate::toolset::ToolVersion;
use crate::{file, github};
Expand All @@ -22,7 +21,6 @@ use xx::regex;
#[derive(Debug)]
pub struct SPMBackend {
ba: BackendArg,
remote_version_cache: CacheManager<Vec<String>>,
}

// https://github.com/apple/swift-package-manager
Expand All @@ -42,15 +40,11 @@ impl Backend for SPMBackend {

fn _list_remote_versions(&self) -> eyre::Result<Vec<String>> {
let repo = SwiftPackageRepo::new(&self.tool_name())?;
self.remote_version_cache
.get_or_try_init(|| {
Ok(github::list_releases(repo.shorthand.as_str())?
.into_iter()
.map(|r| r.tag_name)
.rev()
.collect())
})
.cloned()
Ok(github::list_releases(repo.shorthand.as_str())?
.into_iter()
.map(|r| r.tag_name)
.rev()
.collect())
}

fn install_version_impl(
Expand Down Expand Up @@ -89,14 +83,7 @@ impl Backend for SPMBackend {

impl SPMBackend {
pub fn from_arg(ba: BackendArg) -> Self {
Self {
remote_version_cache: CacheManagerBuilder::new(
ba.cache_path.join("remote_versions.msgpack.z"),
)
.with_fresh_duration(SETTINGS.fetch_remote_versions_cache())
.build(),
ba,
}
Self { ba }
}

fn clone_package_repo(
Expand Down
Loading

0 comments on commit 1d04a5d

Please sign in to comment.