Skip to content

Commit

Permalink
feat: DfxInterfaceBuilder can provide extension manager (#3976)
Browse files Browse the repository at this point in the history
This is so extension commands can load dfx.json if it references a canister with a type defined by an extension.

See https://dfinity.atlassian.net/browse/SDK-1832
  • Loading branch information
ericswanson-dfinity authored Nov 8, 2024
1 parent 7d36bdd commit 72cb257
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
19 changes: 17 additions & 2 deletions src/dfx-core/src/config/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
use crate::config::directories::project_dirs;
use crate::error::cache::{
DeleteCacheError, EnsureCacheVersionsDirError, GetBinaryCommandPathError, GetCacheRootError,
IsCacheInstalledError, ListCacheVersionsError,
GetVersionFromCachePathError, IsCacheInstalledError, ListCacheVersionsError,
};
#[cfg(not(windows))]
use crate::foundation::get_user_home;
use crate::fs::composite::ensure_dir_exists;
use semver::Version;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

pub trait Cache {
fn version_str(&self) -> String;
Expand Down Expand Up @@ -50,6 +50,21 @@ pub fn get_cache_path_for_version(v: &str) -> Result<PathBuf, GetCacheRootError>
Ok(p)
}

pub fn get_version_from_cache_path(
cache_path: &Path,
) -> Result<Version, GetVersionFromCachePathError> {
let version = cache_path
.file_name()
.ok_or(GetVersionFromCachePathError::NoCachePathFilename(
cache_path.to_path_buf(),
))?
.to_str()
.ok_or(GetVersionFromCachePathError::CachePathFilenameNotUtf8(
cache_path.to_path_buf(),
))?;
Ok(Version::parse(version)?)
}

/// Return the binary cache root. It constructs it if not present
/// already.
pub fn ensure_cache_versions_dir() -> Result<PathBuf, EnsureCacheVersionsDirError> {
Expand Down
13 changes: 13 additions & 0 deletions src/dfx-core/src/error/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::error::fs::{
};
use crate::error::get_current_exe::GetCurrentExeError;
use crate::error::get_user_home::GetUserHomeError;
use std::path::PathBuf;
use thiserror::Error;

#[derive(Error, Debug)]
Expand Down Expand Up @@ -34,6 +35,18 @@ pub enum EnsureCacheVersionsDirError {
GetCacheRoot(#[from] GetCacheRootError),
}

#[derive(Error, Debug)]
pub enum GetVersionFromCachePathError {
#[error("no filename in cache path '{0}'")]
NoCachePathFilename(PathBuf),

#[error("filename in cache path '{0}' is not valid UTF-8")]
CachePathFilenameNotUtf8(PathBuf),

#[error("cannot parse version from cache path filename")]
ParseVersion(#[from] semver::Error),
}

#[derive(Error, Debug)]
pub enum GetCacheRootError {
#[error(transparent)]
Expand Down
12 changes: 12 additions & 0 deletions src/dfx-core/src/error/interface.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::error::cache::GetVersionFromCachePathError;
use crate::error::extension::NewExtensionManagerError;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum NewExtensionManagerFromCachePathError {
#[error(transparent)]
GetVersionFromCachePath(#[from] GetVersionFromCachePathError),

#[error(transparent)]
NewExtensionManager(#[from] NewExtensionManagerError),
}
1 change: 1 addition & 0 deletions src/dfx-core/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod fs;
pub mod get_current_exe;
pub mod get_user_home;
pub mod identity;
pub mod interface;
pub mod keyring;
pub mod load_dfx_config;
pub mod load_networks_config;
Expand Down
33 changes: 31 additions & 2 deletions src/dfx-core/src/interface/builder.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use crate::{
config::cache::get_version_from_cache_path,
config::model::{
dfinity::{Config, NetworksConfig},
network_descriptor::NetworkDescriptor,
},
error::{
builder::{BuildAgentError, BuildDfxInterfaceError, BuildIdentityError},
extension::NewExtensionManagerError,
interface::NewExtensionManagerFromCachePathError,
network_config::NetworkConfigError,
},
extension::manager::ExtensionManager,
identity::{identity_manager::InitializeIdentity, IdentityManager},
network::{
provider::{create_network_descriptor, LocalBindDetermination},
Expand All @@ -16,6 +20,8 @@ use crate::{
};
use ic_agent::{agent::route_provider::RoundRobinRouteProvider, Agent, Identity};
use reqwest::Client;
use semver::Version;
use std::path::Path;
use std::sync::Arc;

#[derive(PartialEq)]
Expand All @@ -42,14 +48,17 @@ pub struct DfxInterfaceBuilder {
/// There is no need to set this for the local network, where the root key is fetched by default.
/// This would typically be set for a testnet, or an alias for the local network.
force_fetch_root_key_insecure_non_mainnet_only: bool,

extension_manager: Option<ExtensionManager>,
}

impl DfxInterfaceBuilder {
pub(crate) fn new() -> Self {
pub fn new() -> Self {
Self {
identity: IdentityPicker::Selected,
network: NetworkPicker::Local,
force_fetch_root_key_insecure_non_mainnet_only: false,
extension_manager: None,
}
}

Expand Down Expand Up @@ -77,6 +86,26 @@ impl DfxInterfaceBuilder {
self.with_network(NetworkPicker::Named(name.to_string()))
}

pub fn with_extension_manager(
self,
version: Version,
) -> Result<Self, NewExtensionManagerError> {
let extension_manager = Some(ExtensionManager::new(&version)?);
Ok(Self {
extension_manager,
..self
})
}

pub fn with_extension_manager_from_cache_path(
self,
cache_path: &Path,
) -> Result<Self, NewExtensionManagerFromCachePathError> {
let version = get_version_from_cache_path(cache_path)?;

Ok(self.with_extension_manager(version)?)
}

pub fn with_force_fetch_root_key_insecure_non_mainnet_only(self) -> Self {
Self {
force_fetch_root_key_insecure_non_mainnet_only: true,
Expand All @@ -88,7 +117,7 @@ impl DfxInterfaceBuilder {
let fetch_root_key = self.network == NetworkPicker::Local
|| self.force_fetch_root_key_insecure_non_mainnet_only;
let networks_config = NetworksConfig::new()?;
let config = Config::from_current_dir(None)?.map(Arc::new);
let config = Config::from_current_dir(self.extension_manager.as_ref())?.map(Arc::new);
let network_descriptor = self.build_network_descriptor(config.clone(), &networks_config)?;
let identity = self.build_identity()?;
let agent = self.build_agent(identity.clone(), &network_descriptor)?;
Expand Down

0 comments on commit 72cb257

Please sign in to comment.