Skip to content

Commit af957e7

Browse files
committed
tasks: TOC + format
1 parent 337a58c commit af957e7

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

Diff for: tasks.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Invoke is broken on Python 3.11
2+
# https://github.com/pyinvoke/invoke/issues/833#issuecomment-1293148106
3+
import inspect
4+
import os
5+
import re
6+
import sys
7+
from typing import Optional
8+
9+
if not hasattr(inspect, "getargspec"):
10+
inspect.getargspec = inspect.getfullargspec
11+
12+
import invoke # pylint: disable=wrong-import-position
13+
from invoke import task # pylint: disable=wrong-import-position
14+
15+
# Specifying encoding because Windows crashes otherwise when running Invoke
16+
# tasks below:
17+
# UnicodeEncodeError: 'charmap' codec can't encode character '\ufffd'
18+
# in position 16: character maps to <undefined>
19+
# People say, it might also be possible to export PYTHONIOENCODING=utf8 but this
20+
# seems to work.
21+
# FIXME: If you are a Windows user and expert, please advise on how to do this
22+
# properly.
23+
sys.stdout = open( # pylint: disable=consider-using-with
24+
1, "w", encoding="utf-8", closefd=False, buffering=1
25+
)
26+
27+
28+
def run_invoke(
29+
context,
30+
cmd,
31+
environment: Optional[dict] = None,
32+
warn: bool = False,
33+
) -> invoke.runners.Result:
34+
def one_line_command(string):
35+
return re.sub("\\s+", " ", string).strip()
36+
37+
return context.run(
38+
one_line_command(cmd),
39+
env=environment,
40+
hide=False,
41+
warn=warn,
42+
pty=False,
43+
echo=True,
44+
)
45+
46+
47+
@task(default=True)
48+
def list_tasks(context):
49+
clean_command = """
50+
invoke --list
51+
"""
52+
run_invoke(context, clean_command)
53+
54+
55+
@task
56+
def toc(context):
57+
run_invoke(context, "doctoc README.md")
58+
59+
60+
@task
61+
def format(context):
62+
run_invoke(context, "prettier --write --print-width 80 --prose-wrap always README.md")
63+
64+
65+
@task(aliases=["l"])
66+
def lint(context):
67+
format(context)
68+
toc(context)

0 commit comments

Comments
 (0)