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::{config, lua};
65    use serial_test::serial;
66
67    #[serial]
68    #[tokio::test]
69    async fn test_build_project_from_vendored() {
70        let cwd = &std::env::current_dir().unwrap();
71        let project_dir = TempDir::new().unwrap();
72        std::env::set_current_dir(&project_dir).unwrap();
73        // This test runs without a network connection when run with Nix
74        let vendor_dir = TempDir::new().unwrap();
75        let foo_dir = vendor_dir.child("foo@1.0.0-1");
76        foo_dir.create_dir_all().unwrap();
77        let foo_rockspec = vendor_dir.child("foo-1.0.0-1.rockspec");
78        foo_rockspec
79            .write_str(
80                r#"
81                package = 'foo'
82                version = '1.0.0-1'
83                source = {
84                    url = 'https://github.com/lumen-oss/luarocks-stub',
85                }
86            "#,
87            )
88            .unwrap();
89        let bar_dir = vendor_dir.child("bar@2.0.0-2");
90        bar_dir.create_dir_all().unwrap();
91        let bar_rockspec = vendor_dir.child("bar-2.0.0-2.rockspec");
92        bar_rockspec
93            .write_str(
94                r#"
95                package = 'bar'
96                version = '2.0.0-2'
97                source = {
98                    url = 'https://github.com/lumen-oss/luarocks-stub',
99                }
100            "#,
101            )
102            .unwrap();
103        let baz_dir = vendor_dir.child("baz@2.0.0-1");
104        baz_dir.create_dir_all().unwrap();
105        let baz_rockspec = vendor_dir.child("baz-2.0.0-1.rockspec");
106        baz_rockspec
107            .write_str(
108                r#"
109                package = 'baz'
110                version = '2.0.0-1'
111                source = {
112                    url = 'https://github.com/lumen-oss/luarocks-stub',
113                }
114            "#,
115            )
116            .unwrap();
117        let toml_content = r#"
118        package = "test_rock"
119        version = "scm-1"
120
121        lua = ">= 5.1"
122
123        [dependencies]
124        foo = ">= 1.0.0"
125        bar = ">= 1.0.0"
126        baz = "== 2.0.0"
127        "#;
128        let toml = project_dir.child("lux.toml");
129        toml.write_str(toml_content).unwrap();
130        let lua_version = lua::detect::installed_version().or(Some(lua::Version::Lua51));
131        let data_dir: PathBuf = assert_fs::TempDir::new().unwrap().path().into();
132        let config = config::Builder::new()
133            .unwrap()
134            .vendor_dir(Some(vendor_dir.to_path_buf()))
135            .lua_version(lua_version)
136            .data_dir(Some(data_dir))
137            .build()
138            .unwrap();
139        build(Build::default(), config).await.unwrap();
140        std::env::set_current_dir(cwd).unwrap();
141    }
142}