Skip to main content

lux_cli/
install_rockspec.rs

1use lux_lib::rockspec;
2use miette::miette;
3use std::path::PathBuf;
4
5use clap::Args;
6use lux_lib::{
7    config::Config,
8    lockfile::PinnedState,
9    operations::{self},
10    rockspec::LuaVersionCompatibility,
11};
12
13use miette::{IntoDiagnostic, Result};
14
15#[derive(Args, Default)]
16pub struct InstallRockspec {
17    /// The path to the RockSpec file to install
18    rockspec_path: PathBuf,
19
20    /// Whether to pin the installed package and dependencies.
21    #[arg(long)]
22    pin: bool,
23}
24
25/// Install a rockspec into the user tree.
26pub async fn install_rockspec(data: InstallRockspec, config: Config) -> Result<()> {
27    let pin = PinnedState::from(data.pin);
28    let path = data.rockspec_path;
29
30    if path
31        .extension()
32        .map(|ext| ext != "rockspec")
33        .unwrap_or(true)
34    {
35        return Err(miette!("provided path is not a valid rockspec!"));
36    }
37
38    let content = std::fs::read_to_string(path).into_diagnostic()?;
39
40    let rockspec = rockspec::lua::Remote::new(&content)?;
41
42    let lua_version = rockspec.lua_version_matches(&config)?;
43    let tree = config.user_tree(lua_version)?;
44
45    operations::InstallRockspec::new()
46        .rockspec(rockspec)
47        .pin(pin)
48        .config(&config)
49        .tree(&tree)
50        .install()
51        .await?;
52
53    Ok(())
54}
55
56#[cfg(test)]
57mod tests {
58
59    use super::*;
60
61    use assert_fs::{
62        prelude::{FileWriteStr, PathChild, PathCreateDir},
63        TempDir,
64    };
65
66    use lux_lib::{config, lua};
67
68    #[tokio::test]
69    async fn test_install_rockspec_from_vendored() {
70        // This test runs without a network connection when run with Nix
71        let vendor_dir = TempDir::new().unwrap();
72        let foo_dir = vendor_dir.child("foo@1.0.0-1");
73        foo_dir.create_dir_all().unwrap();
74        let foo_rockspec = vendor_dir.child("foo-1.0.0-1.rockspec");
75        foo_rockspec
76            .write_str(
77                r#"
78                package = 'foo'
79                version = '1.0.0-1'
80                source = {
81                    url = 'https://github.com/lumen-oss/luarocks-stub',
82                }
83            "#,
84            )
85            .unwrap();
86        let bar_dir = vendor_dir.child("bar@2.0.0-2");
87        bar_dir.create_dir_all().unwrap();
88        let bar_rockspec = vendor_dir.child("bar-2.0.0-2.rockspec");
89        bar_rockspec
90            .write_str(
91                r#"
92                package = 'bar'
93                version = '2.0.0-2'
94                source = {
95                    url = 'https://github.com/lumen-oss/luarocks-stub',
96                }
97            "#,
98            )
99            .unwrap();
100        let baz_dir = vendor_dir.child("baz@2.0.0-1");
101        baz_dir.create_dir_all().unwrap();
102        let baz_rockspec = vendor_dir.child("baz-2.0.0-1.rockspec");
103        baz_rockspec
104            .write_str(
105                r#"
106                package = 'baz'
107                version = '2.0.0-1'
108                source = {
109                    url = 'https://github.com/lumen-oss/luarocks-stub',
110                }
111            "#,
112            )
113            .unwrap();
114        let test_rock_dir = vendor_dir.child("test_rock@scm-1");
115        test_rock_dir.create_dir_all().unwrap();
116        let rockspec_content = r#"
117        package = 'test_rock'
118        version = 'scm-1'
119        source = {
120            url = 'https://github.com/lumen-oss/luarocks-stub',
121        }
122        dependencies = {
123            'foo >= 1.0.0',
124            'bar',
125            'baz == 2.0.0',
126        }
127        "#;
128        let temp_dir = TempDir::new().unwrap();
129        let rockspec = temp_dir.child("test_rock-scm-1.rockspec");
130        rockspec.write_str(rockspec_content).unwrap();
131        let lua_version = lua::detect::installed_version().or(Some(lua::Version::Lua51));
132        let config = config::Builder::new()
133            .unwrap()
134            .vendor_dir(Some(vendor_dir.to_path_buf()))
135            .lua_version(lua_version)
136            .user_tree(Some(temp_dir.to_path_buf()))
137            .build()
138            .unwrap();
139        install_rockspec(
140            InstallRockspec {
141                rockspec_path: rockspec.to_path_buf(),
142                pin: false,
143            },
144            config,
145        )
146        .await
147        .unwrap()
148    }
149
150    /// Non-regression for https://github.com/lumen-oss/lux/issues/2098
151    #[tokio::test]
152    async fn test_install_rockspec_with_git_source_dir() {
153        let vendor_dir = TempDir::new().unwrap();
154        let repo_dir = vendor_dir.child("lubmin@1.0.0-1");
155        repo_dir.child("lub").create_dir_all().unwrap();
156        repo_dir
157            .child("lub/Template.lua")
158            .write_str("return {}")
159            .unwrap();
160        let temp_dir = TempDir::new().unwrap();
161        let rockspec = temp_dir.child("lubmin-1.0.0-1.rockspec");
162        rockspec
163            .write_str(
164                r#"
165                package = 'lubmin'
166                version = '1.0.0-1'
167                source = {
168                    url = 'git+https://github.com/lubyk/lub',
169                    tag = 'REL-1.1.0',
170                    dir = 'lub',
171                }
172                dependencies = {
173                    'lua >= 5.1',
174                }
175                build = {
176                    type = 'builtin',
177                    modules = {
178                        ['lub.Template'] = 'lub/Template.lua',
179                    },
180                }
181            "#,
182            )
183            .unwrap();
184        let lua_version = lua::detect::installed_version().or(Some(lua::Version::Lua51));
185        let config = config::Builder::new()
186            .unwrap()
187            .vendor_dir(Some(vendor_dir.to_path_buf()))
188            .lua_version(lua_version)
189            .user_tree(Some(temp_dir.to_path_buf()))
190            .build()
191            .unwrap();
192        install_rockspec(
193            InstallRockspec {
194                rockspec_path: rockspec.to_path_buf(),
195                pin: false,
196            },
197            config,
198        )
199        .await
200        .unwrap();
201    }
202}