Skip to content

Commit

Permalink
feat(luarocks): install transitive build dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb authored and vhyrro committed Dec 17, 2024
1 parent 251e940 commit 40d62c8
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 26 deletions.
95 changes: 76 additions & 19 deletions rocks-lib/src/luarocks_installation.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
use futures::future::join_all;
use itertools::Itertools;
use std::{
collections::HashMap,
io,
path::Path,
process::{Command, ExitStatus},
sync::Arc,
};
use tempdir::TempDir;
use thiserror::Error;

use crate::{
build::{build, BuildBehaviour, BuildError},
config::{Config, LuaVersion, LuaVersionUnset},
lockfile::{LockConstraint, PinnedState},
lockfile::{LocalPackage, LocalPackageId, LockConstraint, PinnedState},
lua_installation::LuaInstallation,
manifest::{ManifestError, ManifestMetadata},
operations::download_rockspec,
operations::{get_all_dependencies, SearchAndDownloadError},
package::PackageReq,
path::Paths,
progress::{Progress, ProgressBar},
progress::{MultiProgress, Progress, ProgressBar},
rockspec::{Rockspec, RockspecFormat},
tree::Tree,
};
Expand Down Expand Up @@ -46,6 +49,8 @@ pub enum InstallBuildDependenciesError {
#[error(transparent)]
ManifestError(#[from] ManifestError),
#[error(transparent)]
SearchAndDownloadError(#[from] SearchAndDownloadError),
#[error(transparent)]
BuildError(#[from] BuildError),
}

Expand Down Expand Up @@ -120,7 +125,7 @@ impl LuaRocksInstallation {
self,
build_backend: &str,
rockspec: &Rockspec,
progress: &Progress<ProgressBar>,
progress: &Progress<MultiProgress>,
) -> Result<(), InstallBuildDependenciesError> {
let mut lockfile = self.tree.lockfile()?;
let manifest = ManifestMetadata::from_config(&self.config).await?;
Expand All @@ -137,24 +142,76 @@ impl LuaRocksInstallation {
.collect_vec()
}
_ => rockspec.build_dependencies.current_platform().to_vec(),
};
for package in build_dependencies {
if self.tree.has_rock(&package).is_none() {
let rockspec = download_rockspec(&package, &manifest, &self.config, progress)
.await
.unwrap();
let pkg = build(
}
.into_iter()
.map(|dep| (BuildBehaviour::NoForce, dep))
.collect_vec();

let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
let pin = PinnedState::Unpinned;
get_all_dependencies(
tx,
build_dependencies,
pin,
Arc::new(manifest),
&self.config,
progress,
)
.await?;

let mut all_packages = HashMap::with_capacity(rx.len());
while let Some(dep) = rx.recv().await {
all_packages.insert(dep.spec.id(), dep);
}

let installed_packages = join_all(all_packages.clone().into_values().map(|install_spec| {
let progress = progress.clone();
let bar = progress.map(|p| {
p.add(ProgressBar::from(format!(
"💻 Installing build dependency: {}",
install_spec.rockspec.package,
)))
});
let config = self.config.clone();
tokio::spawn(async move {
let rockspec = install_spec.rockspec;
let pkg = crate::build::build(
rockspec,
PinnedState::Unpinned,
LockConstraint::Constrained(package.version_req().clone()),
BuildBehaviour::NoForce,
&self.config,
progress,
pin,
install_spec.spec.constraint(),
install_spec.build_behaviour,
&config,
&bar,
)
.await?;
lockfile.add(&pkg);
}
}

bar.map(|b| b.finish_and_clear());

Ok::<_, InstallBuildDependenciesError>((pkg.id(), pkg))
})
}))
.await
.into_iter()
.flatten()
.try_collect::<_, HashMap<LocalPackageId, LocalPackage>, _>()?;

installed_packages.iter().for_each(|(id, pkg)| {
lockfile.add(pkg);

all_packages
.get(id)
.map(|pkg| pkg.spec.dependencies())
.unwrap_or_default()
.into_iter()
.for_each(|dependency_id| {
lockfile.add_dependency(
pkg,
installed_packages
.get(dependency_id)
.expect("required dependency not found"),
);
});
});
lockfile.flush()?;
Ok(())
}
Expand Down
11 changes: 5 additions & 6 deletions rocks-lib/src/operations/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use std::{collections::HashMap, io, sync::Arc};
use crate::{
build::{BuildBehaviour, BuildError},
config::{Config, LuaVersion, LuaVersionUnset},
lockfile::{
LocalPackage, LocalPackageId, Lockfile, PinnedState,
},
lockfile::{LocalPackage, LocalPackageId, Lockfile, PinnedState},
luarocks_installation::{
InstallBuildDependenciesError, LuaRocksError, LuaRocksInstallError, LuaRocksInstallation,
},
Expand All @@ -16,11 +14,11 @@ use crate::{
tree::Tree,
};

use itertools::Itertools;
use futures::future::join_all;
use itertools::Itertools;
use thiserror::Error;

use super::{get_all_dependencies, SearchAndDownloadError};
use super::{resolve::get_all_dependencies, SearchAndDownloadError};

#[derive(Error, Debug)]
pub enum InstallError {
Expand Down Expand Up @@ -88,6 +86,7 @@ async fn install_impl(
}

let installed_packages = join_all(all_packages.clone().into_values().map(|install_spec| {
let progress = progress.clone();
let package = install_spec.rockspec.package.clone();

let bar = progress.map(|p| {
Expand All @@ -106,7 +105,7 @@ async fn install_impl(
let luarocks = LuaRocksInstallation::new(&config)?;
luarocks.ensure_installed(&bar).await?;
luarocks
.install_build_dependencies(build_backend, &rockspec, &bar)
.install_build_dependencies(build_backend, &rockspec, &progress)
.await?;
}

Expand Down
3 changes: 2 additions & 1 deletion rocks-lib/src/operations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ pub use fetch::*;
pub use install::*;
pub use pin::*;
pub use remove::*;
pub use resolve::*;
pub use run::*;
pub use test::*;
pub use unpack::*;
pub use update::*;

pub(crate) use resolve::*;

0 comments on commit 40d62c8

Please sign in to comment.