-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtasks.py
160 lines (115 loc) · 3.56 KB
/
tasks.py
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""
SPDX-FileCopyrightText: 2022 Aurélien Gâteau <[email protected]>
SPDX-License-Identifier: GPL-3.0-or-later
A set of tasks to simplify the release process. See RELEASE_CHECK_LIST.md
for details.
"""
import os
import re
import shutil
import sys
from pathlib import Path
from typing import List
from invoke import task, run
ARTIFACTS_DIR = Path("artifacts")
MAIN_BRANCH = "master"
def get_version():
return os.environ["VERSION"]
def erun(*args, **kwargs):
"""Like run, but with echo on"""
kwargs["echo"] = True
return run(*args, **kwargs)
def cerun(c, *args, **kwargs):
"""Like Context.run, but with echo on"""
kwargs["echo"] = True
return c.run(*args, **kwargs)
def ask(msg: str) -> str:
"""Show a message, wait for input and returns it"""
print(msg, end=" ")
return input()
def is_ok(msg: str) -> bool:
"""Show a message, append (y/n) and return True if user select y or Y"""
answer = ask(f"{msg} (y/n)").lower()
return answer == "y"
@task
def create_pr(c):
"""Create a pull-request and mark it as auto-mergeable"""
result = cerun(c, "gh pr create --fill", warn=True)
if not result:
sys.exit(1)
cerun(c, f"gh pr merge --auto -dm")
@task
def update_version(c):
version = get_version()
path = Path("Cargo.toml")
text = path.read_text()
text, count = re.subn(r"^version = .*", f"version = \"{version}\"", text,
flags=re.MULTILINE)
assert count == 0 or count == 1
path.write_text(text)
@task
def prepare_release(c):
version = get_version()
run(f"gh issue list -m {version}", pty=True)
run("gh pr list", pty=True)
if not is_ok("Continue?"):
sys.exit(1)
erun(f"git checkout {MAIN_BRANCH}")
erun("git pull")
erun("git status -s")
if not is_ok("Continue?"):
sys.exit(1)
prepare_release2(c)
@task
def prepare_release2(c):
version = get_version()
erun("git checkout -b prep-release")
update_version(c)
erun(f"changie batch {version}")
print(f"Review/edit changelog (.changes/{version}.md)")
if not is_ok("Looks good?"):
sys.exit(1)
erun("changie merge")
print("Review CHANGELOG.md")
if not is_ok("Looks good?"):
sys.exit(1)
prepare_release3(c)
@task
def prepare_release3(c):
version = get_version()
# Rebuild to ensure Cargo.lock is updated
erun("cargo build")
erun("git add Cargo.toml Cargo.lock CHANGELOG.md .changes")
erun("cargo publish --dry-run")
erun(f"git commit -m 'Prepare {version}'")
erun("git push -u origin prep-release")
create_pr(c)
@task
def tag(c):
version = get_version()
erun(f"git checkout {MAIN_BRANCH}")
erun("git pull")
changes_file = Path(".changes") / f"{version}.md"
if not changes_file.exists():
print(f"{changes_file} does not exist, check previous PR has been merged")
sys.exit(1)
if not is_ok("Create tag?"):
sys.exit(1)
erun(f"git tag -a {version} -m 'Releasing version {version}'")
erun("git push")
erun("git push --tags")
def get_artifact_list() -> List[Path]:
assert ARTIFACTS_DIR.exists()
return list(ARTIFACTS_DIR.glob("*.tar.bz2"))
@task
def download_artifacts(c):
if ARTIFACTS_DIR.exists():
shutil.rmtree(ARTIFACTS_DIR)
ARTIFACTS_DIR.mkdir()
erun(f"gh run download --dir {ARTIFACTS_DIR}", pty=True)
@task
def publish(c):
version = get_version()
files_str = " ".join(str(x) for x in get_artifact_list())
erun(f"gh release create {version} -F.changes/{version}.md {files_str}")
erun("cargo publish")