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