Skip to main content

lux_cli/
install_rockspec.rs

1use miette::miette;
2use std::path::PathBuf;
3
4use clap::Args;
5use lux_lib::{
6    config::Config,
7    lockfile::PinnedState,
8    lua_rockspec::RemoteLuaRockspec,
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 = RemoteLuaRockspec::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::{
67        config::ConfigBuilder, lua_installation::detect_installed_lua_version,
68        lua_version::LuaVersion,
69    };
70
71    #[tokio::test]
72    async fn test_install_rockspec_from_vendored() {
73        // This test runs without a network connection when run with Nix
74        let vendor_dir = TempDir::new().unwrap();
75        let foo_dir = vendor_dir.child("foo@1.0.0-1");
76        foo_dir.create_dir_all().unwrap();
77        let foo_rockspec = vendor_dir.child("foo-1.0.0-1.rockspec");
78        foo_rockspec
79            .write_str(
80                r#"
81                package = 'foo'
82                version = '1.0.0-1'
83                source = {
84                    url = 'https://github.com/lumen-oss/luarocks-stub',
85                }
86            "#,
87            )
88            .unwrap();
89        let bar_dir = vendor_dir.child("bar@2.0.0-2");
90        bar_dir.create_dir_all().unwrap();
91        let bar_rockspec = vendor_dir.child("bar-2.0.0-2.rockspec");
92        bar_rockspec
93            .write_str(
94                r#"
95                package = 'bar'
96                version = '2.0.0-2'
97                source = {
98                    url = 'https://github.com/lumen-oss/luarocks-stub',
99                }
100            "#,
101            )
102            .unwrap();
103        let baz_dir = vendor_dir.child("baz@2.0.0-1");
104        baz_dir.create_dir_all().unwrap();
105        let baz_rockspec = vendor_dir.child("baz-2.0.0-1.rockspec");
106        baz_rockspec
107            .write_str(
108                r#"
109                package = 'baz'
110                version = '2.0.0-1'
111                source = {
112                    url = 'https://github.com/lumen-oss/luarocks-stub',
113                }
114            "#,
115            )
116            .unwrap();
117        let test_rock_dir = vendor_dir.child("test_rock@scm-1");
118        test_rock_dir.create_dir_all().unwrap();
119        let rockspec_content = r#"
120        package = 'test_rock'
121        version = 'scm-1'
122        source = {
123            url = 'https://github.com/lumen-oss/luarocks-stub',
124        }
125        dependencies = {
126            'foo >= 1.0.0',
127            'bar',
128            'baz == 2.0.0',
129        }
130        "#;
131        let temp_dir = TempDir::new().unwrap();
132        let rockspec = temp_dir.child("test_rock-scm-1.rockspec");
133        rockspec.write_str(rockspec_content).unwrap();
134        let lua_version = detect_installed_lua_version().or(Some(LuaVersion::Lua51));
135        let config = ConfigBuilder::new()
136            .unwrap()
137            .vendor_dir(Some(vendor_dir.to_path_buf()))
138            .lua_version(lua_version)
139            .user_tree(Some(temp_dir.to_path_buf()))
140            .build()
141            .unwrap();
142        install_rockspec(
143            InstallRockspec {
144                rockspec_path: rockspec.to_path_buf(),
145                pin: false,
146            },
147            config,
148        )
149        .await
150        .unwrap()
151    }
152
153    /// Non-regression for https://github.com/lumen-oss/lux/issues/2098
154    #[tokio::test]
155    async fn test_install_rockspec_with_git_source_dir() {
156        let vendor_dir = TempDir::new().unwrap();
157        let repo_dir = vendor_dir.child("lubmin@1.0.0-1");
158        repo_dir.child("lub").create_dir_all().unwrap();
159        repo_dir
160            .child("lub/Template.lua")
161            .write_str("return {}")
162            .unwrap();
163        let temp_dir = TempDir::new().unwrap();
164        let rockspec = temp_dir.child("lubmin-1.0.0-1.rockspec");
165        rockspec
166            .write_str(
167                r#"
168                package = 'lubmin'
169                version = '1.0.0-1'
170                source = {
171                    url = 'git+https://github.com/lubyk/lub',
172                    tag = 'REL-1.1.0',
173                    dir = 'lub',
174                }
175                dependencies = {
176                    'lua >= 5.1',
177                }
178                build = {
179                    type = 'builtin',
180                    modules = {
181                        ['lub.Template'] = 'lub/Template.lua',
182                    },
183                }
184            "#,
185            )
186            .unwrap();
187        let lua_version = detect_installed_lua_version().or(Some(LuaVersion::Lua51));
188        let config = ConfigBuilder::new()
189            .unwrap()
190            .vendor_dir(Some(vendor_dir.to_path_buf()))
191            .lua_version(lua_version)
192            .user_tree(Some(temp_dir.to_path_buf()))
193            .build()
194            .unwrap();
195        install_rockspec(
196            InstallRockspec {
197                rockspec_path: rockspec.to_path_buf(),
198                pin: false,
199            },
200            config,
201        )
202        .await
203        .unwrap();
204    }
205}