Skip to content

Commit 4f0d5a4

Browse files
committed
feat(cli): Add argument to ease soarch for 3rd party module(s)
1 parent 302b0cc commit 4f0d5a4

5 files changed

Lines changed: 50 additions & 4 deletions

File tree

core/cli.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ cli.parseArguments = function ()
2323
cliargs:option("-E, --evaluate-after=VALUE", "Evaluate Lua expression after processing input", {})
2424
cliargs:option("-f, --fontmanager=VALUE", "Specify which font manager to use")
2525
cliargs:option("-I, --include=FILE", "Deprecated, see --use, --preamble, --postamble, or multiple input files", {})
26+
cliargs:option(
27+
" --luarocks-tree=LUAROCKS_TREE",
28+
"Add a path to the list of LuaRocks trees searched for modules",
29+
{}
30+
)
2631
cliargs:option("-m, --makedeps=FILE", "Generate a Makefile format list of dependencies and white them to a file")
2732
cliargs:option("-o, --output=FILE", "Explicitly set output file name")
2833
cliargs:option("-O, --options=PARAMETER=VALUE[,PARAMETER=VALUE]", "Set or override document class options", {})
@@ -92,6 +97,9 @@ cli.parseArguments = function ()
9297
if opts.fontmanager then
9398
SILE.input.fontmanager = opts.fontmanager
9499
end
100+
for _, tree in ipairs(opts["luarocks-tree"]) do
101+
table.insert(SILE.input.luarocksTrees, tree)
102+
end
95103
if opts.makedeps then
96104
SILE.makeDeps = require("core.makedeps")
97105
SILE.makeDeps.filename = opts.makedeps

core/sile.lua

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ SILE.input = {
150150
filenames = {},
151151
evaluates = {},
152152
evaluateAfters = {},
153+
luarocksTrees = {},
153154
uses = {},
154155
options = {},
155156
preambles = {}, -- deprecated, undocumented
@@ -209,6 +210,9 @@ function SILE.init ()
209210
SU.deprecated("SILE.backend", "SILE.input.backend", "0.15.7", "0.17.0")
210211
SILE.input.backend = SILE.backend
211212
end
213+
for _, tree in ipairs(SILE.input.luarocksTrees) do
214+
_G["extendSilePathRocks"](tree)
215+
end
212216
if not SILE.input.backend then
213217
SILE.input.backend = "libtexpdf"
214218
end
@@ -267,12 +271,22 @@ local function suggest_luarocks (module)
267271
268272
eval $(luarocks --lua-version %s --tree path)
269273
270-
Thereafter running SILE as normal in the same shell should work as
271-
expected:
274+
Thereafter running `sile` as normal in the same shell should work as
275+
expected. This code can be used in your shell's initialization script
276+
to avoid having to do it manually in each new shell. This is true for
277+
user home directory installations using `--local` or any specific values
278+
for `--tree` other than 'lua_modules'.
279+
280+
As an anternative to setting up environment variables when using a
281+
non-default tree location, you can use the `--luarocks-tree` option to
282+
add path(s) at runtime. This is simpler to type, but must be used on each
283+
and every invocation. The value for tree should be the same as used when
284+
installing the LuaRock(s), or an appropriate full path to the location
285+
used by `--local` (generally "$HOME/.luarocks"):
272286
273-
sile %s
287+
sile --luarocks-tree path %s
274288
275-
]]):format(SILE.lua_version, guessed_module_name, SILE.lua_version, pl.stringx.join(" ", _G.arg or {}))
289+
]]):format(SILE.lua_version, guessed_module_name, SILE.lua_version, pl.stringx.join(" ", _G.arg or {}))
276290
end
277291

278292
--- Multi-purpose loader to load and initialize modules.

src/bin/sile.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ fn main() -> Result<()> {
4545
args.evaluate,
4646
args.evaluate_after,
4747
args.fontmanager,
48+
args.luarocks_tree,
4849
args.makedeps,
4950
args.output,
5051
args.option,

src/cli.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ pub struct Cli {
6868
#[clap(short, long, value_name = "FONTMANAGER")]
6969
pub fontmanager: Option<String>,
7070

71+
/// Add a path to the list of LuaRocks trees searched for modules.
72+
///
73+
/// When installing 3rd party SILE modules via LuaRocks there are several possible installation locations.
74+
/// They may be installed to the host system, the user's home directory, a project-local path, or any arbitrary location.
75+
/// In the case of the system or a project-local path called exactly `lua_modules` they will be found automatically.
76+
/// In the case of any other path, SILE needs to be told where to find them.
77+
/// This can be done by exporting a LUA_PATH environment variable before running SILE, or by using this option.
78+
#[clap(long, value_name = "LUAROCKS_TREE")]
79+
pub luarocks_tree: Option<Vec<PathBuf>>,
80+
7181
/// Generate a Makefile format list of dependencies and white them to a file.
7282
///
7383
/// This tracks all the files (input files, Lua libraries, fonts, images, etc.) use during the

src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ pub fn start_luavm() -> crate::Result<Lua> {
2424
{
2525
lua = embed::inject_embedded_loaders(lua)?;
2626
}
27+
// For parity with the legacy Lua arg parser, allows inspection of CLI args at runtime in Lua.
28+
{
29+
let args: Vec<String> = std::env::args().skip(1).collect();
30+
let arg_table = lua.create_table()?;
31+
for (i, arg) in args.iter().enumerate() {
32+
arg_table.set(i + 1, arg.clone())?;
33+
}
34+
lua.globals().set("arg", arg_table)?;
35+
}
2736
lua = inject_paths(lua)?;
2837
lua = load_sile(lua)?;
2938
lua = inject_version(lua)?;
@@ -104,6 +113,7 @@ pub fn run(
104113
evaluates: Option<Vec<String>>,
105114
evaluate_afters: Option<Vec<String>>,
106115
fontmanager: Option<String>,
116+
luarocks_tree: Option<Vec<PathBuf>>,
107117
makedeps: Option<PathBuf>,
108118
output: Option<PathBuf>,
109119
options: Option<Vec<String>>,
@@ -138,6 +148,9 @@ pub fn run(
138148
if let Some(fontmanager) = fontmanager {
139149
sile_input.set("fontmanager", fontmanager)?;
140150
}
151+
if let Some(trees) = luarocks_tree {
152+
sile_input.set("luarocksTrees", trees)?;
153+
}
141154
if let Some(class) = class {
142155
sile_input.set("class", class)?;
143156
}

0 commit comments

Comments
 (0)