Skip to content

Commit

Permalink
lazily create warg registry client (#123)
Browse files Browse the repository at this point in the history
Fixes #122
  • Loading branch information
calvinrp authored May 20, 2024
1 parent 92342a0 commit 7f789ad
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
3 changes: 3 additions & 0 deletions crates/wac-resolver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ use wac_types::BorrowedPackageKey;
#[derive(thiserror::Error, Diagnostic, Debug)]
#[diagnostic(code("failed to resolve document"))]
pub enum Error {
/// Failed to create registry client.
#[error("failed to create registry client: {0:#}")]
RegistryClientFailed(anyhow::Error),
/// An unknown package was encountered.
#[error("unknown package `{name}`")]
UnknownPackage {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl EncodeCommand {

let document = Document::parse(&contents).map_err(|e| fmt_err(e, &self.path, &contents))?;

let resolver = PackageResolver::new(
let mut resolver = PackageResolver::new(
self.deps_dir,
self.deps.into_iter().collect(),
#[cfg(feature = "registry")]
Expand Down
2 changes: 1 addition & 1 deletion src/commands/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl ResolveCommand {

let document = Document::parse(&contents).map_err(|e| fmt_err(e, &self.path, &contents))?;

let resolver = PackageResolver::new(
let mut resolver = PackageResolver::new(
self.deps_dir,
self.deps.into_iter().collect(),
#[cfg(feature = "registry")]
Expand Down
32 changes: 24 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn fmt_err(e: impl Into<Report>, path: &Path, source: &str) -> anyhow::Error {
pub struct PackageResolver {
fs: FileSystemPackageResolver,
#[cfg(feature = "registry")]
registry: wac_resolver::RegistryPackageResolver,
registry: Option<wac_resolver::RegistryPackageResolver>,
}

impl PackageResolver {
Expand All @@ -60,17 +60,23 @@ impl PackageResolver {
Ok(Self {
fs: FileSystemPackageResolver::new(dir, overrides, false),
#[cfg(feature = "registry")]
registry: wac_resolver::RegistryPackageResolver::new(
registry,
Some(Box::new(progress::ProgressBar::new())),
)
.await?,
registry: if registry.is_some() {
Some(
wac_resolver::RegistryPackageResolver::new(
registry,
Some(Box::new(progress::ProgressBar::new())),
)
.await?,
)
} else {
None
},
})
}

/// Resolve all packages referenced in the given document.
pub async fn resolve<'a>(
&self,
&mut self,
document: &'a Document<'a>,
) -> Result<IndexMap<BorrowedPackageKey<'a>, Vec<u8>>, Error> {
let mut keys = packages(document)?;
Expand All @@ -85,7 +91,17 @@ impl PackageResolver {
// The registry resolver will error on missing package
#[cfg(feature = "registry")]
if !keys.is_empty() {
let reg_packages = self.registry.resolve(&keys).await?;
if self.registry.is_none() {
self.registry = Some(
wac_resolver::RegistryPackageResolver::new(
None,
Some(Box::new(progress::ProgressBar::new())),
)
.await
.map_err(Error::RegistryClientFailed)?,
);
}
let reg_packages = self.registry.as_ref().unwrap().resolve(&keys).await?;
keys.retain(|key, _| !reg_packages.contains_key(key));
packages.extend(reg_packages);
}
Expand Down

0 comments on commit 7f789ad

Please sign in to comment.