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

feat: command build backend #258

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,17 @@ The following table provides a brief (incomplete) comparison:
| `builtin` build spec | :white_check_mark: | :white_check_mark: |
| `make` build spec | :white_check_mark: | :white_check_mark: |
| `rust-mlua` build spec | :white_check_mark: (builtin) | :white_check_mark: (external build backend) |
| `tree-sitter` build spec | :x: (planned) | :white_check_mark: (external build backend) |
| `cmake` build spec | :x: (planned) | :white_check_mark: |
| `command` build spec | :x: (planned) | :white_check_mark: |
| custom build backends | :x: (planned[^1]) | :white_check_mark: |
| `command` build spec | :white_check_mark: | :white_check_mark: |
| custom build backends | :white_check_mark:[^1] | :white_check_mark: |
| install pre-built binary rocks | :x: (planned) | :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: |
| lockfiles | :white_check_mark: | :white_check_mark: (basic, dependency versions only) |
| formatting with stylua | :white_check_mark: | :x: |

[^1]: planned via a luarocks compatibility layer.
[^1]: Supported via a compatibility layer that uses luarocks as a backend.

## :book: License

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local _MODREV, _SPECREV = 'scm', '-1'
rockspec_format = '3.0'
package = 'sample-project-busted'
package = 'sample-project'
version = _MODREV .. _SPECREV

test_dependencies = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
rockspec_format = "3.0"
package = "foo"
version = "1.0.0-1"
source = {
url = 'https://github.com/nvim-neorocks/luarocks-stub',
}
83 changes: 83 additions & 0 deletions rocks-lib/src/build/command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use std::{
io,
path::Path,
process::{Command, ExitStatus},
};
use thiserror::Error;

use crate::{
build::variables::HasVariables,
config::Config,
lua_installation::LuaInstallation,
progress::{Progress, ProgressBar},
rockspec::{Build, CommandBuildSpec},
tree::RockLayout,
};

#[derive(Error, Debug)]
pub enum CommandError {
#[error("failed to execute command:\n{command}\nstatus: {status}\nstdout: {stdout}\nstderr: {stderr}")]
CommandFailure {
command: String,
status: ExitStatus,
stdout: String,
stderr: String,
},
#[error("failed to run command build step: {0}")]
Io(io::Error),
#[error("failed to run command build step: {0} command not found!")]
CommandNotFound(String),
}

impl Build for CommandBuildSpec {
type Err = CommandError;

async fn run(
self,
output_paths: &RockLayout,
no_install: bool,
lua: &LuaInstallation,
config: &Config,
build_dir: &Path,
progress: &Progress<ProgressBar>,
) -> Result<(), Self::Err> {
progress.map(|bar| bar.set_message("Running build_command..."));
run_command(&self.build_command, output_paths, lua, config, build_dir)?;
if !no_install {
progress.map(|bar| bar.set_message("Running install_command..."));
run_command(&self.install_command, output_paths, lua, config, build_dir)?;
}
Ok(())
}
}

fn run_command(
command: &str,
output_paths: &RockLayout,
lua: &LuaInstallation,
config: &Config,
build_dir: &Path,
) -> Result<(), CommandError> {
let mut substituted_cmd = output_paths.substitute_variables(command);
substituted_cmd = lua.substitute_variables(&substituted_cmd);
substituted_cmd = config.substitute_variables(&substituted_cmd);
match Command::new(&substituted_cmd)
.current_dir(build_dir)
.spawn()
{
Err(_) => return Err(CommandError::CommandNotFound(substituted_cmd)),
Ok(child) => match child.wait_with_output() {
Ok(output) if output.status.success() => {}
Ok(output) => {
return Err(CommandError::CommandFailure {
command: substituted_cmd,
status: output.status,
stdout: String::from_utf8_lossy(&output.stdout).into(),
stderr: String::from_utf8_lossy(&output.stderr).into(),
});
}
Err(err) => return Err(CommandError::Io(err)),
},
}
Ok(())
}
86 changes: 86 additions & 0 deletions rocks-lib/src/build/luarocks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use std::{io, path::Path};

use crate::{
config::Config,
lua_installation::LuaInstallation,
luarocks_installation::{ExecLuaRocksError, LuaRocksError, LuaRocksInstallation},
progress::{Progress, ProgressBar},
rockspec::Rockspec,
tree::RockLayout,
};

use tempdir::TempDir;
use thiserror::Error;

use super::utils::recursive_copy_dir;

#[derive(Error, Debug)]
pub enum LuarocksBuildError {
#[error(transparent)]
Io(#[from] io::Error),
#[error("error instantiating luarocks compatibility layer: {0}")]
LuaRocksError(#[from] LuaRocksError),
#[error("error running 'luarocks make': {0}")]
ExecLuaRocksError(#[from] ExecLuaRocksError),
}

pub(crate) async fn build(
rockspec: &Rockspec,
output_paths: &RockLayout,
lua: &LuaInstallation,
config: &Config,
build_dir: &Path,
progress: &Progress<ProgressBar>,
) -> Result<(), LuarocksBuildError> {
progress.map(|p| {
p.set_message(format!(
"Building {} {} with luarocks...",
rockspec.package, rockspec.version
))
});
let rockspec_temp_dir = TempDir::new("temp-rockspec")?.into_path();
let rockspec_file = rockspec_temp_dir.join(format!(
"{}-{}.rockspec",
rockspec.package, rockspec.version
));
std::fs::write(&rockspec_file, &rockspec.raw_content)?;
let luarocks = LuaRocksInstallation::new(config)?;
let luarocks_tree = TempDir::new("luarocks-compat-tree")?;
luarocks.make(&rockspec_file, build_dir, luarocks_tree.path(), lua)?;
install(rockspec, &luarocks_tree.into_path(), output_paths, config)
}

fn install(
rockspec: &Rockspec,
luarocks_tree: &Path,
output_paths: &RockLayout,
config: &Config,
) -> Result<(), LuarocksBuildError> {
let lua_version = rockspec
.lua_version_from_config(config)
.expect("could not get lua version!");
std::fs::create_dir_all(&output_paths.bin)?;
let package_dir = luarocks_tree
.join("lib")
.join("lib")
.join("luarocks")
.join(format!(
"rocks-{}",
&lua_version.version_compatibility_str()
))
.join(format!("{}", rockspec.package))
.join(format!("{}", rockspec.version));
recursive_copy_dir(&package_dir.join("doc"), &output_paths.doc)?;
recursive_copy_dir(&luarocks_tree.join("bin"), &output_paths.bin)?;
let src_dir = luarocks_tree
.join("share")
.join("lua")
.join(lua_version.version_compatibility_str());
recursive_copy_dir(&src_dir, &output_paths.src)?;
let lib_dir = luarocks_tree
.join("lib")
.join("lua")
.join(lua_version.version_compatibility_str());
recursive_copy_dir(&lib_dir, &output_paths.lib)?;
Ok(())
}
37 changes: 18 additions & 19 deletions rocks-lib/src/build/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ use crate::{
tree::RockLayout,
};

use super::BuildError;

#[derive(Error, Debug)]
pub enum MakeError {
#[error("make step failed.\nstatus: {status}\nstdout: {stdout}\nstderr: {stderr}")]
#[error("{name} step failed.\nstatus: {status}\nstdout: {stdout}\nstderr: {stderr}")]
CommandFailure {
name: String,
status: ExitStatus,
stdout: String,
stderr: String,
Expand All @@ -32,7 +31,7 @@ pub enum MakeError {
}

impl Build for MakeBuildSpec {
type Err = BuildError;
type Err = MakeError;

async fn run(
self,
Expand All @@ -49,15 +48,15 @@ impl Build for MakeBuildSpec {
.build_variables
.into_iter()
.map(|(key, value)| {
let mut substituted_value = output_paths.substitute_variables(value);
substituted_value = lua.substitute_variables(substituted_value);
substituted_value = config.substitute_variables(substituted_value);
let mut substituted_value = output_paths.substitute_variables(&value);
substituted_value = lua.substitute_variables(&substituted_value);
substituted_value = config.substitute_variables(&substituted_value);
format!("{key}={substituted_value}")
})
.collect_vec();
match Command::new(config.make_cmd())
.current_dir(build_dir)
.arg(self.build_target)
.arg(&self.build_target)
.args(["-f", self.makefile.to_str().unwrap()])
.args(build_args)
.spawn()
Expand All @@ -66,15 +65,15 @@ impl Build for MakeBuildSpec {
Ok(output) if output.status.success() => {}
Ok(output) => {
return Err(MakeError::CommandFailure {
name: format!("{} {}", config.make_cmd(), self.build_target),
status: output.status,
stdout: String::from_utf8_lossy(&output.stdout).into(),
stderr: String::from_utf8_lossy(&output.stderr).into(),
}
.into());
});
}
Err(err) => return Err(MakeError::Io(err).into()),
Err(err) => return Err(MakeError::Io(err)),
},
Err(_) => return Err(MakeError::CommandNotFound(config.make_cmd().clone()).into()),
Err(_) => return Err(MakeError::CommandNotFound(config.make_cmd().clone())),
}
};

Expand All @@ -84,29 +83,29 @@ impl Build for MakeBuildSpec {
.install_variables
.into_iter()
.map(|(key, value)| {
let mut substituted_value = output_paths.substitute_variables(value);
substituted_value = lua.substitute_variables(substituted_value);
substituted_value = config.substitute_variables(substituted_value);
let mut substituted_value = output_paths.substitute_variables(&value);
substituted_value = lua.substitute_variables(&substituted_value);
substituted_value = config.substitute_variables(&substituted_value);
format!("{key}={substituted_value}")
})
.collect_vec();
match Command::new(config.make_cmd())
.current_dir(build_dir)
.arg(self.install_target)
.arg(&self.install_target)
.args(["-f", self.makefile.to_str().unwrap()])
.args(install_args)
.output()
{
Ok(output) if output.status.success() => {}
Ok(output) => {
return Err(MakeError::CommandFailure {
name: format!("{} {}", config.make_cmd(), self.install_target),
status: output.status,
stdout: String::from_utf8_lossy(&output.stdout).into(),
stderr: String::from_utf8_lossy(&output.stderr).into(),
}
.into())
})
}
Err(err) => return Err(MakeError::Io(err).into()),
Err(err) => return Err(MakeError::Io(err)),
}
};

Expand Down
33 changes: 20 additions & 13 deletions rocks-lib/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ use crate::{
rockspec::{Build as _, BuildBackendSpec, LuaVersionError, Rockspec},
tree::{RockLayout, Tree},
};
pub(crate) mod utils; // Make build utilities available as a submodule
pub(crate) mod utils;
use command::CommandError;
// Make build utilities available as a submodule
use indicatif::style::TemplateError;
use luarocks::LuarocksBuildError;
use make::MakeError;
use rust_mlua::RustError;
use thiserror::Error;
use utils::recursive_copy_dir;
mod builtin;
mod command;
mod luarocks;
mod make;
mod rust_mlua;
pub mod variables;
Expand All @@ -32,6 +38,8 @@ pub enum BuildError {
#[error(transparent)]
MakeError(#[from] MakeError),
#[error(transparent)]
CommandError(#[from] CommandError),
#[error(transparent)]
RustError(#[from] RustError),
#[error(transparent)]
LuaVersionError(#[from] LuaVersionError),
Expand All @@ -43,6 +51,8 @@ pub enum BuildError {
stdout: String,
stderr: String,
},
#[error(transparent)]
LuarocksBuildError(#[from] LuarocksBuildError),
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -82,11 +92,19 @@ async fn run_build(
.run(output_paths, false, lua, config, build_dir, progress)
.await?
}
Some(BuildBackendSpec::Command(command_spec)) => {
command_spec
.run(output_paths, false, lua, config, build_dir, progress)
.await?
}
Some(BuildBackendSpec::RustMlua(rust_mlua_spec)) => {
rust_mlua_spec
.run(output_paths, false, lua, config, build_dir, progress)
.await?
}
Some(BuildBackendSpec::LuaRock(_)) => {
luarocks::build(rockspec, output_paths, lua, config, build_dir, progress).await?;
}
None => (),
_ => unimplemented!(),
}
Expand Down Expand Up @@ -214,19 +232,8 @@ pub async fn build(

install(&rockspec, &tree, &output_paths, &lua, &build_dir, progress).await?;

// Copy over all `copy_directories` to their respective paths
for directory in &rockspec.build.current_platform().copy_directories {
for file in walkdir::WalkDir::new(build_dir.join(directory))
.into_iter()
.flatten()
{
if file.file_type().is_file() {
let filepath = file.path();
let target = output_paths.etc.join(filepath);
std::fs::create_dir_all(target.parent().unwrap())?;
std::fs::copy(filepath, target)?;
}
}
recursive_copy_dir(&build_dir.join(directory), &output_paths.etc)?;
}

Ok(package)
Expand Down
Loading
Loading