mirror of
https://github.com/daylinmorgan/task.mk.git
synced 2024-12-21 17:40:45 -06:00
refactor(regex): regex's are the enemy
updates the parsing syntax now the only acceptable delimiter is |>
This commit is contained in:
parent
3244dba8dc
commit
fd96a454e8
4 changed files with 61 additions and 78 deletions
19
Makefile
19
Makefile
|
@ -4,9 +4,8 @@ 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
|
||||
## bootstrap | generate local dev environment |> -ms b_magenta -gs b_cyan
|
||||
.PHONY: bootstrap env hooks
|
||||
bootstrap: env hooks
|
||||
bootstrap: env hooks ## generate local dev environment |> -ms b_magenta -gs b_cyan
|
||||
env:
|
||||
$(call msg,Bootstrapping Environment)
|
||||
@mamba create -p ./env python jinja2 black -y
|
||||
|
@ -16,16 +15,15 @@ hooks:
|
|||
docs-env:
|
||||
@mamba run -p ./env pip install mkdocs-material mkdocs-git-revision-date-localized-plugin
|
||||
|
||||
## l, lint | lint the python
|
||||
.PHONY: l lint
|
||||
l lint:
|
||||
l lint: ## lint the python
|
||||
$(call msg,Linting)
|
||||
@black generate.py
|
||||
@black src/*.py --fast
|
||||
|
||||
## assets | generate assets
|
||||
|
||||
.PHONY: assets
|
||||
assets:
|
||||
assets: ## generate assets
|
||||
@yartsu -o assets/help.svg -t "make help" -- make --no-print-directory help
|
||||
|
||||
define release_sh
|
||||
|
@ -36,15 +34,13 @@ git commit -m "release: $(VERSION)" --no-verify
|
|||
git tag $(VERSION)
|
||||
endef
|
||||
|
||||
## release | release new version of task.mk
|
||||
.PHONY: release
|
||||
release: version-check
|
||||
release: version-check ## release new version of task.mk
|
||||
$(call msg,Release Project)
|
||||
$(call tbash,release_sh)
|
||||
|
||||
## c, clean | remove the generated files
|
||||
.PHONY: clean
|
||||
c clean:
|
||||
c clean: ## remove the generated files
|
||||
@rm -f task.mk .task.mk
|
||||
|
||||
define version_check_sh
|
||||
|
@ -63,9 +59,8 @@ version-check:
|
|||
@$(call tbash,version_check_sh)
|
||||
@$(call tprint,>> {a.green}VERSION LOOKS GOOD!{a.end})
|
||||
|
||||
## info | demonstrate usage of tprint
|
||||
.PHONY: task
|
||||
info:
|
||||
info: ## demonstrate usage of tprint
|
||||
$(call msg,Info Message)
|
||||
$(call tprint,{a.black_on_cyan}This is task-print output:{a.end})
|
||||
$(call tprint,$(mlmsg))
|
||||
|
|
|
@ -3,8 +3,7 @@ ifeq (help,$(firstword $(MAKECMDGOALS)))
|
|||
HELP_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
|
||||
export HELP_ARGS
|
||||
endif
|
||||
## h, help | show this help
|
||||
h help:
|
||||
h help: ## show this help
|
||||
$(call py,help_py)
|
||||
_help: export SHOW_HIDDEN=true
|
||||
_help: help
|
||||
|
@ -14,17 +13,15 @@ $(foreach v,$(PRINT_VARS),$(eval export $(v)))
|
|||
vars v:
|
||||
$(call py,vars_py,$(PRINT_VARS))
|
||||
endif
|
||||
### | args: -ws --hidden
|
||||
### task.mk builtins: | args: -d --hidden
|
||||
## _print-ansi | show all possible ansi color code combinations
|
||||
_print-ansi:
|
||||
### |> -ws --hidden
|
||||
### task.mk builtins: |> -d --hidden
|
||||
_print-ansi: ## show all possible ansi color code combinations
|
||||
$(call py,print_ansi_py)
|
||||
# functions to take f-string literals and pass to python print
|
||||
tprint = $(call py,print_py,$(1))
|
||||
tprint-sh = $(call pysh,print_py,$(1))
|
||||
tconfirm = $(call py,confirm_py,$(1))
|
||||
## _update-task.mk | downloads latest development version of task.mk
|
||||
_update-task.mk:
|
||||
_update-task.mk: ## downloads latest development version of task.mk
|
||||
$(call tprint,{a.b_cyan}Updating task.mk{a.end})
|
||||
curl https://raw.githubusercontent.com/daylinmorgan/task.mk/main/task.mk -o .task.mk
|
||||
.PHONY: h help _help _print-ansi _update-task.mk
|
||||
|
|
48
src/help.py
48
src/help.py
|
@ -10,8 +10,7 @@ import sys
|
|||
from textwrap import wrap
|
||||
|
||||
###-
|
||||
# this is just to trick the LSP during development
|
||||
from utils import Ansi, cfg
|
||||
from utils import Ansi, cfg # this is just to trick the LSP during development
|
||||
|
||||
# -###
|
||||
##- '$(utils_py)' -##
|
||||
|
@ -20,29 +19,23 @@ a = ansi = Ansi(target="stdout")
|
|||
MaxLens = namedtuple("MaxLens", "goal msg")
|
||||
|
||||
###- double dollar signs to prevent make escaping them -###
|
||||
###- re.X requires all important whitespace is escaped -###
|
||||
###- bets on how long until I break this regex? -###
|
||||
pattern = re.compile(
|
||||
r"""
|
||||
^\#\#\
|
||||
(?P<goal>.*?)\s?\|\s?(?P<msg>.*?)
|
||||
\s?
|
||||
(?:
|
||||
(?:\|\s?args:\s?|\|>)
|
||||
\s?
|
||||
(?P<msgargs>.*?)
|
||||
)?
|
||||
$$
|
||||
|
|
||||
^\#\#\#\
|
||||
(?P<rawmsg>.*?)
|
||||
\s?
|
||||
(?:
|
||||
(?:\|\s?args:|\|\>)
|
||||
\s?
|
||||
(?P<rawargs>.*?)
|
||||
)?
|
||||
$$
|
||||
""",
|
||||
(?:
|
||||
^\#\#\#\s+
|
||||
|
|
||||
^(?:
|
||||
(?:\#\#\s+)?
|
||||
(?P<goal>.*?)(?:\s+\|>|:.*?\#\#)\s+
|
||||
)
|
||||
)
|
||||
(?P<msg>.*?)?\s?
|
||||
(?:\|>\s+
|
||||
(?P<msgargs>.*?)
|
||||
)?
|
||||
$$
|
||||
""",
|
||||
re.X,
|
||||
)
|
||||
goal_pattern = re.compile(r"""^(?!#|\t)(.*):.*\n\t""", re.MULTILINE)
|
||||
|
@ -189,7 +182,10 @@ def print_help():
|
|||
|
||||
items = list(parse_help(gen_makefile()))
|
||||
maxlens = MaxLens(
|
||||
*(max((len(item[x]) for item in items if x in item)) for x in ["goal", "msg"])
|
||||
*(
|
||||
max((*(len(item[x]) for item in items if x in item), 0))
|
||||
for x in ["goal", "msg"]
|
||||
)
|
||||
)
|
||||
for item in items:
|
||||
if "goal" in item:
|
||||
|
@ -198,8 +194,8 @@ def print_help():
|
|||
item["goal"], item["msg"], maxlens.goal, item.get("msgargs", "")
|
||||
)
|
||||
)
|
||||
if "rawmsg" in item:
|
||||
lines.extend(fmt_rawmsg(item["rawmsg"], item.get("rawargs", ""), maxlens))
|
||||
else:
|
||||
lines.extend(fmt_rawmsg(item["msg"], item.get("msgargs", ""), maxlens))
|
||||
lines.append(cfg.epilog)
|
||||
print("\n".join(lines))
|
||||
|
||||
|
|
59
task.mk
59
task.mk
|
@ -1,12 +1,13 @@
|
|||
# }> [github.com/daylinmorgan/task.mk] <{ #
|
||||
# Copyright (c) 2022 Daylin Morgan
|
||||
# MIT License
|
||||
# version: v22.9.28-8-g4fcab2adev
|
||||
# version: v22.9.28-12-g3244dbadev
|
||||
#
|
||||
# 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`
|
||||
# ---- [config] ---- #
|
||||
-include .task.cfg.mk
|
||||
HEADER_STYLE ?= b_cyan
|
||||
ACCENT_STYLE ?= b_yellow
|
||||
PARAMS_STYLE ?= $(ACCENT_STYLE)
|
||||
|
@ -25,8 +26,7 @@ ifeq (help,$(firstword $(MAKECMDGOALS)))
|
|||
HELP_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
|
||||
export HELP_ARGS
|
||||
endif
|
||||
## h, help | show this help
|
||||
h help:
|
||||
h help: ## show this help
|
||||
$(call py,help_py)
|
||||
_help: export SHOW_HIDDEN=true
|
||||
_help: help
|
||||
|
@ -36,17 +36,15 @@ $(foreach v,$(PRINT_VARS),$(eval export $(v)))
|
|||
vars v:
|
||||
$(call py,vars_py,$(PRINT_VARS))
|
||||
endif
|
||||
### | args: -ws --hidden
|
||||
### task.mk builtins: | args: -d --hidden
|
||||
## _print-ansi | show all possible ansi color code combinations
|
||||
_print-ansi:
|
||||
### |> -ws --hidden
|
||||
### task.mk builtins: |> -d --hidden
|
||||
_print-ansi: ## show all possible ansi color code combinations
|
||||
$(call py,print_ansi_py)
|
||||
# functions to take f-string literals and pass to python print
|
||||
tprint = $(call py,print_py,$(1))
|
||||
tprint-sh = $(call pysh,print_py,$(1))
|
||||
tconfirm = $(call py,confirm_py,$(1))
|
||||
## _update-task.mk | downloads latest development version of task.mk
|
||||
_update-task.mk:
|
||||
_update-task.mk: ## downloads latest development version of task.mk
|
||||
$(call tprint,{a.b_cyan}Updating task.mk{a.end})
|
||||
curl https://raw.githubusercontent.com/daylinmorgan/task.mk/main/task.mk -o .task.mk
|
||||
.PHONY: h help _help _print-ansi _update-task.mk
|
||||
|
@ -92,26 +90,20 @@ a = ansi = Ansi(target="stdout")
|
|||
MaxLens = namedtuple("MaxLens", "goal msg")
|
||||
pattern = re.compile(
|
||||
r"""
|
||||
^\#\#\
|
||||
(?P<goal>.*?)\s?\|\s?(?P<msg>.*?)
|
||||
\s?
|
||||
(?:
|
||||
(?:\|\s?args:\s?|\|>)
|
||||
\s?
|
||||
(?P<msgargs>.*?)
|
||||
)?
|
||||
$$
|
||||
|
|
||||
^\#\#\#\
|
||||
(?P<rawmsg>.*?)
|
||||
\s?
|
||||
(?:
|
||||
(?:\|\s?args:|\|\>)
|
||||
\s?
|
||||
(?P<rawargs>.*?)
|
||||
)?
|
||||
$$
|
||||
""",
|
||||
(?:
|
||||
^\#\#\#\s+
|
||||
|
|
||||
^(?:
|
||||
(?:\#\#\s+)?
|
||||
(?P<goal>.*?)(?:\s+\|>|:.*?\#\#)\s+
|
||||
)
|
||||
)
|
||||
(?P<msg>.*?)?\s?
|
||||
(?:\|>\s+
|
||||
(?P<msgargs>.*?)
|
||||
)?
|
||||
$$
|
||||
""",
|
||||
re.X,
|
||||
)
|
||||
goal_pattern = re.compile(r"""^(?!#|\t)(.*):.*\n\t""", re.MULTILINE)
|
||||
|
@ -234,7 +226,10 @@ def print_help():
|
|||
lines = [cfg.usage]
|
||||
items = list(parse_help(gen_makefile()))
|
||||
maxlens = MaxLens(
|
||||
*(max((len(item[x]) for item in items if x in item)) for x in ["goal", "msg"])
|
||||
*(
|
||||
max((*(len(item[x]) for item in items if x in item), 0))
|
||||
for x in ["goal", "msg"]
|
||||
)
|
||||
)
|
||||
for item in items:
|
||||
if "goal" in item:
|
||||
|
@ -243,8 +238,8 @@ def print_help():
|
|||
item["goal"], item["msg"], maxlens.goal, item.get("msgargs", "")
|
||||
)
|
||||
)
|
||||
if "rawmsg" in item:
|
||||
lines.extend(fmt_rawmsg(item["rawmsg"], item.get("rawargs", ""), maxlens))
|
||||
else:
|
||||
lines.extend(fmt_rawmsg(item["msg"], item.get("msgargs", ""), maxlens))
|
||||
lines.append(cfg.epilog)
|
||||
print("\n".join(lines))
|
||||
def print_arg_help(help_args):
|
||||
|
|
Loading…
Reference in a new issue