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 std::path::PathBuf;
38
39    use assert_fs::{
40        prelude::{FileWriteStr, PathChild, PathCreateDir},
41        TempDir,
42    };
43
44    use lux_lib::{
45        config::{ConfigBuilder, LuaVersion},
46        lua_installation::detect_installed_lua_version,
47    };
48    use serial_test::serial;
49
50    #[serial]
51    #[tokio::test]
52    async fn test_build_project_from_vendored() {
53        let cwd = &std::env::current_dir().unwrap();
54        let project_dir = TempDir::new().unwrap();
55        std::env::set_current_dir(&project_dir).unwrap();
56        // This test runs without a network connection when run with Nix
57        let vendor_dir = TempDir::new().unwrap();
58        let foo_dir = vendor_dir.child("foo@1.0.0-1");
59        foo_dir.create_dir_all().unwrap();
60        let foo_rockspec = vendor_dir.child("foo-1.0.0-1.rockspec");
61        foo_rockspec
62            .write_str(
63                r#"
64                package = 'foo'
65                version = '1.0.0-1'
66                source = {
67                    url = 'https://github.com/lumen-oss/luarocks-stub',
68                }
69            "#,
70            )
71            .unwrap();
72        let bar_dir = vendor_dir.child("bar@2.0.0-2");
73        bar_dir.create_dir_all().unwrap();
74        let bar_rockspec = vendor_dir.child("bar-2.0.0-2.rockspec");
75        bar_rockspec
76            .write_str(
77                r#"
78                package = 'bar'
79                version = '2.0.0-2'
80                source = {
81                    url = 'https://github.com/lumen-oss/luarocks-stub',
82                }
83            "#,
84            )
85            .unwrap();
86        let baz_dir = vendor_dir.child("baz@2.0.0-1");
87        baz_dir.create_dir_all().unwrap();
88        let baz_rockspec = vendor_dir.child("baz-2.0.0-1.rockspec");
89        baz_rockspec
90            .write_str(
91                r#"
92                package = 'baz'
93                version = '2.0.0-1'
94                source = {
95                    url = 'https://github.com/lumen-oss/luarocks-stub',
96                }
97            "#,
98            )
99            .unwrap();
100        let toml_content = r#"
101        package = "test_rock"
102        version = "scm-1"
103
104        lua = ">= 5.1"
105
106        [dependencies]
107        foo = ">= 1.0.0"
108        bar = ">= 1.0.0"
109        baz = "== 2.0.0"
110        "#;
111        let toml = project_dir.child("lux.toml");
112        toml.write_str(toml_content).unwrap();
113        let lua_version = detect_installed_lua_version().or(Some(LuaVersion::Lua51));
114        let data_dir: PathBuf = assert_fs::TempDir::new().unwrap().path().into();
115        let config = ConfigBuilder::new()
116            .unwrap()
117            .vendor_dir(Some(vendor_dir.to_path_buf()))
118            .lua_version(lua_version)
119            .data_dir(Some(data_dir))
120            .build()
121            .unwrap();
122        build(Build::default(), config).await.unwrap();
123        std::env::set_current_dir(cwd).unwrap();
124    }
125}