Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve recipe dependencies #547

Merged
merged 1 commit into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/alias_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ where
'a: 'b,
{
aliases: &'b Table<'a, Alias<'a>>,
recipes: &'b Table<'a, Recipe<'a>>,
recipes: &'b Table<'a, Rc<Recipe<'a>>>,
}

impl<'a: 'b, 'b> AliasResolver<'a, 'b> {
pub(crate) fn resolve_aliases(
aliases: &Table<'a, Alias<'a>>,
recipes: &Table<'a, Recipe<'a>>,
recipes: &Table<'a, Rc<Recipe<'a>>>,
) -> CompilationResult<'a, ()> {
let resolver = AliasResolver { aliases, recipes };

Expand Down
16 changes: 3 additions & 13 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::common::*;
use CompilationErrorKind::*;

pub(crate) struct Analyzer<'src> {
recipes: Table<'src, Recipe<'src>>,
recipes: Table<'src, Recipe<'src, Name<'src>>>,
assignments: Table<'src, Assignment<'src>>,
aliases: Table<'src, Alias<'src>>,
sets: Table<'src, Set<'src>>,
Expand Down Expand Up @@ -50,13 +50,12 @@ impl<'src> Analyzer<'src> {
}
}

let recipes = self.recipes;
let assignments = self.assignments;
let aliases = self.aliases;

AssignmentResolver::resolve_assignments(&assignments)?;

RecipeResolver::resolve_recipes(&recipes, &assignments)?;
let recipes = RecipeResolver::resolve_recipes(self.recipes, &assignments)?;

for recipe in recipes.values() {
for parameter in &recipe.parameters {
Expand All @@ -66,15 +65,6 @@ impl<'src> Analyzer<'src> {
}));
}
}

for dependency in &recipe.dependencies {
if !recipes[dependency.lexeme()].parameters.is_empty() {
return Err(dependency.error(DependencyHasParameters {
recipe: recipe.name(),
dependency: dependency.lexeme(),
}));
}
}
}

AliasResolver::resolve_aliases(&aliases, &recipes)?;
Expand All @@ -99,7 +89,7 @@ impl<'src> Analyzer<'src> {
})
}

fn analyze_recipe(&self, recipe: &Recipe<'src>) -> CompilationResult<'src, ()> {
fn analyze_recipe(&self, recipe: &Recipe<'src, Name<'src>>) -> CompilationResult<'src, ()> {
if let Some(original) = self.recipes.get(recipe.name.lexeme()) {
return Err(recipe.name.token().error(DuplicateRecipe {
recipe: original.name(),
Expand Down
13 changes: 7 additions & 6 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub(crate) use std::{
ops::{Index, Range, RangeInclusive},
path::{Path, PathBuf},
process::{self, Command},
rc::Rc,
str::{self, Chars},
sync::{Mutex, MutexGuard},
usize, vec,
Expand Down Expand Up @@ -50,12 +51,12 @@ pub(crate) use crate::{
assignment_evaluator::AssignmentEvaluator, assignment_resolver::AssignmentResolver, color::Color,
compilation_error::CompilationError, compilation_error_kind::CompilationErrorKind,
compiler::Compiler, config::Config, config_error::ConfigError, count::Count,
enclosure::Enclosure, expression::Expression, fragment::Fragment, function::Function,
function_context::FunctionContext, functions::Functions, interrupt_guard::InterruptGuard,
interrupt_handler::InterruptHandler, item::Item, justfile::Justfile, lexer::Lexer, line::Line,
list::List, load_error::LoadError, module::Module, name::Name, output_error::OutputError,
parameter::Parameter, parser::Parser, platform::Platform, position::Position,
positional::Positional, recipe::Recipe, recipe_context::RecipeContext,
dependency::Dependency, enclosure::Enclosure, expression::Expression, fragment::Fragment,
function::Function, function_context::FunctionContext, functions::Functions,
interrupt_guard::InterruptGuard, interrupt_handler::InterruptHandler, item::Item,
justfile::Justfile, lexer::Lexer, line::Line, list::List, load_error::LoadError, module::Module,
name::Name, output_error::OutputError, parameter::Parameter, parser::Parser, platform::Platform,
position::Position, positional::Positional, recipe::Recipe, recipe_context::RecipeContext,
recipe_resolver::RecipeResolver, runtime_error::RuntimeError, search::Search,
search_config::SearchConfig, search_error::SearchError, set::Set, setting::Setting,
settings::Settings, shebang::Shebang, show_whitespace::ShowWhitespace, state::State,
Expand Down
4 changes: 4 additions & 0 deletions src/dependency.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use crate::common::*;

#[derive(PartialEq, Debug)]
pub(crate) struct Dependency<'a>(pub(crate) Rc<Recipe<'a>>);
4 changes: 1 addition & 3 deletions src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ pub(crate) enum Expression<'src> {
/// `(contents)`
Group { contents: Box<Expression<'src>> },
/// `"string_literal"` or `'string_literal'`
StringLiteral {
string_literal: StringLiteral<'src>,
},
StringLiteral { string_literal: StringLiteral<'src> },
/// `variable`
Variable { name: Name<'src> },
}
Expand Down
2 changes: 1 addition & 1 deletion src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ use crate::common::*;
pub(crate) enum Item<'src> {
Alias(Alias<'src>),
Assignment(Assignment<'src>),
Recipe(Recipe<'src>),
Recipe(Recipe<'src, Name<'src>>),
Set(Set<'src>),
}
13 changes: 6 additions & 7 deletions src/justfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::common::*;

#[derive(Debug, PartialEq)]
pub(crate) struct Justfile<'src> {
pub(crate) recipes: Table<'src, Recipe<'src>>,
pub(crate) recipes: Table<'src, Rc<Recipe<'src>>>,
pub(crate) assignments: Table<'src, Assignment<'src>>,
pub(crate) aliases: Table<'src, Alias<'src>>,
pub(crate) settings: Settings<'src>,
Expand All @@ -11,7 +11,7 @@ pub(crate) struct Justfile<'src> {

impl<'src> Justfile<'src> {
pub(crate) fn first(&self) -> Option<&Recipe> {
let mut first: Option<&Recipe> = None;
let mut first: Option<&Recipe<Dependency>> = None;
for recipe in self.recipes.values() {
if let Some(first_recipe) = first {
if recipe.line_number() < first_recipe.line_number() {
Expand Down Expand Up @@ -166,7 +166,7 @@ impl<'src> Justfile<'src> {
if let Some(recipe) = self.recipes.get(name) {
Some(recipe)
} else if let Some(alias) = self.aliases.get(name) {
self.recipes.get(alias.target.lexeme())
self.recipes.get(alias.target.lexeme()).map(Rc::as_ref)
} else {
None
}
Expand All @@ -181,10 +181,9 @@ impl<'src> Justfile<'src> {
ran: &mut BTreeSet<&'src str>,
overrides: &BTreeMap<String, String>,
) -> RunResult<()> {
for dependency_name in &recipe.dependencies {
let lexeme = dependency_name.lexeme();
if !ran.contains(lexeme) {
self.run_recipe(context, &self.recipes[lexeme], &[], dotenv, ran, overrides)?;
for Dependency(dependency) in &recipe.dependencies {
if !ran.contains(dependency.name()) {
self.run_recipe(context, dependency, &[], dotenv, ran, overrides)?;
}
}
recipe.run(context, arguments, dotenv, overrides)?;
Expand Down
8 changes: 8 additions & 0 deletions src/keyed.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
use crate::common::*;

pub(crate) trait Keyed<'key> {
fn key(&self) -> &'key str;
}

impl<'key, T: Keyed<'key>> Keyed<'key> for Rc<T> {
fn key(&self) -> &'key str {
self.as_ref().key()
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ mod config;
mod config_error;
mod count;
mod default;
mod dependency;
mod empty;
mod enclosure;
mod error;
Expand Down
2 changes: 1 addition & 1 deletion src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl<'src> Node<'src> for Expression<'src> {
}
}

impl<'src> Node<'src> for Recipe<'src> {
impl<'src> Node<'src> for Recipe<'src, Name<'src>> {
fn tree(&self) -> Tree<'src> {
let mut t = Tree::atom("recipe");

Expand Down
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ impl<'tokens, 'src> Parser<'tokens, 'src> {
&mut self,
doc: Option<&'src str>,
quiet: bool,
) -> CompilationResult<'src, Recipe<'src>> {
) -> CompilationResult<'src, Recipe<'src, Name<'src>>> {
let name = self.parse_name()?;

let mut positional = Vec::new();
Expand Down
29 changes: 24 additions & 5 deletions src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ fn error_from_signal(

/// A recipe, e.g. `foo: bar baz`
#[derive(PartialEq, Debug)]
pub(crate) struct Recipe<'a> {
pub(crate) dependencies: Vec<Name<'a>>,
pub(crate) struct Recipe<'a, D = Dependency<'a>> {
pub(crate) dependencies: Vec<D>,
pub(crate) doc: Option<&'a str>,
pub(crate) body: Vec<Line<'a>>,
pub(crate) name: Name<'a>,
Expand All @@ -35,7 +35,7 @@ pub(crate) struct Recipe<'a> {
pub(crate) shebang: bool,
}

impl<'a> Recipe<'a> {
impl<'a, D> Recipe<'a, D> {
pub(crate) fn argument_range(&self) -> RangeInclusive<usize> {
self.min_arguments()..=self.max_arguments()
}
Expand Down Expand Up @@ -319,7 +319,26 @@ impl<'a> Recipe<'a> {
}
}

impl<'src> Keyed<'src> for Recipe<'src> {
impl<'src> Recipe<'src, Name<'src>> {
pub(crate) fn resolve(self, resolved: Vec<Dependency<'src>>) -> Recipe<'src> {
assert_eq!(self.dependencies.len(), resolved.len());
for (name, resolved) in self.dependencies.iter().zip(&resolved) {
assert_eq!(name.lexeme(), resolved.0.name.lexeme());
}
Recipe {
dependencies: resolved,
doc: self.doc,
body: self.body,
name: self.name,
parameters: self.parameters,
private: self.private,
quiet: self.quiet,
shebang: self.shebang,
}
}
}

impl<'src, D> Keyed<'src> for Recipe<'src, D> {
fn key(&self) -> &'src str {
self.name.lexeme()
}
Expand All @@ -342,7 +361,7 @@ impl<'a> Display for Recipe<'a> {
}
write!(f, ":")?;
for dependency in &self.dependencies {
write!(f, " {}", dependency)?;
write!(f, " {}", dependency.0.name())?;
}

for (i, line) in self.body.iter().enumerate() {
Expand Down
Loading