use crate::packages::Ecosystem;
struct Runner {
ecosystem: Ecosystem,
subcommand: &'static [&'static str],
package: &'static [&'static str],
extra: &'static [&'static str],
values: &'static [&'static str],
offline: &'static [&'static str],
}
impl Runner {
fn takes_value(&self, flag: &str) -> bool {
[self.package, self.extra, self.values]
.iter()
.any(|list| list.contains(&flag))
}
}
const NPM_VALUES: &[&str] = &[
"-c",
"--call",
"--registry",
"--cache",
"--userconfig",
"-w",
"--workspace",
"--prefix",
"--loglevel",
"-C",
"--dir",
"--cwd",
];
const fn npm(subcommand: &'static [&'static str]) -> Runner {
Runner {
ecosystem: Ecosystem::Npm,
subcommand,
package: &["-p", "--package"],
extra: &[],
values: NPM_VALUES,
offline: &["--no", "--no-install", "--offline"],
}
}
const UV_VALUES: &[&str] = &[
"-p",
"--python",
"--with-requirements",
"--with-editable",
"--index",
"--default-index",
"-i",
"--index-url",
"--extra-index-url",
"-f",
"--find-links",
"--directory",
"--project",
"--config-file",
"--cache-dir",
"--exclude-newer",
"--color",
"--env-file",
"-C",
"--config-setting",
];
const fn uv(subcommand: &'static [&'static str]) -> Runner {
Runner {
ecosystem: Ecosystem::Pypi,
subcommand,
package: &["--from"],
extra: &["-w", "--with"],
values: UV_VALUES,
offline: &["--offline"],
}
}
const PIPX_RUN: Runner = Runner {
ecosystem: Ecosystem::Pypi,
subcommand: &["run"],
package: &["--spec"],
extra: &[],
values: &["--python", "--pip-args", "--index-url", "--backend"],
offline: &[],
};
const NPX: Runner = npm(&[]);
const NPM_EXEC: [Runner; 2] = [npm(&["exec"]), npm(&["x"])];
const DLX: Runner = npm(&["dlx"]);
const BUN_X: Runner = npm(&["x"]);
const UVX: Runner = uv(&[]);
const UV_TOOL_RUN: Runner = uv(&["tool", "run"]);
fn runners(program: &str) -> &'static [Runner] {
match program {
"npx" | "bunx" | "pnpx" => std::slice::from_ref(&NPX),
"npm" => &NPM_EXEC,
"pnpm" | "yarn" => std::slice::from_ref(&DLX),
"bun" => std::slice::from_ref(&BUN_X),
"uvx" => std::slice::from_ref(&UVX),
"uv" => std::slice::from_ref(&UV_TOOL_RUN),
"pipx" => std::slice::from_ref(&PIPX_RUN),
_ => &[],
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct Fetched<'a> {
pub(super) word: &'a str,
pub(super) program_word: bool,
}
pub(super) fn fetched<'a>(
argv: &'a [String],
program: &str,
) -> Option<(Ecosystem, Vec<Fetched<'a>>)> {
let operands = argv.get(1..)?;
runners(program).iter().find_map(|runner| {
let n = runner.subcommand.len();
let starts = operands.len() >= n
&& operands[..n]
.iter()
.zip(runner.subcommand)
.all(|(a, w)| a == w);
if !starts {
return None;
}
Some((runner.ecosystem, packages(&operands[n..], runner)?))
})
}
fn packages<'a>(rest: &'a [String], runner: &Runner) -> Option<Vec<Fetched<'a>>> {
let mut named = Vec::new();
let mut extra = Vec::new();
let mut first = None;
let mut words = rest.iter();
while let Some(arg) = words.next() {
if arg == "--" {
first = words.next().map(String::as_str);
break;
}
if !arg.starts_with('-') || arg.len() == 1 {
first = Some(arg.as_str());
break;
}
let (flag, attached) = match arg.split_once('=') {
Some((f, v)) => (f, Some(v)),
None => (arg.as_str(), None),
};
if runner.offline.contains(&flag) {
return None;
}
let value = match attached {
Some(v) => Some(v),
None if runner.takes_value(flag) => words.next().map(String::as_str),
None => None,
};
if runner.package.contains(&flag) {
named.extend(value);
} else if runner.extra.contains(&flag) {
extra.extend(value);
}
}
let option = |word| Fetched {
word,
program_word: false,
};
let mut fetched: Vec<Fetched<'a>> = named.into_iter().map(option).collect();
if fetched.is_empty() {
fetched.extend(first.map(|word| Fetched {
word,
program_word: true,
}));
}
fetched.extend(extra.into_iter().map(option));
Some(fetched)
}