Compare commits

...

4 commits

6 changed files with 102 additions and 29 deletions

View file

@ -2,12 +2,12 @@ VERSION ?= $(shell git describe --tags --always --dirty | sed s'/dirty/dev/')
TEMPLATES := $(shell find src/ -type f) TEMPLATES := $(shell find src/ -type f)
.DEFAULT_GOAL := help .DEFAULT_GOAL := help
header = $(call tprint,{a.bold}==>{a.end} {a.magenta}$(1){a.end} {a.bold}<=={a.end}) msg = $(call tprint,{a.bold}==>{a.end} {a.magenta}$(1){a.end} {a.bold}<=={a.end})
## bootstrap | generate local dev environment ## bootstrap | generate local dev environment
.PHONY: bootstrap .PHONY: bootstrap
bootstrap: bootstrap:
$(call header,Bootstrap Environment) $(call msg,Bootstrap Environment)
@mamba create -p ./env python jinja2 black -y @mamba create -p ./env python jinja2 black -y
@mamba run -p ./env pip install yartsu @mamba run -p ./env pip install yartsu
@ -35,11 +35,12 @@ list-%:
## release | release new version of task.mk ## release | release new version of task.mk
.PHONY: release .PHONY: release
release: version-check release: version-check
$(call header,Release Project) $(call msg,Release Project)
@./generate.py $(VERSION) > task.mk @./generate.py $(VERSION) > task.mk
@sed -i 's/task.mk\/.*\/task.mk/task.mk\/v$(VERSION)\/task.mk/g' README.md @sed -i 's/task.mk\/.*\/task.mk/task.mk\/v$(VERSION)\/task.mk/g' README.md
@git add task.mk README.md @git add task.mk README.md
@git commit -m "release: v$(VERSION)" @git commit -m "release: v$(VERSION)"
@git tag v$(VERSION)
## c, clean | remove the generated files ## c, clean | remove the generated files
.PHONY: clean .PHONY: clean
@ -69,7 +70,7 @@ test-bash:
$(call tbash,bash_script,test bash multiline) $(call tbash,bash_script,test bash multiline)
define msg define mlmsg
{a.b_yellow} {a.b_yellow}
It can even be multiline!{a.end} It can even be multiline!{a.end}
and styles can be defined{a.end} and styles can be defined{a.end}
@ -80,9 +81,10 @@ endef
## info | demonstrate usage of tprint ## info | demonstrate usage of tprint
.PHONY: task .PHONY: task
info: info:
$(call header, Info Message) $(call msg, Info Message)
$(call tprint,{a.black_on_cyan}This is task-print output:{a.end}) $(call tprint,{a.black_on_cyan}This is task-print output:{a.end})
$(call tprint,$(msg)) $(call tprint,$(mlmsg))
$(call tprint,{a.custom(fg=(148, 255, 15),bg=(103, 2, 15))}Custom Colors TOO!{a.end})
task.mk: task.mk:
./generate.py $(shell git describe --tags) > task.mk ./generate.py $(shell git describe --tags) > task.mk

View file

@ -30,14 +30,14 @@ If someone tries to invoke `make help` it will download `.task.mk` for them.
```make ```make
-include .task.mk -include .task.mk
$(if $(filter help,$(MAKECMDGOALS)),$(if $(wildcard .task.mk),,.task.mk: ; curl -fsSL https://raw.githubusercontent.com/daylinmorgan/task.mk/v22.9.5/task.mk -o .task.mk)) $(if $(filter help,$(MAKECMDGOALS)),$(if $(wildcard .task.mk),,.task.mk: ; curl -fsSL https://raw.githubusercontent.com/daylinmorgan/task.mk/v22.9.7/task.mk -o .task.mk))
``` ```
You might also consider making it a consistently downloaded dependency if you plan to use any of it's advanced feature set, by dropping the `$(MAKECMDGOALS)` check. You might also consider making it a consistently downloaded dependency if you plan to use any of it's advanced feature set, by dropping the `$(MAKECMDGOALS)` check.
```make ```make
-include .task.mk -include .task.mk
$(if $(wildcard .task.mk),,.task.mk: ; curl -fsSL https://raw.githubusercontent.com/daylinmorgan/task.mk/v22.9.5/task.mk -o .task.mk) $(if $(wildcard .task.mk),,.task.mk: ; curl -fsSL https://raw.githubusercontent.com/daylinmorgan/task.mk/v22.9.7/task.mk -o .task.mk)
``` ```
## Usage ## Usage
@ -59,7 +59,7 @@ build:
... ...
``` ```
Now when you invoke `task.mk` it will parse these and generate your help output. Now when you invoke `make help` it will parse these and generate your help output.
In addition to a generic help output you can expose some configuration settings with `make vars`. In addition to a generic help output you can expose some configuration settings with `make vars`.
To do so define the variables you'd like to print with `PRINT_VARS := VAR1 VAR2 VAR3`. To do so define the variables you'd like to print with `PRINT_VARS := VAR1 VAR2 VAR3`.
@ -88,6 +88,11 @@ To see the available colors and formatting(bold,italic,etc.) use the hidden reci
**Note**: Any help commands starting with an underscore will be ignored. **Note**: Any help commands starting with an underscore will be ignored.
To view hidden `tasks` (or recipes in GNU Make land) you can use `make _help`. To view hidden `tasks` (or recipes in GNU Make land) you can use `make _help`.
In addition, you can use custom colors using the builtin `ansi.custom` or (`a.custom`) method.
It has two optional arguments `fg` and `bg`. Which can be used to specify either an 8-bit color from the [256 colors](https://en.wikipedia.org/wiki/8-bit_color).
Or a tuple/list to define an RBG 24-bit color, for instance `a.custom(fg=(5,10,255))`.
See this project's `make info` for an example.
## Configuration ## Configuration
You can quickly customize some of the default behavior of `task.mk` by overriding the below variables prior to the `-include .task.mk`. You can quickly customize some of the default behavior of `task.mk` by overriding the below variables prior to the `-include .task.mk`.
@ -111,6 +116,12 @@ define USAGE ?=
endef endef
``` ```
To use a custom color for one of the predefined configuration variables specify only the custom method.
```make
HEADER_COLOR = custom(fg=171,bg=227)
```
**NOTE**: `HELP_SEP` does not change the argument definitions syntax only the format of `make help`. **NOTE**: `HELP_SEP` does not change the argument definitions syntax only the format of `make help`.
## Advanced Usage: Embedded Python Scripts ## Advanced Usage: Embedded Python Scripts
@ -162,7 +173,7 @@ zstyle ':completion::complete:make:*:targets' call-command true
## Why Make? ## Why Make?
There are lot of `GNU Make` alternatives but none have near the same level of ubiquity. There are lot of `GNU Make` alternatives but none have near the same level of ubiquity.
This project attaches to `make` some of native features of [`just`](https://github.com/casey/just), a command runner. This project attaches to `make` some of the native features of [`just`](https://github.com/casey/just), a command runner.
Just is a great task runner, but it suffers two problems, users probably don't have it installed already, and there is no way to define file specific recipes. Just is a great task runner, but it suffers two problems, users probably don't have it installed already, and there is no way to define file specific recipes.
Most of my `Makefile`'s are comprised primarily of handy `.PHONY` recipes, but I always end up with a few file specific recipes. Most of my `Makefile`'s are comprised primarily of handy `.PHONY` recipes, but I always end up with a few file specific recipes.
@ -170,13 +181,13 @@ Most of my `Makefile`'s are comprised primarily of handy `.PHONY` recipes, but I
Another interesting project I've evaluated for these purposes is [`go-task/task`](https://github.com/go-task/task). Another interesting project I've evaluated for these purposes is [`go-task/task`](https://github.com/go-task/task).
`Task` has many of the features of `GNU Make` and some novel features of it's own. `Task` has many of the features of `GNU Make` and some novel features of it's own.
But like `just` it's a tool people don't usually already have and it's configured using a `yaml` file. But like `just` it's a tool people don't usually already have and it's configured using a `yaml` file.
`Yaml` files can be finicky to work with and and it uses a golang based shell runtime not your native shell, which might lead to unexpected behavior. `Yaml` files can be finicky to work with and and it uses a golang based shell runtime, not your native shell, which might lead to unexpected behavior.
## Simpler Alternative ## Simpler Alternative
But I just want a basic help output, surely I don't need python for this... you would be right. But I just want a basic help output, surely I don't need python for this... you would be right.
`Task.mk` replaces my old `make help` recipe boilerplate which may better serve you. `Task.mk` replaces my old `make help` recipe boilerplate which may better serve you (so long as you have `sed`/`awk`).
```make ```make

View file

@ -31,12 +31,6 @@ def bg(byte):
class Ansi: class Ansi:
"""ANSI color codes""" """ANSI color codes"""
def setcode(self, name, escape_code):
if not sys.stdout.isatty() or os.getenv("NO_COLOR", False):
setattr(self, name, "")
else:
setattr(self, name, escape_code)
def __init__(self): def __init__(self):
self.setcode("end", "\033[0m") self.setcode("end", "\033[0m")
for name, byte in color2byte.items(): for name, byte in color2byte.items():
@ -48,6 +42,42 @@ class Ansi:
for name, byte in state2byte.items(): for name, byte in state2byte.items():
self.setcode(name, f"\033[{byte}m") self.setcode(name, f"\033[{byte}m")
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
a = ansi = Ansi() a = ansi = Ansi()
#% endblock %# #% endblock %#

View file

@ -1,7 +1,7 @@
# }> [github.com/daylinmorgan/task.mk] <{ # # }> [github.com/daylinmorgan/task.mk] <{ #
# Copyright (c) 2022 Daylin Morgan # Copyright (c) 2022 Daylin Morgan
# MIT License # MIT License
# ##- version -## # version: ##- version -##
# #
# task.mk should be included at the bottom of your Makefile. # task.mk should be included at the bottom of your Makefile.
# See below for the standard configuration options that should be set prior to including this file. # See below for the standard configuration options that should be set prior to including this file.

View file

@ -27,7 +27,7 @@ def get_help(file):
print(f"""$(USAGE)""") print(f"""$(USAGE)""")
goals = list(get_help(makefile)) goals = list(get_help(makefile))
if os.getenv("SORT_HELP",False): if os.getenv("SORT_HELP", False):
goals.sort(key=lambda i: i[0]) goals.sort(key=lambda i: i[0])
goal_len = max(len(goal[0]) for goal in goals) goal_len = max(len(goal[0]) for goal in goals)

46
task.mk
View file

@ -1,7 +1,7 @@
# }> [github.com/daylinmorgan/task.mk] <{ # # }> [github.com/daylinmorgan/task.mk] <{ #
# Copyright (c) 2022 Daylin Morgan # Copyright (c) 2022 Daylin Morgan
# MIT License # MIT License
# v22.9.5-3-g9c67418 # version: 22.9.7
# #
# task.mk should be included at the bottom of your Makefile. # task.mk should be included at the bottom of your Makefile.
# See below for the standard configuration options that should be set prior to including this file. # See below for the standard configuration options that should be set prior to including this file.
@ -129,7 +129,7 @@ def get_help(file):
print(f"""$(USAGE)""") print(f"""$(USAGE)""")
goals = list(get_help(makefile)) goals = list(get_help(makefile))
if os.getenv("SORT_HELP",False): if os.getenv("SORT_HELP", False):
goals.sort(key=lambda i: i[0]) goals.sort(key=lambda i: i[0])
goal_len = max(len(goal[0]) for goal in goals) goal_len = max(len(goal[0]) for goal in goals)
@ -174,12 +174,6 @@ def bg(byte):
class Ansi: class Ansi:
"""ANSI color codes""" """ANSI color codes"""
def setcode(self, name, escape_code):
if not sys.stdout.isatty() or os.getenv("NO_COLOR", False):
setattr(self, name, "")
else:
setattr(self, name, escape_code)
def __init__(self): def __init__(self):
self.setcode("end", "\033[0m") self.setcode("end", "\033[0m")
for name, byte in color2byte.items(): for name, byte in color2byte.items():
@ -191,6 +185,42 @@ class Ansi:
for name, byte in state2byte.items(): for name, byte in state2byte.items():
self.setcode(name, f"\033[{byte}m") self.setcode(name, f"\033[{byte}m")
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
a = ansi = Ansi() a = ansi = Ansi()