File: tasks.py

package info (click to toggle)
python-dunamai 1.25.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 512 kB
  • sloc: python: 3,645; sh: 7; makefile: 4
file content (116 lines) | stat: -rw-r--r-- 2,590 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import datetime as dt
import re
import shutil
import sys
from pathlib import Path

from invoke import task

ROOT = Path(__file__).parent


def get_version() -> str:
    for line in (ROOT / "pyproject.toml").read_text("utf-8").splitlines():
        if line.startswith("version ="):
            return line.replace("version = ", "").strip('"')

    raise RuntimeError("Could not determine version")


def replace_pattern_in_file(file: Path, old: str, new: str, count: int = 1):
    content = file.read_text("utf-8")
    updated = re.sub(old, new, content, count=count)
    file.write_text(updated, "utf-8")


def confirm(prompt: str):
    response = input(f"Confirm by typing '{prompt}': ")
    if response.lower() != prompt.lower():
        sys.exit(1)


@task
def build(ctx, clean=True):
    with ctx.cd(ROOT):
        if clean:
            shutil.rmtree("dist", ignore_errors=True)
        ctx.run("poetry build")


@task
def install(ctx):
    ctx.run("pip uninstall -y dunamai")
    build(ctx)
    wheel = next(ROOT.glob("dist/*.whl"))
    ctx.run('pip install "{}"'.format(wheel))


@task
def docs(ctx):
    version = get_version()
    manpage = "docs/dunamai.1"

    args = [
        "poetry",
        "run",
        "argparse-manpage",
        "--pyfile",
        "dunamai/__main__.py",
        "--function",
        "get_parser",
        "--project-name",
        "dunamai",
        "--prog",
        "dunamai",
        "--version",
        version,
        "--author",
        "Matthew T. Kennerly (mtkennerly)",
        "--url",
        "https://github.com/mtkennerly/dunamai",
        "--format",
        "single-commands-section",
        "--output",
        manpage,
        "--manual-title",
        "Dunamai",
    ]

    # Join manually to avoid issues with single quotes on Windows using `shlex.join`
    joined = " ".join(arg if " " not in arg else f'"{arg}"' for arg in args)

    ctx.run(joined)


@task
def prerelease(ctx, new_version):
    date = dt.datetime.now().strftime("%Y-%m-%d")

    replace_pattern_in_file(
        ROOT / "pyproject.toml",
        'version = ".+"',
        f'version = "{new_version}"',
    )

    replace_pattern_in_file(
        ROOT / "CHANGELOG.md",
        "## Unreleased",
        f"## v{new_version} ({date})",
    )

    build(ctx)
    docs(ctx)


@task
def release(ctx):
    version = get_version()

    confirm(f"release {version}")

    ctx.run(f'git commit -m "Release v{version}"')
    ctx.run(f'git tag v{version} -m "Release"')
    ctx.run("git push")
    ctx.run(f"git push origin tag v{version}")

    ctx.run("poetry publish")