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

Fix bug indexing target defs when there are two recipes in a row #6

Merged
merged 4 commits into from
Dec 22, 2023
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
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 1.0.4
current_version = 1.0.5
tag = False
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\-(?P<release>[a-z]+)(?P<build>\d+))?
serialize =
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
branches: ["release"]

env:
VERSION: 1.0.4
VERSION: 1.0.5

jobs:
release:
Expand Down
19 changes: 16 additions & 3 deletions booty/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from dataclasses import dataclass, field
import time
from pprint import pprint
from typing import Any, Dict, Generator, List, Literal, cast

from booty.ast_util import get_dependencies, get_executable_index, get_recipe_definition_index
Expand All @@ -25,11 +26,11 @@ class StatusResult:


class App:
def __init__(self, config_path: str) -> None:
def __init__(self, config_path: str, debug: bool = False) -> None:
self.config_path = config_path
self.data = self.setup()
self.data = self.setup(debug)

def setup(self) -> SystemconfData:
def setup(self, debug: bool) -> SystemconfData:
"""
Parse the config file and create all of the indexes that we'll need to execute the booty.
Also runs validation.
Expand All @@ -38,11 +39,23 @@ def setup(self) -> SystemconfData:
config = f.read()

ast = parse(config)
if debug:
print("AST:")
print(ast.pretty())
stdlib_ast = parse(stdlib)
executables = get_executable_index(ast)
if debug:
print("Executables:")
pprint(executables)
dependencies = get_dependencies(ast, executables)
if debug:
print("Dependencies:")
pprint(dependencies)
G = get_dependency_graph(dependencies)
recipes = get_recipe_definition_index(ast)
if debug:
print("Recipes:")
pprint(recipes)
std_recipes = get_recipe_definition_index(stdlib_ast)
all_recipes = {**std_recipes, **recipes} # Make the user recipes overwrite the stdlib ones
conf = SystemconfData(execution_index=executables, recipe_index=all_recipes, G=G, ast=ast, dependency_index=dependencies)
Expand Down
74 changes: 47 additions & 27 deletions booty/ast_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,35 +112,55 @@ def get_executable_index(ast: ParseTree) -> ExecutableIndex:
for definition in target.find_data("def"):
for single_or_multi_def in definition.find_pred(lambda node: node.data == "single_line_def" or node.data == "multi_line_def"):
implements = find_token_value(single_or_multi_def, "IMPLEMENTS_NAME").strip()

executables[target_name][implements] = executables[target_name].get(implements, None) or []

for shell_or_recipe in single_or_multi_def.find_pred(
lambda node: node.data == "recipe_invocation" or node.data == "shell_line"
):
if isinstance(shell_or_recipe, Token):
raise Exception("This should be impossible")

recipe_parameters = list(shell_or_recipe.find_data("recipe_parameter_list"))
shell_lines = find_token_values(shell_or_recipe, "SHELL_LINE")

if len(recipe_parameters) > 0 and len(shell_lines) > 0:
raise Exception(
f"This should be impossible, they're mutually exclusive in the grammar: {recipe_parameters}\n{shell_lines}"
)

if len(shell_lines) > 0:
cmd = "\n".join([it for it in shell_lines])
shell_command = ShellCommand(cmd)
executables[target_name][implements].append(shell_command)
else:
recipe_name = find_token_value(single_or_multi_def, "NAME")

args: Sequence[Sequence[str]] = []
for parameter in recipe_parameters:
args.append(find_token_values(parameter, "INVOCATION_ARGS"))

recipe_invocation = RecipeInvocation(recipe_name, args)
executables[target_name][implements].append(recipe_invocation)
if single_or_multi_def.data == "multi_line_def":
for multiline_shell_or_recipe in single_or_multi_def.find_pred(
lambda node: node.data == "recipe_invocation" or node.data == "shell_line"
):
if multiline_shell_or_recipe.data == "shell_line":
cmd = "\n".join([it for it in find_token_values(multiline_shell_or_recipe, "SHELL_LINE")])
shell_command = ShellCommand(cmd)
executables[target_name][implements].append(shell_command)
else:
recipe_name = find_token_value(multiline_shell_or_recipe, "NAME")

args: Sequence[Sequence[str]] = []
for parameter in list(multiline_shell_or_recipe.find_data("recipe_parameter_list")):
args.append(find_token_values(parameter, "INVOCATION_ARGS"))

recipe_invocation = RecipeInvocation(recipe_name, args)
executables[target_name][implements].append(recipe_invocation)

else: # single line def
for shell_or_recipe in single_or_multi_def.find_pred(
lambda node: node.data == "recipe_invocation" or node.data == "shell_line"
):
if isinstance(shell_or_recipe, Token):
raise Exception("This should be impossible")

recipe_parameters = list(shell_or_recipe.find_data("recipe_parameter_list"))
shell_lines = find_token_values(shell_or_recipe, "SHELL_LINE")

if len(recipe_parameters) > 0 and len(shell_lines) > 0:
raise Exception(
f"This should be impossible, they're mutually exclusive in the grammar: {recipe_parameters}\n{shell_lines}"
)

if len(shell_lines) > 0:
cmd = "\n".join([it for it in shell_lines])
shell_command = ShellCommand(cmd)
executables[target_name][implements].append(shell_command)
else:
recipe_name = find_token_value(single_or_multi_def, "NAME")

args: Sequence[Sequence[str]] = []
for parameter in recipe_parameters:
args.append(find_token_values(parameter, "INVOCATION_ARGS"))

recipe_invocation = RecipeInvocation(recipe_name, args)
executables[target_name][implements].append(recipe_invocation)

# targets that just call a recipe, they don't define their logic inline
for recipe_invocation in target.find_data("recipe_invocation"):
Expand Down
5 changes: 4 additions & 1 deletion booty/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
@click.option("-c", "--config", type=str, required=False, help="Path to the install.sysc file", default="./install.booty")
@click.option("-s", "--status", type=bool, is_flag=True, required=False, help="Check the status of all known targets")
@click.option("-i", "--install", type=bool, is_flag=True, required=False, help="Install all uninstalled targets")
@click.option("-d", "--debug", type=bool, is_flag=True, required=False, help="See the AST of the config file")
@click.option("-y", "--yes", type=bool, is_flag=True, required=False, help="Don't prompt for confirmation")
def cli(config: str, yes: bool, status: bool = True, install: bool = False):
def cli(config: str, yes: bool, status: bool = True, install: bool = False, debug: bool = False):
# Make sure config exists
if not pathlib.Path(config).exists():
click.echo(f"Config file {config} does not exist. Use -c to specify the location of an install.booty file.")
sys.exit(1)

app = App(config, debug=debug)

if not install and not status:
install = True

Expand Down
7 changes: 6 additions & 1 deletion booty/lang/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
fi
done

recipe ppa(name):
setup:
sudo add-apt-repository $((name))
sudo apt update
is_setup: grep $((name)) /etc/apt/sources.list

recipe pipx(packages):
setup: pipx install $((packages))
is_setup: pipx list | grep $((packages))


recipe git(repo dist):
setup: git clone $((repo)) $((dist))
is_setup: test -d $((dist))
Expand Down
17 changes: 17 additions & 0 deletions examples/install.booty
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,20 @@ node:
is_setup:
fish -c "nvm exec --lts node --version"


##
## Racket
##

racket:
setup:
ppa(ppa:plt/racket)
apt(racket)

is_setup: which racket

frog -> racket
frog:
setup: raco pkg install frog
is_setup: raco pkg show frog | grep Package

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "booty-cli"
version = "1.0.4"
version = "1.0.5"
description = ""
authors = ["Anthony Naddeo <[email protected]>"]
license = "MIT"
Expand Down
Loading