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;
77
78/// A luxurious package manager for Lua.
79#[derive(Parser)]
80#[command(author, version, about, long_about = None, arg_required_else_help = true)]
81pub struct Cli {
82 /// Enable the sub-repositories in luarocks servers for rockspecs of in-development versions.
83 #[arg(long)]
84 pub dev: bool,
85
86 /// Fetch rocks/rockspecs from this server (takes priority over config file).
87 #[arg(long, value_name = "server")]
88 pub server: Option<Url>,
89
90 /// Fetch rocks/rockspecs from this server in addition to the main server{n}
91 /// (overrides any entries in the config file).
92 #[arg(long, value_name = "extra-server")]
93 pub extra_servers: Option<Vec<Url>>,
94
95 /// Restrict downloads to paths matching the given URL.
96 #[arg(long, value_name = "url")]
97 pub only_sources: Option<String>,
98
99 /// Specify the luarocks server namespace to use.
100 #[arg(long, value_name = "namespace")]
101 pub namespace: Option<String>,
102
103 /// Specify the directory in which to install Lua{n}
104 /// if not found.
105 #[arg(long, value_name = "prefix")]
106 pub lua_dir: Option<PathBuf>,
107
108 /// Which Lua installation to use.{n}
109 /// Valid versions are: '5.1', '5.2', '5.3', '5.4', 'jit' and 'jit52'.
110 #[arg(long, value_name = "ver")]
111 pub lua_version: Option<LuaVersion>,
112
113 /// Which tree to operate on.
114 #[arg(long, value_name = "tree")]
115 pub tree: Option<PathBuf>,
116
117 /// Specifies the cache directory for e.g. luarocks manifests.
118 #[arg(long, value_name = "cache-dir")]
119 pub cache_dir: Option<PathBuf>,
120
121 /// Specifies the data directory (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 #[command(subcommand)]
168 pub command: Commands,
169}
170
171#[derive(Subcommand)]
172pub enum Commands {
173 /// Add a dependency to the current project.
174 Add(Add),
175 /// Build/compile a project.
176 Build(Build),
177 /// [EXPERIMENTAL]{n}
178 /// Type check the current project based on EmmyLua/LuaCATS annotations.{n}
179 /// Respects `.emmyrc.json` and `.luarc.json` files in the project directory.
180 Check(Check),
181 /// Interact with the lux configuration.
182 #[command(subcommand, arg_required_else_help = true)]
183 Config(ConfigCmd),
184 /// Generate autocompletion scripts for the shell.{n}
185 /// Example: `lx completion zsh > ~/.zsh/completions/_lx`
186 Completion(Completion),
187 /// Internal commands for debugging Lux itself.
188 #[command(subcommand, arg_required_else_help = true)]
189 Debug(Debug),
190 /// Show documentation for an installed rock.
191 Doc(Doc),
192 /// Download a specific rock file from a luarocks server.
193 #[command(arg_required_else_help = true)]
194 Download(Download),
195 /// Formats the codebase with stylua.
196 Fmt(Fmt),
197 /// Generate a rockspec file from a project.
198 GenerateRockspec(GenerateRockspec),
199 /// Show metadata for any rock.
200 Info(Info),
201 /// Install a rock for use on the system.
202 #[command(arg_required_else_help = true)]
203 Install(Install),
204 /// Install a local rockspec for use on the system.
205 #[command(arg_required_else_help = true)]
206 InstallRockspec(InstallRockspec),
207 /// Manually install and manage Lua headers for various Lua versions.
208 InstallLua,
209 /// Lint the current project using `luacheck`.
210 Lint(Lint),
211 /// List currently installed rocks.
212 List(ListCmd),
213 /// Run lua, with the `LUA_PATH` and `LUA_CPATH` set to the specified lux tree.
214 Lua(RunLua),
215 /// Create a new Lua project.
216 New(NewProject),
217 /// List outdated rocks.
218 Outdated(Outdated),
219 /// Create a packed rock for distribution, packing sources or binaries.
220 Pack(Pack),
221 /// Return the currently configured package path.
222 Path(Path),
223 /// Pin an existing rock, preventing any updates to the package.
224 Pin(ChangePin),
225 /// Remove all installed rocks from a tree.
226 Purge,
227 /// Remove a rock from the current project's lux.toml dependencies.
228 Remove(Remove),
229 /// Run the current project with the provided arguments.
230 Run(Run),
231 /// Execute a command that has been installed with lux.
232 /// If the command is not found, a package named after the command
233 /// will be installed.
234 Exec(Exec),
235 /// Query the luarocks servers.
236 #[command(arg_required_else_help = true)]
237 Search(Search),
238 /// Run the test suite in the current project directory.{n}
239 /// Lux supports the following test backends, specified by the `[test]` table in the lux.toml:{n}
240 /// {n}
241 /// - busted:{n}
242 /// {n}
243 /// https://lunarmodules.github.io/busted/{n}
244 /// {n}
245 /// Example:{n}
246 /// {n}
247 /// ```toml{n}
248 /// [test]{n}
249 /// type = "busted"{n}
250 /// flags = [ ] # Optional CLI flags to pass to busted{n}
251 /// ```{n}
252 /// {n}
253 /// `lx test` will default to using `busted` if no test backend is specified and:{n}
254 /// * there is a `.busted` file in the project root{n}
255 /// * or `busted` is one of the `test_dependencies`).{n}
256 /// {n}
257 /// - busted-nlua:{n}:
258 /// {n}
259 /// [currently broken on Windows]{n}
260 /// A build backend for running busted tests with Neovim as the Lua interpreter.
261 /// Used for testing Neovim plugins.
262 /// {n}
263 /// Example:{n}
264 /// {n}
265 /// ```toml{n}
266 /// [test]{n}
267 /// type = "busted-nlua"{n}
268 /// flags = [ ] # Optional CLI flags to pass to busted{n}
269 /// ```{n}
270 /// {n}
271 /// `lx test` will default to using `busted-nlua` if no test backend is specified and:{n}
272 /// * there is a `.busted` file in the project root{n}
273 /// * or `busted` and `nlua` are `test_dependencies`.{n}
274 /// {n}
275 /// - command:{n}
276 /// {n}
277 /// Name/file name of a shell command that will run the test suite.{n}
278 /// Example:{n}
279 /// {n}
280 /// ```toml{n}
281 /// [test]{n}
282 /// type = "command"{n}
283 /// command = "make"{n}
284 /// flags = [ "test" ]{n}
285 /// ```{n}
286 /// {n}
287 /// - script:{n}
288 /// {n}
289 /// Relative path to a Lua script that will run the test suite.{n}
290 /// Example:{n}
291 /// {n}
292 /// ```toml{n}
293 /// [test]{n}
294 /// type = "script"{n}
295 /// script = "tests.lua" # Expects a tests.lua file in the project root{n}
296 /// flags = [ ] # Optional arguments passed to the test script{n}
297 /// ```{n}
298 Test(Test),
299 /// Uninstall a rock from the system.
300 Uninstall(Uninstall),
301 /// Unpins an existing rock, allowing updates to alter the package.
302 Unpin(ChangePin),
303 /// Updates all rocks in a project.
304 Update(Update),
305 /// Generate a Lua rockspec for a Lux project and upload it to the public luarocks repository.{n}
306 /// You can specify a source template for release and dev packages in the lux.toml.{n}
307 /// {n}
308 /// Example:{n}
309 /// {n}
310 /// ```toml{n}
311 /// [source]{n}
312 /// url = "https://host.com/owner/$(PACKAGE)/refs/tags/$(REF).zip"{n}
313 /// dev = "git+https://host.com/owner/$(PACKAGE).git"{n}
314 /// ```{n}
315 /// {n}
316 /// You can use the following variables in the source template:{n}
317 /// {n}
318 /// - $(PACKAGE): The package name.{n}
319 /// - $(VERSION): The package version.{n}
320 /// - $(REF): The git tag or revision (if in a git repository).{n}
321 /// - You may also specify environment variables with `$(<VAR_NAME>)`.{n}
322 /// {n}
323 /// If the `version` is not set in the lux.toml, lux will search the current
324 /// commit for SemVer tags and if found, will use it to generate the package version.
325 Upload(Upload),
326 /// Vendor the dependencies of a project or RockSpec locally.
327 /// When building or installing a package with the `--vendor-dir` option{n}
328 /// or the `[vendor_dir]` config option, Lux will fetch sources from the <vendor-dir>{n}
329 /// instead of from a remote server.
330 Vendor(Vendor),
331 /// Tell which file corresponds to a given module name.
332 Which(Which),
333 /// Spawns an interactive shell with PATH, LUA_PATH, LUA_CPATH and LUA_INIT set.
334 Shell(Shell),
335 /// Synchronize the project tree with the current lux.toml,{n}
336 /// ensuring all packages are installed correctly.
337 Sync(SyncProject),
338}
339
340/// Parse a key=value pair.
341fn parse_key_val<T, U>(s: &str) -> Result<(T, U), Box<dyn Error + Send + Sync + 'static>>
342where
343 T: std::str::FromStr,
344 T::Err: Error + Send + Sync + 'static,
345 U: std::str::FromStr,
346 U::Err: Error + Send + Sync + 'static,
347{
348 let pos = s
349 .find('=')
350 .ok_or_else(|| format!("invalid KEY=value: no `=` found in `{s}`"))?;
351 Ok((s[..pos].parse()?, s[pos + 1..].parse()?))
352}