feat: add dependency listing to recipe help

This commit is contained in:
Daylin Morgan 2022-09-24 15:24:36 -05:00
parent b1449d8991
commit cc81782069
2 changed files with 31 additions and 18 deletions

View file

@ -4,17 +4,8 @@ ifeq (help,$(firstword $(MAKECMDGOALS)))
export HELP_ARGS export HELP_ARGS
endif endif
## h, help | show this help ## h, help | show this help
ifdef HELP_ARGS
help: help-args
$(error exiting early)
.PHONY: help-args
help-args:
$(call py,help_py)
else
.PHONY: help h
h help: h help:
$(call py,help_py) $(call py,help_py) || { echo "exiting early!"; exit 1; }
endif
.PHONY: _help .PHONY: _help
_help: export SHOW_HIDDEN=true _help: export SHOW_HIDDEN=true
_help: help _help: help
@ -38,7 +29,7 @@ tconfirm = $(call py,confirm_py,$(1))
_update-task.mk: _update-task.mk:
$(call tprint,{a.b_cyan}Updating task.mk{a.end}) $(call tprint,{a.b_cyan}Updating task.mk{a.end})
curl https://raw.githubusercontent.com/daylinmorgan/task.mk/main/task.mk -o .task.mk curl https://raw.githubusercontent.com/daylinmorgan/task.mk/main/task.mk -o .task.mk
export MAKEFILE_LIST export MAKEFILE_LIST MAKE
ifndef INHERIT_SHELL ifndef INHERIT_SHELL
SHELL := $(shell which bash) SHELL := $(shell which bash)
endif endif

View file

@ -5,6 +5,9 @@ import argparse
from collections import namedtuple from collections import namedtuple
import os import os
import re import re
import subprocess
import sys
from textwrap import wrap
##- '$(ansi_py)' -## ##- '$(ansi_py)' -##
@ -28,6 +31,10 @@ def parseargs(argstring):
return parser.parse_args(argstring.split()) return parser.parse_args(argstring.split())
def divider(len):
return ansi.style(f" {'$(DIVIDER)'*len}", "$(DIVIDER_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():
@ -64,7 +71,21 @@ 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_STYLE)')}"
def get_goal_deps(goal="task.mk"):
make = os.getenv("MAKE", "make")
database = subprocess.check_output([make, "-p", "-n"], universal_newlines=True)
dep_pattern = re.compile(r"^" + goal + ":(.*)?")
for line in database.splitlines():
match = dep_pattern.search(line)
if match and match.groups()[0]:
return wrap(
f"{ansi.style('deps','default')}: {ansi.style(match.groups()[0].strip(),'$(MSG_STYLE)')}",
initial_indent=" ",
subsequent_indent=" ",
)
def parse_goal(file, goal): def parse_goal(file, goal):
@ -74,15 +95,19 @@ def parse_goal(file, goal):
if matched_goal: if matched_goal:
output.append(recipe_help_header(matched_goal[0])) output.append(recipe_help_header(matched_goal[0]))
deps = get_goal_deps(matched_goal[0])
if deps:
output.extend(deps)
lines = file.splitlines() lines = file.splitlines()
loc = [n for n, l in enumerate(lines) if l.startswith(f"{matched_goal[0]}:")][0] loc = [n for n, l in enumerate(lines) if l.startswith(f"{matched_goal[0]}:")][0]
recipe = [] recipe = []
for line in lines[loc + 1 :]: for line in lines[loc + 1 :]:
if not line.startswith("\t"): if not line.startswith("\t"):
break break
recipe.append(f" {line.strip()}") recipe.append(f" {line.strip()}")
output.append(divider(max((len(l.strip()) for l in recipe)))) output.append(divider(max((len(l.strip()) for l in recipe))))
output.append("\n".join(recipe) + "\n") output.append("\n".join(recipe))
else: else:
output.append(f"{ansi.b_red}ERROR{ansi.end} Failed to find goal: {goal}") output.append(f"{ansi.b_red}ERROR{ansi.end} Failed to find goal: {goal}")
@ -100,10 +125,6 @@ def fmt_goal(goal, msg, max_goal_len, argstr):
) )
def divider(len):
return ansi.style(f" {'$(DIVIDER)'*len}", "$(DIVIDER_STYLE)")
def fmt_rawmsg(msg, argstr, maxlens): def fmt_rawmsg(msg, argstr, maxlens):
args = parseargs(argstr) args = parseargs(argstr)
lines = [] lines = []
@ -148,8 +169,8 @@ def print_help():
def print_arg_help(help_args): def print_arg_help(help_args):
for arg in help_args.split():
print(f"{ansi.style('task.mk recipe help','$(HEADER_STYLE)')}\n") print(f"{ansi.style('task.mk recipe help','$(HEADER_STYLE)')}\n")
for arg in help_args.split():
print("\n".join(parse_goal(gen_makefile(), arg))) print("\n".join(parse_goal(gen_makefile(), arg)))
@ -158,6 +179,7 @@ def main():
if help_args: if help_args:
print_arg_help(help_args) print_arg_help(help_args)
print(a.faint) print(a.faint)
sys.exit(1)
else: else:
print_help() print_help()