lux_lib/luarocks/installation/
mod.rs1use miette::Diagnostic;
2use ssri::Integrity;
3use std::io;
4use thiserror::Error;
5
6pub mod exec;
7
8use crate::{build, config::Config, fs, lua, operations::unpack, tree::InstallTree};
9
10#[cfg(target_family = "unix")]
11use crate::tree::{self, Tree};
12#[cfg(target_family = "windows")]
13use crate::tree::{self, Tree};
14
15#[cfg(target_family = "unix")]
16use crate::pipeline::build_local::Build;
17
18#[cfg(target_family = "unix")]
19const LUAROCKS_EXE: &str = "luarocks";
20#[cfg(target_family = "windows")]
21const LUAROCKS_EXE: &str = "luarocks.exe";
22
23pub(crate) const LUAROCKS_VERSION: &str = "3.13.0-1";
24
25#[cfg(target_family = "unix")]
26const LUAROCKS_ROCKSPEC: &str = "
27rockspec_format = '3.0'
28package = 'luarocks'
29version = '3.13.0-1'
30source = {
31 url = 'git+https://github.com/luarocks/luarocks',
32 tag = 'v3.13.0',
33}
34build = {
35 type = 'builtin',
36}
37";
38
39#[derive(Error, Debug, Diagnostic)]
40#[non_exhaustive]
41pub enum Error {
42 #[error(transparent)]
43 #[diagnostic(transparent)]
44 LuaVersion(#[from] lua::version::Error),
45 #[error("IO error occurred while trying to install luarocks")]
46 Io(#[from] io::Error),
47 #[error(transparent)]
48 #[diagnostic(transparent)]
49 Fs(#[from] fs::Error),
50 #[error(transparent)]
51 #[diagnostic(transparent)]
52 Tree(#[from] tree::Error),
53 #[error(transparent)]
54 #[diagnostic(transparent)]
55 Build(#[from] Box<build::Error>),
56 #[error(transparent)]
57 Request(#[from] crate::reqwest::Error),
58 #[error(transparent)]
59 #[diagnostic(transparent)]
60 Unpack(#[from] unpack::Error),
61 #[error("luarocks integrity mismatch.\nExpected: {expected}\nBut got: {got}")]
62 IntegrityMismatch { expected: Integrity, got: Integrity },
63}
64
65impl From<build::Error> for Error {
66 fn from(source: build::Error) -> Self {
67 Self::Build(Box::new(source))
68 }
69}
70
71pub struct Installation {
72 tree: Tree,
73 config: Config,
74}
75
76impl Installation {
77 pub fn new(config: &Config, tree: Tree) -> Result<Self, Error> {
78 let luarocks_installation = Self {
79 tree,
80 config: config.clone(),
81 };
82 Ok(luarocks_installation)
83 }
84
85 #[cfg(target_family = "unix")]
86 #[tracing::instrument(level = "trace", skip(self))]
87 pub async fn ensure_installed(&self, lua: &lua::Installation) -> Result<(), Error> {
88 use crate::{package, rockspec};
89
90 let mut lockfile = self.tree.lockfile()?.write_guard();
91
92 let luarocks_req = unsafe {
93 package::Req::new_unchecked("luarocks".into(), Some(LUAROCKS_VERSION.into()))
94 };
95
96 if !self.tree.match_rocks(&luarocks_req)?.is_found() {
97 let rockspec =
98 unsafe { rockspec::lua::Remote::new(LUAROCKS_ROCKSPEC).unwrap_unchecked() };
99 let pkg = Build::new()
100 .rockspec(&rockspec)
101 .lua(lua)
102 .tree(&self.tree)
103 .entry_type(tree::EntryType::Entrypoint)
104 .config(&self.config)
105 .behaviour(build::Behaviour::Ignore)
106 .constraint(luarocks_req.version_req().clone().into())
107 .build()
108 .await?;
109 lockfile.add_entrypoint(&pkg);
110 }
111 Ok(())
112 }
113
114 #[cfg(target_family = "windows")]
115 #[tracing::instrument(level = "trace", skip(self))]
116 pub async fn ensure_installed(&self, _lua: &lua::Installation) -> Result<(), Error> {
117 use crate::hash::HasIntegrity;
118 use std::io::Cursor;
119 let file_name = "luarocks-3.13.0-windows-64";
120 let url = format!("https://luarocks.github.io/luarocks/releases/{file_name}.zip");
121 let response = reqwest::get(url).await?.error_for_status()?.bytes().await?;
122 let hash = response.hash().await?;
123 let expected_hash: Integrity = unsafe {
124 "sha256-CJet5dRZ1VzRliqUgVN0WmdJ/rNFQDxoqqkgc4hVerk="
125 .parse()
126 .unwrap_unchecked()
127 };
128 if expected_hash.matches(&hash).is_none() {
129 return Err(Error::IntegrityMismatch {
130 expected: expected_hash,
131 got: hash,
132 });
133 }
134 let cursor = Cursor::new(response);
135 let mime_type = infer::get(cursor.get_ref()).map(|file_type| file_type.mime_type());
136 let unpack_dir = fs::tempfile::tempdir()?;
137 unpack::extract(
138 mime_type,
139 cursor,
140 false,
141 format!("{file_name}.zip"),
142 unpack_dir.path(),
143 )
144 .await?;
145 let luarocks_exe = unpack_dir.path().join(file_name).join(LUAROCKS_EXE);
146 fs::tokio::copy(luarocks_exe, &self.tree.bin().join(LUAROCKS_EXE)).await?;
147
148 Ok(())
149 }
150}
151
152impl From<reqwest::Error> for Error {
153 fn from(err: reqwest::Error) -> Self {
154 Self::Request(err.into())
155 }
156}