chore: make folder names match crate name
This commit is contained in:
120
crates/aiken-project/src/deps/downloader.rs
Normal file
120
crates/aiken-project/src/deps/downloader.rs
Normal file
@@ -0,0 +1,120 @@
|
||||
use std::{io::Cursor, path::Path};
|
||||
|
||||
use futures::future;
|
||||
use reqwest::Client;
|
||||
|
||||
use crate::{config::PackageName, error::Error, paths};
|
||||
|
||||
use super::manifest::Package;
|
||||
|
||||
pub struct Downloader<'a> {
|
||||
http: Client,
|
||||
root_path: &'a Path,
|
||||
}
|
||||
|
||||
impl<'a> Downloader<'a> {
|
||||
pub fn new(root_path: &'a Path) -> Self {
|
||||
Self {
|
||||
http: Client::new(),
|
||||
root_path,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn download_packages<T>(
|
||||
&self,
|
||||
packages: T,
|
||||
project_name: &PackageName,
|
||||
) -> Result<(), Error>
|
||||
where
|
||||
T: Iterator<Item = &'a Package>,
|
||||
{
|
||||
let tasks = packages
|
||||
.filter(|package| project_name != &package.name)
|
||||
.map(|package| self.ensure_package_in_build_directory(package));
|
||||
|
||||
let _results = future::try_join_all(tasks).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn ensure_package_in_build_directory(
|
||||
&self,
|
||||
package: &Package,
|
||||
) -> Result<bool, Error> {
|
||||
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);
|
||||
}
|
||||
|
||||
let url = format!(
|
||||
"https://api.github.com/repos/{}/{}/zipball/{}",
|
||||
package.name.owner, package.name.repo, package.version
|
||||
);
|
||||
|
||||
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, 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)
|
||||
}
|
||||
}
|
||||
137
crates/aiken-project/src/deps/manifest.rs
Normal file
137
crates/aiken-project/src/deps/manifest.rs
Normal file
@@ -0,0 +1,137 @@
|
||||
use std::{fs, path::Path};
|
||||
|
||||
use aiken_lang::ast::Span;
|
||||
use miette::NamedSource;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
config::{Config, Dependency, PackageName, Platform},
|
||||
error::Error,
|
||||
paths,
|
||||
telemetry::{Event, EventListener},
|
||||
};
|
||||
|
||||
use super::UseManifest;
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Manifest {
|
||||
pub requirements: Vec<Dependency>,
|
||||
pub packages: Vec<Package>,
|
||||
}
|
||||
|
||||
impl Manifest {
|
||||
pub fn load<T>(
|
||||
runtime: tokio::runtime::Handle,
|
||||
event_listener: &T,
|
||||
config: &Config,
|
||||
use_manifest: UseManifest,
|
||||
root_path: &Path,
|
||||
) -> Result<(Self, bool), Error>
|
||||
where
|
||||
T: EventListener,
|
||||
{
|
||||
let manifest_path = root_path.join(paths::manifest());
|
||||
|
||||
// If there's no manifest (or we have been asked not to use it) then resolve
|
||||
// the versions anew
|
||||
let should_resolve = match use_manifest {
|
||||
_ if !manifest_path.exists() => true,
|
||||
UseManifest::No => true,
|
||||
UseManifest::Yes => false,
|
||||
};
|
||||
|
||||
if should_resolve {
|
||||
let manifest = resolve_versions(runtime, config, None, event_listener)?;
|
||||
|
||||
return Ok((manifest, true));
|
||||
}
|
||||
|
||||
let toml = fs::read_to_string(&manifest_path)?;
|
||||
|
||||
let manifest: Self = toml::from_str(&toml).map_err(|e| Error::TomlLoading {
|
||||
path: manifest_path.clone(),
|
||||
src: toml.clone(),
|
||||
named: NamedSource::new(manifest_path.display().to_string(), toml),
|
||||
// this isn't actually a legit way to get the span
|
||||
location: e.line_col().map(|(line, col)| Span {
|
||||
start: line,
|
||||
end: col,
|
||||
}),
|
||||
help: e.to_string(),
|
||||
})?;
|
||||
|
||||
// If the config has unchanged since the manifest was written then it is up
|
||||
// to date so we can return it unmodified.
|
||||
if manifest.requirements == config.dependencies {
|
||||
Ok((manifest, false))
|
||||
} else {
|
||||
let manifest = resolve_versions(runtime, config, Some(&manifest), event_listener)?;
|
||||
|
||||
Ok((manifest, true))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(&self, root_path: &Path) -> Result<(), Error> {
|
||||
let manifest_path = root_path.join(paths::manifest());
|
||||
|
||||
let mut toml = toml::to_string(&self).expect("aiken.lock serialization");
|
||||
|
||||
toml.insert_str(
|
||||
0,
|
||||
"# This file was generated by Aiken\n# You typically do not need to edit this file\n\n",
|
||||
);
|
||||
|
||||
fs::write(manifest_path, toml)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Package {
|
||||
pub name: PackageName,
|
||||
pub version: String,
|
||||
pub requirements: Vec<String>,
|
||||
pub source: Platform,
|
||||
}
|
||||
|
||||
fn resolve_versions<T>(
|
||||
_runtime: tokio::runtime::Handle,
|
||||
config: &Config,
|
||||
_manifest: Option<&Manifest>,
|
||||
event_listener: &T,
|
||||
) -> Result<Manifest, Error>
|
||||
where
|
||||
T: EventListener,
|
||||
{
|
||||
event_listener.handle_event(Event::ResolvingVersions);
|
||||
|
||||
// let resolved = hex::resolve_versions(
|
||||
// PackageFetcher::boxed(runtime.clone()),
|
||||
// mode,
|
||||
// config,
|
||||
// manifest,
|
||||
// )?;
|
||||
|
||||
// let packages = runtime.block_on(future::try_join_all(
|
||||
// resolved
|
||||
// .into_iter()
|
||||
// .map(|(name, version)| lookup_package(name, version)),
|
||||
// ))?;
|
||||
|
||||
let manifest = Manifest {
|
||||
packages: config
|
||||
.dependencies
|
||||
.iter()
|
||||
.map(|dep| Package {
|
||||
name: dep.name.clone(),
|
||||
version: dep.version.clone(),
|
||||
requirements: vec![],
|
||||
source: dep.source,
|
||||
})
|
||||
.collect(),
|
||||
requirements: config.dependencies.clone(),
|
||||
};
|
||||
|
||||
Ok(manifest)
|
||||
}
|
||||
Reference in New Issue
Block a user