Skip to content

Commit

Permalink
fix(build): auto-detect modules broken
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Dec 4, 2024
1 parent a8a532a commit 0a91129
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return true
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',
}
2 changes: 1 addition & 1 deletion rocks-lib/resources/test/sample-tree/5.1/lock.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"1.0.0","rocks":{"d7164c4921869877c7f29fce3f14b8ea78e0aec0d0c36123a3a920a894785ea5":{"name":"neorg","version":"8.0.0-1","pinned":false,"dependencies":["48ec344951668eca0e0a4ff284d804a11e4e709194df3191a72ed8fac89cf2e0"],"constraint":null,"hashes":{"rockspec":"sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=","source":"sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="}},"48ec344951668eca0e0a4ff284d804a11e4e709194df3191a72ed8fac89cf2e0":{"name":"lua-cjson","version":"2.1.0-1","pinned":false,"dependencies":[],"constraint":null,"hashes":{"rockspec":"sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=","source":"sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="}},"aa0a5bcd396f3b8e59fa9dd8059a06c3bf4952f48473214d96fc46895edb59f0":{"name":"neorg","version":"8.8.1-1","pinned":false,"dependencies":["48ec344951668eca0e0a4ff284d804a11e4e709194df3191a72ed8fac89cf2e0"],"constraint":null,"hashes":{"rockspec":"sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=","source":"sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="}}},"entrypoints":["d7164c4921869877c7f29fce3f14b8ea78e0aec0d0c36123a3a920a894785ea5","aa0a5bcd396f3b8e59fa9dd8059a06c3bf4952f48473214d96fc46895edb59f0"]}
{"version":"1.0.0","rocks":{"48ec344951668eca0e0a4ff284d804a11e4e709194df3191a72ed8fac89cf2e0":{"name":"lua-cjson","version":"2.1.0-1","pinned":false,"dependencies":[],"constraint":null,"hashes":{"rockspec":"sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=","source":"sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="}},"aa0a5bcd396f3b8e59fa9dd8059a06c3bf4952f48473214d96fc46895edb59f0":{"name":"neorg","version":"8.8.1-1","pinned":false,"dependencies":["48ec344951668eca0e0a4ff284d804a11e4e709194df3191a72ed8fac89cf2e0"],"constraint":null,"hashes":{"rockspec":"sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=","source":"sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="}},"d7164c4921869877c7f29fce3f14b8ea78e0aec0d0c36123a3a920a894785ea5":{"name":"neorg","version":"8.0.0-1","pinned":false,"dependencies":["48ec344951668eca0e0a4ff284d804a11e4e709194df3191a72ed8fac89cf2e0"],"constraint":null,"hashes":{"rockspec":"sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek=","source":"sha256-uU0nuZNNPgilLlLX2n2r+sSE7+N6U4DukIj3rOLvzek="}}},"entrypoints":["aa0a5bcd396f3b8e59fa9dd8059a06c3bf4952f48473214d96fc46895edb59f0","d7164c4921869877c7f29fce3f14b8ea78e0aec0d0c36123a3a920a894785ea5"]}
11 changes: 3 additions & 8 deletions rocks-lib/src/build/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ use std::{
collections::HashMap,
path::{Path, PathBuf},
};
use walkdir::WalkDir;

use crate::{
build::utils,
config::Config,
lua_installation::LuaInstallation,
progress::{
Progress::{self},
ProgressBar,
},
rockspec::{utils, Build, BuiltinBuildSpec, LuaModule, ModuleSpec},
rockspec::{Build, BuiltinBuildSpec, LuaModule, ModuleSpec},
tree::RockLayout,
};
use walkdir::WalkDir;

use super::BuildError;

Expand Down Expand Up @@ -126,12 +127,6 @@ fn autodetect_modules(build_dir: &Path) -> HashMap<LuaModule, ModuleSpec> {
let diff: PathBuf = pathdiff::diff_paths(build_dir.join(file.into_path()), build_dir)
.expect("failed to autodetect modules");

// NOTE(vhyrro): You may ask why we convert all paths to Lua module paths
// just to convert them back later in the `run()` stage.
//
// The rockspec requires the format to be like this, and representing our
// data in this form allows us to respect any overrides made by the user (which follow
// the `module.name` format, not our internal one).
let pathbuf = diff.components().skip(1).collect::<PathBuf>();
let lua_module = LuaModule::from_pathbuf(pathbuf);

Expand Down
5 changes: 4 additions & 1 deletion rocks-lib/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use crate::{
operations::{self, FetchSrcRockError},
package::RemotePackage,
progress::{Progress, ProgressBar},
rockspec::{utils, Build as _, BuildBackendSpec, LuaVersionError, Rockspec},
rockspec::{Build as _, BuildBackendSpec, LuaVersionError, Rockspec},
tree::{RockLayout, Tree},
};
pub(crate) mod utils; // Make build utilities available as a submodule
use indicatif::style::TemplateError;
use make::MakeError;
use rust_mlua::RustError;
Expand Down Expand Up @@ -231,3 +232,5 @@ pub async fn build(
}
}
}

// TODO: Add tests using sample projects
15 changes: 7 additions & 8 deletions rocks-lib/src/build/rust_mlua.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
use itertools::Itertools;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus};
use std::{fs, io};
use thiserror::Error;

use super::utils::lua_lib_extension;
use crate::config::LuaVersionUnset;
use crate::progress::{Progress, ProgressBar};
use crate::rockspec::utils::lua_lib_extension;
use crate::{
config::{Config, LuaVersion},
lua_installation::LuaInstallation,
rockspec::{Build, RustMluaBuildSpec},
tree::RockLayout,
};
use itertools::Itertools;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus};
use std::{fs, io};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum RustError {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
use crate::{
build::BuildError,
lua_installation::LuaInstallation,
rockspec::{LuaModule, ModulePaths},
};
use itertools::Itertools;
use std::{
io,
Expand All @@ -6,13 +11,9 @@ use std::{
};
use target_lexicon::Triple;

use crate::{build::BuildError, lua_installation::LuaInstallation};

use super::{LuaModule, ModulePaths};

/// Copies a lua source file to a specific destination. The destination is described by a
/// `module.path` syntax (equivalent to the syntax provided to Lua's `require()` function).
pub fn copy_lua_to_module_path(
pub(crate) fn copy_lua_to_module_path(
source: &PathBuf,
target_module: &LuaModule,
target_dir: &Path,
Expand Down Expand Up @@ -40,7 +41,7 @@ fn validate_output(output: Output) -> Result<(), BuildError> {
/// Compiles a set of C files into a single dynamic library and places them under `{target_dir}/{target_file}`.
/// # Panics
/// Panics if no parent or no filename can be determined for the target path.
pub fn compile_c_files(
pub(crate) fn compile_c_files(
files: &Vec<PathBuf>,
target_module: &LuaModule,
target_dir: &Path,
Expand Down Expand Up @@ -93,7 +94,7 @@ pub fn compile_c_files(
}

/// the extension for Lua libraries.
pub fn lua_lib_extension() -> &'static str {
pub(crate) fn lua_lib_extension() -> &'static str {
if cfg!(target_os = "windows") {
"dll"
} else {
Expand All @@ -104,7 +105,7 @@ pub fn lua_lib_extension() -> &'static str {
/// Compiles a set of C files (with extra metadata) to a given destination.
/// # Panics
/// Panics if no filename for the target path can be determined.
pub fn compile_c_modules(
pub(crate) fn compile_c_modules(
data: &ModulePaths,
source_dir: &Path,
target_module: &LuaModule,
Expand Down
2 changes: 1 addition & 1 deletion rocks-lib/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use itertools::Itertools;
use serde::Serialize;
use std::{env, fmt::Display, io, path::PathBuf, str::FromStr};

use crate::{rockspec::utils::lua_lib_extension, tree::Tree};
use crate::{build::utils::lua_lib_extension, tree::Tree};

const LUA_PATH_SEPARATOR: &str = ";";

Expand Down
27 changes: 21 additions & 6 deletions rocks-lib/src/rockspec/build/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ use serde::{de, Deserialize, Deserializer};
use std::{collections::HashMap, convert::Infallible, fmt::Display, path::PathBuf, str::FromStr};
use thiserror::Error;

use crate::rockspec::{
deserialize_vec_from_lua, FromPlatformOverridable, PartialOverride, PerPlatform,
PlatformOverridable,
use crate::{
build::utils::lua_lib_extension,
rockspec::{
deserialize_vec_from_lua, FromPlatformOverridable, PartialOverride, PerPlatform,
PlatformOverridable,
},
};

use super::utils::lua_lib_extension;

#[derive(Debug, PartialEq, Deserialize, Default, Clone)]
pub struct BuiltinBuildSpec {
/// Keys are module names in the format normally used by the `require()` function
Expand Down Expand Up @@ -39,7 +40,9 @@ impl LuaModule {
.unwrap_or("".into());
let module = path
.to_string_lossy()
.trim_end_matches(extension.as_str())
.trim_end_matches(format!("init.{}", extension).as_str())
.trim_end_matches(format!(".{}", extension).as_str())
.trim_end_matches(std::path::MAIN_SEPARATOR_STR)
.replace(std::path::MAIN_SEPARATOR_STR, ".");
LuaModule(module)
}
Expand Down Expand Up @@ -291,6 +294,18 @@ mod tests {

use super::*;

#[tokio::test]
pub async fn parse_lua_module_from_path() {
let lua_module = LuaModule::from_pathbuf("foo/init.lua".into());
assert_eq!(&lua_module.0, "foo");
let lua_module = LuaModule::from_pathbuf("foo/bar.lua".into());
assert_eq!(&lua_module.0, "foo.bar");
let lua_module = LuaModule::from_pathbuf("foo/bar/init.lua".into());
assert_eq!(&lua_module.0, "foo.bar");
let lua_module = LuaModule::from_pathbuf("foo/bar/baz.lua".into());
assert_eq!(&lua_module.0, "foo.bar.baz");
}

#[tokio::test]
pub async fn modules_spec_from_lua() {
let lua_content = "
Expand Down
1 change: 0 additions & 1 deletion rocks-lib/src/rockspec/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ mod cmake;
mod make;
mod rust_mlua;

pub mod utils; // Make build utilities available as a submodule
pub use builtin::{BuiltinBuildSpec, LuaModule, ModulePaths, ModuleSpec};
pub use cmake::*;
pub use make::*;
Expand Down

0 comments on commit 0a91129

Please sign in to comment.