feat: dep downloading now works

This commit is contained in:
rvcas
2022-12-19 22:32:09 -05:00
committed by Lucas
parent 6a0b1ce5c3
commit b06bf610b7
6 changed files with 172 additions and 13 deletions

View File

@@ -1,4 +1,4 @@
use std::path::Path;
use std::{io::Cursor, path::Path};
use futures::future;
use reqwest::Client;
@@ -41,14 +41,20 @@ impl<'a> Downloader<'a> {
&self,
package: &Package,
) -> Result<bool, Error> {
self.ensure_package_downloaded(package).await
// self.extract_package_from_cache(&package.name, &package.version)
self.ensure_package_downloaded(package).await?;
self.extract_package_from_cache(&package.name, &package.version)
.await
}
pub async fn ensure_package_downloaded(&self, package: &Package) -> Result<bool, Error> {
let packages_cache_path = paths::packages_cache();
let zipball_path =
paths::package_cache_zipball(&package.name, &package.version.to_string());
if !packages_cache_path.exists() {
tokio::fs::create_dir_all(packages_cache_path).await?;
}
if zipball_path.is_file() {
return Ok(false);
}
@@ -58,13 +64,56 @@ impl<'a> Downloader<'a> {
package.name.owner, package.name.repo, package.version
);
let response = self.http.get(url).send().await?;
dbg!(response);
let response = self
.http
.get(url)
.header("User-Agent", "aiken-lang")
.send()
.await?
.bytes()
.await?;
// let PackageSource::Github { url } = &package.source;
// tokio::fs::write(&zipball_path, zipball).await?;
tokio::fs::write(&zipball_path, response).await?;
Ok(true)
}
pub async fn extract_package_from_cache(
&self,
name: &PackageName,
version: &str,
) -> Result<bool, Error> {
let destination = self.root_path.join(paths::build_deps_package(name));
// If the directory already exists then there's nothing for us to do
if destination.is_dir() {
return Ok(false);
}
tokio::fs::create_dir_all(&destination).await?;
let zipball_path = self
.root_path
.join(paths::package_cache_zipball(name, version));
let zipball = tokio::fs::read(zipball_path).await?;
let result = {
let d = destination.clone();
tokio::task::spawn_blocking(move || {
zip_extract::extract(Cursor::new(zipball), &d, true)
})
.await?
};
if result.is_err() {
tokio::fs::remove_dir_all(destination).await?;
}
result?;
Ok(true)
}

View File

@@ -81,8 +81,6 @@ impl Manifest {
"# This file was generated by Aiken\n# You typically do not need to edit this file\n\n",
);
dbg!(&manifest_path.display());
fs::write(manifest_path, toml)?;
Ok(())

View File

@@ -13,6 +13,7 @@ use std::{
path::{Path, PathBuf},
};
use uplc::machine::cost_model::ExBudget;
use zip_extract::ZipExtractError;
#[allow(dead_code)]
#[derive(thiserror::Error)]
@@ -36,6 +37,12 @@ pub enum Error {
#[error(transparent)]
Http(#[from] reqwest::Error),
#[error(transparent)]
ZipExtract(#[from] ZipExtractError),
#[error(transparent)]
JoinError(#[from] tokio::task::JoinError),
#[error("{help}")]
TomlLoading {
path: PathBuf,
@@ -177,6 +184,8 @@ impl Error {
Error::WrongValidatorArity { path, .. } => Some(path.to_path_buf()),
Error::TestFailure { path, .. } => Some(path.to_path_buf()),
Error::Http(_) => None,
Error::ZipExtract(_) => None,
Error::JoinError(_) => None,
}
}
@@ -195,6 +204,8 @@ impl Error {
Error::WrongValidatorArity { src, .. } => Some(src.to_string()),
Error::TestFailure { .. } => None,
Error::Http(_) => None,
Error::ZipExtract(_) => None,
Error::JoinError(_) => None,
}
}
}
@@ -238,6 +249,8 @@ impl Diagnostic for Error {
Error::WrongValidatorArity { .. } => Some(Box::new("aiken::validators")),
Error::TestFailure { path, .. } => Some(Box::new(path.to_str().unwrap_or(""))),
Error::Http(_) => Some(Box::new("aiken::deps")),
Error::ZipExtract(_) => None,
Error::JoinError(_) => None,
}
}
@@ -290,6 +303,8 @@ impl Diagnostic for Error {
}
},
Error::Http(_) => None,
Error::ZipExtract(_) => None,
Error::JoinError(_) => None,
}
}
@@ -320,6 +335,8 @@ impl Diagnostic for Error {
)),
Error::TestFailure { .. } => None,
Error::Http(_) => None,
Error::ZipExtract(_) => None,
Error::JoinError(_) => None,
}
}
@@ -338,6 +355,8 @@ impl Diagnostic for Error {
Error::WrongValidatorArity { named, .. } => Some(named),
Error::TestFailure { .. } => None,
Error::Http(_) => None,
Error::ZipExtract(_) => None,
Error::JoinError(_) => None,
}
}
}

View File

@@ -29,7 +29,7 @@ pub fn package_cache_zipball(package_name: &PackageName, version: &str) -> PathB
))
}
fn packages_cache() -> PathBuf {
pub fn packages_cache() -> PathBuf {
default_aiken_cache().join("packages")
}