From 2074d17a20b8575e5f5bdadc9fa2a6bdb6338a43 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Sun, 22 Sep 2019 08:38:28 -0400 Subject: [PATCH] improvements --- Bakefile | 4 +- bake/bakefile.py | 28 +++++------- bake/cli.py | 22 +--------- bake/constants.py | 15 +++++++ bake/scripts/stdlib.sh | 81 +++++++++++++++++++++++++++++++++++ examples/inter_shell/Bakefile | 9 ++-- examples/stdlib/Bakefile | 9 ++++ 7 files changed, 125 insertions(+), 43 deletions(-) create mode 100644 examples/stdlib/Bakefile diff --git a/Bakefile b/Bakefile index cf83d13..8ad031a 100644 --- a/Bakefile +++ b/Bakefile @@ -61,8 +61,8 @@ ci/setup: @skip:key=Pipfile.lock random/python: #!/usr/bin/env python - import site - print(site) + import requests + print(requests) diff --git a/bake/bakefile.py b/bake/bakefile.py index adc5689..abcf5df 100644 --- a/bake/bakefile.py +++ b/bake/bakefile.py @@ -223,27 +223,19 @@ class Bakefile: """The source of the 'root level' of the Bashfile.""" return "\n".join(list(self.iter_root_source_lines)) + def iter_funcs_source(self): + """The standard library.""" + p = os.path.join(os.path.dirname(__file__), "scripts", "stdlib.sh") + with open(p, "r") as f: + for i, line in enumerate(f.readlines()): + # Skip the shebang. + if i != 1: + yield line + @property def funcs_source(self): """Functions (_task_name), inserted into the Bash runtime.""" - source = [] - - # TODO: Recommend bake taskname instead. - # for task in self.tasks: - # task = self[task] - # f_name = task.name.replace("/", "_") - # f_name = f_name.replace("-", "_") - # f_name = f"task_{f_name}" - - # source.append( - # # Replace / namespacing with _ namespacing, for functions. - # f"{f_name}()" - # + " { \n" - # + f" bake --silent {task.name} $@;\n" - # + "}\n" - # + f"declare -x {f_name};" - # ) - + source = list(self.iter_funcs_source()) return "\n".join(source) diff --git a/bake/cli.py b/bake/cli.py index d7c861a..1a2f605 100644 --- a/bake/cli.py +++ b/bake/cli.py @@ -11,21 +11,7 @@ import pygments import pygments.lexers import pygments.formatters -SKIP_NEXT = False -SAFE_ENVIRONS = [ - "HOME", - "PATH", - "LANG", - "LOCALE", - "LANGUAGE", - "USER", - "TERM", - "VIRTUAL_ENV", - "BAKEFILE_PATH", - "PYTHONUNBUFFERED", - "PYTHONDONTWRITEBYTECODE", - "BAKE_SILENT", -] +from .constants import SKIP_NEXT, SAFE_ENVIRONS def indent(line): @@ -118,9 +104,6 @@ def echo_json(obj): @click.option( "--help", "-h", default=False, is_flag=True, help="Show this message and exit." ) -@click.option( - "--skip-done", default=False, is_flag=True, hidden=True, envvar="BAKE_SKIP_DONE" -) @click.option("--debug", default=False, is_flag=True, hidden=True) @click.option("--source", default=False, nargs=1, hidden=True) @click.option( @@ -201,7 +184,6 @@ def entrypoint( insecure, allow, _json, - skip_done, no_deps, interactive, yes, @@ -423,7 +405,7 @@ def entrypoint( for task in tasks: execute_task(task, silent=silent) - if not silent and not skip_done: + if not silent: click.echo( click.style(" + ", fg="white") + click.style("Done", fg="green") diff --git a/bake/constants.py b/bake/constants.py index 0750c0b..90c7f93 100644 --- a/bake/constants.py +++ b/bake/constants.py @@ -1 +1,16 @@ INDENT_STYLES = ("\t", " " * 4) +SKIP_NEXT = False +SAFE_ENVIRONS = [ + "HOME", + "PATH", + "LANG", + "LOCALE", + "LANGUAGE", + "USER", + "TERM", + "VIRTUAL_ENV", + "BAKEFILE_PATH", + "PYTHONUNBUFFERED", + "PYTHONDONTWRITEBYTECODE", + "BAKE_SILENT", +] diff --git a/bake/scripts/stdlib.sh b/bake/scripts/stdlib.sh index e69de29..01862aa 100644 --- a/bake/scripts/stdlib.sh +++ b/bake/scripts/stdlib.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash + +# ---------- +# - Colors - +# ---------- + +function bake_fg_color { + BLACK="\033[0;30m" + BLACK_BOLD="\033[1;30m" + WHITE="\033[0;37m" + WHITE_BOLD="\033[1;37m" + RED="\033[0;31m" + RED_BOLD="\033[1;31m" + GREEN="\033[0;32m" + GREEN_BOLD="\033[1;32m" + YELLOW="\033[0;33m" + YELLOW_BOLD="\033[1;33m" + BLUE="\033[0;34m" + BLUE_BOLD="\033[1;34m" + PURPLE="\033[0;35m" + PURPLE_BOLD="\033[1;35m" + CYAN="\033[0;36m" + CYAN_BOLD="\033[1;36m" + NO_COLOR="\033[0m" + + CHOSEN_COLOR="${1}" + ARGV_INPUT="${2}" + + COLOR="${!CHOSEN_COLOR}" + + if [ -z "$ARGV_INPUT" ]; then + read -r INPUT + else + INPUT="$ARGV_INPUT" + fi + echo -e "${COLOR}${INPUT}${NO_COLOR}" +} + +function bake_black { + bake_fg_color 'BLACK' "$1" +} + +function bake_white { + bake_fg_color 'WHITE' "$1" +} + +function bake_red { + bake_fg_color 'RED' "$1" +} + +function bake_green { + bake_fg_color 'GREEN' "$1" +} + +function bake_yellow { + bake_fg_color 'YELLOW' "$1" +} + +function bake_blue { + bake_fg_color 'BLUE' "$1" +} + +function bake_purple { + bake_fg_color 'PURPLE' "$1" +} + +function bake_cyan { + bake_fg_color 'CYAN' "$1" +} + +function red { + bake_fg_color 'RED' "$1" +} + +# ---------- +# - Indent - +# ---------- + +function bake_indent { + exit 0 +} diff --git a/examples/inter_shell/Bakefile b/examples/inter_shell/Bakefile index 2a37c4c..cb97abd 100644 --- a/examples/inter_shell/Bakefile +++ b/examples/inter_shell/Bakefile @@ -1,6 +1,9 @@ hello: - echo 'hi' - echo 'there' + sleep 1 + echo hello hello/reuse: - hello + bake hello + +hello/concurrent: + bake hello & bake hello diff --git a/examples/stdlib/Bakefile b/examples/stdlib/Bakefile new file mode 100644 index 0000000..4fde27c --- /dev/null +++ b/examples/stdlib/Bakefile @@ -0,0 +1,9 @@ +TEXT='Hi, there!' + +echo: echo/normal echo/red + +echo/normal: + echo "$TEXT" + +echo/red: + echo "$TEXT" | red