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