Skip to content

Commit

Permalink
Replace DownloadDepot with plain function
Browse files Browse the repository at this point in the history
  • Loading branch information
mkaput committed Jan 23, 2023
1 parent 58112cb commit 29d2030
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 59 deletions.
8 changes: 0 additions & 8 deletions scarb/src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use once_cell::sync::OnceCell;
use tracing::trace;
use which::which_in;

use crate::core::registry::download_depot::DownloadDepot;
#[cfg(doc)]
use crate::core::Workspace;
use crate::dirs::AppDirs;
Expand All @@ -24,7 +23,6 @@ pub struct Config {
dirs: Arc<AppDirs>,
target_dir: RootFilesystem,
app_exe: OnceCell<PathBuf>,
download_depot: DownloadDepot,
ui: Ui,
creation_time: Instant,
}
Expand All @@ -48,14 +46,12 @@ impl Config {

let dirs = Arc::new(dirs);

let download_depot = DownloadDepot::new(dirs.clone());

Ok(Self {
manifest_path,
dirs,
target_dir,
app_exe: OnceCell::new(),
download_depot,
ui,
creation_time,
})
Expand Down Expand Up @@ -127,10 +123,6 @@ impl Config {
.map(AsRef::as_ref)
}

pub fn download_depot(&self) -> &DownloadDepot {
&self.download_depot
}

pub fn ui(&self) -> &Ui {
&self.ui
}
Expand Down
25 changes: 25 additions & 0 deletions scarb/src/core/registry/download.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use anyhow::Result;
use camino::{Utf8Path, Utf8PathBuf};

use crate::core::Config;
use crate::internal::fsx;

pub fn download_package_to_cache(
category: &str,
package_key: &str,
config: &Config,
downloader: impl FnOnce(&Utf8Path) -> Result<()>,
) -> Result<Utf8PathBuf> {
// TODO(mkaput): Computing checksum.
let registry_dir = config.dirs().registry_dir();
let category_dir = registry_dir.child(category);
let extracted_path = category_dir.child(package_key);

if extracted_path.path_unchecked().exists() {
fsx::remove_dir_all(extracted_path.path_unchecked())?;
}

downloader(extracted_path.path_existent()?)?;

Ok(extracted_path.path_unchecked().to_path_buf())
}
38 changes: 0 additions & 38 deletions scarb/src/core/registry/download_depot.rs

This file was deleted.

2 changes: 1 addition & 1 deletion scarb/src/core/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use async_trait::async_trait;
use crate::core::{ManifestDependency, Package, PackageId, Summary};

pub mod cache;
pub mod download_depot;
pub mod download;
pub mod source_map;

#[async_trait(?Send)]
Expand Down
22 changes: 10 additions & 12 deletions scarb/src/sources/corelib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rust_embed::RustEmbed;
use crate::core::config::Config;
use crate::core::manifest::{ManifestDependency, Summary};
use crate::core::package::{Package, PackageId};
use crate::core::registry::download::download_package_to_cache;
use crate::core::source::{Source, SourceId};
use crate::core::MANIFEST_FILE_NAME;
use crate::internal::fsx;
Expand Down Expand Up @@ -36,19 +37,16 @@ impl<'c> CorelibSource<'c> {

fn load(&self) -> Result<Package> {
// TODO(mkaput): Include core version or hash part here.
let root = self
.config
.download_depot()
.get_or_download("core", "core", |tmp| {
for path in Corelib::iter() {
let full_path = tmp.join(path.as_ref());
let data = Corelib::get(path.as_ref()).unwrap().data;
fsx::create_dir_all(full_path.parent().unwrap())?;
fsx::write(full_path, data)?;
}
let root = download_package_to_cache("core", "core", self.config, |tmp| {
for path in Corelib::iter() {
let full_path = tmp.join(path.as_ref());
let data = Corelib::get(path.as_ref()).unwrap().data;
fsx::create_dir_all(full_path.parent().unwrap())?;
fsx::write(full_path, data)?;
}

Ok(())
})?;
Ok(())
})?;

let manifest_path = root.join(MANIFEST_FILE_NAME);
ops::read_package_with_source_id(&manifest_path, SourceId::for_core())
Expand Down

0 comments on commit 29d2030

Please sign in to comment.