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