-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrelease.py
161 lines (129 loc) · 4.41 KB
/
release.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
161
"""A script to automate building and uploading a release archive.
This is in python instead of bash because I abhor bash. Even though it's a
little nicer for running commands, it's worse at everything else.
"""
import dataclasses
import enum
import os
import os.path
import pathlib
import shutil
import subprocess
RELEASE_TAG = os.getenv("RELEASE_TAG")
BUILD = os.getenv("BUILD")
if BUILD is None:
raise Exception("you *must* set the BUILD environment variable")
class Archive(enum.Enum):
TARBALL = 1
ZIP = 2
DEB = 3
@dataclasses.dataclass
class BuildSettings:
target: str # The rust target to build for
test: bool = True # Whether or not to run tests
man_page: bool = True # Whether or not to generate a man page
strip: bool = True # Whether or not to strip binaries
archive: Archive = Archive.TARBALL # Archive type
ext: str = "" # The file extension of the binary
print(f"doing release: {BUILD}")
build = {
"linux": BuildSettings(
target="x86_64-unknown-linux-musl",
),
"debian": BuildSettings(
target="x86_64-unknown-linux-musl",
test=False,
archive=Archive.DEB,
),
"macos": BuildSettings(
target="x86_64-apple-darwin",
),
"arm-macos": BuildSettings(
target="aarch64-apple-darwin",
),
"windows": BuildSettings(
target="x86_64-pc-windows-msvc",
strip=False,
man_page=False,
archive=Archive.ZIP,
ext=".exe",
),
}[BUILD]
print(f"settings: {build}")
target_dir = pathlib.Path("target") / build.target / "release"
bins = [(target_dir / bin).with_suffix(build.ext) for bin in ["fwd", "fwd-browse"]]
def build_and_test(staging: pathlib.Path):
# Tools
subprocess.run(
["rustup", "target", "add", build.target],
check=True,
)
# Test...?
if build.test:
subprocess.run(
["cargo", "test", "--verbose", "--release", "--target", build.target],
check=True,
)
# Build
subprocess.run(
["cargo", "build", "--verbose", "--release", "--target", build.target],
check=True,
)
# Strip
if build.strip:
for bin in bins:
subprocess.run(["strip", bin], check=True)
# Copy
for bin in bins:
shutil.copyfile(bin, os.path.join(staging, os.path.basename(bin)))
def build_docs(staging: pathlib.Path):
shutil.copyfile("README.md", staging / "README.md")
if build.man_page:
print("Creating man page...")
proc = subprocess.run(
["pandoc", "-s", "-tman", os.path.join("doc", "fwd.man.md")],
check=True,
capture_output=True,
encoding="utf8",
)
contents = proc.stdout
with open(staging / "fwd.1", "w", encoding="utf-8") as f:
f.write(contents)
def build_archive(staging: pathlib.Path) -> pathlib.Path:
print("Creating archive...")
if build.archive == Archive.ZIP:
archive = pathlib.Path(f"{staging}.zip")
subprocess.run(["7z", "a", archive, f"{staging}"], check=True)
elif build.archive == Archive.DEB:
subprocess.run(["cargo", "install", "cargo-deb"], check=True)
shutil.copyfile(staging / "fwd.1", target_dir / "fwd.1")
subprocess.run(["cargo", "deb", "--target", build.target], check=True)
# Knowing the deb path means knowing the target version but I don't
# actually have the version here. (Or, like, I have the release tag
# but not in testing.) So just find the hopefully singular .deb that
# we made.
deb_path = pathlib.Path("target") / build.target / "debian"
archives = list(deb_path.glob("*.deb"))
assert len(archives) == 1
archive = archives[0]
else:
assert build.archive == Archive.TARBALL
archive = pathlib.Path(f"{staging}.tar.gz")
subprocess.run(["tar", "czf", archive, f"{staging}"], check=True)
return archive
staging = pathlib.Path(f"fwd-{build.target}")
os.makedirs(staging, exist_ok=True)
build_and_test(staging)
build_docs(staging)
archive = build_archive(staging)
shutil.rmtree(staging)
assert archive.exists()
if RELEASE_TAG is None:
print(f"Not releasing {archive} to github, RELEASE_TAG is none.")
else:
print(f"Uploading {archive} to github release {RELEASE_TAG}...")
subprocess.run(
["gh", "release", "upload", RELEASE_TAG, archive, "--clobber"],
check=True,
)
os.unlink(archive)