mirror of
https://github.com/daylinmorgan/task.mk.git
synced 2024-10-07 17:28:41 -05:00
feat: add experimental support for phonify
This commit is contained in:
parent
cc198afad3
commit
25666e8ef4
13 changed files with 114 additions and 76 deletions
|
@ -7,4 +7,5 @@ endef
|
|||
|
||||
EPILOG = \nfor more info: gh.dayl.in/task.mk
|
||||
PRINT_VARS := VERSION SHELL
|
||||
PHONIFY = 1
|
||||
|
||||
|
|
20
Makefile
20
Makefile
|
@ -4,25 +4,21 @@ TEMPLATES := $(shell find src/ -type f)
|
|||
msg = $(if $(tprint),$(call tprint,{a.bold}==> {a.magenta}$(1){a.end}),@echo '==> $(1)')
|
||||
|
||||
### task.mk development |> -d -ms b_green --align center
|
||||
.PHONY: bootstrap env hooks
|
||||
bootstrap: env hooks ## generate local dev environment |> -ms b_magenta -gs b_cyan
|
||||
env:
|
||||
env: ##
|
||||
$(call msg,Bootstrapping Environment)
|
||||
@mamba create -p ./env python jinja2 black -y
|
||||
@mamba run -p ./env pip install yartsu
|
||||
hooks:
|
||||
hooks: ##
|
||||
@git config core.hooksPath .githooks
|
||||
docs-env:
|
||||
docs-env: ##
|
||||
@mamba run -p ./env pip install mkdocs-material mkdocs-git-revision-date-localized-plugin
|
||||
|
||||
.PHONY: l lint
|
||||
l lint: ## lint the python
|
||||
$(call msg,Linting)
|
||||
@black generate.py
|
||||
@black src/*.py --fast
|
||||
|
||||
|
||||
.PHONY: assets
|
||||
assets: ## generate assets
|
||||
@yartsu -o assets/help.svg -t "make help" -- make --no-print-directory help
|
||||
|
||||
|
@ -35,14 +31,12 @@ git commit -m "release: $(VERSION)" --no-verify
|
|||
git tag $(VERSION)
|
||||
endef
|
||||
|
||||
.PHONY: release
|
||||
release: version-check ## release new version of task.mk
|
||||
$(call msg,Release Project)
|
||||
$(call tbash,release_sh)
|
||||
|
||||
.PHONY: clean
|
||||
c clean: ## remove the generated files
|
||||
@rm -f task.mk .task.mk
|
||||
@rm -f .task.mk
|
||||
|
||||
define version_check_sh
|
||||
if git rev-parse -q --verify "refs/tags/${VERSION}" >/dev/null; then
|
||||
|
@ -56,13 +50,11 @@ elif [[ $(shell echo "${VERSION}" | awk -F. '{ print NF }') -lt 3 ]];then\
|
|||
fi
|
||||
endef
|
||||
|
||||
.PHONY: version-check
|
||||
version-check:
|
||||
version-check: ##
|
||||
@$(call tprint,>> version: {a.green}$(VERSION){a.end})
|
||||
@$(call tbash,version_check_sh)
|
||||
@$(call tprint,>> {a.green}VERSION LOOKS GOOD!{a.end})
|
||||
|
||||
.PHONY: task
|
||||
info: ## demonstrate usage of tprint
|
||||
$(call msg,Info Message)
|
||||
$(call tprint,{a.black_on_cyan}This is task-print output:{a.end})
|
||||
|
@ -74,5 +66,5 @@ task.mk: $(TEMPLATES) generate.py
|
|||
|
||||
-include .task.cfg.mk .task.mk
|
||||
.task.mk: $(TEMPLATES) generate.py
|
||||
$(call msg,re-jinjaing the local .task.mk)
|
||||
$(call msg,re-jinjaing the local {a.b_cyan}.task.mk{a.end})
|
||||
@./generate.py $(VERSION) > .task.mk || (echo "generator failed!!" && rm .task.mk)
|
||||
|
|
|
@ -11,6 +11,8 @@ py_script_names = [
|
|||
"vars",
|
||||
"confirm",
|
||||
"utils",
|
||||
"phonify",
|
||||
"parsers",
|
||||
]
|
||||
|
||||
|
||||
|
|
|
@ -26,5 +26,3 @@ markdown_extensions:
|
|||
- pymdownx.inlinehilite
|
||||
- pymdownx.snippets
|
||||
- pymdownx.superfences
|
||||
|
||||
|
||||
|
|
|
@ -17,16 +17,19 @@ endif
|
|||
### task.mk builtins: |> -d --hidden
|
||||
_print-ansi: ## show all possible ansi color code combinations
|
||||
$(call py,print_ansi_py)
|
||||
_update-task.mk: ## downloads version of task.mk (TASKMK_VERSION=)
|
||||
$(call tprint,{a.b_cyan}Updating task.mk{a.end})
|
||||
curl https://raw.githubusercontent.com/daylinmorgan/task.mk/$(TASKMK_VERSION)/task.mk -o .task.mk
|
||||
# functions to take f-string literals and pass to python print
|
||||
tprint = $(call py,print_py,$(1))
|
||||
tprint-verbose= $(call py-verbose,print_py,$(1))
|
||||
tconfirm = $(call py,confirm_py,$(1))
|
||||
_update-task.mk: ## downloads version of task.mk (TASKMK_VERSION=)
|
||||
$(call tprint,{a.b_cyan}Updating task.mk{a.end})
|
||||
curl https://raw.githubusercontent.com/daylinmorgan/task.mk/$(TASKMK_VERSION)/task.mk -o .task.mk
|
||||
.PHONY: h help _help _print-ansi _update-task.mk
|
||||
TASK_MAKEFILE_LIST := $(filter-out $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)),$(MAKEFILE_LIST))
|
||||
export MAKEFILE_LIST MAKE TASK_MAKEFILE_LIST
|
||||
ifndef INHERIT_SHELL
|
||||
SHELL := $(shell which bash)
|
||||
endif
|
||||
ifdef PHONIFY
|
||||
$(shell $(call py-verbose,phonify_py))
|
||||
endif
|
||||
|
|
|
@ -5,3 +5,4 @@ TASKMK_VERSION ?= ##- version -##
|
|||
# task.mk should be included at the bottom of your Makefile with `-include .task.mk`
|
||||
# See below for the standard configuration options that should be set prior to including this file.
|
||||
# You can update your .task.mk with `make _update-task.mk`
|
||||
# and initialize a repo with `bash <(curl -fsSL gh.dayl.in/task.mk/init)`.
|
||||
|
|
67
src/help.py
67
src/help.py
|
@ -3,43 +3,22 @@
|
|||
#% block script %#
|
||||
import argparse
|
||||
from collections import namedtuple
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
from textwrap import wrap
|
||||
|
||||
###-
|
||||
from utils import Ansi, cfg # this is just to trick the LSP during development
|
||||
###- LSP TRICK ONLY
|
||||
import sys, os
|
||||
from utils import Ansi, cfg
|
||||
from parsers import pattern, goal_pattern, gen_makefile, parse_help
|
||||
|
||||
# -###
|
||||
|
||||
##- '$(utils_py)' -##
|
||||
##- '$(parsers_py)' -##
|
||||
|
||||
a = ansi = Ansi(target="stdout")
|
||||
MaxLens = namedtuple("MaxLens", "goal msg")
|
||||
|
||||
###- double dollar signs to prevent make escaping them -###
|
||||
###- bets on how long until I break this regex? -###
|
||||
pattern = re.compile(
|
||||
r"""
|
||||
(?:
|
||||
^\#\#\#\s+ # <- raw message
|
||||
|
|
||||
^(?:
|
||||
(?:\#\#\s+)?
|
||||
(?P<goal>.*?)(?:\s+\|>|:.*?\#\#)\s+
|
||||
) # <- a custom goal or actual recipe
|
||||
)
|
||||
(?P<msg>.*?)?\s? # <- help text (optional)
|
||||
(?:\|>\s+
|
||||
(?P<msgargs>.*?)
|
||||
)? # <- style args (optional)
|
||||
$$
|
||||
""",
|
||||
re.X,
|
||||
)
|
||||
goal_pattern = re.compile(r"""^(?!#|\t)(.*):.*\n\t""", re.MULTILINE)
|
||||
|
||||
|
||||
def parseargs(argstring):
|
||||
parser = argparse.ArgumentParser()
|
||||
|
@ -56,28 +35,6 @@ def divider(len):
|
|||
return ansi.style(f" {cfg.div*len}", "div_style")
|
||||
|
||||
|
||||
def gen_makefile():
|
||||
makefile = ""
|
||||
for file in os.getenv("MAKEFILE_LIST", "").split():
|
||||
with open(file, "r") as f:
|
||||
makefile += f.read() + "\n\n"
|
||||
return makefile
|
||||
|
||||
|
||||
def parse_help(file, hidden=False):
|
||||
for line in file.splitlines():
|
||||
match = pattern.search(line)
|
||||
if match:
|
||||
if (
|
||||
not hidden
|
||||
and not os.getenv("SHOW_HIDDEN")
|
||||
and str(match.groupdict().get("goal")).startswith("_")
|
||||
):
|
||||
pass
|
||||
else:
|
||||
yield {k: v for k, v in match.groupdict().items() if v is not None}
|
||||
|
||||
|
||||
def recipe_help_header(goal):
|
||||
item = [
|
||||
i
|
||||
|
@ -147,6 +104,10 @@ def fmt_goal(goal, msg, max_goal_len, argstr):
|
|||
args = parseargs(argstr)
|
||||
goal_style = args.goal_style.strip() if args.goal_style else "goal"
|
||||
msg_style = args.msg_style.strip() if args.msg_style else "msg"
|
||||
# TODO: refactor this to be closer to parse_goal?
|
||||
if not msg or (not os.getenv("SHOW_HIDDEN") and args.hidden):
|
||||
return
|
||||
|
||||
return (
|
||||
ansi.style(f" {goal:>{max_goal_len}}", goal_style)
|
||||
+ f" $(HELP_SEP) "
|
||||
|
@ -189,11 +150,11 @@ def print_help():
|
|||
)
|
||||
for item in items:
|
||||
if "goal" in item:
|
||||
lines.append(
|
||||
fmt_goal(
|
||||
item["goal"], item["msg"], maxlens.goal, item.get("msgargs", "")
|
||||
)
|
||||
newgoal = fmt_goal(
|
||||
item["goal"], item["msg"], maxlens.goal, item.get("msgargs", "")
|
||||
)
|
||||
if newgoal:
|
||||
lines.append(newgoal)
|
||||
else:
|
||||
lines.extend(fmt_rawmsg(item["msg"], item.get("msgargs", ""), maxlens))
|
||||
lines.append(cfg.epilog)
|
||||
|
|
59
src/parsers.py
Normal file
59
src/parsers.py
Normal file
|
@ -0,0 +1,59 @@
|
|||
#% extends "py-script.mk" %#
|
||||
#% block name %#parsers#% endblock %#
|
||||
#% block script %#
|
||||
import re
|
||||
|
||||
###- LSP TRICK ONLY
|
||||
import os, sys
|
||||
|
||||
# -###
|
||||
|
||||
##- '$(utils_py)' -##
|
||||
|
||||
###- double dollar signs to prevent make escaping them -###
|
||||
###- bets on how long until I break this regex? -###
|
||||
pattern = re.compile(
|
||||
r"""
|
||||
(?:
|
||||
^\#\#\#\s+ # <- raw message
|
||||
|
|
||||
^(?:
|
||||
(?:\#\#\s+)?
|
||||
(?P<goal>.*?)(?:\s+\|>|:.*?\#\#)\s?
|
||||
) # <- a custom goal or actual recipe
|
||||
)
|
||||
(?P<msg>.*?)?\s? # <- help text (optional)
|
||||
(?:\|>\s+
|
||||
(?P<msgargs>.*?)
|
||||
)? # <- style args (optional)
|
||||
$$
|
||||
""",
|
||||
re.X,
|
||||
)
|
||||
|
||||
goal_pattern = re.compile(r"""^(?!#|\t)(.*):.*\n\t""", re.MULTILINE)
|
||||
|
||||
|
||||
def gen_makefile():
|
||||
makefile = ""
|
||||
for file in os.getenv("MAKEFILE_LIST", "").split():
|
||||
with open(file, "r") as f:
|
||||
makefile += f.read() + "\n\n"
|
||||
return makefile
|
||||
|
||||
|
||||
def parse_help(file, hidden=False):
|
||||
for line in file.splitlines():
|
||||
match = pattern.search(line)
|
||||
if match:
|
||||
if (
|
||||
not hidden
|
||||
and not os.getenv("SHOW_HIDDEN")
|
||||
and str(match.groupdict().get("goal")).startswith("_")
|
||||
):
|
||||
pass
|
||||
else:
|
||||
yield {k: v for k, v in match.groupdict().items() if v is not None}
|
||||
|
||||
|
||||
#% endblock %#
|
17
src/phonify.py
Normal file
17
src/phonify.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
#% extends "py-script.mk" %#
|
||||
#% block name %#phonify#% endblock %#
|
||||
#% block script %#
|
||||
|
||||
##- '$(utils_py)' -##
|
||||
##- '$(parsers_py)' -##
|
||||
|
||||
|
||||
def main():
|
||||
items = " ".join((i["goal"] for i in parse_help(gen_makefile()) if "goal" in i))
|
||||
sys.stdout.write(".PHONY: " + items)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
#% endblock %#
|
|
@ -2,8 +2,11 @@
|
|||
#% block name %#print_ansi#% endblock %#
|
||||
#% block script %#
|
||||
##- '$(utils_py)' -##
|
||||
###- LSP TRICK ONLY
|
||||
import sys
|
||||
|
||||
# -###
|
||||
|
||||
codes_names = {
|
||||
getattr(ansi, attr): attr
|
||||
for attr in ansi.__dict__
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
#% include 'header.mk' %#
|
||||
#% include 'config.mk' %#
|
||||
#% include 'builtins.mk' %#
|
||||
#% include 'runners.mk' %#
|
||||
# ---- [python scripts] ---- #
|
||||
#%- for script in py_scripts %#
|
||||
##- script -##
|
||||
#%- endfor %#
|
||||
#% include 'runners.mk' %#
|
||||
#% include 'builtins.mk' %#
|
||||
|
|
|
@ -96,6 +96,7 @@ class Ansi:
|
|||
sys.exit(1)
|
||||
|
||||
return code + end
|
||||
|
||||
###- the below $() variables are injected by make -###
|
||||
def add_cfg(self):
|
||||
cfg_styles = {
|
||||
|
|
|
@ -12,8 +12,8 @@ from utils import Ansi
|
|||
|
||||
ansi = Ansi(target="stdout")
|
||||
###- $2 is a list of variables set by task.mk delimited with '<|>' -###
|
||||
task_vars = tuple(v.split('=') for v in "$2".split('<|>'))
|
||||
task_vars = tuple(v.split("=") for v in "$2".split("<|>"))
|
||||
length = max((len(v[0]) for v in task_vars))
|
||||
rows = (f" {ansi.params}{v[0]:<{length}}{ansi.end} = {v[1]}" for v in task_vars)
|
||||
print('\n'.join((f"{ansi.header}vars{ansi.end}:\n", *rows,'')))
|
||||
print("\n".join((f"{ansi.header}vars{ansi.end}:\n", *rows, "")))
|
||||
#% endblock %#
|
||||
|
|
Loading…
Reference in a new issue