lux_cli/
install_rockspec.rs1use 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 rockspec_path: PathBuf,
19
20 #[arg(long)]
22 pin: bool,
23}
24
25pub 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 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}