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