Skip to main content

lux_lib/operations/
build_workspace.rs

1use crate::package;
2use crate::{
3    build::BuildBehaviour,
4    config::Config,
5    drivers::sync::{self, Sync, SyncMode, TargetSet},
6    lockfile::LockedPackage,
7    workspace::Workspace,
8};
9use bon::Builder;
10use thiserror::Error;
11use tracing::{info_span, Instrument};
12
13#[derive(Debug, Error, miette::Diagnostic)]
14#[non_exhaustive]
15pub enum Error {
16    #[error(transparent)]
17    #[diagnostic(transparent)]
18    Sync(Box<sync::Error>),
19}
20
21impl From<sync::Error> for Error {
22    fn from(source: sync::Error) -> Self {
23        Self::Sync(Box::new(source))
24    }
25}
26
27#[derive(Builder)]
28#[builder(start_fn = new, finish_fn(name = _build, vis = ""))]
29pub struct BuildWorkspace<'a> {
30    #[builder(start_fn)]
31    workspace: &'a Workspace,
32
33    #[builder(start_fn)]
34    config: &'a Config,
35
36    /// Package to build
37    package: Option<package::Name>,
38
39    /// Ignore the project's lockfile and don't create one
40    no_lock: bool,
41
42    /// Build only the dependencies
43    only_deps: bool,
44
45    #[builder(default = BuildBehaviour::Force)]
46    behaviour: BuildBehaviour,
47}
48
49impl<State: build_workspace_builder::State + build_workspace_builder::IsComplete>
50    BuildWorkspaceBuilder<'_, State>
51{
52    pub async fn build(self) -> Result<Vec<LockedPackage>, Error> {
53        let build = self._build();
54        let span = match &build.package {
55            Some(package) => info_span!("Building workspace", package = package.to_string()),
56            None => info_span!("Building workspace"),
57        };
58        async move {
59            let report = Sync::new(build.workspace, build.config)
60                .mode(SyncMode::Open)
61                .targets(match build.package {
62                    Some(package) => TargetSet::member(package, false),
63                    None => TargetSet::all(build.workspace, false),
64                })
65                .behaviour(build.behaviour)
66                .only_deps(build.only_deps)
67                .no_lock(build.no_lock)
68                .sync()
69                .await?;
70            Ok(report.added().to_vec())
71        }
72        .instrument(span)
73        .await
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    use crate::{config, lua};
82    #[cfg(feature = "impure_tests")]
83    use crate::{fs, tree::InstallTree};
84    use assert_fs::prelude::PathCopy;
85    use std::path::PathBuf;
86
87    #[cfg(feature = "impure_tests")]
88    #[tokio::test]
89    /// Non-regression for #980
90    async fn builtin_build_autodetect_bin_scripts() {
91        let project_root =
92            PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("resources/test/sample-projects/init/");
93        let data_dir: PathBuf = assert_fs::TempDir::new().unwrap().path().into();
94        let temp_dir = assert_fs::TempDir::new().unwrap();
95        temp_dir.copy_from(&project_root, &["**"]).unwrap();
96        let project_root = temp_dir.path();
97        let foo_bin_dir = project_root.join("src").join("bin");
98        fs::tokio::create_dir_all(&foo_bin_dir).await.unwrap();
99        let foo_bin_file = foo_bin_dir.join("foo");
100        fs::tokio::write(&foo_bin_file, "print('hello')")
101            .await
102            .unwrap();
103        let bar_bin_dir = project_root.join("bin");
104        fs::tokio::create_dir_all(&bar_bin_dir).await.unwrap();
105        let bar_bin_file = bar_bin_dir.join("bar");
106        fs::tokio::write(&bar_bin_file, "print('hello')")
107            .await
108            .unwrap();
109        let lua_version = lua::detect::installed_version().or(Some(lua::Version::Lua51));
110        let config = config::Builder::new()
111            .unwrap()
112            .data_dir(Some(data_dir))
113            .lua_version(lua_version)
114            .build()
115            .unwrap();
116        let workspace = Workspace::from_exact(project_root).unwrap().unwrap();
117        let tree = workspace.tree(&config).unwrap();
118        BuildWorkspace::new(&workspace, &config)
119            .no_lock(false)
120            .only_deps(false)
121            .build()
122            .await
123            .unwrap();
124        let bin_dir = tree.bin();
125        assert!(bin_dir.join("foo").is_file());
126        assert!(bin_dir.join("bar").is_file());
127    }
128
129    #[cfg(feature = "impure_tests")]
130    #[tokio::test]
131    /// Non-regression for #1563
132    async fn builtin_build_support_src_init_lua() {
133        let data_dir: PathBuf = assert_fs::TempDir::new().unwrap().path().into();
134        let project_root =
135            PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("resources/test/sample-projects/init/");
136        let temp_dir = assert_fs::TempDir::new().unwrap();
137        temp_dir.copy_from(&project_root, &["**"]).unwrap();
138        let project_root = temp_dir.path();
139        let src_dir = project_root.join("src");
140        fs::tokio::create_dir_all(&src_dir).await.unwrap();
141        let init_lua_file = src_dir.join("init.lua");
142        fs::tokio::write(&init_lua_file, "print('hello')")
143            .await
144            .unwrap();
145        let lua_version = lua::detect::installed_version().or(Some(lua::Version::Lua51));
146        let config = config::Builder::new()
147            .unwrap()
148            .data_dir(Some(data_dir))
149            .lua_version(lua_version)
150            .build()
151            .unwrap();
152        let workspace = Workspace::from_exact(project_root).unwrap().unwrap();
153        let package = BuildWorkspace::new(&workspace, &config)
154            .no_lock(false)
155            .only_deps(false)
156            .build()
157            .await
158            .unwrap();
159        let package = package.first().unwrap();
160        let tree = workspace.tree(&config).unwrap();
161        let src_dir = tree.layout_for(&package.spec).src;
162        assert!(src_dir.join("init.lua").is_file());
163    }
164
165    #[tokio::test]
166    /// Non-regression for #2101
167    async fn build_install_post_build_artifact() {
168        let data_dir: PathBuf = assert_fs::TempDir::new().unwrap().path().into();
169        let project_root =
170            PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("resources/test/sample-projects/c-src/");
171        let temp_dir = assert_fs::TempDir::new().unwrap();
172        temp_dir.copy_from(&project_root, &["**"]).unwrap();
173        let project_root = temp_dir.path();
174        let lua_version = lua::detect::installed_version().or(Some(lua::Version::Lua51));
175        let config = config::Builder::new()
176            .unwrap()
177            .data_dir(Some(data_dir))
178            .lua_version(lua_version)
179            .build()
180            .unwrap();
181        let workspace = Workspace::from_exact(project_root).unwrap().unwrap();
182        // TODO: Investigate if build::compile is outputting the .so to a different directory
183        BuildWorkspace::new(&workspace, &config)
184            .no_lock(false)
185            .only_deps(false)
186            .build()
187            .await
188            .unwrap();
189    }
190}