Reform positional argument parsing (#523)
This diff makes positional argument parsing much cleaner, along with
adding a bunch of tests. Just's positional argument parsing is rather,
complex, so hopefully this reform allows it to both be correct and stay
correct.
User-visible changes:
- `just ..` is now accepted, with the same effect as `just ../`
- `just .` is also accepted, with the same effect as `just`
- It is now an error to pass arguments or overrides to subcommands
that do not accept them, namely `--dump`, `--edit`, `--list`,
`--show`, and `--summary`. It is also an error to pass arguments to
`--evaluate`, although `--evaluate` does of course still accept
overrides.
(This is a breaking change, but hopefully worth it, as it will allow us
to add arguments to subcommands which did not previously take
them, if we so desire.)
- Subcommands which do not accept arguments may now accept a
single search-directory argument, so `just --list ../` and
`just --dump foo/` are now accepted, with the former starting the
search for the justfile to list in the parent directory, and the latter
starting the search for the justfile to dump in `foo`.
2019-11-10 18:02:36 -08:00
|
|
|
use crate::common::*;
|
|
|
|
|
2022-02-23 11:47:43 -08:00
|
|
|
const INIT_JUSTFILE: &str = "default:\n echo 'Hello, world!'\n";
|
2021-07-26 17:19:52 -07:00
|
|
|
|
Gargantuan refactor (#522)
- Instead of changing the current directory with `env::set_current_dir`
to be implicitly inherited by subprocesses, we now use
`Command::current_dir` to set it explicitly. This feels much better,
since we aren't dependent on the implicit state of the process's
current directory.
- Subcommand execution is much improved.
- Added a ton of tests for config parsing, config execution, working
dir, and search dir.
- Error messages are improved. Many more will be colored.
- The Config is now onwed, instead of borrowing from the arguments and
the `clap::ArgMatches` object. This is a huge ergonomic improvement,
especially in tests, and I don't think anyone will notice.
- `--edit` now uses `$VISUAL`, `$EDITOR`, or `vim`, in that order,
matching git, which I think is what most people will expect.
- Added a cute `tmptree!{}` macro, for creating temporary directories
populated with directories and files for tests.
- Admitted that grammer is LL(k) and I don't know what `k` is.
2019-11-09 21:43:20 -08:00
|
|
|
#[derive(PartialEq, Clone, Debug)]
|
|
|
|
pub(crate) enum Subcommand {
|
2021-07-31 13:53:27 -07:00
|
|
|
Changelog,
|
2020-09-17 19:43:04 -07:00
|
|
|
Choose {
|
|
|
|
overrides: BTreeMap<String, String>,
|
2021-09-16 06:44:40 -07:00
|
|
|
chooser: Option<String>,
|
2020-09-17 19:43:04 -07:00
|
|
|
},
|
2021-05-09 20:35:35 -07:00
|
|
|
Command {
|
|
|
|
arguments: Vec<OsString>,
|
2021-09-16 06:44:40 -07:00
|
|
|
binary: OsString,
|
2021-05-09 20:35:35 -07:00
|
|
|
overrides: BTreeMap<String, String>,
|
|
|
|
},
|
2020-01-15 01:20:38 -08:00
|
|
|
Completions {
|
|
|
|
shell: String,
|
|
|
|
},
|
2019-10-07 04:04:39 -07:00
|
|
|
Dump,
|
2019-10-31 17:39:25 -07:00
|
|
|
Edit,
|
Reform positional argument parsing (#523)
This diff makes positional argument parsing much cleaner, along with
adding a bunch of tests. Just's positional argument parsing is rather,
complex, so hopefully this reform allows it to both be correct and stay
correct.
User-visible changes:
- `just ..` is now accepted, with the same effect as `just ../`
- `just .` is also accepted, with the same effect as `just`
- It is now an error to pass arguments or overrides to subcommands
that do not accept them, namely `--dump`, `--edit`, `--list`,
`--show`, and `--summary`. It is also an error to pass arguments to
`--evaluate`, although `--evaluate` does of course still accept
overrides.
(This is a breaking change, but hopefully worth it, as it will allow us
to add arguments to subcommands which did not previously take
them, if we so desire.)
- Subcommands which do not accept arguments may now accept a
single search-directory argument, so `just --list ../` and
`just --dump foo/` are now accepted, with the former starting the
search for the justfile to list in the parent directory, and the latter
starting the search for the justfile to dump in `foo`.
2019-11-10 18:02:36 -08:00
|
|
|
Evaluate {
|
|
|
|
overrides: BTreeMap<String, String>,
|
2021-09-16 06:44:40 -07:00
|
|
|
variable: Option<String>,
|
Reform positional argument parsing (#523)
This diff makes positional argument parsing much cleaner, along with
adding a bunch of tests. Just's positional argument parsing is rather,
complex, so hopefully this reform allows it to both be correct and stay
correct.
User-visible changes:
- `just ..` is now accepted, with the same effect as `just ../`
- `just .` is also accepted, with the same effect as `just`
- It is now an error to pass arguments or overrides to subcommands
that do not accept them, namely `--dump`, `--edit`, `--list`,
`--show`, and `--summary`. It is also an error to pass arguments to
`--evaluate`, although `--evaluate` does of course still accept
overrides.
(This is a breaking change, but hopefully worth it, as it will allow us
to add arguments to subcommands which did not previously take
them, if we so desire.)
- Subcommands which do not accept arguments may now accept a
single search-directory argument, so `just --list ../` and
`just --dump foo/` are now accepted, with the former starting the
search for the justfile to list in the parent directory, and the latter
starting the search for the justfile to dump in `foo`.
2019-11-10 18:02:36 -08:00
|
|
|
},
|
2021-06-08 01:01:27 -07:00
|
|
|
Format,
|
2019-11-19 23:07:44 -08:00
|
|
|
Init,
|
|
|
|
List,
|
Reform positional argument parsing (#523)
This diff makes positional argument parsing much cleaner, along with
adding a bunch of tests. Just's positional argument parsing is rather,
complex, so hopefully this reform allows it to both be correct and stay
correct.
User-visible changes:
- `just ..` is now accepted, with the same effect as `just ../`
- `just .` is also accepted, with the same effect as `just`
- It is now an error to pass arguments or overrides to subcommands
that do not accept them, namely `--dump`, `--edit`, `--list`,
`--show`, and `--summary`. It is also an error to pass arguments to
`--evaluate`, although `--evaluate` does of course still accept
overrides.
(This is a breaking change, but hopefully worth it, as it will allow us
to add arguments to subcommands which did not previously take
them, if we so desire.)
- Subcommands which do not accept arguments may now accept a
single search-directory argument, so `just --list ../` and
`just --dump foo/` are now accepted, with the former starting the
search for the justfile to list in the parent directory, and the latter
starting the search for the justfile to dump in `foo`.
2019-11-10 18:02:36 -08:00
|
|
|
Run {
|
|
|
|
arguments: Vec<String>,
|
2022-03-30 22:13:59 -07:00
|
|
|
overrides: BTreeMap<String, String>,
|
Reform positional argument parsing (#523)
This diff makes positional argument parsing much cleaner, along with
adding a bunch of tests. Just's positional argument parsing is rather,
complex, so hopefully this reform allows it to both be correct and stay
correct.
User-visible changes:
- `just ..` is now accepted, with the same effect as `just ../`
- `just .` is also accepted, with the same effect as `just`
- It is now an error to pass arguments or overrides to subcommands
that do not accept them, namely `--dump`, `--edit`, `--list`,
`--show`, and `--summary`. It is also an error to pass arguments to
`--evaluate`, although `--evaluate` does of course still accept
overrides.
(This is a breaking change, but hopefully worth it, as it will allow us
to add arguments to subcommands which did not previously take
them, if we so desire.)
- Subcommands which do not accept arguments may now accept a
single search-directory argument, so `just --list ../` and
`just --dump foo/` are now accepted, with the former starting the
search for the justfile to list in the parent directory, and the latter
starting the search for the justfile to dump in `foo`.
2019-11-10 18:02:36 -08:00
|
|
|
},
|
|
|
|
Show {
|
|
|
|
name: String,
|
|
|
|
},
|
2019-10-31 17:39:25 -07:00
|
|
|
Summary,
|
2020-03-13 22:19:43 -07:00
|
|
|
Variables,
|
2019-10-07 04:04:39 -07:00
|
|
|
}
|
2020-03-16 17:20:14 -07:00
|
|
|
|
2021-07-26 17:19:52 -07:00
|
|
|
impl Subcommand {
|
2022-03-30 22:13:59 -07:00
|
|
|
pub(crate) fn execute<'src>(
|
|
|
|
&self,
|
|
|
|
config: &Config,
|
|
|
|
loader: &'src Loader,
|
|
|
|
) -> Result<(), Error<'src>> {
|
2021-07-26 17:19:52 -07:00
|
|
|
use Subcommand::*;
|
2020-03-16 17:20:14 -07:00
|
|
|
|
2021-07-31 13:53:27 -07:00
|
|
|
match self {
|
|
|
|
Changelog => {
|
|
|
|
Self::changelog();
|
|
|
|
return Ok(());
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2022-01-30 12:16:10 -08:00
|
|
|
Completions { shell } => return Self::completions(shell),
|
2021-07-31 13:53:27 -07:00
|
|
|
Init => return Self::init(config),
|
2022-03-30 22:13:59 -07:00
|
|
|
Run {
|
|
|
|
arguments,
|
|
|
|
overrides,
|
|
|
|
} => return Self::run(config, loader, arguments, overrides),
|
2021-09-16 06:44:40 -07:00
|
|
|
_ => {}
|
2021-07-26 17:19:52 -07:00
|
|
|
}
|
2020-10-05 19:12:48 -07:00
|
|
|
|
2021-07-26 17:19:52 -07:00
|
|
|
let search = Search::find(&config.search_config, &config.invocation_directory)?;
|
2020-10-05 19:12:48 -07:00
|
|
|
|
2021-07-26 17:19:52 -07:00
|
|
|
if let Edit = self {
|
|
|
|
return Self::edit(&search);
|
|
|
|
}
|
2020-10-05 19:12:48 -07:00
|
|
|
|
2022-03-30 22:13:59 -07:00
|
|
|
let (src, ast, justfile) = Self::compile(config, loader, &search)?;
|
2020-10-05 19:12:48 -07:00
|
|
|
|
2021-07-26 17:19:52 -07:00
|
|
|
match self {
|
2021-09-16 06:44:40 -07:00
|
|
|
Choose { overrides, chooser } => {
|
|
|
|
Self::choose(config, justfile, &search, overrides, chooser.as_deref())?;
|
|
|
|
}
|
2021-09-16 07:51:45 -07:00
|
|
|
Command { overrides, .. } | Evaluate { overrides, .. } => {
|
2022-01-30 12:16:10 -08:00
|
|
|
justfile.run(config, &search, overrides, &[])?;
|
2021-09-16 07:51:45 -07:00
|
|
|
}
|
2021-11-17 00:07:48 -08:00
|
|
|
Dump => Self::dump(config, ast, justfile)?,
|
2022-01-30 12:16:10 -08:00
|
|
|
Format => Self::format(config, &search, src, ast)?,
|
2021-07-28 18:06:57 -07:00
|
|
|
List => Self::list(config, justfile),
|
2022-01-30 12:16:10 -08:00
|
|
|
Show { ref name } => Self::show(config, name, justfile)?,
|
2021-07-28 18:06:57 -07:00
|
|
|
Summary => Self::summary(config, justfile),
|
2021-07-26 17:19:52 -07:00
|
|
|
Variables => Self::variables(justfile),
|
2022-03-30 22:13:59 -07:00
|
|
|
Changelog | Completions { .. } | Edit | Init | Run { .. } => unreachable!(),
|
2021-07-26 17:19:52 -07:00
|
|
|
}
|
2020-10-05 17:58:30 -07:00
|
|
|
|
2021-07-26 17:19:52 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-03-30 22:13:59 -07:00
|
|
|
pub(crate) fn run<'src>(
|
|
|
|
config: &Config,
|
|
|
|
loader: &'src Loader,
|
|
|
|
arguments: &[String],
|
|
|
|
overrides: &BTreeMap<String, String>,
|
|
|
|
) -> Result<(), Error<'src>> {
|
|
|
|
if config.unstable && config.search_config == SearchConfig::FromInvocationDirectory {
|
|
|
|
let mut path = config.invocation_directory.clone();
|
|
|
|
|
|
|
|
let mut unknown_recipes_errors = None;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let search = match Search::find_next(&path) {
|
|
|
|
Err(SearchError::NotFound) => match unknown_recipes_errors {
|
|
|
|
Some(err) => return Err(err),
|
|
|
|
None => return Err(SearchError::NotFound.into()),
|
|
|
|
},
|
|
|
|
Err(err) => return Err(err.into()),
|
|
|
|
Ok(search) => {
|
|
|
|
if config.verbosity.loud() && path != config.invocation_directory {
|
|
|
|
eprintln!(
|
|
|
|
"Trying {}",
|
|
|
|
config
|
|
|
|
.invocation_directory
|
|
|
|
.strip_prefix(path)
|
|
|
|
.unwrap()
|
|
|
|
.components()
|
|
|
|
.map(|_| path::Component::ParentDir)
|
|
|
|
.collect::<PathBuf>()
|
|
|
|
.join(search.justfile.file_name().unwrap())
|
|
|
|
.display()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
search
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match Self::run_inner(config, loader, arguments, overrides, &search) {
|
|
|
|
Err(err @ Error::UnknownRecipes { .. }) => {
|
|
|
|
match search.justfile.parent().unwrap().parent() {
|
|
|
|
Some(parent) => {
|
|
|
|
unknown_recipes_errors.get_or_insert(err);
|
|
|
|
path = parent.into();
|
|
|
|
}
|
|
|
|
None => return Err(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result => return result,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Self::run_inner(
|
|
|
|
config,
|
|
|
|
loader,
|
|
|
|
arguments,
|
|
|
|
overrides,
|
|
|
|
&Search::find(&config.search_config, &config.invocation_directory)?,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run_inner<'src>(
|
|
|
|
config: &Config,
|
|
|
|
loader: &'src Loader,
|
|
|
|
arguments: &[String],
|
|
|
|
overrides: &BTreeMap<String, String>,
|
|
|
|
search: &Search,
|
|
|
|
) -> Result<(), Error<'src>> {
|
|
|
|
let (_src, _ast, justfile) = Self::compile(config, loader, search)?;
|
|
|
|
justfile.run(config, search, overrides, arguments)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compile<'src>(
|
|
|
|
config: &Config,
|
|
|
|
loader: &'src Loader,
|
|
|
|
search: &Search,
|
|
|
|
) -> Result<(&'src str, Ast<'src>, Justfile<'src>), Error<'src>> {
|
|
|
|
let src = loader.load(&search.justfile)?;
|
|
|
|
|
|
|
|
let tokens = Lexer::lex(src)?;
|
|
|
|
let ast = Parser::parse(&tokens)?;
|
|
|
|
let justfile = Analyzer::analyze(ast.clone())?;
|
|
|
|
|
|
|
|
if config.verbosity.loud() {
|
|
|
|
for warning in &justfile.warnings {
|
|
|
|
eprintln!("{}", warning.color_display(config.color.stderr()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok((src, ast, justfile))
|
|
|
|
}
|
|
|
|
|
2021-07-31 13:53:27 -07:00
|
|
|
fn changelog() {
|
|
|
|
print!("{}", include_str!("../CHANGELOG.md"));
|
|
|
|
}
|
|
|
|
|
2021-07-26 17:19:52 -07:00
|
|
|
fn choose<'src>(
|
|
|
|
config: &Config,
|
|
|
|
justfile: Justfile<'src>,
|
|
|
|
search: &Search,
|
|
|
|
overrides: &BTreeMap<String, String>,
|
|
|
|
chooser: Option<&str>,
|
|
|
|
) -> Result<(), Error<'src>> {
|
|
|
|
let recipes = justfile
|
|
|
|
.public_recipes(config.unsorted)
|
|
|
|
.iter()
|
|
|
|
.filter(|recipe| recipe.min_arguments() == 0)
|
2022-01-30 12:16:10 -08:00
|
|
|
.copied()
|
2021-07-26 17:19:52 -07:00
|
|
|
.collect::<Vec<&Recipe<Dependency>>>();
|
|
|
|
|
|
|
|
if recipes.is_empty() {
|
|
|
|
return Err(Error::NoChoosableRecipes);
|
|
|
|
}
|
|
|
|
|
|
|
|
let chooser = chooser
|
|
|
|
.map(OsString::from)
|
|
|
|
.or_else(|| env::var_os(config::CHOOSER_ENVIRONMENT_KEY))
|
|
|
|
.unwrap_or_else(|| OsString::from(config::CHOOSER_DEFAULT));
|
|
|
|
|
|
|
|
let result = justfile
|
|
|
|
.settings
|
2022-01-30 12:16:10 -08:00
|
|
|
.shell_command(config)
|
2021-07-26 17:19:52 -07:00
|
|
|
.arg(&chooser)
|
|
|
|
.current_dir(&search.working_directory)
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.spawn();
|
|
|
|
|
|
|
|
let mut child = match result {
|
|
|
|
Ok(child) => child,
|
|
|
|
Err(io_error) => {
|
|
|
|
return Err(Error::ChooserInvoke {
|
2022-01-30 12:16:10 -08:00
|
|
|
shell_binary: justfile.settings.shell_binary(config).to_owned(),
|
|
|
|
shell_arguments: justfile.settings.shell_arguments(config).join(" "),
|
2021-07-26 17:19:52 -07:00
|
|
|
chooser,
|
|
|
|
io_error,
|
|
|
|
});
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2021-07-26 17:19:52 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
for recipe in recipes {
|
|
|
|
if let Err(io_error) = child
|
|
|
|
.stdin
|
|
|
|
.as_mut()
|
|
|
|
.expect("Child was created with piped stdio")
|
|
|
|
.write_all(format!("{}\n", recipe.name).as_bytes())
|
|
|
|
{
|
|
|
|
return Err(Error::ChooserWrite { io_error, chooser });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let output = match child.wait_with_output() {
|
|
|
|
Ok(output) => output,
|
|
|
|
Err(io_error) => {
|
|
|
|
return Err(Error::ChooserRead { io_error, chooser });
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2021-07-26 17:19:52 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
if !output.status.success() {
|
|
|
|
return Err(Error::ChooserStatus {
|
|
|
|
status: output.status,
|
|
|
|
chooser,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let stdout = String::from_utf8_lossy(&output.stdout);
|
|
|
|
|
|
|
|
let recipes = stdout
|
|
|
|
.trim()
|
|
|
|
.split_whitespace()
|
|
|
|
.map(str::to_owned)
|
|
|
|
.collect::<Vec<String>>();
|
|
|
|
|
|
|
|
justfile.run(config, search, overrides, &recipes)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn completions(shell: &str) -> RunResult<'static, ()> {
|
2020-10-05 17:58:30 -07:00
|
|
|
use clap::Shell;
|
|
|
|
|
2021-07-26 01:26:06 -07:00
|
|
|
fn replace(haystack: &mut String, needle: &str, replacement: &str) -> RunResult<'static, ()> {
|
2020-03-16 17:20:14 -07:00
|
|
|
if let Some(index) = haystack.find(needle) {
|
|
|
|
haystack.replace_range(index..index + needle.len(), replacement);
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2021-07-26 01:26:06 -07:00
|
|
|
Err(Error::internal(format!(
|
|
|
|
"Failed to find text:\n{}\n…in completion script:\n{}",
|
|
|
|
needle, haystack
|
|
|
|
)))
|
2020-03-16 17:20:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let shell = shell
|
2020-10-05 17:58:30 -07:00
|
|
|
.parse::<Shell>()
|
2020-03-16 17:20:14 -07:00
|
|
|
.expect("Invalid value for clap::Shell");
|
|
|
|
|
|
|
|
let buffer = Vec::new();
|
|
|
|
let mut cursor = Cursor::new(buffer);
|
|
|
|
Config::app().gen_completions_to(env!("CARGO_PKG_NAME"), shell, &mut cursor);
|
|
|
|
let buffer = cursor.into_inner();
|
|
|
|
let mut script = String::from_utf8(buffer).expect("Clap completion not UTF-8");
|
|
|
|
|
2020-10-05 17:58:30 -07:00
|
|
|
match shell {
|
2021-09-16 06:44:40 -07:00
|
|
|
Shell::Bash => {
|
2021-07-26 17:19:52 -07:00
|
|
|
for (needle, replacement) in completions::BASH_COMPLETION_REPLACEMENTS {
|
2021-07-26 01:26:06 -07:00
|
|
|
replace(&mut script, needle, replacement)?;
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
|
|
|
}
|
2020-10-05 17:58:30 -07:00
|
|
|
Shell::Fish => {
|
2021-07-26 17:19:52 -07:00
|
|
|
script.insert_str(0, completions::FISH_RECIPE_COMPLETIONS);
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
|
|
|
Shell::PowerShell => {
|
2021-07-26 17:19:52 -07:00
|
|
|
for (needle, replacement) in completions::POWERSHELL_COMPLETION_REPLACEMENTS {
|
2021-07-26 01:26:06 -07:00
|
|
|
replace(&mut script, needle, replacement)?;
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
|
|
|
}
|
2020-10-05 19:12:48 -07:00
|
|
|
|
2021-09-16 06:44:40 -07:00
|
|
|
Shell::Zsh => {
|
2021-07-26 17:19:52 -07:00
|
|
|
for (needle, replacement) in completions::ZSH_COMPLETION_REPLACEMENTS {
|
2021-07-26 01:26:06 -07:00
|
|
|
replace(&mut script, needle, replacement)?;
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Shell::Elvish => {}
|
2020-05-03 20:35:53 -07:00
|
|
|
}
|
|
|
|
|
2020-03-16 17:20:14 -07:00
|
|
|
println!("{}", script.trim());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-07-26 17:19:52 -07:00
|
|
|
|
2021-11-17 00:07:48 -08:00
|
|
|
fn dump(config: &Config, ast: Ast, justfile: Justfile) -> Result<(), Error<'static>> {
|
|
|
|
match config.dump_format {
|
|
|
|
DumpFormat::Json => {
|
|
|
|
config.require_unstable("The JSON dump format is currently unstable.")?;
|
|
|
|
serde_json::to_writer(io::stdout(), &justfile)
|
|
|
|
.map_err(|serde_json_error| Error::DumpJson { serde_json_error })?;
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
DumpFormat::Just => print!("{}", ast),
|
|
|
|
}
|
|
|
|
Ok(())
|
2021-07-26 17:19:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn edit(search: &Search) -> Result<(), Error<'static>> {
|
|
|
|
let editor = env::var_os("VISUAL")
|
|
|
|
.or_else(|| env::var_os("EDITOR"))
|
|
|
|
.unwrap_or_else(|| "vim".into());
|
|
|
|
|
|
|
|
let error = Command::new(&editor)
|
|
|
|
.current_dir(&search.working_directory)
|
|
|
|
.arg(&search.justfile)
|
|
|
|
.status();
|
|
|
|
|
|
|
|
let status = match error {
|
|
|
|
Err(io_error) => return Err(Error::EditorInvoke { editor, io_error }),
|
|
|
|
Ok(status) => status,
|
|
|
|
};
|
|
|
|
|
|
|
|
if !status.success() {
|
|
|
|
return Err(Error::EditorStatus { editor, status });
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-10-31 23:18:11 -07:00
|
|
|
fn format(config: &Config, search: &Search, src: &str, ast: Ast) -> Result<(), Error<'static>> {
|
2021-07-26 17:19:52 -07:00
|
|
|
config.require_unstable("The `--fmt` command is currently unstable.")?;
|
|
|
|
|
2021-10-31 23:18:11 -07:00
|
|
|
let formatted = ast.to_string();
|
|
|
|
|
2021-10-31 21:27:59 -07:00
|
|
|
if config.check {
|
2021-10-31 23:18:11 -07:00
|
|
|
return if formatted != src {
|
|
|
|
use similar::{ChangeTag, TextDiff};
|
|
|
|
|
|
|
|
let diff = TextDiff::configure()
|
|
|
|
.algorithm(similar::Algorithm::Patience)
|
|
|
|
.diff_lines(src, &formatted);
|
|
|
|
|
|
|
|
for op in diff.ops() {
|
|
|
|
for change in diff.iter_changes(op) {
|
|
|
|
let (symbol, color) = match change.tag() {
|
|
|
|
ChangeTag::Delete => ("-", config.color.stderr().diff_deleted()),
|
|
|
|
ChangeTag::Equal => (" ", config.color.stderr()),
|
|
|
|
ChangeTag::Insert => ("+", config.color.stderr().diff_added()),
|
|
|
|
};
|
|
|
|
|
|
|
|
eprint!("{}{}{}{}", color.prefix(), symbol, change, color.suffix());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-31 21:27:59 -07:00
|
|
|
Err(Error::FormatCheckFoundDiff)
|
2021-10-31 23:18:11 -07:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fs::write(&search.justfile, formatted).map_err(|io_error| Error::WriteJustfile {
|
|
|
|
justfile: search.justfile.clone(),
|
|
|
|
io_error,
|
|
|
|
})?;
|
|
|
|
|
|
|
|
if config.verbosity.loud() {
|
|
|
|
eprintln!("Wrote justfile to `{}`", search.justfile.display());
|
2021-07-26 17:19:52 -07:00
|
|
|
}
|
2021-10-31 23:18:11 -07:00
|
|
|
|
|
|
|
Ok(())
|
2021-07-26 17:19:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn init(config: &Config) -> Result<(), Error<'static>> {
|
|
|
|
let search = Search::init(&config.search_config, &config.invocation_directory)?;
|
|
|
|
|
|
|
|
if search.justfile.is_file() {
|
|
|
|
Err(Error::InitExists {
|
|
|
|
justfile: search.justfile,
|
|
|
|
})
|
|
|
|
} else if let Err(io_error) = fs::write(&search.justfile, INIT_JUSTFILE) {
|
|
|
|
Err(Error::WriteJustfile {
|
|
|
|
justfile: search.justfile,
|
|
|
|
io_error,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
if config.verbosity.loud() {
|
|
|
|
eprintln!("Wrote justfile to `{}`", search.justfile.display());
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn list(config: &Config, justfile: Justfile) {
|
|
|
|
// Construct a target to alias map.
|
|
|
|
let mut recipe_aliases: BTreeMap<&str, Vec<&str>> = BTreeMap::new();
|
|
|
|
for alias in justfile.aliases.values() {
|
|
|
|
if alias.is_private() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !recipe_aliases.contains_key(alias.target.name.lexeme()) {
|
|
|
|
recipe_aliases.insert(alias.target.name.lexeme(), vec![alias.name.lexeme()]);
|
|
|
|
} else {
|
|
|
|
let aliases = recipe_aliases.get_mut(alias.target.name.lexeme()).unwrap();
|
|
|
|
aliases.push(alias.name.lexeme());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut line_widths: BTreeMap<&str, usize> = BTreeMap::new();
|
|
|
|
|
|
|
|
for (name, recipe) in &justfile.recipes {
|
|
|
|
if recipe.private {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for name in iter::once(name).chain(recipe_aliases.get(name).unwrap_or(&Vec::new())) {
|
|
|
|
let mut line_width = UnicodeWidthStr::width(*name);
|
|
|
|
|
|
|
|
for parameter in &recipe.parameters {
|
2021-07-28 18:06:57 -07:00
|
|
|
line_width += UnicodeWidthStr::width(
|
|
|
|
format!(" {}", parameter.color_display(Color::never())).as_str(),
|
|
|
|
);
|
2021-07-26 17:19:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if line_width <= 30 {
|
|
|
|
line_widths.insert(name, line_width);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-30 12:16:10 -08:00
|
|
|
let max_line_width = cmp::min(line_widths.values().copied().max().unwrap_or(0), 30);
|
2021-07-26 17:19:52 -07:00
|
|
|
|
|
|
|
let doc_color = config.color.stdout().doc();
|
|
|
|
print!("{}", config.list_heading);
|
|
|
|
|
|
|
|
for recipe in justfile.public_recipes(config.unsorted) {
|
|
|
|
let name = recipe.name();
|
|
|
|
|
|
|
|
for (i, name) in iter::once(&name)
|
|
|
|
.chain(recipe_aliases.get(name).unwrap_or(&Vec::new()))
|
|
|
|
.enumerate()
|
|
|
|
{
|
|
|
|
print!("{}{}", config.list_prefix, name);
|
|
|
|
for parameter in &recipe.parameters {
|
2021-07-28 18:06:57 -07:00
|
|
|
print!(" {}", parameter.color_display(config.color.stdout()));
|
2021-07-26 17:19:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Declaring this outside of the nested loops will probably be more efficient,
|
|
|
|
// but it creates all sorts of lifetime issues with variables inside the loops.
|
|
|
|
// If this is inlined like the docs say, it shouldn't make any difference.
|
|
|
|
let print_doc = |doc| {
|
|
|
|
print!(
|
|
|
|
" {:padding$}{} {}",
|
|
|
|
"",
|
|
|
|
doc_color.paint("#"),
|
|
|
|
doc_color.paint(doc),
|
|
|
|
padding = max_line_width
|
2022-01-30 12:16:10 -08:00
|
|
|
.saturating_sub(line_widths.get(name).copied().unwrap_or(max_line_width))
|
2021-07-26 17:19:52 -07:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
match (i, recipe.doc) {
|
|
|
|
(0, Some(doc)) => print_doc(doc),
|
|
|
|
(0, None) => (),
|
|
|
|
_ => {
|
|
|
|
let alias_doc = format!("alias for `{}`", recipe.name);
|
|
|
|
print_doc(&alias_doc);
|
2021-09-16 06:44:40 -07:00
|
|
|
}
|
2021-07-26 17:19:52 -07:00
|
|
|
}
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 18:06:57 -07:00
|
|
|
fn show<'src>(config: &Config, name: &str, justfile: Justfile<'src>) -> Result<(), Error<'src>> {
|
2021-07-26 17:19:52 -07:00
|
|
|
if let Some(alias) = justfile.get_alias(name) {
|
|
|
|
let recipe = justfile.get_recipe(alias.target.name.lexeme()).unwrap();
|
|
|
|
println!("{}", alias);
|
2021-07-28 18:06:57 -07:00
|
|
|
println!("{}", recipe.color_display(config.color.stdout()));
|
2021-07-26 17:19:52 -07:00
|
|
|
Ok(())
|
|
|
|
} else if let Some(recipe) = justfile.get_recipe(name) {
|
2021-07-28 18:06:57 -07:00
|
|
|
println!("{}", recipe.color_display(config.color.stdout()));
|
2021-07-26 17:19:52 -07:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(Error::UnknownRecipes {
|
2021-09-16 06:44:40 -07:00
|
|
|
recipes: vec![name.to_owned()],
|
2021-07-26 17:19:52 -07:00
|
|
|
suggestion: justfile.suggest_recipe(name),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn summary(config: &Config, justfile: Justfile) {
|
|
|
|
if justfile.count() == 0 {
|
|
|
|
if config.verbosity.loud() {
|
|
|
|
eprintln!("Justfile contains no recipes.");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let summary = justfile
|
|
|
|
.public_recipes(config.unsorted)
|
|
|
|
.iter()
|
|
|
|
.map(|recipe| recipe.name())
|
|
|
|
.collect::<Vec<&str>>()
|
|
|
|
.join(" ");
|
|
|
|
println!("{}", summary);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn variables(justfile: Justfile) {
|
|
|
|
for (i, (_, assignment)) in justfile.assignments.iter().enumerate() {
|
|
|
|
if i > 0 {
|
|
|
|
print!(" ");
|
|
|
|
}
|
|
|
|
print!("{}", assignment.name);
|
|
|
|
}
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn init_justfile() {
|
|
|
|
testing::compile(INIT_JUSTFILE);
|
|
|
|
}
|
2020-03-16 17:20:14 -07:00
|
|
|
}
|