2022-09-05 18:14:50 -05:00
|
|
|
# }> [github.com/daylinmorgan/task.mk] <{ #
|
|
|
|
# Copyright (c) 2022 Daylin Morgan
|
|
|
|
# MIT License
|
2022-09-16 12:47:21 -05:00
|
|
|
# version: v22.9.14-8-gc3fa7cd-dev
|
2022-09-05 18:14:50 -05:00
|
|
|
#
|
2022-09-14 17:21:36 -05:00
|
|
|
# task.mk should be included at the bottom of your Makefile with `-include .task.mk`
|
2022-09-05 18:14:50 -05:00
|
|
|
# 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`
|
2022-09-16 12:47:21 -05:00
|
|
|
# ---- [config] ---- #
|
2022-09-05 18:14:50 -05:00
|
|
|
HEADER_COLOR ?= b_cyan
|
|
|
|
PARAMS_COLOR ?= b_magenta
|
|
|
|
ACCENT_COLOR ?= b_yellow
|
|
|
|
GOAL_COLOR ?= $(ACCENT_COLOR)
|
|
|
|
MSG_COLOR ?= faint
|
2022-09-13 16:40:07 -05:00
|
|
|
DIVIDER_COLOR ?= default
|
2022-09-14 00:06:25 -05:00
|
|
|
DIVIDER ?= ─
|
2022-09-14 00:40:17 -05:00
|
|
|
HELP_SEP ?= │
|
2022-09-05 18:14:50 -05:00
|
|
|
# python f-string literals
|
|
|
|
EPILOG ?=
|
2022-09-16 12:47:21 -05:00
|
|
|
USAGE ?={ansi.$(HEADER_COLOR)}usage{ansi.end}:\n make <recipe>
|
2022-09-05 18:14:50 -05:00
|
|
|
# ---- [buitlin recipes] ---- #
|
|
|
|
## h, help | show this help
|
|
|
|
.PHONY: help h
|
|
|
|
help h:
|
|
|
|
$(call py,help_py)
|
|
|
|
.PHONY: _help
|
|
|
|
_help: export SHOW_HIDDEN=true
|
|
|
|
_help: help
|
|
|
|
ifdef PRINT_VARS
|
|
|
|
$(foreach v,$(PRINT_VARS),$(eval export $(v)))
|
|
|
|
.PHONY: vars v
|
|
|
|
vars v:
|
|
|
|
$(call py,vars_py,$(PRINT_VARS))
|
|
|
|
endif
|
2022-09-14 17:15:46 -05:00
|
|
|
### | args: -ws --hidden
|
|
|
|
### task.mk builtins: | args: -d --hidden
|
2022-09-05 18:14:50 -05:00
|
|
|
## _print-ansi | show all possible ansi color code combinations
|
|
|
|
.PHONY:
|
|
|
|
_print-ansi:
|
|
|
|
$(call py,print_ansi_py)
|
|
|
|
# functions to take f-string literals and pass to python print
|
|
|
|
tprint = $(call py,info_py,$(1))
|
|
|
|
tprint-sh = $(call pysh,info_py,$(1))
|
2022-09-14 16:52:49 -05:00
|
|
|
tconfirm = $(call py,confirm_py,$(1))
|
2022-09-14 17:15:46 -05:00
|
|
|
## _update-task.mk | downloads latest development version of task.mk
|
2022-09-05 18:14:50 -05:00
|
|
|
_update-task.mk:
|
2022-09-14 17:15:46 -05:00
|
|
|
$(call tprint,{a.b_cyan}Updating task.mk{a.end})
|
2022-09-05 18:14:50 -05:00
|
|
|
curl https://raw.githubusercontent.com/daylinmorgan/task.mk/main/task.mk -o .task.mk
|
|
|
|
export MAKEFILE_LIST
|
|
|
|
# ---- [python/bash script runner] ---- #
|
|
|
|
define \n
|
|
|
|
|
|
|
|
|
|
|
|
endef
|
|
|
|
escape_shellstring = $(subst `,\`,$(subst ",\",$(subst $$,\$$,$(subst \,\\,$1))))
|
|
|
|
escape_printf = $(subst \,\\,$(subst %,%%,$1))
|
|
|
|
create_string = $(subst $(\n),\n,$(call escape_shellstring,$(call escape_printf,$1)))
|
2022-09-16 12:47:21 -05:00
|
|
|
printline = printf -- "<----------------------------------->\n"
|
2022-09-05 18:14:50 -05:00
|
|
|
ifdef DEBUG
|
2022-09-07 16:11:32 -05:00
|
|
|
define _debug_runner
|
2022-09-16 12:47:21 -05:00
|
|
|
@printf "$(1) Script:\n";$(printline);printf "$(call create_string,$(3))\n";$(printline)
|
2022-09-07 16:11:32 -05:00
|
|
|
@printf "$(call create_string,$(3))" | $(2)
|
2022-09-05 18:14:50 -05:00
|
|
|
endef
|
2022-09-07 16:11:32 -05:00
|
|
|
py = $(call _debug_runner,Python,python3,$($(1)))
|
|
|
|
tbash = $(call _debug_runner,Bash,bash,$($(1)))
|
2022-09-05 18:14:50 -05:00
|
|
|
else
|
2022-09-14 16:52:49 -05:00
|
|
|
py = @python3 <(printf "$(call create_string,$($(1)))")
|
|
|
|
tbash = @bash <(printf "$(call create_string,$($(1)))")
|
2022-09-05 18:14:50 -05:00
|
|
|
endif
|
2022-09-14 16:52:49 -05:00
|
|
|
pysh = python3 <(printf "$(call create_string,$($(1)))")
|
2022-09-05 18:14:50 -05:00
|
|
|
# ---- [python scripts] ---- #
|
|
|
|
define help_py
|
2022-09-13 16:40:07 -05:00
|
|
|
import argparse
|
|
|
|
from collections import namedtuple
|
2022-09-05 18:14:50 -05:00
|
|
|
import os
|
|
|
|
import re
|
2022-09-13 18:51:01 -05:00
|
|
|
from typing import Any
|
2022-09-05 18:14:50 -05:00
|
|
|
$(ansi_py)
|
2022-09-13 18:51:01 -05:00
|
|
|
ansi: Any
|
2022-09-13 16:40:07 -05:00
|
|
|
MaxLens = namedtuple("MaxLens", "goal msg")
|
|
|
|
# double dollar signs to prevent make escaping them
|
|
|
|
pattern = re.compile(
|
|
|
|
r"^## (?P<goal>.*) \| (?P<msg>.*)|^### (?P<rawmsg>.*?)?(?:\s?\| args: (?P<args>.*?))?$$"
|
|
|
|
)
|
|
|
|
def rawargs(argstring):
|
|
|
|
parser = argparse.ArgumentParser()
|
2022-09-14 00:37:16 -05:00
|
|
|
parser.add_argument("--align")
|
2022-09-13 16:40:07 -05:00
|
|
|
parser.add_argument("-d", "--divider", action="store_true")
|
2022-09-14 00:06:25 -05:00
|
|
|
parser.add_argument("-ws", "--whitespace", action="store_true")
|
2022-09-14 09:26:02 -05:00
|
|
|
parser.add_argument("-ms", "--msg-style", type=str)
|
2022-09-16 12:47:21 -05:00
|
|
|
parser.add_argument("--hidden", action="store_true")
|
2022-09-14 00:37:16 -05:00
|
|
|
return parser.parse_args(argstring.split())
|
2022-09-13 16:40:07 -05:00
|
|
|
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_make(file):
|
2022-09-05 18:14:50 -05:00
|
|
|
for line in file.splitlines():
|
|
|
|
match = pattern.search(line)
|
|
|
|
if match:
|
2022-09-13 16:40:07 -05:00
|
|
|
if not os.getenv("SHOW_HIDDEN") and str(
|
|
|
|
match.groupdict().get("goal")
|
|
|
|
).startswith("_"):
|
|
|
|
pass
|
2022-09-05 18:14:50 -05:00
|
|
|
else:
|
2022-09-13 16:40:07 -05:00
|
|
|
yield {k: v for k, v in match.groupdict().items() if v is not None}
|
|
|
|
def print_goal(goal, msg, max_goal_len):
|
|
|
|
print(
|
2022-09-13 18:51:01 -05:00
|
|
|
ansi.style(f" {goal:>{max_goal_len}}", "$(GOAL_COLOR)")
|
|
|
|
+ " $(HELP_SEP) "
|
|
|
|
+ ansi.style(msg, "$(MSG_COLOR)")
|
2022-09-13 16:40:07 -05:00
|
|
|
)
|
|
|
|
def print_rawmsg(msg, argstr, maxlens):
|
2022-09-14 00:37:16 -05:00
|
|
|
args = rawargs(argstr)
|
2022-09-14 09:26:02 -05:00
|
|
|
msg_style = args.msg_style if args.msg_style else "$(MSG_COLOR)"
|
2022-09-14 17:15:46 -05:00
|
|
|
if not os.getenv("SHOW_HIDDEN") and args.hidden:
|
|
|
|
return
|
2022-09-13 16:40:07 -05:00
|
|
|
if msg:
|
|
|
|
if args.align == "sep":
|
|
|
|
print(
|
2022-09-14 09:26:02 -05:00
|
|
|
f"{' '*(maxlens.goal+len('$(HELP_SEP)')+4)}{ansi.style(msg,msg_style)}"
|
2022-09-13 16:40:07 -05:00
|
|
|
)
|
|
|
|
elif args.align == "center":
|
2022-09-14 09:26:02 -05:00
|
|
|
print(f" {ansi.style(msg.center(sum(maxlens)),msg_style)}")
|
2022-09-13 16:40:07 -05:00
|
|
|
else:
|
2022-09-14 09:26:02 -05:00
|
|
|
print(f" {ansi.style(msg,msg_style)}")
|
2022-09-13 16:40:07 -05:00
|
|
|
if args.divider:
|
|
|
|
print(
|
2022-09-13 18:51:01 -05:00
|
|
|
ansi.style(
|
2022-09-14 00:06:25 -05:00
|
|
|
f" {'$(DIVIDER)'*(len('$(HELP_SEP)')+sum(maxlens)+2)}",
|
|
|
|
"$(DIVIDER_COLOR)",
|
2022-09-13 18:51:01 -05:00
|
|
|
)
|
2022-09-13 16:40:07 -05:00
|
|
|
)
|
2022-09-13 18:51:01 -05:00
|
|
|
if args.whitespace:
|
|
|
|
print()
|
2022-09-13 16:40:07 -05:00
|
|
|
def print_help():
|
|
|
|
print(f"""$(USAGE)""")
|
|
|
|
items = list(parse_make(gen_makefile()))
|
|
|
|
maxlens = MaxLens(
|
|
|
|
*(max((len(item[x]) for item in items if x in item)) for x in ["goal", "msg"])
|
2022-09-05 18:14:50 -05:00
|
|
|
)
|
2022-09-13 16:40:07 -05:00
|
|
|
for item in items:
|
|
|
|
if "goal" in item:
|
|
|
|
print_goal(item["goal"], item["msg"], maxlens.goal)
|
|
|
|
if "rawmsg" in item:
|
|
|
|
print_rawmsg(item["rawmsg"], item.get("args", ""), maxlens)
|
|
|
|
print(f"""$(EPILOG)""")
|
|
|
|
print_help()
|
2022-09-05 18:14:50 -05:00
|
|
|
endef
|
|
|
|
define ansi_py
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
color2byte = dict(
|
|
|
|
black=0,
|
|
|
|
red=1,
|
|
|
|
green=2,
|
|
|
|
yellow=3,
|
|
|
|
blue=4,
|
|
|
|
magenta=5,
|
|
|
|
cyan=6,
|
|
|
|
white=7,
|
|
|
|
)
|
|
|
|
state2byte = dict(
|
|
|
|
bold=1, faint=2, italic=3, underline=4, blink=5, fast_blink=6, crossed=9
|
|
|
|
)
|
|
|
|
def fg(byte):
|
|
|
|
return 30 + byte
|
|
|
|
def bg(byte):
|
|
|
|
return 40 + byte
|
|
|
|
class Ansi:
|
|
|
|
"""ANSI color codes"""
|
|
|
|
def __init__(self):
|
|
|
|
self.setcode("end", "\033[0m")
|
2022-09-13 16:40:07 -05:00
|
|
|
self.setcode("default", "\033[38m")
|
|
|
|
self.setcode("bg_default", "\033[48m")
|
2022-09-05 18:14:50 -05:00
|
|
|
for name, byte in color2byte.items():
|
|
|
|
self.setcode(name, f"\033[{fg(byte)}m")
|
|
|
|
self.setcode(f"b_{name}", f"\033[1;{fg(byte)}m")
|
|
|
|
self.setcode(f"d_{name}", f"\033[2;{fg(byte)}m")
|
|
|
|
for bgname, bgbyte in color2byte.items():
|
|
|
|
self.setcode(f"{name}_on_{bgname}", f"\033[{bg(bgbyte)};{fg(byte)}m")
|
|
|
|
for name, byte in state2byte.items():
|
|
|
|
self.setcode(name, f"\033[{byte}m")
|
2022-09-07 00:15:46 -05:00
|
|
|
def setcode(self, name, escape_code):
|
|
|
|
"""create attr for style and escape code"""
|
|
|
|
if not sys.stdout.isatty() or os.getenv("NO_COLOR", False):
|
|
|
|
setattr(self, name, "")
|
|
|
|
else:
|
|
|
|
setattr(self, name, escape_code)
|
|
|
|
def custom(self, fg=None, bg=None):
|
|
|
|
"""use custom color"""
|
|
|
|
code, end = "\033[", "m"
|
|
|
|
if fg:
|
|
|
|
if isinstance(fg, int):
|
|
|
|
code += f"38;5;{fg}"
|
|
|
|
elif (isinstance(fg, list) or isinstance(fg, tuple)) and len(fg) == 1:
|
|
|
|
code += f"38;5;{fg[0]}"
|
|
|
|
elif (isinstance(fg, list) or isinstance(fg, tuple)) and len(fg) == 3:
|
|
|
|
code += f"38;2;{';'.join((str(i) for i in fg))}"
|
|
|
|
else:
|
|
|
|
print("Expected one or three values for fg as a list")
|
|
|
|
sys.exit(1)
|
|
|
|
if bg:
|
|
|
|
if isinstance(bg, int):
|
|
|
|
code += f"{';' if fg else ''}48;5;{bg}"
|
|
|
|
elif (isinstance(bg, list) or isinstance(bg, tuple)) and len(bg) == 1:
|
|
|
|
code += f"{';' if fg else ''}48;5;{bg[0]}"
|
|
|
|
elif (isinstance(bg, list) or isinstance(bg, tuple)) and len(bg) == 3:
|
|
|
|
code += f"{';' if fg else ''}48;2;{';'.join((str(i) for i in bg))}"
|
|
|
|
else:
|
|
|
|
print("Expected one or three values for bg as a list")
|
|
|
|
sys.exit(1)
|
|
|
|
return code + end
|
2022-09-13 18:51:01 -05:00
|
|
|
def style(self, text, style):
|
|
|
|
if style not in self.__dict__:
|
|
|
|
print(f"unknown style {style}")
|
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
return f"{self.__dict__[style]}{text}{self.__dict__['end']}"
|
2022-09-05 18:14:50 -05:00
|
|
|
a = ansi = Ansi()
|
|
|
|
endef
|
|
|
|
define info_py
|
|
|
|
$(ansi_py)
|
|
|
|
print(f"""$(2)""")
|
|
|
|
endef
|
|
|
|
define print_ansi_py
|
|
|
|
$(ansi_py)
|
2022-09-13 16:40:07 -05:00
|
|
|
codes_names = {getattr(ansi, attr): attr for attr in ansi.__dict__}
|
2022-09-05 18:14:50 -05:00
|
|
|
for code in sorted(codes_names.keys(), key=lambda item: (len(item), item)):
|
2022-09-16 12:47:21 -05:00
|
|
|
print(
|
|
|
|
"{:>20} $(HELP_SEP) {} $(HELP_SEP) {}".format(
|
|
|
|
codes_names[code], code + "******" + ansi.end, repr(code)
|
|
|
|
)
|
|
|
|
)
|
2022-09-05 18:14:50 -05:00
|
|
|
endef
|
|
|
|
define vars_py
|
|
|
|
import os
|
|
|
|
$(ansi_py)
|
|
|
|
vars = "$2".split()
|
|
|
|
length = max((len(v) for v in vars))
|
|
|
|
print(f"{ansi.$(HEADER_COLOR)}vars:{ansi.end}\n")
|
|
|
|
for v in vars:
|
|
|
|
print(f" {ansi.b_magenta}{v:<{length}}{ansi.end} = {os.getenv(v)}")
|
|
|
|
print()
|
|
|
|
endef
|
2022-09-14 16:52:49 -05:00
|
|
|
define confirm_py
|
|
|
|
import sys
|
|
|
|
$(ansi_py)
|
|
|
|
def confirm():
|
|
|
|
"""
|
|
|
|
Ask user to enter Y or N (case-insensitive).
|
|
|
|
:return: True if the answer is Y.
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
|
|
|
answer = ""
|
|
|
|
while answer not in ["y", "n"]:
|
|
|
|
answer = input(f"""$(2) {a.b_red}[Y/n]{a.end} """).lower()
|
|
|
|
return answer == "y"
|
|
|
|
if confirm():
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
sys.exit(1)
|
|
|
|
endef
|