1use itertools::{Either, Itertools};
2use lux_lib::package;
3use lux_lib::{
4 config::Config,
5 package::db::PackageDB,
6 rockspec::{dependency, lua_dependency},
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 dependency::Kind::Regular(dependencies.iter().collect()),
61 &db,
62 )
63 .await?;
64 project
65 .add_git(
66 lua_dependency::Kind::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(dependency::Kind::Build(dependencies.iter().collect()), &db)
81 .await?;
82 project
83 .add_git(
84 lua_dependency::Kind::Build(git_dependencies.iter().collect()),
85 &config,
86 )
87 .await?;
88 }
89
90 let test_packages = data.test.unwrap_or_default();
91 if !test_packages.is_empty() {
92 let (dependencies, git_dependencies): (Vec<_>, Vec<_>) =
93 test_packages.iter().partition_map(|req| match req {
94 PackageReqOrGitShorthand::PackageReq(req) => Either::Left(req.clone()),
95 PackageReqOrGitShorthand::GitShorthand(url) => Either::Right(url.clone()),
96 });
97 project
98 .add(dependency::Kind::Test(dependencies.iter().collect()), &db)
99 .await?;
100 project
101 .add_git(
102 lua_dependency::Kind::Test(git_dependencies.iter().collect()),
103 &config,
104 )
105 .await?;
106 }
107
108 sync_dependencies_if_locked(&workspace, &config).await?;
109
110 Ok(())
111}
112
113#[cfg(all(test, feature = "impure_tests"))]
114mod tests {
115 use assert_fs::{prelude::PathCopy, TempDir};
116 use lux_lib::config;
117 use serial_test::serial;
118
119 use super::*;
120 use flaky_test::flaky_test;
121 use std::path::PathBuf;
122
123 #[serial]
124 #[flaky_test(tokio, times = 5)]
125 async fn test_add_regular_dependencies() {
126 let sample_project =
127 PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("resources/test/sample-projects/init/");
128 let project_root = TempDir::new().unwrap();
129 project_root.copy_from(&sample_project, &["**"]).unwrap();
130 let cwd = std::env::current_dir().unwrap();
131 std::env::set_current_dir(&project_root).unwrap();
132 let config = config::Builder::new().unwrap().build().unwrap();
133 let args = Add {
134 package: None,
135 package_req: vec!["penlight@1.5".parse().unwrap()],
136 force: false,
137 build: Option::None,
138 test: Option::None,
139 };
140 add(args, config.clone()).await.unwrap();
141 let lockfile_path = project_root.join("lux.lock");
142 let lockfile_content =
143 String::from_utf8(tokio::fs::read(&lockfile_path).await.unwrap()).unwrap();
144 assert!(lockfile_content.contains("penlight"));
145 assert!(lockfile_content.contains("luafilesystem")); let args = Add {
148 package: None,
149 package_req: vec!["md5".parse().unwrap()],
150 force: false,
151 build: Option::None,
152 test: Option::None,
153 };
154 add(args, config.clone()).await.unwrap();
155 let lockfile_path = project_root.join("lux.lock");
156 let lockfile_content =
157 String::from_utf8(tokio::fs::read(&lockfile_path).await.unwrap()).unwrap();
158 assert!(lockfile_content.contains("penlight"));
159 assert!(lockfile_content.contains("luafilesystem"));
160 assert!(lockfile_content.contains("md5"));
161
162 std::env::set_current_dir(&cwd).unwrap();
163 }
164
165 #[serial]
166 #[flaky_test(tokio, times = 5)]
167 async fn test_add_build_dependencies() {
168 let sample_project =
169 PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("resources/test/sample-projects/init/");
170 let project_root = TempDir::new().unwrap();
171 project_root.copy_from(&sample_project, &["**"]).unwrap();
172 let cwd = std::env::current_dir().unwrap();
173 std::env::set_current_dir(&project_root).unwrap();
174 let config = config::Builder::new().unwrap().build().unwrap();
175 let args = Add {
176 package: None,
177 package_req: Vec::new(),
178 force: false,
179 build: Option::Some(vec!["penlight@1.5".parse().unwrap()]),
180 test: Option::None,
181 };
182 add(args, config.clone()).await.unwrap();
183 let lockfile_path = project_root.join("lux.lock");
184 let lockfile_content =
185 String::from_utf8(tokio::fs::read(&lockfile_path).await.unwrap()).unwrap();
186 assert!(lockfile_content.contains("penlight"));
187 assert!(lockfile_content.contains("luafilesystem")); let args = Add {
190 package: None,
191 package_req: Vec::new(),
192 force: false,
193 build: Option::Some(vec!["md5".parse().unwrap()]),
194 test: Option::None,
195 };
196 add(args, config.clone()).await.unwrap();
197 let lockfile_path = project_root.join("lux.lock");
198 let lockfile_content =
199 String::from_utf8(tokio::fs::read(&lockfile_path).await.unwrap()).unwrap();
200 assert!(lockfile_content.contains("penlight"));
201 assert!(lockfile_content.contains("luafilesystem"));
202 assert!(lockfile_content.contains("md5"));
203
204 std::env::set_current_dir(&cwd).unwrap();
205 }
206
207 #[serial]
208 #[flaky_test(tokio, times = 5)]
209 async fn test_add_test_dependencies() {
210 let sample_project =
211 PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("resources/test/sample-projects/init/");
212 let project_root = TempDir::new().unwrap();
213 project_root.copy_from(&sample_project, &["**"]).unwrap();
214 let cwd = std::env::current_dir().unwrap();
215 std::env::set_current_dir(&project_root).unwrap();
216 let config = config::Builder::new().unwrap().build().unwrap();
217 let args = Add {
218 package: None,
219 package_req: Vec::new(),
220 force: false,
221 build: Option::None,
222 test: Option::Some(vec!["penlight@1.5".parse().unwrap()]),
223 };
224 add(args, config.clone()).await.unwrap();
225 let lockfile_path = project_root.join("lux.lock");
226 let lockfile_content =
227 String::from_utf8(tokio::fs::read(&lockfile_path).await.unwrap()).unwrap();
228 assert!(lockfile_content.contains("penlight"));
229 assert!(lockfile_content.contains("luafilesystem")); let args = Add {
232 package: None,
233 package_req: Vec::new(),
234 force: false,
235 build: Option::None,
236 test: Option::Some(vec!["md5".parse().unwrap()]),
237 };
238 add(args, config.clone()).await.unwrap();
239 let lockfile_path = project_root.join("lux.lock");
240 let lockfile_content =
241 String::from_utf8(tokio::fs::read(&lockfile_path).await.unwrap()).unwrap();
242 assert!(lockfile_content.contains("penlight"));
243 assert!(lockfile_content.contains("luafilesystem"));
244 assert!(lockfile_content.contains("md5"));
245
246 std::env::set_current_dir(&cwd).unwrap();
247 }
248}