-
Notifications
You must be signed in to change notification settings - Fork 0
/
x.py
executable file
·179 lines (150 loc) · 4.17 KB
/
x.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/python3
import os
import sys
import subprocess as sp
import multiprocessing as mp
from copy import deepcopy
class InDir:
def __init__(self, dir, cmds):
self.dir = dir
self.cmds = Target(cmds)
def run(self):
where_we_used_to_be = os.getcwd()
os.chdir(self.dir)
self.cmds.begin()
os.chdir(where_we_used_to_be)
class Cmd:
def __init__(self, cmd):
self.cmd = cmd
def run(self):
sp.run(self.cmd, shell=True, check=True)
class Fn:
def __init__(self, fn):
self.fn = fn
def run(self):
self.fn()
class Spawn:
def __init__(self, target):
self.int_target = target
def run(self):
proc = mp.Process(target=self.int_target.begin)
proc.start()
return proc
class Exec:
def __init__(self, exec, args=[]):
self.exec = exec
self.args = [exec] + args
def run(self):
os.execvp(self.exec, self.args)
class Target:
def __init__(self, cmds, desc=""):
self.cmds = cmds
self.desc = desc
def __repr__(self):
return self.desc
def begin(self):
handles = [x.run() for x in self.cmds]
[x.join() for x in handles if x is not None]
def clean_files_dir():
from glob import iglob
for finp in iglob("./files/*"):
if finp.split('/')[-1][0] != '.':
os.remove(finp)
TARGETS = {
"setup": Target(
[
Cmd("cargo install --locked genemichaels sqlx-cli tokio-console flutter_rust_bridge_codegen"),
Cmd("sqlx migrate run --source backend/migrations"),
InDir(
"frontend",
[
Cmd("flutter precache"),
Cmd("flutter pub get"),
],
),
],
desc="Setup the environment for developing/building in",
),
"fmt": Target(
[
Cmd("genemichaels"),
Cmd("cargo fmt"),
InDir("frontend", [Cmd("dart format .")]),
],
desc="Format all code files",
),
"build": Target(
[
InDir(
"frontend",
[
Cmd("dart run build_runner build --delete-conflicting-outputs"),
Cmd("flutter_rust_bridge_codegen generate")
],
),
Cmd("cargo build"),
],
desc="Build programs in debug mode",
),
"server": Target(
[
Cmd("DATA_DIR='./files' IP_ADDR='127.0.0.1' PORT=8081 SIGNUP_ENABLED=1 cargo run -p mio-backend")
],
desc="Run debug mode server"
),
"client": Target(
[
InDir(
"frontend",
[
Exec("flutter", ['run'])
],
)
],
desc="Run debug mode client"
),
"clean": Target(
[
Cmd("cargo clean"),
Fn(clean_files_dir),
InDir(
"frontend",
[
Cmd("flutter clean")
]
)
],
desc="cleanup all build/runtime dirs"
),
}
def print_targets():
print("Currently configured targets:")
for k, v in TARGETS.items():
print(f"\t{k}: {v.desc}")
print("Postfix any target with a '!' to run that target in parallel with the next target.")
print("For example, running target 'a' and 'b': `a! b`")
sys.exit(0)
def main():
if len(sys.argv) < 2:
print(f"usage: {sys.argv[0]} <targets>")
print_targets()
# check for target
for target in sys.argv[1:]:
if target[-1] == '!':
target = deepcopy(target[:-1])
if target not in TARGETS:
print(f"selected target '{target}' not in list of targets.")
print_targets()
# runner
queue = []
for target in sys.argv[1:]:
if target[-1] == '!':
queue += [target[:-1]]
continue
elif len(queue) != 0:
Target([Spawn(TARGETS[x]) for x in (queue + [target])]).begin()
continue
TARGETS[target].begin()
if __name__ == "__main__":
mp.set_start_method("spawn")
main()