Skip to main content

lux_cli/
lib.rs

1use crate::{completion::Completion, dist::Dist, format::Fmt, project::NewProject};
2use std::error::Error;
3use std::path::PathBuf;
4
5use add::Add;
6use build::Build;
7use check::Check;
8use clap::{Parser, Subcommand};
9use config::ConfigCmd;
10use debug::Debug;
11use doc::Doc;
12use download::Download;
13use exec::Exec;
14use generate_rockspec::GenerateRockspec;
15use info::Info;
16use install::Install;
17use install_rockspec::InstallRockspec;
18use lint::Lint;
19use list::ListCmd;
20use lux_lib::lua_version::LuaVersion;
21use outdated::Outdated;
22use pack::Pack;
23use path::Path;
24use pin::ChangePin;
25use remove::Remove;
26use run::Run;
27use run_lua::RunLua;
28use search::Search;
29use shell::Shell;
30use sync::SyncProject;
31use test::Test;
32use uninstall::Uninstall;
33use update::Update;
34use upload::Upload;
35use url::Url;
36use vendor::Vendor;
37use which::Which;
38
39pub mod add;
40pub mod args;
41pub mod build;
42pub mod check;
43pub mod completion;
44pub mod config;
45pub mod debug;
46pub mod dist;
47pub mod doc;
48pub mod download;
49pub mod exec;
50pub mod fetch;
51pub mod format;
52pub mod generate_rockspec;
53pub mod info;
54pub mod install;
55pub mod install_lua;
56pub mod install_rockspec;
57pub mod lint;
58pub mod list;
59pub mod outdated;
60pub mod pack;
61pub mod path;
62pub mod pin;
63pub mod project;
64pub mod purge;
65pub mod remove;
66pub mod run;
67pub mod run_lua;
68pub mod search;
69pub mod shell;
70pub mod sync;
71pub mod test;
72pub mod uninstall;
73pub mod unpack;
74pub mod update;
75pub mod upload;
76pub mod utils;
77pub mod vendor;
78pub mod which;
79pub mod workspace;
80
81/// A luxurious package manager for Lua.
82#[derive(Parser)]
83#[command(author, version, about, long_about = None, arg_required_else_help = true)]
84pub struct Cli {
85    /// Enable the sub-repositories in luarocks servers for rockspecs of in-development versions.
86    #[arg(long)]
87    pub dev: bool,
88
89    /// Fetch rocks/rockspecs from this server (takes priority over config file).
90    #[arg(long, value_name = "server")]
91    pub server: Option<Url>,
92
93    /// Fetch rocks/rockspecs from these servers in addition to the main server{n}
94    /// (overrides any entries in the config file).
95    #[arg(long, value_name = "extra-server")]
96    pub extra_servers: Option<Vec<Url>>,
97
98    /// Specify the luarocks server namespace to use.
99    #[arg(long, value_name = "namespace")]
100    pub namespace: Option<String>,
101
102    /// Specify the directory in which to install Lua if not found.
103    #[arg(long, value_name = "prefix")]
104    pub lua_dir: Option<PathBuf>,
105
106    /// Which Lua installation to use.{n}
107    /// Valid versions are: '5.1', '5.2', '5.3', '5.4', 'jit' and 'jit52'.
108    #[arg(long, value_name = "ver")]
109    pub lua_version: Option<LuaVersion>,
110
111    /// Which tree to operate on.
112    #[arg(long, value_name = "tree")]
113    pub tree: Option<PathBuf>,
114
115    /// Specifies the cache directory, e.g. for luarocks manifests.
116    #[arg(long, value_name = "cache-dir")]
117    pub cache_dir: Option<PathBuf>,
118
119    /// Specifies the data directory,{n}
120    /// in which the default user install tree resides{n}
121    /// (e.g. ~/.local/share/lux).
122    #[arg(long, value_name = "data-dir")]
123    pub data_dir: Option<PathBuf>,
124
125    /// Specifies a directory with locally vendored sources and RockSpecs.{n}
126    /// When building or installing a package with this flag,{n}
127    /// Lux will fetch sources from the <vendor-dir> instead of from a remote server.
128    #[arg(long, value_name = "vendor-dir")]
129    pub vendor_dir: Option<PathBuf>,
130
131    /// Override config variables.{n}
132    /// Example: `lx -v "LUA=/path/to/lua" ...`
133    #[arg(long, value_name = "variable", visible_short_alias = 'v', value_parser = parse_key_val::<String, String>)]
134    pub variables: Option<Vec<(String, String)>>,
135
136    /// Display verbose output of commands executed.
137    #[arg(long)]
138    pub verbose: bool,
139
140    /// Don't print any progress bars or spinners.
141    #[arg(long)]
142    pub no_progress: bool,
143
144    /// Skip prompts, selecting the default option.
145    #[arg(long)]
146    pub no_prompt: bool,
147
148    /// Configure lux for installing Neovim packages.
149    #[arg(long)]
150    pub nvim: bool,
151
152    /// Timeout on network operations, in seconds.{n}
153    /// 0 means no timeout (wait forever). Default is 30.
154    #[arg(long, value_name = "seconds")]
155    pub timeout: Option<usize>,
156
157    /// Maximum buffer size for parallel jobs, such as downloading rockspecs and installing rocks.
158    /// 0 means no limit. Default is 0.
159    #[arg(long, visible_short_alias = 'j')]
160    pub max_jobs: Option<usize>,
161
162    /// Do not generate or update a `.luarc.json` file when building{n}
163    /// a project.
164    #[arg(long)]
165    pub no_luarc: bool,
166
167    /// Do not wrap Lua `bin` scripts.
168    #[arg(long)]
169    pub no_wrap_bin: bool,
170
171    /// The user agent to set when making web requests.
172    /// Default is "lux/<version>"
173    #[arg(long)]
174    pub user_agent: Option<String>,
175
176    #[command(subcommand)]
177    pub command: Commands,
178}
179
180#[derive(Subcommand)]
181pub enum Commands {
182    /// Add a dependency to the current project.
183    Add(Add),
184    /// Build/compile a project.
185    Build(Build),
186    /// [EXPERIMENTAL]{n}
187    /// Type check the current project based on EmmyLua/LuaCATS annotations.{n}
188    /// Respects `.emmyrc.json` and `.luarc.json` files in the project directory.
189    Check(Check),
190    /// Interact with the lux configuration.
191    #[command(subcommand, arg_required_else_help = true)]
192    Config(ConfigCmd),
193    /// Generate autocompletion scripts for the shell.{n}
194    /// Example: `lx completion zsh > ~/.zsh/completions/_lx`
195    Completion(Completion),
196    /// Internal commands for debugging Lux itself.
197    #[command(subcommand, arg_required_else_help = true)]
198    Debug(Debug),
199    /// Distribute a Lux project.
200    #[command(subcommand, arg_required_else_help = true)]
201    Dist(Dist),
202    /// Show documentation for an installed rock.
203    Doc(Doc),
204    /// Download a specific rock file from a luarocks server.
205    #[command(arg_required_else_help = true)]
206    Download(Download),
207    /// Formats the codebase with stylua.
208    Fmt(Fmt),
209    /// Generate a rockspec file from a project.
210    GenerateRockspec(GenerateRockspec),
211    /// Show metadata for any rock.
212    Info(Info),
213    /// Install a rock for use on the system.
214    #[command(arg_required_else_help = true)]
215    Install(Install),
216    /// Install a local rockspec for use on the system.
217    #[command(arg_required_else_help = true)]
218    InstallRockspec(InstallRockspec),
219    /// Manually install and manage Lua headers for various Lua versions.
220    InstallLua,
221    /// Lint the current project using `luacheck`.
222    Lint(Lint),
223    /// List currently installed rocks.
224    List(ListCmd),
225    /// Run lua, with the `LUA_PATH` and `LUA_CPATH` set to the specified lux tree.
226    Lua(RunLua),
227    /// Create a new Lua project.
228    New(NewProject),
229    /// List outdated rocks.
230    Outdated(Outdated),
231    /// Create a packed rock for distribution, packing sources or binaries.
232    Pack(Pack),
233    /// Return the currently configured package path.
234    Path(Path),
235    /// Pin an existing rock, preventing any updates to the package.
236    Pin(ChangePin),
237    /// Remove all installed rocks from a tree.
238    Purge,
239    /// Remove a rock from the current project's lux.toml dependencies.
240    Remove(Remove),
241    /// Run the current project with the provided arguments.
242    Run(Run),
243    /// Execute a command that has been installed with lux.
244    /// If the command is not found, a package named after the command
245    /// will be installed.
246    Exec(Exec),
247    /// Query the luarocks servers.
248    #[command(arg_required_else_help = true)]
249    Search(Search),
250    /// Run the test suite in the current project directory.{n}
251    /// Lux supports the following test backends, specified by the `[test]` table in the lux.toml:{n}
252    /// {n}
253    ///   - busted:{n}
254    ///     {n}
255    ///     https://lunarmodules.github.io/busted/{n}
256    ///     {n}
257    ///     Example:{n}
258    ///     {n}
259    ///     ```toml{n}
260    ///     [test]{n}
261    ///     type = "busted"{n}
262    ///     flags = [ ] # Optional CLI flags to pass to busted{n}
263    ///     ```{n}
264    ///     {n}
265    ///     `lx test` will default to using `busted` if no test backend is specified and:{n}
266    ///         * there is a `.busted` file in the project root{n}
267    ///         * or `busted` is one of the `test_dependencies`).{n}
268    /// {n}
269    ///   - busted-nlua:{n}:
270    ///     {n}
271    ///     [currently broken on Windows]{n}
272    ///     A build backend for running busted tests with Neovim as the Lua interpreter.
273    ///     Used for testing Neovim plugins.
274    ///     {n}
275    ///     Example:{n}
276    ///     {n}
277    ///     ```toml{n}
278    ///     [test]{n}
279    ///     type = "busted-nlua"{n}
280    ///     flags = [ ] # Optional CLI flags to pass to busted{n}
281    ///     ```{n}
282    ///     {n}
283    ///     `lx test` will default to using `busted-nlua` if no test backend is specified and:{n}
284    ///         * there is a `.busted` file in the project root{n}
285    ///         * or `busted` and `nlua` are `test_dependencies`.{n}
286    /// {n}
287    ///   - command:{n}
288    ///     {n}
289    ///     Name/file name of a shell command that will run the test suite.{n}
290    ///     Example:{n}
291    ///     {n}
292    ///     ```toml{n}
293    ///     [test]{n}
294    ///     type = "command"{n}
295    ///     command = "make"{n}
296    ///     flags = [ "test" ]{n}
297    ///     ```{n}
298    ///     {n}
299    ///   - script:{n}
300    ///     {n}
301    ///     Relative path to a Lua script that will run the test suite.{n}
302    ///     Example:{n}
303    ///     {n}
304    ///     ```toml{n}
305    ///     [test]{n}
306    ///     type = "script"{n}
307    ///     script = "tests.lua" # Expects a tests.lua file in the project root{n}
308    ///     flags = [ ] # Optional arguments passed to the test script{n}
309    ///     ```{n}
310    Test(Test),
311    /// Uninstall a rock from the system.
312    Uninstall(Uninstall),
313    /// Unpins an existing rock, allowing updates to alter the package.
314    Unpin(ChangePin),
315    /// Updates all rocks in a project.
316    Update(Update),
317    /// Generate a Lua rockspec for a Lux project and upload it to the public luarocks repository.{n}
318    /// You can specify a source template for release and dev packages in the lux.toml.{n}
319    /// {n}
320    /// Example:{n}
321    /// {n}
322    /// ```toml{n}
323    /// [source]{n}
324    /// url = "https://host.com/owner/$(PACKAGE)/refs/tags/$(REF).zip"{n}
325    /// dev = "git+https://host.com/owner/$(PACKAGE).git"{n}
326    /// ```{n}
327    /// {n}
328    /// You can use the following variables in the source template:{n}
329    /// {n}
330    ///  - $(PACKAGE): The package name.{n}
331    ///  - $(VERSION): The package version.{n}
332    ///  - $(REF): The git tag or revision (if in a git repository).{n}
333    ///  - You may also specify environment variables with `$(<VAR_NAME>)`.{n}
334    /// {n}
335    /// If the `version` is not set in the lux.toml, lux will search the current
336    /// commit for SemVer tags and if found, will use it to generate the package version.
337    Upload(Upload),
338    /// Vendor the dependencies of a project or RockSpec locally.
339    /// When building or installing a package with the `--vendor-dir` option{n}
340    /// or the `[vendor_dir]` config option, Lux will fetch sources from the <vendor-dir>{n}
341    /// instead of from a remote server.
342    Vendor(Vendor),
343    /// Tell which file corresponds to a given module name.
344    Which(Which),
345    /// Spawns an interactive shell with PATH, LUA_PATH, LUA_CPATH and LUA_INIT set.
346    Shell(Shell),
347    /// Synchronize the project tree with the current lux.toml,{n}
348    /// ensuring all packages are installed correctly.
349    Sync(SyncProject),
350}
351
352/// Parse a key=value pair.
353fn parse_key_val<T, U>(s: &str) -> Result<(T, U), Box<dyn Error + Send + Sync + 'static>>
354where
355    T: std::str::FromStr,
356    T::Err: Error + Send + Sync + 'static,
357    U: std::str::FromStr,
358    U::Err: Error + Send + Sync + 'static,
359{
360    let pos = s
361        .find('=')
362        .ok_or_else(|| format!("invalid KEY=value: no `=` found in `{s}`"))?;
363    Ok((s[..pos].parse()?, s[pos + 1..].parse()?))
364}