Skip to main content

lux_cli/dist/
bin.rs

1use std::path::PathBuf;
2
3use clap::Args;
4use eyre::Result;
5use lux_lib::{
6    config::{Config, ConfigBuilder},
7    operations::DistProjectBin,
8    package::PackageName,
9    tree::FlatDistTree,
10    workspace::Workspace,
11};
12use tempfile::tempdir;
13
14#[derive(Args)]
15pub struct Bin {
16    /// Output path for the compiled binary.{n}
17    /// Defaults to `<package>[.exe]` in the current directory.
18    #[arg(short, long, visible_short_alias = 'o')]
19    pub output: Option<PathBuf>,
20
21    /// Package to compile.{n}
22    /// Prioritises local projects if in a workspace.{n}
23    /// If not set, lux will attempt to compile the current project.{n}
24    /// Must be set in multi-project workspaces.
25    #[arg(short, long, visible_short_alias = 'p')]
26    package: Option<PackageName>,
27
28    /// Output a JSON path.
29    #[arg(long)]
30    pub porcelain: bool,
31}
32
33pub async fn bin(data: Bin, config: Config) -> Result<()> {
34    let staging_dir = tempdir()?;
35    let config = ConfigBuilder::from(config)
36        .wrap_bin_scripts(Some(false))
37        .user_tree(Some(staging_dir.path().to_path_buf()))
38        .build()?;
39
40    let workspace = Workspace::current_or_err()?;
41    let project = match &data.package {
42        None => workspace.single_member()?,
43        Some(package) => workspace.select_member(package)?,
44    };
45
46    let lua_version = project.lua_version(&config)?;
47    let tree = FlatDistTree::new(staging_dir.path().to_path_buf(), lua_version, &config)?;
48
49    let out = DistProjectBin::new()
50        .project(project)
51        .config(&config)
52        .tree(&tree)
53        .maybe_output(data.output)
54        .compile()
55        .await?;
56
57    if data.porcelain {
58        println!("{}", serde_json::to_string(&out)?);
59    } else {
60        println!("Binary written to {}", out.display());
61    }
62
63    Ok(())
64}