Skip to main content

lux_lib/luarocks/
mod.rs

1use std::{
2    collections::HashMap,
3    path::{Path, PathBuf},
4};
5
6use crate::{fs, project, tree::RockLayout};
7use miette::Diagnostic;
8use thiserror::Error;
9
10use self::rock_manifest::RockManifest;
11
12pub mod install_binary_rock;
13pub mod installation;
14
15pub use installation::Installation;
16pub mod rock_manifest;
17
18/// Retrieves the target compilation platform and returns it as a luarocks identifier.
19pub(crate) fn current_platform_luarocks_identifier() -> String {
20    let platform = match std::env::consts::OS {
21        "macos" => "macosx",
22        p => p,
23    };
24    format!("{}-{}", platform, std::env::consts::ARCH)
25}
26
27#[derive(Error, Debug, Diagnostic)]
28#[non_exhaustive]
29pub enum Error {
30    #[error(transparent)]
31    #[diagnostic(transparent)]
32    Fs(#[from] fs::Error),
33    #[error("rock_manifest not found")]
34    #[diagnostic(help("the rock may have been packed using LuaRocks version 1"))]
35    RockManifestNotFound,
36    #[error(transparent)]
37    #[diagnostic(transparent)]
38    RockManifest(#[from] rock_manifest::Error),
39}
40
41pub(crate) struct RockManifestSources {
42    pub root: PathBuf,
43    pub lib: PathBuf,
44    pub lua: PathBuf,
45    pub bin: PathBuf,
46}
47
48#[tracing::instrument(level = "trace", skip(entry))]
49async fn install_manifest_entries<T>(
50    entry: &HashMap<PathBuf, T>,
51    src: &Path,
52    dest: &Path,
53) -> Result<(), fs::Error> {
54    for relative_src_path in entry.keys() {
55        let target = dest.join(relative_src_path);
56        let src_path = src.join(relative_src_path);
57        if src_path.is_dir() {
58            project::copy_dir(&src_path, &target).await?;
59        } else if src_path.is_file() {
60            if let Some(target_parent_dir) = target.parent() {
61                fs::tokio::create_dir_all(target_parent_dir).await?;
62            }
63            fs::tokio::copy(&src_path, target).await?;
64        } else {
65            return Err(fs::Error::Other(format!(
66                "the entry {} listed in the `rock_manifest` is neither a file nor a directory",
67                src_path.display()
68            )));
69        }
70    }
71    Ok(())
72}
73
74async fn install_bin_manifest_entries(
75    entries: &HashMap<PathBuf, String>,
76    src: &Path,
77    dest: &Path,
78) -> Result<(), fs::Error> {
79    install_manifest_entries(entries, src, dest).await?;
80    #[cfg(unix)]
81    for relative_src_path in entries.keys() {
82        fs::set_executable_permissions(&dest.join(relative_src_path)).await?;
83    }
84    Ok(())
85}
86
87pub(crate) async fn install_rock_manifest(
88    sources: RockManifestSources,
89    layout: &RockLayout,
90    bin_dest: &Path,
91) -> Result<(), Error> {
92    let rock_manifest_file = sources.root.join("rock_manifest");
93    if !rock_manifest_file.is_file() {
94        return Err(Error::RockManifestNotFound);
95    }
96    let rock_manifest_content = fs::tokio::read_to_string(rock_manifest_file).await?;
97    let rock_manifest = RockManifest::new(&rock_manifest_content)?;
98    install_manifest_entries(&rock_manifest.lib.entries, &sources.lib, &layout.lib).await?;
99    install_manifest_entries(&rock_manifest.lua.entries, &sources.lua, &layout.src).await?;
100    install_bin_manifest_entries(&rock_manifest.bin.entries, &sources.bin, bin_dest).await?;
101    install_manifest_entries(
102        &rock_manifest.doc.entries,
103        &sources.root.join("doc"),
104        &layout.doc,
105    )
106    .await?;
107    install_manifest_entries(
108        &rock_manifest.conf.entries,
109        &sources.root.join("conf"),
110        &layout.conf,
111    )
112    .await?;
113    install_manifest_entries(&rock_manifest.root.entries, &sources.root, &layout.etc).await?;
114    Ok(())
115}