Skip to content

Commit

Permalink
Fix bug with recipes that call other recipes
Browse files Browse the repository at this point in the history
I didn't have any examples of recipes calling other recipes, which was
the only way that the path that had the old execute logic would happen.
Now there's only a single way to execute things and commands properly
flow through from nested recipes.
  • Loading branch information
naddeoa committed Jan 9, 2024
1 parent 6815320 commit f920491
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 34 deletions.
3 changes: 0 additions & 3 deletions booty/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
from booty.validation import validate


_REFRESH_RATE = 8


@dataclass
class StatusResult:
missing: List[str] = field(default_factory=list)
Expand Down
33 changes: 2 additions & 31 deletions booty/types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from collections.abc import Generator
from dataclasses import dataclass, field
import re
import subprocess
from typing import Dict, List, Literal, Sequence, Union


Expand Down Expand Up @@ -59,32 +58,6 @@ class RecipeDefinition:
defs: Dict[str, List[Executable]] = field(default_factory=dict)
parameters: Sequence[str] = field(default_factory=list)

def setup(self, args: Sequence[Sequence[str]], recipes: Dict[str, "RecipeDefinition"]) -> None:
"""
Execute the setup method of the recipe.
"""
setup = self.defs["setup"]
self._execute(args, setup, recipes, method="setup")

def is_setup(self, args: Sequence[Sequence[str]], recipes: Dict[str, "RecipeDefinition"]) -> None:
"""
Check if the recipe is setup.
"""
# next up, string templating with the def/parameters, assuming the order of parameters matches the order of args

is_setup = self.defs["is_setup"]
self._execute(args, is_setup, recipes, method="is_setup")

def _execute(
self,
args: Sequence[Sequence[str]],
executables: List[Executable],
recipes: Dict[str, "RecipeDefinition"],
method: Literal["setup", "is_setup"],
) -> None:
for command in self.iter_commands(args, executables, recipes, method):
subprocess.run(["bash", "-c", command], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)

def get_setup_commands(self, args: Sequence[Sequence[str]], recipes: Dict[str, "RecipeDefinition"]) -> List[str]:
return list(self.iter_commands(args, self.defs["setup"], recipes, method="setup"))

Expand All @@ -105,11 +78,9 @@ def iter_commands(
final_command = self._substitute_arg_values(command, self.parameters, args)
yield final_command
else:
# this path ends up happening when executing a recipe that calls another recipe.
recipe = recipes[executable.name]
if method == "setup":
recipe.setup(executable.args, recipes)
else:
recipe.is_setup(executable.args, recipes)
yield from recipe.iter_commands(executable.args, recipe.defs[method], recipes, method=method)

def _substitute_arg_values(self, command: str, arg_names: Sequence[str], args: Sequence[Sequence[str]]) -> str:
"""
Expand Down
13 changes: 13 additions & 0 deletions examples/always_reinstalls.booty
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@

recipe first():
setup: sleep .1
is_setup:
sleep .1
false

recipe calls_first():
setup: first()
is_setup: first()


first_target: calls_first()

recipe always_install():
setup: sleep .1
is_setup:
Expand Down
19 changes: 19 additions & 0 deletions examples/install.booty
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,22 @@ frog:
setup: raco pkg install --auto frog
is_setup: raco pkg show frog | grep Package




##
## Silly test cases that aren't real
##
recipe first():
setup: sleep .1
is_setup:
sleep .1
false

# recipe that calls another recipe
recipe calls_first():
setup: first()
is_setup: first()


first_target: calls_first()

0 comments on commit f920491

Please sign in to comment.