1use std::path::PathBuf;
2
3use clap::Args;
4use lux_lib::{
5 config::{Config, ConfigBuilder},
6 lua_version::LuaVersion,
7 operations::DistProjectBin,
8 package::PackageName,
9 tree::FlatDistTree,
10 workspace::Workspace,
11};
12use miette::{miette, IntoDiagnostic, Result};
13use tempfile::tempdir;
14
15use crate::args::OutputFormat;
16
17#[derive(Args)]
18pub struct Bin {
19 #[arg(short, long, visible_short_alias = 'o')]
22 pub output: Option<PathBuf>,
23
24 #[arg(short, long, visible_short_alias = 'p')]
29 pub(crate) package: Option<PackageName>,
30
31 #[arg(long, default_value = "text", value_enum, ignore_case = true)]
32 output_format: OutputFormat,
33}
34
35pub async fn bin(data: Bin, config: Config) -> Result<()> {
36 let staging_dir = tempdir().into_diagnostic()?;
37 let config = ConfigBuilder::from(config)
38 .wrap_bin_scripts(Some(false))
39 .user_tree(Some(staging_dir.path().to_path_buf()))
40 .build()?;
41
42 let workspace = Workspace::current_or_err()?;
43 let project = match &data.package {
44 None => workspace.single_member()?,
45 Some(package) => workspace.select_member(package)?,
46 };
47
48 let lua_version = project.lua_version(&config)?;
49 if lua_version == LuaVersion::Luau {
50 return Err(miette!("Lux cannot distribute binaries for Luau projects"));
51 }
52 let tree = FlatDistTree::new(staging_dir.path().to_path_buf(), lua_version, &config)?;
53
54 let out = DistProjectBin::new()
55 .project(project)
56 .config(&config)
57 .tree(&tree)
58 .maybe_output(data.output)
59 .compile()
60 .await
61 .into_diagnostic()?;
62
63 match data.output_format {
64 OutputFormat::Json => println!("{}", serde_json::to_string(&out).into_diagnostic()?),
65 OutputFormat::Text => println!("Binary written to {}", out.display()),
66 }
67
68 Ok(())
69}