Skip to main content

lux_cli/
build.rs

1use clap::Args;
2use eyre::Result;
3use lux_lib::{
4    config::Config,
5    lockfile::LocalPackage,
6    operations::{self},
7    project::Project,
8};
9
10#[derive(Args, Default)]
11pub struct Build {
12    /// Ignore the project's lockfile and don't create one.
13    #[arg(long)]
14    no_lock: bool,
15
16    /// Build only the dependencies
17    #[arg(long)]
18    only_deps: bool,
19}
20
21/// Returns `Some` if the `only_deps` arg is set to `false`.
22pub async fn build(data: Build, config: Config) -> Result<Option<LocalPackage>> {
23    let project = Project::current_or_err()?;
24    let result = operations::BuildProject::new(&project, &config)
25        .no_lock(data.no_lock)
26        .only_deps(data.only_deps)
27        .build()
28        .await?;
29    Ok(result)
30}
31
32#[cfg(test)]
33mod tests {
34
35    use super::*;
36
37    use assert_fs::{
38        prelude::{FileWriteStr, PathChild, PathCreateDir},
39        TempDir,
40    };
41
42    use lux_lib::{
43        config::{ConfigBuilder, LuaVersion},
44        lua_installation::detect_installed_lua_version,
45    };
46    use serial_test::serial;
47
48    #[serial]
49    #[tokio::test]
50    async fn test_build_project_from_vendored() {
51        let cwd = &std::env::current_dir().unwrap();
52        let project_dir = TempDir::new().unwrap();
53        std::env::set_current_dir(&project_dir).unwrap();
54        // This test runs without a network connection when run with Nix
55        let vendor_dir = TempDir::new().unwrap();
56        let foo_dir = vendor_dir.child("foo@1.0.0-1");
57        foo_dir.create_dir_all().unwrap();
58        let foo_rockspec = vendor_dir.child("foo-1.0.0-1.rockspec");
59        foo_rockspec
60            .write_str(
61                r#"
62                package = 'foo'
63                version = '1.0.0-1'
64                source = {
65                    url = 'https://github.com/lumen-oss/luarocks-stub',
66                }
67            "#,
68            )
69            .unwrap();
70        let bar_dir = vendor_dir.child("bar@2.0.0-2");
71        bar_dir.create_dir_all().unwrap();
72        let bar_rockspec = vendor_dir.child("bar-2.0.0-2.rockspec");
73        bar_rockspec
74            .write_str(
75                r#"
76                package = 'bar'
77                version = '2.0.0-2'
78                source = {
79                    url = 'https://github.com/lumen-oss/luarocks-stub',
80                }
81            "#,
82            )
83            .unwrap();
84        let baz_dir = vendor_dir.child("baz@2.0.0-1");
85        baz_dir.create_dir_all().unwrap();
86        let baz_rockspec = vendor_dir.child("baz-2.0.0-1.rockspec");
87        baz_rockspec
88            .write_str(
89                r#"
90                package = 'baz'
91                version = '2.0.0-1'
92                source = {
93                    url = 'https://github.com/lumen-oss/luarocks-stub',
94                }
95            "#,
96            )
97            .unwrap();
98        let toml_content = r#"
99        package = "test_rock"
100        version = "scm-1"
101
102        lua = ">= 5.1"
103
104        [dependencies]
105        foo = ">= 1.0.0"
106        bar = ">= 1.0.0"
107        baz = "== 2.0.0"
108        "#;
109        let toml = project_dir.child("lux.toml");
110        toml.write_str(toml_content).unwrap();
111        let lua_version = detect_installed_lua_version().or(Some(LuaVersion::Lua51));
112        let config = ConfigBuilder::new()
113            .unwrap()
114            .vendor_dir(Some(vendor_dir.to_path_buf()))
115            .lua_version(lua_version)
116            .build()
117            .unwrap();
118        build(Build::default(), config).await.unwrap();
119        std::env::set_current_dir(cwd).unwrap();
120    }
121}