Skip to content

Commit

Permalink
feat: rocks install-lua command + Lua compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro committed Sep 11, 2024
1 parent e34686f commit c958ae2
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 50 deletions.
28 changes: 28 additions & 0 deletions Cargo.lock

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

24 changes: 24 additions & 0 deletions rocks-bin/src/install_lua.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use clap::Args;
use eyre::Result;
use rocks_lib::{
config::{Config, LuaVersion},
lua::Lua,
};

#[derive(Args)]
pub struct InstallLua {
version: LuaVersion,
}

// TODO: Make `config` useful: allow custom paths to install lua into, perhaps `--lua-dir`?
pub fn install_lua(install_data: InstallLua, _config: &Config) -> Result<()> {
let version_stringified = install_data.version.to_string();

// TODO: Detect when path already exists by checking `Lua::path()` and prompt the user
// whether they'd like to forcefully reinstall.
Lua::new(&install_data.version)?;

print!("Succesfully installed Lua {version_stringified}.");

Ok(())
}
21 changes: 8 additions & 13 deletions rocks-bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use clap::{Parser, Subcommand};
use debug::Debug;
use download::Download;
use install::Install;
use install_lua::InstallLua;
use list::ListCmd;
use outdated::Outdated;
use rocks_lib::config::{Config, LuaVersion};
Expand All @@ -16,6 +17,7 @@ mod build;
mod debug;
mod download;
mod install;
mod install_lua;
mod list;
mod outdated;
mod project;
Expand All @@ -24,18 +26,6 @@ mod unpack;
mod update;
mod utils;

fn parse_lua_version(s: &str) -> Result<LuaVersion, String> {
match s {
"5.1" | "51" | "jit" | "luajit" => Ok(LuaVersion::Lua51),
"5.2" | "52" => Ok(LuaVersion::Lua52),
"5.3" | "53" => Ok(LuaVersion::Lua53),
"5.4" | "54" => Ok(LuaVersion::Lua54),
_ => Err(
"unrecognized Lua version. Allowed versions: '5.1', '5.2', '5.3', '5.4', 'jit'.".into(),
),
}
}

/// A fast and efficient Lua package manager.
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
Expand Down Expand Up @@ -68,7 +58,7 @@ struct Cli {
lua_dir: Option<PathBuf>,

/// Which Lua installation to use.
#[arg(long, value_name = "ver", value_parser = parse_lua_version)]
#[arg(long, value_name = "ver")]
lua_version: Option<LuaVersion>,

/// Which tree to operate on.
Expand Down Expand Up @@ -115,6 +105,8 @@ enum Commands {
Init,
/// Install a rock for use on the system.
Install(Install),
/// Manually install and manage Lua headers for various Lua versions.
InstallLua(InstallLua),
/// Check syntax of a rockspec.
Lint,
/// List currently installed rocks.
Expand Down Expand Up @@ -189,6 +181,9 @@ async fn main() {
install::install(install_data, &config).await.unwrap()
}
Commands::Outdated(outdated) => outdated::outdated(outdated, &config).await.unwrap(),
Commands::InstallLua(install_lua) => {
install_lua::install_lua(install_lua, &config).unwrap()
}
_ => unimplemented!(),
},
None => {
Expand Down
12 changes: 6 additions & 6 deletions rocks-bin/src/outdated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub async fn outdated(outdated_data: Outdated, config: &Config) -> Result<()> {
.expect("TODO")
.map(|version| (rock, version))
})
.sorted_by_key(|(rock, _)| rock.name.clone())
.into_group_map_by(|(rock, _)| rock.name.clone());
.sorted_by_key(|(rock, _)| rock.name().to_owned())
.into_group_map_by(|(rock, _)| rock.name().to_owned());

if outdated_data.porcelain {
let jsonified_rock_list = rock_list
Expand All @@ -52,7 +52,7 @@ pub async fn outdated(outdated_data: Outdated, config: &Config) -> Result<()> {
key,
values
.iter()
.map(|(k, v)| (k.version.to_string(), v.to_string()))
.map(|(k, v)| (k.version().to_string(), v.to_string()))
.collect::<HashMap<_, _>>(),
)
})
Expand All @@ -62,11 +62,11 @@ pub async fn outdated(outdated_data: Outdated, config: &Config) -> Result<()> {
} else {
let formatting = TreeFormatting::dir_tree(FormatCharacters::box_chars());

for (rock, updates) in rock_list {
let mut tree = StringTreeNode::new(rock);
for (rock_name, updates) in rock_list {
let mut tree = StringTreeNode::new(rock_name.to_string());

for (rock, latest_version) in updates {
tree.push(format!("{} => {}", rock.version, latest_version));
tree.push(format!("{} => {}", rock.version(), latest_version));
}

println!("{}", tree.to_string_with_format(&formatting)?);
Expand Down
3 changes: 3 additions & 0 deletions rocks-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ zip = "0.6.6"
insta = { version = "1.39.0", features = ["redactions", "yaml"] }
which = "6.0.3"
lets_find_up = "0.0.3"
lua-src = "547.0.0"
target-lexicon = "0.12.16"
luajit-src = "210.5.10"

[dev-dependencies]
httptest = { version = "0.15.5" }
Expand Down
23 changes: 21 additions & 2 deletions rocks-lib/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use directories::ProjectDirs;
use eyre::{eyre, Result};
use std::{fmt::Display, path::PathBuf, time::Duration};
use std::{fmt::Display, path::PathBuf, str::FromStr, time::Duration};

#[derive(Clone)]
pub enum LuaVersion {
Expand All @@ -13,13 +13,32 @@ pub enum LuaVersion {
// LuaU,
}

impl FromStr for LuaVersion {
type Err = String;

fn from_str(s: &str) -> std::prelude::v1::Result<Self, Self::Err> {
match s {
"5.1" | "51" => Ok(LuaVersion::Lua51),
"5.2" | "52" => Ok(LuaVersion::Lua52),
"5.3" | "53" => Ok(LuaVersion::Lua53),
"5.4" | "54" => Ok(LuaVersion::Lua54),
"jit" | "luajit" => Ok(LuaVersion::LuaJIT),
_ => Err(
"unrecognized Lua version. Allowed versions: '5.1', '5.2', '5.3', '5.4', 'jit'."
.into(),
),
}
}
}

impl Display for LuaVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
LuaVersion::Lua51 | LuaVersion::LuaJIT => "5.1",
LuaVersion::Lua51 => "5.1",
LuaVersion::Lua52 => "5.2",
LuaVersion::Lua53 => "5.3",
LuaVersion::Lua54 => "5.4",
LuaVersion::LuaJIT => "jit",
})
}
}
Expand Down
1 change: 1 addition & 0 deletions rocks-lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod build;
pub mod config;
pub mod lua;
pub mod lua_package;
pub mod manifest;
pub mod operations;
Expand Down
71 changes: 71 additions & 0 deletions rocks-lib/src/lua/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use eyre::Result;
use std::path::PathBuf;
use target_lexicon::Triple;

use crate::config::{Config, LuaVersion};

pub struct Lua {
pub include_dir: PathBuf,
pub lib_dir: PathBuf,
}

impl Lua {
pub fn new(version: &LuaVersion) -> Result<Self> {
let output = Self::path(version)?;

if output.exists() {
Ok(Lua {
include_dir: output.join("include"),
lib_dir: output.join("lib"),
})
} else {
let host = Triple::host();
let target = &host.to_string();
let host_operating_system = &host.operating_system.to_string();

let (include_dir, lib_dir) = match version {
LuaVersion::LuaJIT => {
let build = lua_src::Build::new()
.target(target)
.host(host_operating_system)
.out_dir(output)
.build(match version {
LuaVersion::Lua51 => lua_src::Version::Lua51,
LuaVersion::Lua52 => lua_src::Version::Lua52,
LuaVersion::Lua53 => lua_src::Version::Lua53,
LuaVersion::Lua54 => lua_src::Version::Lua54,
LuaVersion::LuaJIT => unreachable!(),
});

(
build.include_dir().to_path_buf(),
build.lib_dir().to_path_buf(),
)
}
_ => {
let build = luajit_src::Build::new()
.target(target)
.host(host_operating_system)
.out_dir(output)
.build();

(
build.include_dir().to_path_buf(),
build.lib_dir().to_path_buf(),
)
}
};

Ok(Lua {
include_dir,
lib_dir,
})
}
}

pub fn path(version: &LuaVersion) -> Result<PathBuf> {
Ok(Config::get_default_cache_path()?
.join("lua")
.join(version.to_string()))
}
}
1 change: 1 addition & 0 deletions rocks-lib/src/lua_package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use semver::Version;
use serde::{Deserialize, Serialize};
use std::fmt::Display;

mod outdated;
mod version;

pub use version::{parse_version, parse_version_req};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use semver::Version;

use crate::manifest::ManifestMetadata;

use super::LuaRock;
use super::LuaPackage;

impl LuaRock {
impl LuaPackage {
/// Tries to find a newer version of a given rock.
/// Returns the latest version if found.
pub fn has_update(&self, manifest: &ManifestMetadata) -> Result<Option<Version>> {
Expand Down
21 changes: 0 additions & 21 deletions rocks-lib/src/luarock/mod.rs

This file was deleted.

12 changes: 6 additions & 6 deletions rocks-lib/src/tree/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::HashMap;
use itertools::Itertools;
use walkdir::WalkDir;

use crate::luarock::LuaRock;
use crate::lua_package::LuaPackage;

use super::Tree;

Expand All @@ -27,22 +27,22 @@ impl<'a> Tree<'a> {
.into_group_map()
}

pub fn into_rock_list(self) -> Result<Vec<LuaRock>> {
pub fn into_rock_list(self) -> Result<Vec<LuaPackage>> {
let rock_list = self.list();

Ok(rock_list
rock_list
.into_iter()
.flat_map(|(name, versions)| {
versions
.into_iter()
.map(|version| LuaRock::new(name.clone(), version))
.map(|version| LuaPackage::new(name.clone(), version))
.collect_vec()
})
.try_collect()?)
.try_collect()
}
}

impl<'a> TryFrom<Tree<'a>> for Vec<LuaRock> {
impl<'a> TryFrom<Tree<'a>> for Vec<LuaPackage> {
type Error = eyre::Report;

fn try_from(tree: Tree<'a>) -> Result<Self> {
Expand Down

0 comments on commit c958ae2

Please sign in to comment.