From cc81782069d24b021e43daab7f8ecc84a1862044 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Sat, 24 Sep 2022 15:24:36 -0500 Subject: [PATCH] feat: add dependency listing to recipe help --- src/builtins.mk | 13 ++----------- src/help.py | 36 +++++++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/builtins.mk b/src/builtins.mk index 55cb4f8..ea9c166 100644 --- a/src/builtins.mk +++ b/src/builtins.mk @@ -4,17 +4,8 @@ ifeq (help,$(firstword $(MAKECMDGOALS))) export HELP_ARGS endif ## 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: - $(call py,help_py) -endif + $(call py,help_py) || { echo "exiting early!"; exit 1; } .PHONY: _help _help: export SHOW_HIDDEN=true _help: help @@ -38,7 +29,7 @@ tconfirm = $(call py,confirm_py,$(1)) _update-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 -export MAKEFILE_LIST +export MAKEFILE_LIST MAKE ifndef INHERIT_SHELL SHELL := $(shell which bash) endif diff --git a/src/help.py b/src/help.py index 64d292d..bf5e44d 100644 --- a/src/help.py +++ b/src/help.py @@ -5,6 +5,9 @@ import argparse from collections import namedtuple import os import re +import subprocess +import sys +from textwrap import wrap ##- '$(ansi_py)' -## @@ -28,6 +31,10 @@ def parseargs(argstring): return parser.parse_args(argstring.split()) +def divider(len): + return ansi.style(f" {'$(DIVIDER)'*len}", "$(DIVIDER_STYLE)") + + def gen_makefile(): makefile = "" for file in os.getenv("MAKEFILE_LIST").split(): @@ -64,7 +71,21 @@ def recipe_help_header(goal): item[0].get("msgargs", ""), ) 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): @@ -74,15 +95,19 @@ def parse_goal(file, goal): if matched_goal: output.append(recipe_help_header(matched_goal[0])) + deps = get_goal_deps(matched_goal[0]) + if deps: + output.extend(deps) lines = file.splitlines() loc = [n for n, l in enumerate(lines) if l.startswith(f"{matched_goal[0]}:")][0] recipe = [] + for line in lines[loc + 1 :]: if not line.startswith("\t"): break recipe.append(f" {line.strip()}") output.append(divider(max((len(l.strip()) for l in recipe)))) - output.append("\n".join(recipe) + "\n") + output.append("\n".join(recipe)) else: 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): args = parseargs(argstr) lines = [] @@ -148,8 +169,8 @@ def print_help(): def print_arg_help(help_args): + print(f"{ansi.style('task.mk recipe help','$(HEADER_STYLE)')}\n") for arg in help_args.split(): - print(f"{ansi.style('task.mk recipe help','$(HEADER_STYLE)')}\n") print("\n".join(parse_goal(gen_makefile(), arg))) @@ -158,6 +179,7 @@ def main(): if help_args: print_arg_help(help_args) print(a.faint) + sys.exit(1) else: print_help()