-
Notifications
You must be signed in to change notification settings - Fork 11
/
dev
executable file
·200 lines (158 loc) · 6.11 KB
/
dev
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env python3
# This script is an entrypoint for all developer activities in this project.
import argparse
import os
import subprocess
import sys
def cmd(cmds: argparse._SubParsersAction, name: str, help: str):
"""Decorator that registers subcommands as functions to be executed."""
def wrapper(func):
p = cmds.add_parser(name, help=help)
p.set_defaults(func=func)
return wrapper
def run(cmd: str, args: list = None, capture_output=False, env: dict = None):
"""Helper to run cmd in a shell."""
if args:
if args[0] == "--":
args = args[1:]
cmd += " -- " + " ".join(args)
return subprocess.run(
cmd, check=True, shell=True, capture_output=capture_output, env=env
)
def main():
if not os.getenv("DIRENV_DIR"):
print("Direnv is not enabled. Fix it first! See README.md for instructions.")
sys.exit(1)
cli = argparse.ArgumentParser(
usage="./dev COMMAND [FLAGS...]",
description="CLI for developing Mintter. Provides commands for most common developer tasks in this project.",
)
cmds = cli.add_subparsers(
title="commands",
# This is ugly, but otherwise argparse prints the redundant list of subcommands.
# And if we just use an empty string it messes up help message alignment for some subcommands.
metavar=" ",
)
@cmd(
cmds,
"gen",
"Check all the generated code is up to date. Otherwise run the code generation process to fix it.",
)
def gen(args):
out = run(
"plz run parallel $(plz query filter -i 'generated:check')",
capture_output=True,
)
targets_to_gen = []
for line in out.stdout.decode("utf-8").split("\n"):
idx = line.find("plz run")
if idx == -1:
continue
targets_to_gen.append(line[idx + 7 : -1]) # 7 is length of 'plz run'
if len(targets_to_gen) == 0:
return
return run("plz run parallel " + " ".join(targets_to_gen))
@cmd(cmds, "build-backend", "Build mintterd binary for the current platform.")
def build_backend(args):
return run("plz build //backend:mintterd")
@cmd(cmds, "build-frontend", "Build production bundle of the frontend web app.")
def build_frontend(args):
return run("pnpm build")
@cmd(
cmds, "build-desktop", "Builds the Tauri desktop app for the current platform."
)
def build_desktop(args):
run("plz build //backend:mintterd")
run("cargo tauri build --debug")
@cmd(cmds, "ping-p2p", "Execute ping utility to check visibility.")
def ping_p2p(args):
return run("plz run //backend:pingp2p", args=args)
@cmd(cmds, "run-gw-backend", "Build and run backend for mintter web gateway.")
def run_gateway(args):
return run("plz run //backend:minttergw", args=args)
@cmd(cmds, "run-backend", "Build and run mintterd binary for the current platform.")
def run_backend(args):
return run("plz run //backend:mintterd", args=args)
@cmd(cmds, "run-frontend", "Run frontend web app for development.")
def run_frontend(args):
run("plz build //:pnpm")
return run("exec pnpm app:dev", args=args)
@cmd(cmds, "run-gw-frontend", "Run frontend web app for development.")
def run_frontend(args):
run("plz build //:pnpm")
return run(
"exec pnpm gw:dev",
args=args,
env={**os.environ, "GW_NEXT_HOST": "http://localhost:3000"},
)
@cmd(cmds, "run-desktop", "Run Tauri desktop app for development.")
def run_desktop(args):
run("plz build //backend:mintterd //:pnpm")
return run("pnpm tauri:dev", args=args)
@cmd(cmds, "release", "Cut a new release.")
def release(args):
allowed_components = ["desktop", "relay"]
want_branch = "master"
if len(args) != 1:
raise ValueError(
"Must specify a component name to release. One of %s."
% allowed_components
)
component = args[0]
if component not in allowed_components:
raise ValueError(
"Unknown component name to release. Must be one of %s."
% allowed_components
)
branch = (
run("git branch --show-current", capture_output=True)
.stdout.decode()
.strip()
)
if branch != want_branch:
raise ValueError("Must only release on the '%s' branch!" % want_branch)
print("Checking if we're up to date with remote...")
run("git fetch origin %s" % want_branch)
diff = (
run("git diff-index origin/%s" % want_branch, capture_output=True)
.stdout.decode()
.strip()
)
if diff:
raise ValueError("Local repository is not up to date with remote!")
prev_tag = (
run(
"git describe --tags --abbrev=0 --match '%s/*'" % component,
capture_output=True,
)
.stdout.decode()
.strip()
)
prev_version = prev_tag.removeprefix(component + "/")
new_version = input(
"Last version was %s. Specify the new version: " % prev_version
)
if not new_version:
raise ValueError("Must specify version.")
parts = new_version.split(".")
if len(parts) != 3:
raise ValueError(
"Invalid version number %s. Version must be 3 numbers separated by dots."
% new_version
)
print("Tagging release...")
run("git tag -a %s" % component + "/" + new_version)
run("git push --follow-tags")
if len(sys.argv) == 1:
cli.print_help()
return
namespace, args = cli.parse_known_args()
try:
namespace.func(args)
except ValueError as err:
print(str(err))
sys.exit(1)
except (subprocess.CalledProcessError, KeyboardInterrupt):
return
if __name__ == "__main__":
main()