Skip to content

Commit

Permalink
feat!: add build/test dependencies to rocks.lock
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Feb 14, 2025
1 parent bddea78 commit c269f7a
Show file tree
Hide file tree
Showing 14 changed files with 980 additions and 257 deletions.
83 changes: 46 additions & 37 deletions rocks-bin/src/add.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use eyre::{OptionExt, Result};
use eyre::{Context, OptionExt, Result};
use rocks_lib::{
config::Config,
lockfile::PinnedState,
operations,
luarocks::luarocks_installation::LuaRocksInstallation,
operations::Sync,
package::PackageReq,
progress::{MultiProgress, Progress, ProgressBar},
project::Project,
remote_package_db::RemotePackageDB,
};

use crate::utils::install::apply_build_behaviour;

#[derive(clap::Args)]
pub struct Add {
/// Package or list of packages to install.
Expand Down Expand Up @@ -41,56 +40,66 @@ pub async fn add(data: Add, config: Config) -> Result<()> {
let tree = project.tree(&config)?;
let db = RemotePackageDB::from_config(&config, &Progress::Progress(ProgressBar::new())).await?;

let regular_packages = apply_build_behaviour(data.package_req, pin, data.force, &tree);
if !regular_packages.is_empty() {
let progress = MultiProgress::new_arc();

if !data.package_req.is_empty() {
// NOTE: We only update the lockfile if one exists.
// Otherwise, the next `rocks build` will install the packages.
if let Some(mut lockfile) = project.try_lockfile()? {
Sync::new(&tree, &mut lockfile, &config)
.progress(progress.clone())
.packages(data.package_req.clone())
.pin(pin)
.sync_dependencies()
.await
.wrap_err("syncing dependencies with the project lockfile failed.")?;
}

project
.add(
rocks_lib::project::DependencyType::Regular(
regular_packages
.iter()
.map(|(_, req)| req.clone())
.collect(),
),
rocks_lib::project::DependencyType::Regular(data.package_req),
&db,
)
.await?;
}

let build_packages =
apply_build_behaviour(data.build.unwrap_or_default(), pin, data.force, &tree);
let build_packages = data.build.unwrap_or_default();
if !build_packages.is_empty() {
if let Some(mut lockfile) = project.try_lockfile()? {
let luarocks = LuaRocksInstallation::new(&config)?;
Sync::new(luarocks.tree(), &mut lockfile, luarocks.config())
.progress(progress.clone())
.packages(build_packages.clone())
.pin(pin)
.sync_build_dependencies()
.await
.wrap_err("syncing build dependencies with the project lockfile failed.")?;
}

project
.add(
rocks_lib::project::DependencyType::Build(
build_packages.iter().map(|(_, req)| req.clone()).collect(),
),
rocks_lib::project::DependencyType::Build(build_packages),
&db,
)
.await?;
}

let test_packages =
apply_build_behaviour(data.test.unwrap_or_default(), pin, data.force, &tree);
let test_packages = data.test.unwrap_or_default();
if !test_packages.is_empty() {
project
.add(
rocks_lib::project::DependencyType::Test(
test_packages.iter().map(|(_, req)| req.clone()).collect(),
),
&db,
)
.await?;
}
if let Some(mut lockfile) = project.try_lockfile()? {
Sync::new(&tree, &mut lockfile, &config)
.progress(progress.clone())
.packages(test_packages.clone())
.pin(pin)
.sync_test_dependencies()
.await
.wrap_err("syncing test dependencies with the project lockfile failed.")?;

operations::Install::new(&tree, &config)
.packages(regular_packages)
.packages(build_packages)
.packages(test_packages)
.pin(pin)
.progress(MultiProgress::new_arc())
.project(&project)
.install()
.await?;
project
.add(rocks_lib::project::DependencyType::Test(test_packages), &db)
.await?;
}
}

Ok(())
}
103 changes: 70 additions & 33 deletions rocks-bin/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rocks_lib::{
build::{self, BuildBehaviour},
config::Config,
lockfile::PinnedState,
luarocks::luarocks_installation::LuaRocksInstallation,
operations::{Install, Sync},
package::PackageName,
progress::MultiProgress,
Expand All @@ -21,9 +22,9 @@ pub struct Build {
#[arg(long)]
pin: bool,

/// Ignore the project's existing lockfile.
/// Ignore the project's lockfile and don't create one.
#[arg(long)]
ignore_lockfile: bool,
no_lock: bool,
}

pub async fn build(data: Build, config: Config) -> Result<()> {
Expand All @@ -35,12 +36,6 @@ pub async fn build(data: Build, config: Config) -> Result<()> {
let tree = project.tree(&config)?;
let rocks = project.new_local_rockspec()?;

let lockfile = match project.try_lockfile()? {
None => None,
Some(_) if data.ignore_lockfile => None,
Some(lockfile) => Some(lockfile),
};

let dependencies = rocks
.dependencies()
.current_platform()
Expand All @@ -49,37 +44,79 @@ pub async fn build(data: Build, config: Config) -> Result<()> {
.cloned()
.collect_vec();

match lockfile {
Some(mut project_lockfile) => {
Sync::new(&tree, &mut project_lockfile, &config)
.progress(progress.clone())
.packages(dependencies)
.sync()
.await
.wrap_err(
"
syncing with the project lockfile failed.
Use --ignore-lockfile to force a new build.
",
)?;
}
None => {
let dependencies_to_install = dependencies
.into_iter()
.filter(|req| {
tree.match_rocks(req)
.is_ok_and(|rock_match| !rock_match.is_found())
})
.map(|dep| (BuildBehaviour::NoForce, dep));

Install::new(&tree, &config)
.packages(dependencies_to_install)
let build_dependencies = rocks
.build_dependencies()
.current_platform()
.iter()
.filter(|package| !package.name().eq(&PackageName::new("lua".into())))
.cloned()
.collect_vec();

let luarocks = LuaRocksInstallation::new(&config)?;

if data.no_lock {
let dependencies_to_install = dependencies
.into_iter()
.filter(|req| {
tree.match_rocks(req)
.is_ok_and(|rock_match| !rock_match.is_found())
})
.map(|dep| (BuildBehaviour::NoForce, dep));

Install::new(&tree, &config)
.packages(dependencies_to_install)
.project(&project)
.pin(pin)
.progress(progress.clone())
.install()
.await?;

let build_dependencies_to_install = build_dependencies
.into_iter()
.filter(|req| {
tree.match_rocks(req)
.is_ok_and(|rock_match| !rock_match.is_found())
})
.map(|dep| (BuildBehaviour::NoForce, dep))
.collect_vec();

if !build_dependencies_to_install.is_empty() {
let bar = progress.map(|p| p.new_bar());
luarocks.ensure_installed(&bar).await?;
Install::new(luarocks.tree(), luarocks.config())
.packages(build_dependencies_to_install)
.project(&project)
.pin(pin)
.progress(progress.clone())
.install()
.await?;
}
} else {
let mut project_lockfile = project.lockfile()?;

Sync::new(&tree, &mut project_lockfile, &config)
.progress(progress.clone())
.packages(dependencies)
.sync_dependencies()
.await
.wrap_err(
"
syncing dependencies with the project lockfile failed.
Use --ignore-lockfile to force a new build.
",
)?;

Sync::new(luarocks.tree(), &mut project_lockfile, luarocks.config())
.progress(progress.clone())
.packages(build_dependencies)
.sync_build_dependencies()
.await
.wrap_err(
"
syncing build dependencies with the project lockfile failed.
Use --ignore-lockfile to force a new build.
",
)?;
}

build::Build::new(&rocks, &tree, &config, &progress.map(|p| p.new_bar()))
Expand Down
6 changes: 3 additions & 3 deletions rocks-bin/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use clap::Args;
use eyre::{eyre, Context, Result};
use rocks_lib::{
config::{Config, LuaVersion},
lockfile::Lockfile,
lockfile::ProjectLockfile,
operations,
package::PackageReq,
project::{rocks_toml::RocksToml, ROCKS_TOML},
Expand Down Expand Up @@ -34,7 +34,7 @@ pub struct Sync {
pub async fn sync(args: Sync, config: Config) -> Result<()> {
let tree = config.tree(LuaVersion::from(&config)?)?;

let mut lockfile = Lockfile::new(args.lockfile.clone())?;
let mut lockfile = ProjectLockfile::new(args.lockfile.clone())?;

let mut sync = operations::Sync::new(&tree, &mut lockfile, &config)
.validate_integrity(!args.no_integrity_check);
Expand Down Expand Up @@ -71,7 +71,7 @@ pub async fn sync(args: Sync, config: Config) -> Result<()> {
sync.add_packages(dependencies);
}

sync.sync().await.wrap_err("sync failed.")?;
sync.sync_dependencies().await.wrap_err("sync failed.")?;

Ok(())
}
5 changes: 5 additions & 0 deletions rocks-bin/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ pub struct Test {
/// Don't isolate the user environment (keep `HOME` and `XDG` environment variables).
#[arg(long)]
impure: bool,

/// Ignore the project's lockfile and don't create one.
#[arg(long)]
no_lock: bool,
}

pub async fn test(test: Test, config: Config) -> Result<()> {
Expand All @@ -27,6 +31,7 @@ pub async fn test(test: Test, config: Config) -> Result<()> {
operations::Test::new(project, &config)
.args(test_args)
.env(test_env)
.no_lock(test.no_lock)
.run()
.await?;
Ok(())
Expand Down
Loading

0 comments on commit c269f7a

Please sign in to comment.