1use itertools::{Either, Itertools};
2use lux_lib::package;
3use lux_lib::{
4 config::Config,
5 package_db::PackageDB,
6 rockspec::lua_dependency::{self},
7 workspace::Workspace,
8};
9
10use miette::Result;
11
12use crate::workspace::{sync_dependencies_if_locked, PackageReqOrGitShorthand};
13
14#[derive(clap::Args)]
15pub struct Add {
16 package_req: Vec<PackageReqOrGitShorthand>,
27
28 #[arg(long, visible_short_alias = 'f')]
30 force: bool,
31
32 #[arg(short, long, alias = "dev", visible_short_aliases = ['d', 'b'])]
35 build: Option<Vec<PackageReqOrGitShorthand>>,
36
37 #[arg(short, long, visible_short_alias = 't')]
39 test: Option<Vec<PackageReqOrGitShorthand>>,
40
41 #[arg(short, long, visible_short_alias = 'p')]
43 pub(crate) package: Option<package::Name>,
44}
45
46pub async fn add(data: Add, config: Config) -> Result<()> {
47 let mut workspace = Workspace::current_or_err()?;
48 let project = workspace.single_member_or_select_mut(&data.package)?;
49 let db = PackageDB::from_config(&config).await?;
50
51 let (dependencies, git_dependencies): (Vec<_>, Vec<_>) =
52 data.package_req.iter().partition_map(|req| match req {
53 PackageReqOrGitShorthand::PackageReq(req) => Either::Left(req.clone()),
54 PackageReqOrGitShorthand::GitShorthand(url) => Either::Right(url.clone()),
55 });
56
57 if !data.package_req.is_empty() {
58 project
59 .add(
60 lua_dependency::DependencyType::Regular(dependencies.iter().collect()),
61 &db,
62 )
63 .await?;
64 project
65 .add_git(
66 lua_dependency::LuaDependencyType::Regular(git_dependencies.iter().collect()),
67 &config,
68 )
69 .await?;
70 }
71
72 let build_packages = data.build.unwrap_or_default();
73 if !build_packages.is_empty() {
74 let (dependencies, git_dependencies): (Vec<_>, Vec<_>) =
75 build_packages.iter().partition_map(|req| match req {
76 PackageReqOrGitShorthand::PackageReq(req) => Either::Left(req.clone()),
77 PackageReqOrGitShorthand::GitShorthand(url) => Either::Right(url.clone()),
78 });
79 project
80 .add(
81 lua_dependency::DependencyType::Build(dependencies.iter().collect()),
82 &db,
83 )
84 .await?;
85 project
86 .add_git(
87 lua_dependency::LuaDependencyType::Build(git_dependencies.iter().collect()),
88 &config,
89 )
90 .await?;
91 }
92
93 let test_packages = data.test.unwrap_or_default();
94 if !test_packages.is_empty() {
95 let (dependencies, git_dependencies): (Vec<_>, Vec<_>) =
96 test_packages.iter().partition_map(|req| match req {
97 PackageReqOrGitShorthand::PackageReq(req) => Either::Left(req.clone()),
98 PackageReqOrGitShorthand::GitShorthand(url) => Either::Right(url.clone()),
99 });
100 project
101 .add(
102 lua_dependency::DependencyType::Test(dependencies.iter().collect()),
103 &db,
104 )
105 .await?;
106 project
107 .add_git(
108 lua_dependency::LuaDependencyType::Test(git_dependencies.iter().collect()),
109 &config,
110 )
111 .await?;
112 }
113
114 sync_dependencies_if_locked(&workspace, &config).await?;
115
116 Ok(())
117}
118
119#[cfg(all(test, feature = "impure_tests"))]
120mod tests {
121 use assert_fs::{prelude::PathCopy, TempDir};
122 use lux_lib::config::ConfigBuilder;
123 use serial_test::serial;
124
125 use super::*;
126 use flaky_test::flaky_test;
127 use std::path::PathBuf;
128
129 #[serial]
130 #[flaky_test(tokio, times = 5)]
131 async fn test_add_regular_dependencies() {
132 let sample_project =
133 PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("resources/test/sample-projects/init/");
134 let project_root = TempDir::new().unwrap();
135 project_root.copy_from(&sample_project, &["**"]).unwrap();
136 let cwd = std::env::current_dir().unwrap();
137 std::env::set_current_dir(&project_root).unwrap();
138 let config = ConfigBuilder::new().unwrap().build().unwrap();
139 let args = Add {
140 package: None,
141 package_req: vec!["penlight@1.5".parse().unwrap()],
142 force: false,
143 build: Option::None,
144 test: Option::None,
145 };
146 add(args, config.clone()).await.unwrap();
147 let lockfile_path = project_root.join("lux.lock");
148 let lockfile_content =
149 String::from_utf8(tokio::fs::read(&lockfile_path).await.unwrap()).unwrap();
150 assert!(lockfile_content.contains("penlight"));
151 assert!(lockfile_content.contains("luafilesystem")); let args = Add {
154 package: None,
155 package_req: vec!["md5".parse().unwrap()],
156 force: false,
157 build: Option::None,
158 test: Option::None,
159 };
160 add(args, config.clone()).await.unwrap();
161 let lockfile_path = project_root.join("lux.lock");
162 let lockfile_content =
163 String::from_utf8(tokio::fs::read(&lockfile_path).await.unwrap()).unwrap();
164 assert!(lockfile_content.contains("penlight"));
165 assert!(lockfile_content.contains("luafilesystem"));
166 assert!(lockfile_content.contains("md5"));
167
168 std::env::set_current_dir(&cwd).unwrap();
169 }
170
171 #[serial]
172 #[flaky_test(tokio, times = 5)]
173 async fn test_add_build_dependencies() {
174 let sample_project =
175 PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("resources/test/sample-projects/init/");
176 let project_root = TempDir::new().unwrap();
177 project_root.copy_from(&sample_project, &["**"]).unwrap();
178 let cwd = std::env::current_dir().unwrap();
179 std::env::set_current_dir(&project_root).unwrap();
180 let config = ConfigBuilder::new().unwrap().build().unwrap();
181 let args = Add {
182 package: None,
183 package_req: Vec::new(),
184 force: false,
185 build: Option::Some(vec!["penlight@1.5".parse().unwrap()]),
186 test: Option::None,
187 };
188 add(args, config.clone()).await.unwrap();
189 let lockfile_path = project_root.join("lux.lock");
190 let lockfile_content =
191 String::from_utf8(tokio::fs::read(&lockfile_path).await.unwrap()).unwrap();
192 assert!(lockfile_content.contains("penlight"));
193 assert!(lockfile_content.contains("luafilesystem")); let args = Add {
196 package: None,
197 package_req: Vec::new(),
198 force: false,
199 build: Option::Some(vec!["md5".parse().unwrap()]),
200 test: Option::None,
201 };
202 add(args, config.clone()).await.unwrap();
203 let lockfile_path = project_root.join("lux.lock");
204 let lockfile_content =
205 String::from_utf8(tokio::fs::read(&lockfile_path).await.unwrap()).unwrap();
206 assert!(lockfile_content.contains("penlight"));
207 assert!(lockfile_content.contains("luafilesystem"));
208 assert!(lockfile_content.contains("md5"));
209
210 std::env::set_current_dir(&cwd).unwrap();
211 }
212
213 #[serial]
214 #[flaky_test(tokio, times = 5)]
215 async fn test_add_test_dependencies() {
216 let sample_project =
217 PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("resources/test/sample-projects/init/");
218 let project_root = TempDir::new().unwrap();
219 project_root.copy_from(&sample_project, &["**"]).unwrap();
220 let cwd = std::env::current_dir().unwrap();
221 std::env::set_current_dir(&project_root).unwrap();
222 let config = ConfigBuilder::new().unwrap().build().unwrap();
223 let args = Add {
224 package: None,
225 package_req: Vec::new(),
226 force: false,
227 build: Option::None,
228 test: Option::Some(vec!["penlight@1.5".parse().unwrap()]),
229 };
230 add(args, config.clone()).await.unwrap();
231 let lockfile_path = project_root.join("lux.lock");
232 let lockfile_content =
233 String::from_utf8(tokio::fs::read(&lockfile_path).await.unwrap()).unwrap();
234 assert!(lockfile_content.contains("penlight"));
235 assert!(lockfile_content.contains("luafilesystem")); let args = Add {
238 package: None,
239 package_req: Vec::new(),
240 force: false,
241 build: Option::None,
242 test: Option::Some(vec!["md5".parse().unwrap()]),
243 };
244 add(args, config.clone()).await.unwrap();
245 let lockfile_path = project_root.join("lux.lock");
246 let lockfile_content =
247 String::from_utf8(tokio::fs::read(&lockfile_path).await.unwrap()).unwrap();
248 assert!(lockfile_content.contains("penlight"));
249 assert!(lockfile_content.contains("luafilesystem"));
250 assert!(lockfile_content.contains("md5"));
251
252 std::env::set_current_dir(&cwd).unwrap();
253 }
254}