Skip to main content

lux_cli/
build.rs

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