mirror of
https://github.com/daylinmorgan/task.mk.git
synced 2024-12-22 01:50:44 -06:00
feat: add reusable config object
This commit is contained in:
parent
cc81782069
commit
bc4c95aab9
8 changed files with 75 additions and 23 deletions
|
@ -4,7 +4,14 @@ import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import jinja2
|
import jinja2
|
||||||
|
|
||||||
py_script_names = ["help", "ansi", "info", "print-ansi", "vars", "confirm"]
|
py_script_names = [
|
||||||
|
"help",
|
||||||
|
"info",
|
||||||
|
"print-ansi",
|
||||||
|
"vars",
|
||||||
|
"confirm",
|
||||||
|
"utils",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def get_jinja_env():
|
def get_jinja_env():
|
||||||
|
|
|
@ -4,10 +4,10 @@ ACCENT_STYLE ?= b_yellow
|
||||||
PARAMS_STYLE ?= $(ACCENT_STYLE)
|
PARAMS_STYLE ?= $(ACCENT_STYLE)
|
||||||
GOAL_STYLE ?= $(ACCENT_STYLE)
|
GOAL_STYLE ?= $(ACCENT_STYLE)
|
||||||
MSG_STYLE ?= faint
|
MSG_STYLE ?= faint
|
||||||
DIVIDER_STYLE ?= default
|
|
||||||
DIVIDER ?= ─
|
DIVIDER ?= ─
|
||||||
|
DIVIDER_STYLE ?= default
|
||||||
HELP_SEP ?= │
|
HELP_SEP ?= │
|
||||||
# python f-string literals
|
# python f-string literals
|
||||||
EPILOG ?=
|
EPILOG ?=
|
||||||
USAGE ?={ansi.$(HEADER_STYLE)}usage{ansi.end}:\n make <recipe>\n
|
USAGE ?={ansi.header}usage{ansi.end}:\n make <recipe>\n
|
||||||
INHERIT_SHELL ?=
|
INHERIT_SHELL ?=
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
##- '$(ansi_py)' -##
|
##- '$(utils_py)' -##
|
||||||
|
|
||||||
|
|
||||||
def confirm():
|
def confirm():
|
||||||
|
|
32
src/help.py
32
src/help.py
|
@ -9,7 +9,13 @@ import subprocess
|
||||||
import sys
|
import sys
|
||||||
from textwrap import wrap
|
from textwrap import wrap
|
||||||
|
|
||||||
##- '$(ansi_py)' -##
|
##- '$(utils_py)' -##
|
||||||
|
###-
|
||||||
|
# this is just to trick the LSP during development
|
||||||
|
from utils import ansi, cfg
|
||||||
|
|
||||||
|
# -###
|
||||||
|
|
||||||
|
|
||||||
MaxLens = namedtuple("MaxLens", "goal msg")
|
MaxLens = namedtuple("MaxLens", "goal msg")
|
||||||
|
|
||||||
|
@ -32,12 +38,12 @@ def parseargs(argstring):
|
||||||
|
|
||||||
|
|
||||||
def divider(len):
|
def divider(len):
|
||||||
return ansi.style(f" {'$(DIVIDER)'*len}", "$(DIVIDER_STYLE)")
|
return ansi.style(f" {cfg.div*len}", "div_style")
|
||||||
|
|
||||||
|
|
||||||
def gen_makefile():
|
def gen_makefile():
|
||||||
makefile = ""
|
makefile = ""
|
||||||
for file in os.getenv("MAKEFILE_LIST").split():
|
for file in os.getenv("MAKEFILE_LIST", "").split():
|
||||||
with open(file, "r") as f:
|
with open(file, "r") as f:
|
||||||
makefile += f.read() + "\n\n"
|
makefile += f.read() + "\n\n"
|
||||||
return makefile
|
return makefile
|
||||||
|
@ -71,7 +77,7 @@ def recipe_help_header(goal):
|
||||||
item[0].get("msgargs", ""),
|
item[0].get("msgargs", ""),
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
return f" {ansi.style(goal,'$(GOAL_STYLE)')}"
|
return f" {ansi.style(goal,'goal')}"
|
||||||
|
|
||||||
|
|
||||||
def get_goal_deps(goal="task.mk"):
|
def get_goal_deps(goal="task.mk"):
|
||||||
|
@ -82,7 +88,7 @@ def get_goal_deps(goal="task.mk"):
|
||||||
match = dep_pattern.search(line)
|
match = dep_pattern.search(line)
|
||||||
if match and match.groups()[0]:
|
if match and match.groups()[0]:
|
||||||
return wrap(
|
return wrap(
|
||||||
f"{ansi.style('deps','default')}: {ansi.style(match.groups()[0].strip(),'$(MSG_STYLE)')}",
|
f"{ansi.style('deps','default')}: {ansi.style(match.groups()[0].strip(),'msg')}",
|
||||||
initial_indent=" ",
|
initial_indent=" ",
|
||||||
subsequent_indent=" ",
|
subsequent_indent=" ",
|
||||||
)
|
)
|
||||||
|
@ -116,8 +122,8 @@ def parse_goal(file, goal):
|
||||||
|
|
||||||
def fmt_goal(goal, msg, max_goal_len, argstr):
|
def fmt_goal(goal, msg, max_goal_len, argstr):
|
||||||
args = parseargs(argstr)
|
args = parseargs(argstr)
|
||||||
goal_style = args.goal_style.strip() if args.goal_style else "$(GOAL_STYLE)"
|
goal_style = args.goal_style.strip() if args.goal_style else "goal"
|
||||||
msg_style = args.msg_style.strip() if args.msg_style else "$(MSG_STYLE)"
|
msg_style = args.msg_style.strip() if args.msg_style else "msg"
|
||||||
return (
|
return (
|
||||||
ansi.style(f" {goal:>{max_goal_len}}", goal_style)
|
ansi.style(f" {goal:>{max_goal_len}}", goal_style)
|
||||||
+ f" $(HELP_SEP) "
|
+ f" $(HELP_SEP) "
|
||||||
|
@ -128,20 +134,20 @@ def fmt_goal(goal, msg, max_goal_len, argstr):
|
||||||
def fmt_rawmsg(msg, argstr, maxlens):
|
def fmt_rawmsg(msg, argstr, maxlens):
|
||||||
args = parseargs(argstr)
|
args = parseargs(argstr)
|
||||||
lines = []
|
lines = []
|
||||||
msg_style = args.msg_style.strip() if args.msg_style else "$(MSG_STYLE)"
|
msg_style = args.msg_style.strip() if args.msg_style else "msg"
|
||||||
if not os.getenv("SHOW_HIDDEN") and args.hidden:
|
if not os.getenv("SHOW_HIDDEN") and args.hidden:
|
||||||
return []
|
return []
|
||||||
if msg:
|
if msg:
|
||||||
if args.align == "sep":
|
if args.align == "sep":
|
||||||
lines.append(
|
lines.append(
|
||||||
f"{' '*(maxlens.goal+len('$(HELP_SEP)')+4)}{ansi.style(msg,msg_style)}"
|
f"{' '*(maxlens.goal+len(cfg.sep)+4)}{ansi.style(msg,msg_style)}"
|
||||||
)
|
)
|
||||||
elif args.align == "center":
|
elif args.align == "center":
|
||||||
lines.append(f" {ansi.style(msg.center(sum(maxlens)),msg_style)}")
|
lines.append(f" {ansi.style(msg.center(sum(maxlens)),msg_style)}")
|
||||||
else:
|
else:
|
||||||
lines.append(f" {ansi.style(msg,msg_style)}")
|
lines.append(f" {ansi.style(msg,msg_style)}")
|
||||||
if args.divider:
|
if args.divider:
|
||||||
lines.append(divider(len("$(HELP_SEP)") + sum(maxlens) + 2))
|
lines.append(divider(len(cfg.sep) + sum(maxlens) + 2))
|
||||||
if args.whitespace:
|
if args.whitespace:
|
||||||
lines.append("\n")
|
lines.append("\n")
|
||||||
|
|
||||||
|
@ -149,7 +155,7 @@ def fmt_rawmsg(msg, argstr, maxlens):
|
||||||
|
|
||||||
|
|
||||||
def print_help():
|
def print_help():
|
||||||
lines = [f"""$(USAGE)"""]
|
lines = [cfg.usage]
|
||||||
|
|
||||||
items = list(parse_help(gen_makefile()))
|
items = list(parse_help(gen_makefile()))
|
||||||
maxlens = MaxLens(
|
maxlens = MaxLens(
|
||||||
|
@ -164,12 +170,12 @@ def print_help():
|
||||||
)
|
)
|
||||||
if "rawmsg" in item:
|
if "rawmsg" in item:
|
||||||
lines.extend(fmt_rawmsg(item["rawmsg"], item.get("rawargs", ""), maxlens))
|
lines.extend(fmt_rawmsg(item["rawmsg"], item.get("rawargs", ""), maxlens))
|
||||||
lines.append(f"""$(EPILOG)""")
|
lines.append(cfg.epilog)
|
||||||
print("\n".join(lines))
|
print("\n".join(lines))
|
||||||
|
|
||||||
|
|
||||||
def print_arg_help(help_args):
|
def print_arg_help(help_args):
|
||||||
print(f"{ansi.style('task.mk recipe help','$(HEADER_STYLE)')}\n")
|
print(f"{ansi.style('task.mk recipe help','header')}\n")
|
||||||
for arg in help_args.split():
|
for arg in help_args.split():
|
||||||
print("\n".join(parse_goal(gen_makefile(), arg)))
|
print("\n".join(parse_goal(gen_makefile(), arg)))
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#% extends "py-script.mk" %#
|
#% extends "py-script.mk" %#
|
||||||
#% block name %#info#% endblock %#
|
#% block name %#info#% endblock %#
|
||||||
#% block script %#
|
#% block script %#
|
||||||
##- '$(ansi_py)' -##
|
##- '$(utils_py)' -##
|
||||||
|
|
||||||
print(f"""$(2)""")
|
print(f"""$(2)""")
|
||||||
#% endblock %#
|
#% endblock %#
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#% extends "py-script.mk" %#
|
#% extends "py-script.mk" %#
|
||||||
#% block name %#print_ansi#% endblock %#
|
#% block name %#print_ansi#% endblock %#
|
||||||
#% block script %#
|
#% block script %#
|
||||||
##- '$(ansi_py)' -##
|
##- '$(utils_py)' -##
|
||||||
sep = f"$(HELP_SEP)"
|
sep = f"$(HELP_SEP)"
|
||||||
codes_names = {getattr(ansi, attr): attr for attr in ansi.__dict__}
|
codes_names = {getattr(ansi, attr): attr for attr in ansi.__dict__}
|
||||||
for code in sorted(codes_names.keys(), key=lambda item: (len(item), item)):
|
for code in sorted(codes_names.keys(), key=lambda item: (len(item), item)):
|
||||||
|
|
|
@ -1,8 +1,24 @@
|
||||||
#% extends "py-script.mk" %#
|
#% extends "py-script.mk" %#
|
||||||
#% block name %#ansi#% endblock %#
|
#% block name %#utils#% endblock %#
|
||||||
#% block script %#
|
#% block script %#
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Config:
|
||||||
|
header: str
|
||||||
|
accent: str
|
||||||
|
params: str
|
||||||
|
goal: str
|
||||||
|
msg: str
|
||||||
|
div: str
|
||||||
|
div_style: str
|
||||||
|
sep: str
|
||||||
|
epilog: str
|
||||||
|
usage: str
|
||||||
|
|
||||||
|
|
||||||
color2byte = dict(
|
color2byte = dict(
|
||||||
black=0,
|
black=0,
|
||||||
|
@ -77,6 +93,15 @@ class Ansi:
|
||||||
|
|
||||||
return code + end
|
return code + end
|
||||||
|
|
||||||
|
def add_cfg(self, cfg):
|
||||||
|
cfg_attrs = {
|
||||||
|
attr: getattr(cfg, attr)
|
||||||
|
for attr in cfg.__dict__
|
||||||
|
if attr not in ["div", "sep", "epilog", "usage"]
|
||||||
|
}
|
||||||
|
for name, cfg_attr in cfg_attrs.items():
|
||||||
|
self.setcode(name, getattr(ansi, cfg_attr))
|
||||||
|
|
||||||
def style(self, text, style):
|
def style(self, text, style):
|
||||||
if style not in self.__dict__:
|
if style not in self.__dict__:
|
||||||
print(f"unknown style: {style}")
|
print(f"unknown style: {style}")
|
||||||
|
@ -86,4 +111,18 @@ class Ansi:
|
||||||
|
|
||||||
|
|
||||||
a = ansi = Ansi()
|
a = ansi = Ansi()
|
||||||
|
|
||||||
|
cfg = Config(
|
||||||
|
"$(HEADER_STYLE)",
|
||||||
|
"$(ACCENT_STYLE)",
|
||||||
|
"$(PARAMS_STYLE)",
|
||||||
|
"$(GOAL_STYLE)",
|
||||||
|
"$(MSG_STYLE)",
|
||||||
|
"$(DIVIDER)",
|
||||||
|
"$(DIVIDER_STYLE)",
|
||||||
|
"$(HELP_SEP)",
|
||||||
|
f"""$(EPILOG)""",
|
||||||
|
f"""$(USAGE)""",
|
||||||
|
)
|
||||||
|
ansi.add_cfg(cfg)
|
||||||
#% endblock %#
|
#% endblock %#
|
|
@ -3,15 +3,15 @@
|
||||||
#% block script %#
|
#% block script %#
|
||||||
import os
|
import os
|
||||||
|
|
||||||
##- '$(ansi_py)' -##
|
##- '$(utils_py)' -##
|
||||||
|
|
||||||
vars = "$2".split()
|
vars = "$2".split()
|
||||||
length = max((len(v) for v in vars))
|
length = max((len(v) for v in vars))
|
||||||
|
|
||||||
print(f"{ansi.$(HEADER_STYLE)}vars:{ansi.end}\n")
|
print(f"{ansi.header}vars{ansi.end}:\n")
|
||||||
|
|
||||||
for v in vars:
|
for v in vars:
|
||||||
print(f" {ansi.b_magenta}{v:<{length}}{ansi.end} = {os.getenv(v)}")
|
print(f" {ansi.params}{v:<{length}}{ansi.end} = {os.getenv(v)}")
|
||||||
|
|
||||||
print()
|
print()
|
||||||
#% endblock %#
|
#% endblock %#
|
||||||
|
|
Loading…
Reference in a new issue