Skip to content

Commit 617aeb1

Browse files
fix: remove implicit package download during resolve step
1 parent 19b0ebb commit 617aeb1

File tree

1 file changed

+12
-52
lines changed

1 file changed

+12
-52
lines changed

src/package/mod.rs

Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub mod install;
44
pub mod resolver;
55

66
use std::borrow::Borrow;
7+
use std::fs::File;
78
use std::io::{ErrorKind, Read, Seek};
89
use std::path::{Path, PathBuf};
910

@@ -43,7 +44,7 @@ pub struct Package {
4344
impl Package {
4445
/// Attempt to resolve the package from the local cache or remote.
4546
/// This does not download the package, it just finds its "source".
46-
pub async fn resolve_new(ident: impl Borrow<PackageReference>) -> Result<Self, Error> {
47+
pub async fn from_any(ident: impl Borrow<PackageReference>) -> Result<Self, Error> {
4748
if cache::get_cache_location(ident.borrow()).exists() {
4849
return Package::from_cache(ident).await;
4950
}
@@ -110,7 +111,9 @@ impl Package {
110111
/// Load package metadata from an arbitrary path, extracting it into the cache if it passes
111112
// manifest validation.
112113
pub async fn from_path(ident: PackageReference, path: &Path) -> Result<Self, Error> {
113-
let package = Package::from_repo(ident).await?;
114+
// let package = Package::from_repo(ident).await?;
115+
add_to_cache(&ident, File::open(path).map_fs_error(path)?)?;
116+
let package = Package::from_cache(ident).await?;
114117

115118
Ok(Package {
116119
identifier: package.identifier,
@@ -119,64 +122,21 @@ impl Package {
119122
})
120123
}
121124

122-
/// Resolve the package into a discrete path. This will download the package if it is not cached,
123-
// otherwise it will return its cached path.
124-
pub async fn resolve(&self, reporter: &dyn ProgressBarTrait) -> Result<PathBuf, Error> {
125+
/// Resolve the package into a discrete path, returning None if it does not exist locally.
126+
pub async fn get_path(&self) -> Option<PathBuf> {
125127
match &self.source {
126128
PackageSource::Local(path) => add_to_cache(
127129
&self.identifier,
128-
std::fs::File::open(path).map_fs_error(path)?,
129-
),
130-
PackageSource::Remote(_) => self.download(reporter).await,
131-
PackageSource::Cache(path) => Ok(path.clone()),
130+
File::open(path).map_fs_error(path).ok()?,
131+
).ok(),
132+
PackageSource::Cache(path) => Some(path.clone()),
133+
PackageSource::Remote(_) => None,
132134
}
133135
}
134136

135-
pub async fn add(
136-
&self,
137-
project_state: &Path,
138-
reporter: Box<dyn ProgressBarTrait>,
139-
) -> Result<(), Error> {
140-
let cache_path = self.resolve(reporter.as_ref()).await?;
141-
let install_dir = project_state.join(self.identifier.to_string());
142-
143-
if install_dir.is_dir() {
144-
fs::remove_dir_all(&install_dir)
145-
.await
146-
.map_fs_error(&install_dir)?;
147-
}
148-
149-
for item in walkdir::WalkDir::new(&cache_path).into_iter() {
150-
let item = item?;
151-
152-
let dest_path = install_dir.join(item.path().strip_prefix(&cache_path).unwrap());
153-
154-
if item.file_type().is_dir() {
155-
tokio::fs::create_dir_all(&dest_path)
156-
.await
157-
.map_fs_error(&dest_path)?;
158-
} else if item.file_type().is_file() {
159-
tokio::fs::copy(item.path(), &dest_path)
160-
.await
161-
.map_fs_error(&dest_path)?;
162-
}
163-
}
164-
165-
let finished_msg = format!(
166-
"{} {}-{} ({})",
167-
"[✓]".green(),
168-
self.identifier.namespace.bold(),
169-
self.identifier.name.bold(),
170-
self.identifier.version.to_string().truecolor(90, 90, 90)
171-
);
172-
173-
reporter.println(&finished_msg);
174-
reporter.finish_and_clear();
175-
176-
Ok(())
177137
}
178138

179-
async fn download(&self, reporter: &dyn ProgressBarTrait) -> Result<PathBuf, Error> {
139+
pub async fn download(&self, reporter: &dyn ProgressBarTrait) -> Result<PathBuf, Error> {
180140
let PackageSource::Remote(package_source) = &self.source else {
181141
panic!("Invalid use, this is a local package.")
182142
};

0 commit comments

Comments
 (0)