2019-04-11 16:35:16 -07:00
|
|
|
//! Justfile summary creation, for testing purposes only.
|
|
|
|
//!
|
2020-02-14 04:49:25 -08:00
|
|
|
//! The contents of this module are not bound by any stability guarantees.
|
|
|
|
//! Breaking changes may be introduced at any time.
|
2019-04-11 16:35:16 -07:00
|
|
|
//!
|
2020-02-14 04:49:25 -08:00
|
|
|
//! The main entry point into this module is the `summary` function, which
|
|
|
|
//! parses a justfile at a given path and produces a `Summary` object, which
|
|
|
|
//! broadly captures the functionality of the parsed justfile, or an error
|
|
|
|
//! message.
|
2019-04-11 16:35:16 -07:00
|
|
|
//!
|
2020-02-14 04:49:25 -08:00
|
|
|
//! This functionality is intended to be used with `janus`, a tool for ensuring
|
|
|
|
//! that changes to just do not inadvertently break or change the interpretation
|
|
|
|
//! of existing justfiles.
|
2019-04-11 16:35:16 -07:00
|
|
|
|
2022-12-27 20:16:18 -08:00
|
|
|
use {
|
2023-11-21 11:28:59 -08:00
|
|
|
crate::{compiler::Compiler, error::Error, loader::Loader},
|
|
|
|
std::{collections::BTreeMap, io, path::Path},
|
2022-12-27 20:16:18 -08:00
|
|
|
};
|
2019-11-07 10:55:15 -08:00
|
|
|
|
|
|
|
mod full {
|
|
|
|
pub(crate) use crate::{
|
2024-05-14 18:55:32 -07:00
|
|
|
assignment::Assignment, condition::Condition, conditional_operator::ConditionalOperator,
|
|
|
|
dependency::Dependency, expression::Expression, fragment::Fragment, justfile::Justfile,
|
|
|
|
line::Line, parameter::Parameter, parameter_kind::ParameterKind, recipe::Recipe, thunk::Thunk,
|
2019-11-07 10:55:15 -08:00
|
|
|
};
|
|
|
|
}
|
2019-04-11 16:35:16 -07:00
|
|
|
|
2019-04-15 13:21:35 -07:00
|
|
|
pub fn summary(path: &Path) -> Result<Result<Summary, String>, io::Error> {
|
2023-11-21 11:28:59 -08:00
|
|
|
let loader = Loader::new();
|
2019-04-11 16:35:16 -07:00
|
|
|
|
2023-12-27 20:27:15 -08:00
|
|
|
match Compiler::compile(false, &loader, path) {
|
2023-11-21 11:28:59 -08:00
|
|
|
Ok(compilation) => Ok(Ok(Summary::new(&compilation.justfile))),
|
|
|
|
Err(error) => Ok(Err(if let Error::Compile { compile_error } = error {
|
|
|
|
compile_error.to_string()
|
|
|
|
} else {
|
|
|
|
format!("{error:?}")
|
|
|
|
})),
|
2019-04-11 16:35:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Eq, PartialEq, Hash, Ord, PartialOrd, Debug, Clone)]
|
|
|
|
pub struct Summary {
|
|
|
|
pub assignments: BTreeMap<String, Assignment>,
|
2021-09-16 06:44:40 -07:00
|
|
|
pub recipes: BTreeMap<String, Recipe>,
|
2019-04-11 16:35:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Summary {
|
2024-05-14 20:07:41 -07:00
|
|
|
fn new(justfile: &full::Justfile) -> Self {
|
2019-04-11 16:35:16 -07:00
|
|
|
let mut aliases = BTreeMap::new();
|
|
|
|
|
|
|
|
for alias in justfile.aliases.values() {
|
|
|
|
aliases
|
2019-12-11 17:08:48 -08:00
|
|
|
.entry(alias.target.name())
|
2019-04-11 23:58:08 -07:00
|
|
|
.or_insert_with(Vec::new)
|
2019-04-11 16:35:16 -07:00
|
|
|
.push(alias.name.to_string());
|
|
|
|
}
|
|
|
|
|
2024-05-14 20:07:41 -07:00
|
|
|
Self {
|
2021-09-16 06:44:40 -07:00
|
|
|
recipes: justfile
|
2019-04-11 16:35:16 -07:00
|
|
|
.recipes
|
2023-11-21 11:28:59 -08:00
|
|
|
.iter()
|
2019-04-11 16:35:16 -07:00
|
|
|
.map(|(name, recipe)| {
|
|
|
|
(
|
2023-11-21 11:28:59 -08:00
|
|
|
(*name).to_string(),
|
|
|
|
Recipe::new(recipe, aliases.remove(name).unwrap_or_default()),
|
2019-04-11 16:35:16 -07:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
assignments: justfile
|
|
|
|
.assignments
|
2019-12-11 17:08:48 -08:00
|
|
|
.iter()
|
2021-05-07 00:14:38 -07:00
|
|
|
.map(|(name, assignment)| ((*name).to_owned(), Assignment::new(assignment)))
|
2019-04-11 16:35:16 -07:00
|
|
|
.collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Eq, PartialEq, Hash, Ord, PartialOrd, Debug, Clone)]
|
|
|
|
pub struct Recipe {
|
2021-09-16 06:44:40 -07:00
|
|
|
pub aliases: Vec<String>,
|
2019-12-11 17:08:48 -08:00
|
|
|
pub dependencies: Vec<Dependency>,
|
2021-09-16 06:44:40 -07:00
|
|
|
pub lines: Vec<Line>,
|
|
|
|
pub private: bool,
|
|
|
|
pub quiet: bool,
|
|
|
|
pub shebang: bool,
|
|
|
|
pub parameters: Vec<Parameter>,
|
2019-04-11 16:35:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Recipe {
|
2024-05-14 20:07:41 -07:00
|
|
|
fn new(recipe: &full::Recipe, aliases: Vec<String>) -> Self {
|
|
|
|
Self {
|
2019-04-11 16:35:16 -07:00
|
|
|
private: recipe.private,
|
|
|
|
shebang: recipe.shebang,
|
|
|
|
quiet: recipe.quiet,
|
2022-01-30 12:16:10 -08:00
|
|
|
dependencies: recipe.dependencies.iter().map(Dependency::new).collect(),
|
2019-12-11 17:08:48 -08:00
|
|
|
lines: recipe.body.iter().map(Line::new).collect(),
|
|
|
|
parameters: recipe.parameters.iter().map(Parameter::new).collect(),
|
2019-04-11 16:35:16 -07:00
|
|
|
aliases,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 23:58:08 -07:00
|
|
|
#[derive(Eq, PartialEq, Hash, Ord, PartialOrd, Debug, Clone)]
|
|
|
|
pub struct Parameter {
|
2021-09-16 06:44:40 -07:00
|
|
|
pub kind: ParameterKind,
|
|
|
|
pub name: String,
|
2020-06-18 11:50:47 -07:00
|
|
|
pub default: Option<Expression>,
|
2019-04-11 23:58:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Parameter {
|
2024-05-14 20:07:41 -07:00
|
|
|
fn new(parameter: &full::Parameter) -> Self {
|
|
|
|
Self {
|
2021-09-16 06:44:40 -07:00
|
|
|
kind: ParameterKind::new(parameter.kind),
|
|
|
|
name: parameter.name.lexeme().to_owned(),
|
2020-06-18 11:50:47 -07:00
|
|
|
default: parameter.default.as_ref().map(Expression::new),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
|
|
|
|
pub enum ParameterKind {
|
|
|
|
Singular,
|
|
|
|
Plus,
|
|
|
|
Star,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ParameterKind {
|
|
|
|
fn new(parameter_kind: full::ParameterKind) -> Self {
|
|
|
|
match parameter_kind {
|
|
|
|
full::ParameterKind::Singular => Self::Singular,
|
|
|
|
full::ParameterKind::Plus => Self::Plus,
|
|
|
|
full::ParameterKind::Star => Self::Star,
|
2019-04-11 23:58:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-11 16:35:16 -07:00
|
|
|
#[derive(Eq, PartialEq, Hash, Ord, PartialOrd, Debug, Clone)]
|
|
|
|
pub struct Line {
|
|
|
|
pub fragments: Vec<Fragment>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Line {
|
2024-05-14 20:07:41 -07:00
|
|
|
fn new(line: &full::Line) -> Self {
|
|
|
|
Self {
|
2019-12-11 17:08:48 -08:00
|
|
|
fragments: line.fragments.iter().map(Fragment::new).collect(),
|
2019-04-11 16:35:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Eq, PartialEq, Hash, Ord, PartialOrd, Debug, Clone)]
|
|
|
|
pub enum Fragment {
|
|
|
|
Text { text: String },
|
|
|
|
Expression { expression: Expression },
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Fragment {
|
2024-05-14 20:07:41 -07:00
|
|
|
fn new(fragment: &full::Fragment) -> Self {
|
2019-04-11 16:35:16 -07:00
|
|
|
match fragment {
|
2024-05-14 20:07:41 -07:00
|
|
|
full::Fragment::Text { token } => Self::Text {
|
2019-11-07 10:55:15 -08:00
|
|
|
text: token.lexeme().to_owned(),
|
2019-04-11 16:35:16 -07:00
|
|
|
},
|
2024-05-14 20:07:41 -07:00
|
|
|
full::Fragment::Interpolation { expression } => Self::Expression {
|
2019-04-11 16:35:16 -07:00
|
|
|
expression: Expression::new(expression),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Eq, PartialEq, Hash, Ord, PartialOrd, Debug, Clone)]
|
|
|
|
pub struct Assignment {
|
2021-09-16 06:44:40 -07:00
|
|
|
pub exported: bool,
|
2019-04-11 16:35:16 -07:00
|
|
|
pub expression: Expression,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Assignment {
|
2024-05-14 20:07:41 -07:00
|
|
|
fn new(assignment: &full::Assignment) -> Self {
|
|
|
|
Self {
|
2021-09-16 06:44:40 -07:00
|
|
|
exported: assignment.export,
|
2019-12-11 17:08:48 -08:00
|
|
|
expression: Expression::new(&assignment.value),
|
2019-04-11 16:35:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Eq, PartialEq, Hash, Ord, PartialOrd, Debug, Clone)]
|
|
|
|
pub enum Expression {
|
2024-05-14 18:55:32 -07:00
|
|
|
Assert {
|
|
|
|
condition: Condition,
|
|
|
|
error: Box<Expression>,
|
|
|
|
},
|
2019-04-11 16:35:16 -07:00
|
|
|
Backtick {
|
|
|
|
command: String,
|
|
|
|
},
|
|
|
|
Call {
|
2021-09-16 06:44:40 -07:00
|
|
|
name: String,
|
2019-04-11 16:35:16 -07:00
|
|
|
arguments: Vec<Expression>,
|
|
|
|
},
|
2022-05-28 19:07:53 -07:00
|
|
|
Concatenation {
|
2019-04-11 16:35:16 -07:00
|
|
|
lhs: Box<Expression>,
|
|
|
|
rhs: Box<Expression>,
|
|
|
|
},
|
2020-10-26 18:16:42 -07:00
|
|
|
Conditional {
|
2021-09-16 06:44:40 -07:00
|
|
|
lhs: Box<Expression>,
|
|
|
|
rhs: Box<Expression>,
|
|
|
|
then: Box<Expression>,
|
2020-10-26 18:16:42 -07:00
|
|
|
otherwise: Box<Expression>,
|
2021-09-16 16:45:56 -07:00
|
|
|
operator: ConditionalOperator,
|
2020-10-26 18:16:42 -07:00
|
|
|
},
|
2022-06-25 02:39:06 -07:00
|
|
|
Join {
|
2022-09-11 00:48:02 -07:00
|
|
|
lhs: Option<Box<Expression>>,
|
2022-06-25 02:39:06 -07:00
|
|
|
rhs: Box<Expression>,
|
|
|
|
},
|
2019-04-11 16:35:16 -07:00
|
|
|
String {
|
|
|
|
text: String,
|
|
|
|
},
|
|
|
|
Variable {
|
|
|
|
name: String,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Expression {
|
2024-05-14 20:07:41 -07:00
|
|
|
fn new(expression: &full::Expression) -> Self {
|
2019-11-07 10:55:15 -08:00
|
|
|
use full::Expression::*;
|
2019-04-11 16:35:16 -07:00
|
|
|
match expression {
|
2024-05-14 18:55:32 -07:00
|
|
|
Assert {
|
|
|
|
condition: full::Condition { lhs, rhs, operator },
|
|
|
|
error,
|
|
|
|
} => Expression::Assert {
|
|
|
|
condition: Condition {
|
|
|
|
lhs: Box::new(Expression::new(lhs)),
|
|
|
|
rhs: Box::new(Expression::new(rhs)),
|
|
|
|
operator: ConditionalOperator::new(*operator),
|
|
|
|
},
|
|
|
|
error: Box::new(Expression::new(error)),
|
|
|
|
},
|
2024-05-14 20:07:41 -07:00
|
|
|
Backtick { contents, .. } => Self::Backtick {
|
2021-05-07 00:14:38 -07:00
|
|
|
command: (*contents).clone(),
|
2019-04-11 16:35:16 -07:00
|
|
|
},
|
2019-12-11 17:08:48 -08:00
|
|
|
Call { thunk } => match thunk {
|
2024-05-14 20:07:41 -07:00
|
|
|
full::Thunk::Nullary { name, .. } => Self::Call {
|
2021-09-16 06:44:40 -07:00
|
|
|
name: name.lexeme().to_owned(),
|
2019-12-11 17:08:48 -08:00
|
|
|
arguments: Vec::new(),
|
|
|
|
},
|
2024-05-14 20:07:41 -07:00
|
|
|
full::Thunk::Unary { name, arg, .. } => Self::Call {
|
2021-09-16 06:44:40 -07:00
|
|
|
name: name.lexeme().to_owned(),
|
2024-05-14 20:07:41 -07:00
|
|
|
arguments: vec![Self::new(arg)],
|
2019-12-11 17:08:48 -08:00
|
|
|
},
|
2023-06-13 05:49:46 -07:00
|
|
|
full::Thunk::UnaryOpt {
|
|
|
|
name,
|
|
|
|
args: (a, opt_b),
|
|
|
|
..
|
|
|
|
} => {
|
2024-05-14 20:07:41 -07:00
|
|
|
let mut arguments = Vec::new();
|
2023-06-13 05:49:46 -07:00
|
|
|
|
|
|
|
if let Some(b) = opt_b.as_ref() {
|
2024-05-14 20:07:41 -07:00
|
|
|
arguments.push(Self::new(b));
|
2023-06-13 05:49:46 -07:00
|
|
|
}
|
|
|
|
|
2024-05-14 20:07:41 -07:00
|
|
|
arguments.push(Self::new(a));
|
|
|
|
Self::Call {
|
2023-06-13 05:49:46 -07:00
|
|
|
name: name.lexeme().to_owned(),
|
|
|
|
arguments,
|
|
|
|
}
|
|
|
|
}
|
2019-12-11 17:08:48 -08:00
|
|
|
full::Thunk::Binary {
|
|
|
|
name, args: [a, b], ..
|
2024-05-14 20:07:41 -07:00
|
|
|
} => Self::Call {
|
2021-09-16 06:44:40 -07:00
|
|
|
name: name.lexeme().to_owned(),
|
2024-05-14 20:07:41 -07:00
|
|
|
arguments: vec![Self::new(a), Self::new(b)],
|
2019-12-11 17:08:48 -08:00
|
|
|
},
|
2021-10-14 17:00:58 -07:00
|
|
|
full::Thunk::BinaryPlus {
|
|
|
|
name,
|
|
|
|
args: ([a, b], rest),
|
|
|
|
..
|
|
|
|
} => {
|
2024-05-14 20:07:41 -07:00
|
|
|
let mut arguments = vec![Self::new(a), Self::new(b)];
|
2021-10-14 17:00:58 -07:00
|
|
|
for arg in rest {
|
2024-05-14 20:07:41 -07:00
|
|
|
arguments.push(Self::new(arg));
|
2021-10-14 17:00:58 -07:00
|
|
|
}
|
2024-05-14 20:07:41 -07:00
|
|
|
Self::Call {
|
2021-10-14 17:00:58 -07:00
|
|
|
name: name.lexeme().to_owned(),
|
|
|
|
arguments,
|
|
|
|
}
|
|
|
|
}
|
2021-07-03 12:39:45 -07:00
|
|
|
full::Thunk::Ternary {
|
|
|
|
name,
|
|
|
|
args: [a, b, c],
|
|
|
|
..
|
2024-05-14 20:07:41 -07:00
|
|
|
} => Self::Call {
|
2021-09-16 06:44:40 -07:00
|
|
|
name: name.lexeme().to_owned(),
|
2024-05-14 20:07:41 -07:00
|
|
|
arguments: vec![Self::new(a), Self::new(b), Self::new(c)],
|
2021-07-03 12:39:45 -07:00
|
|
|
},
|
2019-04-11 16:35:16 -07:00
|
|
|
},
|
2024-05-14 20:07:41 -07:00
|
|
|
Concatenation { lhs, rhs } => Self::Concatenation {
|
|
|
|
lhs: Self::new(lhs).into(),
|
|
|
|
rhs: Self::new(rhs).into(),
|
2019-04-11 16:35:16 -07:00
|
|
|
},
|
2024-05-14 20:07:41 -07:00
|
|
|
Join { lhs, rhs } => Self::Join {
|
|
|
|
lhs: lhs.as_ref().map(|lhs| Self::new(lhs).into()),
|
|
|
|
rhs: Self::new(rhs).into(),
|
2022-06-25 02:39:06 -07:00
|
|
|
},
|
2020-10-26 18:16:42 -07:00
|
|
|
Conditional {
|
2024-05-14 18:55:32 -07:00
|
|
|
condition: full::Condition { lhs, rhs, operator },
|
2021-09-16 16:45:56 -07:00
|
|
|
otherwise,
|
2020-10-26 18:16:42 -07:00
|
|
|
then,
|
2024-05-14 20:07:41 -07:00
|
|
|
} => Self::Conditional {
|
|
|
|
lhs: Self::new(lhs).into(),
|
2021-09-16 16:45:56 -07:00
|
|
|
operator: ConditionalOperator::new(*operator),
|
2024-05-14 20:07:41 -07:00
|
|
|
otherwise: Self::new(otherwise).into(),
|
|
|
|
rhs: Self::new(rhs).into(),
|
|
|
|
then: Self::new(then).into(),
|
2020-10-26 18:16:42 -07:00
|
|
|
},
|
2024-05-14 20:07:41 -07:00
|
|
|
StringLiteral { string_literal } => Self::String {
|
2021-05-07 00:14:38 -07:00
|
|
|
text: string_literal.cooked.clone(),
|
2019-04-11 16:35:16 -07:00
|
|
|
},
|
2024-05-14 20:07:41 -07:00
|
|
|
Variable { name, .. } => Self::Variable {
|
2019-11-07 10:55:15 -08:00
|
|
|
name: name.lexeme().to_owned(),
|
2019-04-11 16:35:16 -07:00
|
|
|
},
|
2024-05-14 20:07:41 -07:00
|
|
|
Group { contents } => Self::new(contents),
|
2019-12-11 17:08:48 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-14 18:55:32 -07:00
|
|
|
#[derive(Eq, PartialEq, Hash, Ord, PartialOrd, Debug, Clone)]
|
|
|
|
pub struct Condition {
|
|
|
|
lhs: Box<Expression>,
|
|
|
|
rhs: Box<Expression>,
|
|
|
|
operator: ConditionalOperator,
|
|
|
|
}
|
|
|
|
|
2021-09-16 16:45:56 -07:00
|
|
|
#[derive(Eq, PartialEq, Hash, Ord, PartialOrd, Debug, Clone)]
|
|
|
|
pub enum ConditionalOperator {
|
|
|
|
Equality,
|
|
|
|
Inequality,
|
|
|
|
RegexMatch,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ConditionalOperator {
|
|
|
|
fn new(operator: full::ConditionalOperator) -> Self {
|
|
|
|
match operator {
|
|
|
|
full::ConditionalOperator::Equality => Self::Equality,
|
|
|
|
full::ConditionalOperator::Inequality => Self::Inequality,
|
|
|
|
full::ConditionalOperator::RegexMatch => Self::RegexMatch,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 17:08:48 -08:00
|
|
|
#[derive(Eq, PartialEq, Hash, Ord, PartialOrd, Debug, Clone)]
|
|
|
|
pub struct Dependency {
|
2021-09-16 06:44:40 -07:00
|
|
|
pub recipe: String,
|
2019-12-11 17:08:48 -08:00
|
|
|
pub arguments: Vec<Expression>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Dependency {
|
2021-09-16 16:45:56 -07:00
|
|
|
fn new(dependency: &full::Dependency) -> Self {
|
|
|
|
Self {
|
2021-09-16 06:44:40 -07:00
|
|
|
recipe: dependency.recipe.name().to_owned(),
|
2019-12-11 17:08:48 -08:00
|
|
|
arguments: dependency.arguments.iter().map(Expression::new).collect(),
|
2019-04-11 16:35:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|