Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!(build): use builder pattern #298

Merged
merged 8 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ The following table provides a brief (incomplete) comparison:
| `command` build spec | :white_check_mark: | :white_check_mark: |
| custom build backends | :white_check_mark:[^1] | :white_check_mark: |
| `rust-mlua` build spec | :white_check_mark: (builtin) | :white_check_mark: (external build backend) |
| install pre-built binary rocks | :x: (planned) | :white_check_mark: |
| install pre-built binary rocks | :white_check_mark: | :white_check_mark: |
| parallel builds/installs | :white_check_mark: | :x: |
| install multiple packages with a single command | :white_check_mark: | :x: |
| install packages using version constraints | :white_check_mark: | :x: |
Expand Down
1 change: 1 addition & 0 deletions nix/overlay.nix
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
# disable vendored packages
LIBGIT2_NO_VENDOR = 1;
LIBSSH2_SYS_USE_PKG_CONFIG = 1;
ROCKS_SKIP_IMPURE_TESTS = 1;
};

inherit buildType;
Expand Down
4 changes: 4 additions & 0 deletions rocks-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ walkdir = "2.5.0"
which = "7.0.0"
indicatif = "0.17.8"
path-absolutize = "3.1.1"
url = "2.5.4"

[dev-dependencies]
assert_fs = "1.1.2"

[dependencies.rocks-lib]
path = "../rocks-lib/"
Expand Down
55 changes: 25 additions & 30 deletions rocks-bin/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ use std::{path::PathBuf, sync::Arc};
use clap::Args;
use eyre::Result;
use rocks_lib::{
build::BuildBehaviour,
build::{self, BuildBehaviour},
config::Config,
lockfile::{LockConstraint::Unconstrained, PinnedState},
lockfile::PinnedState,
operations::Install,
package::{PackageName, PackageReq},
progress::MultiProgress,
remote_package_db::RemotePackageDB,
rockspec::Rockspec,
tree::Tree,
tree::{RockMatches, Tree},
};

#[derive(Args, Default)]
Expand Down Expand Up @@ -69,16 +69,15 @@ pub async fn build(data: Build, config: Config) -> Result<()> {
let lua_version = rockspec.lua_version_from_config(&config)?;

let tree = Tree::new(config.tree().clone(), lua_version)?;
let package_db = RemotePackageDB::from_config(&config).await?;

let build_behaviour = match tree.has_rock_and(
let build_behaviour = match tree.match_rocks_and(
&PackageReq::new(
rockspec.package.to_string(),
Some(rockspec.version.to_string()),
)?,
|rock| pin == rock.pinned(),
) {
Some(_) if !data.force => {
)? {
RockMatches::Single(_) | RockMatches::Many(_) if !data.force => {
if Confirm::new(&format!(
"Package {} already exists. Overwrite?",
rockspec.package,
Expand Down Expand Up @@ -107,28 +106,24 @@ pub async fn build(data: Build, config: Config) -> Result<()> {

let dependencies_to_install = dependencies
.into_iter()
.filter(|req| tree.has_rock(req).is_none())
.map(|dep| (build_behaviour, dep.to_owned()))
.collect_vec();

rocks_lib::operations::install(
dependencies_to_install,
pin,
&package_db,
&config,
progress_arc,
)
.await?;

rocks_lib::build::build(
rockspec,
pin,
Unconstrained,
build_behaviour,
&config,
&progress.map(|p| p.new_bar()),
)
.await?;
.filter(|req| {
tree.match_rocks(req)
.is_ok_and(|rock_match| rock_match.is_found())
})
.map(|dep| (build_behaviour, dep.to_owned()));

Install::new(&config)
.packages(dependencies_to_install)
.pin(pin)
.progress(progress_arc)
.install()
.await?;

build::Build::new(&rockspec, &config, &progress.map(|p| p.new_bar()))
.pin(pin)
.behaviour(build_behaviour)
.build()
.await?;

Ok(())
}
40 changes: 16 additions & 24 deletions rocks-bin/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,32 @@ use rocks_lib::{
build::BuildBehaviour,
config::{Config, LuaVersion},
lockfile::PinnedState::Pinned,
operations::{self, install},
operations::{Install, Run},
progress::MultiProgress,
project::Project,
remote_package_db::RemotePackageDB,
};

pub async fn check(config: Config) -> Result<()> {
let project = Project::current()?.ok_or_eyre("Not in a project!")?;

let db = RemotePackageDB::from_config(&config).await?;
Install::new(&config)
.package(BuildBehaviour::NoForce, "luacheck".parse()?)
.pin(Pinned)
.progress(MultiProgress::new_arc())
.install()
.await?;

install(
vec![(BuildBehaviour::NoForce, "luacheck".parse()?)],
Pinned,
&db,
&config,
MultiProgress::new_arc(),
)
.await?;

operations::run(
"luacheck",
vec![
project.root().to_string_lossy().into(),
"--exclude-files".into(),
project
Run::new("luacheck", &config)
.arg(&project.root().to_string_lossy())
.arg("--exclude-files")
.arg(
&project
.tree(LuaVersion::from(&config)?)?
.root()
.to_string_lossy()
.to_string(),
],
config,
)
.await?;
.to_string_lossy(),
)
.run()
.await?;

Ok(())
}
9 changes: 4 additions & 5 deletions rocks-bin/src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use clap::Args;
use eyre::Result;
use rocks_lib::{
config::Config,
operations,
package::PackageReq,
progress::{MultiProgress, Progress},
remote_package_db::RemotePackageDB,
};

#[derive(Args)]
Expand All @@ -13,13 +13,12 @@ pub struct Download {
}

pub async fn download(dl_data: Download, config: Config) -> Result<()> {
let package_db = RemotePackageDB::from_config(&config).await?;
let progress = MultiProgress::new();
let bar = Progress::Progress(progress.new_bar());

let rock =
rocks_lib::operations::download_to_file(&dl_data.package_req, None, &package_db, &bar)
.await?;
let rock = operations::Download::new(&dl_data.package_req, &config, &bar)
.download_src_rock_to_file(None)
.await?;

bar.map(|b| {
b.finish_with_message(format!(
Expand Down
10 changes: 6 additions & 4 deletions rocks-bin/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::path::PathBuf;
use eyre::Result;
use rocks_lib::{
config::Config,
operations::Download,
progress::{MultiProgress, Progress},
remote_package_db::RemotePackageDB,
};

use crate::unpack::UnpackRemote;
Expand All @@ -13,9 +13,11 @@ pub async fn fetch_remote(data: UnpackRemote, config: Config) -> Result<()> {
let package_req = data.package_req;
let progress = MultiProgress::new();
let bar = Progress::Progress(progress.new_bar());
let package_db = RemotePackageDB::from_config(&config).await?;
let rockspec =
rocks_lib::operations::download_rockspec(&package_req, &package_db, &bar).await?;

let rockspec = Download::new(&package_req, &config, &bar)
.download_rockspec()
.await?
.rockspec;

let destination = data
.path
Expand Down
11 changes: 6 additions & 5 deletions rocks-bin/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use clap::Args;
use eyre::Result;
use rocks_lib::{
config::{Config, LuaVersion},
operations::download_rockspec,
operations::Download,
package::PackageReq,
progress::{MultiProgress, Progress},
remote_package_db::RemotePackageDB,
tree::Tree,
};

Expand All @@ -17,16 +16,18 @@ pub struct Info {
pub async fn info(data: Info, config: Config) -> Result<()> {
// TODO(vhyrro): Add `Tree::from(&Config)`
let tree = Tree::new(config.tree().clone(), LuaVersion::from(&config)?)?;
let package_db = RemotePackageDB::from_config(&config).await?;

let progress = MultiProgress::new();
let bar = Progress::Progress(progress.new_bar());

let rockspec = download_rockspec(&data.package, &package_db, &bar).await?;
let rockspec = Download::new(&data.package, &config, &bar)
.download_rockspec()
.await?
.rockspec;

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

if tree.has_rock(&data.package).is_some() {
if tree.match_rocks(&data.package)?.is_found() {
println!("Currently installed in {}", tree.root().display());
}

Expand Down
63 changes: 28 additions & 35 deletions rocks-bin/src/install.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use eyre::Result;
use inquire::Confirm;
use itertools::Itertools;
use rocks_lib::{
build::BuildBehaviour,
config::{Config, LuaVersion},
lockfile::PinnedState,
operations,
package::PackageReq,
progress::MultiProgress,
remote_package_db::RemotePackageDB,
tree::Tree,
tree::{RockMatches, Tree},
};

#[derive(clap::Args)]
Expand All @@ -31,40 +30,34 @@ pub async fn install(data: Install, config: Config) -> Result<()> {
let lua_version = LuaVersion::from(&config)?;
let tree = Tree::new(config.tree().clone(), lua_version)?;

let packages = data
.package_req
.into_iter()
.filter_map(|req| {
let build_behaviour: Option<BuildBehaviour> =
match tree.has_rock_and(&req, |rock| pin == rock.pinned()) {
Some(_) if !data.force => {
if Confirm::new(&format!("Package {} already exists. Overwrite?", req))
.with_default(false)
.prompt()
.expect("Error prompting for reinstall")
{
Some(BuildBehaviour::Force)
} else {
None
}
}
_ => Some(BuildBehaviour::from(data.force)),
};
build_behaviour.map(|it| (it, req))
})
.collect_vec();

let package_db = RemotePackageDB::from_config(&config).await?;
let packages = data.package_req.into_iter().filter_map(|req| {
let build_behaviour: Option<BuildBehaviour> = match tree
.match_rocks_and(&req, |rock| pin == rock.pinned())
.expect("unable to get tree data")
{
RockMatches::Single(_) | RockMatches::Many(_) if !data.force => {
if Confirm::new(&format!("Package {} already exists. Overwrite?", req))
.with_default(false)
.prompt()
.expect("Error prompting for reinstall")
{
Some(BuildBehaviour::Force)
} else {
None
}
}
_ => Some(BuildBehaviour::from(data.force)),
};
build_behaviour.map(|it| (it, req))
});

// TODO(vhyrro): If the tree doesn't exist then error out.
rocks_lib::operations::install(
packages,
pin,
&package_db,
&config,
MultiProgress::new_arc(),
)
.await?;
operations::Install::new(&config)
.packages(packages)
.pin(pin)
.progress(MultiProgress::new_arc())
.install()
.await?;

Ok(())
}
Loading
Loading