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