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