-
Notifications
You must be signed in to change notification settings - Fork 1
/
mmcget.m
303 lines (285 loc) · 10.2 KB
/
mmcget.m
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
:- module mmcget.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module package, manager, solutions.
:- import_module list, string, exception, bool, int.
:- use_module ioextra, dir.
:- type reviewed ---> reviewed(package) ; unreviewed(package).
:- inst unreviewed for reviewed/0 ---> unreviewed(ground).
:- func version = string.
version = "v0.1.1".
:- pred usage(io::di, io::uo) is det.
usage(!IO) :-
io.progname_base("mmc-get", Program, !IO),
io.format(io.stderr_stream,
"usage: %s update # update mmc-get package lists\n" ++
"usage: %s version\n" ++
"usage: %s list [<filter>] # list packages, with optional filter\n" ++
"usage: %s listall [<filter>] # ... including unreviewed packages\n" ++
"usage: %s brief [<filter>] # ... with less information\n" ++
"usage: %s briefall [<filter>]\n" ++
"usage: %s get <name> # clone package into the working directory\n",
[
s(Program), s(Program), s(Program), s(Program),
s(Program), s(Program), s(Program)
], !IO),
io.set_exit_status(1, !IO).
main(!IO) :-
io.command_line_arguments(Args, !IO),
( if Args = ["update"] then
update_packages(!IO),
io.call_system("curl https://mercury-in.space/packages/news", _, !IO)
else if Args = ["version"] then
io.format("mmc-get version %s\n", [s(version)], !IO)
else if Args = ["list"] then
solutions(grep_reviewed(""), L),
foldl(write_package, L, !IO)
else if Args = ["list", Filter] then
solutions(grep_reviewed(Filter), L),
foldl(write_package, L, !IO)
else if Args = ["brief"] then
solutions(grep_reviewed(""), L),
foldl(write_brief_package, L, !IO)
else if Args = ["brief", Filter] then
solutions(grep_reviewed(Filter), L),
foldl(write_brief_package, L, !IO)
else if Args = ["listall"] then
solutions(grep(""), L),
foldl(write_package, L, !IO)
else if Args = ["listall", Filter] then
solutions(grep(Filter), L),
foldl(write_package, L, !IO)
else if Args = ["briefall"] then
solutions(grep(""), L),
foldl(write_brief_package, L, !IO)
else if Args = ["briefall", Filter] then
solutions(grep(Filter), L),
foldl(write_brief_package, L, !IO)
else if Args = ["get", Want] then
( if solutions(exact(to_lower(Want)), [reviewed(Package)]) then
clone(Package^vcs, Package, !IO)
else
solutions(want(Want), L),
( if L = [reviewed(Package)] then
clone(Package^vcs, Package, !IO)
else if L = [unreviewed(Package)] then
write_package(unreviewed(Package), !IO),
ask_to_clone(unreviewed(Package), !IO)
else if L = [] then
io.format(io.stderr_stream, "No package matched ``%s''\n",
[s(Want)], !IO),
io.set_exit_status(1, !IO)
else
show_choices(dump_package, 1, L, !IO),
choose(Res, L, !IO),
(
Res = ok(reviewed(Package)),
clone(Package^vcs, Package, !IO)
;
Res = ok(unreviewed(Package)),
write_package(unreviewed(Package), !IO),
ask_to_clone(unreviewed(Package), !IO)
;
( Res = eof ; Res = error(_) ),
io.set_exit_status(1, !IO)
)
)
)
else if Args = ["help"] then
usage(!IO)
else
io.format(io.stderr_stream, "didn't understand these args: %s\n",
[s(string(Args))], !IO),
usage(!IO)
).
:- pred ask_to_clone(reviewed::in(unreviewed), io::di, io::uo) is det.
ask_to_clone(unreviewed(Package), !IO) :-
yn_prompt("Really get unreviewed package?", B, !IO),
(
B = yes,
clone(Package^vcs, Package, !IO)
;
B = no,
io.set_exit_status(1, !IO)
).
:- pred write_package(reviewed::in, io::di, io::uo) is det.
write_package(P, !IO) :-
io.write_string(dump_package(P), !IO).
:- pred write_brief_package(reviewed::in, io::di, io::uo) is det.
write_brief_package(P, !IO) :-
io.write_string(dump_brief_package(P), !IO).
:- func dump_brief_package(reviewed) = string.
dump_brief_package(reviewed(P)) =
string.format("%-30s %s\n", [s(P^name), s(P^description)]).
dump_brief_package(unreviewed(P)) =
string.format("%-30s !! %s\n", [s(P^name), s(P^description)]).
:- func dump_package(reviewed) = string.
dump_package(reviewed(P)) = string.format("
Name : %s
URL : %s
Tags : %s
License : %s
Builds : %s
Dependencies : %s
Version Control : %s (releases: %s)
FFI : %s
Description : %s
", [
s(P^name), s(P^url), s(join_list(" ", P^tags)), s(string(P^license)),
s(join_list(", ", P^apps ++ P^libs)), s(string(P^deps)), s(string(P^vcs)),
s(string(P^release)), s(string(P^foreign)), s(P^description)
]).
dump_package(unreviewed(P)) = string.format("
Name (UNREVIEWED)? %s
URL ? %s
Tags ? %s
License ? %s
Builds ? %s
Dependencies ? %s
Version Control : %s (releases: %s)
FFI ? %s
Description ? %s
", [
s(P^name), s(P^url), s(join_list(" ", P^tags)), s(string(P^license)),
s(join_list(", ", P^apps ++ P^libs)), s(string(P^deps)), s(string(P^vcs)),
s(string(P^release)), s(string(P^foreign)), s(P^description)
]).
:- pred grep_reviewed(string::in, reviewed::out) is nondet.
grep_reviewed(Substr, R @ reviewed(P)) :-
packages(P),
sub_string_search(to_lower(dump_package(R)), to_lower(Substr), _).
:- pred grep(string::in, reviewed::out) is nondet.
grep(Substr, R) :-
( packages(P), R = reviewed(P) ; unreviewed(P), R = unreviewed(P) ),
sub_string_search(to_lower(dump_package(R)), to_lower(Substr), _).
:- pred want(string::in, reviewed::out) is nondet.
want(Substr, R) :-
( packages(P), R = reviewed(P) ; unreviewed(P), R = unreviewed(P) ),
sub_string_search(to_lower(P^name), to_lower(Substr), _).
:- pred exact(string::in, reviewed::out) is nondet.
exact(Name, R) :-
( packages(P), R = reviewed(P) ; unreviewed(P), R = unreviewed(P) ),
to_lower(P^name, Name).
:- pred clone(vcs::in, package::in, io::di, io::uo) is det.
clone(git, Package, !IO) :-
ioextra.spawn("git", ["clone", Package^url, Package^name], Res, !IO),
(
Res = ok(Pid),
ioextra.waitpid(Pid, Res2, !IO),
(
Res2 = ok,
ioextra.chdir(Package^name, _, !IO),
release(git, Package^release, Package, !IO)
;
Res2 = error(_),
io.write_string(io.stderr_stream, "`git clone' failed; is git in PATH?\n", !IO),
throw(Res2)
)
;
Res = error(_),
throw(Res)
).
clone(fossil, Package, !IO) :-
Target = dir.(Package^name / ".fossil"),
ioextra.exists(Target, Exists, !IO),
(
Exists = no,
dir.make_directory(Package^name, Res1, !IO),
(
Res1 = ok,
ioextra.spawn("fossil", ["clone", Package^url, Target], Res2, !IO),
(
Res2 = ok(Pid),
ioextra.waitpid(Pid, Res, !IO),
(
Res = ok,
ioextra.chdir(Package^name, _, !IO),
ioextra.spawn("fossil", ["open", ".fossil"], Res3, !IO),
(
Res3 = ok(Pid2),
ioextra.waitpid(Pid2, _, !IO),
release(fossil, Package^release, Package, !IO)
;
Res3 = error(_),
throw(Res3)
)
;
Res = error(_),
io.write_string(io.stderr_stream, "`fossil clone' failed; is fossil in PATH?\n", !IO),
throw(Res)
)
;
Res2 = error(_),
throw(Res2)
)
;
Res1 = error(_),
throw(Res1)
)
;
Exists = yes,
throw(Target ++ " already exists")
).
:- pred release(vcs::in, releases::in, package::in, io::di, io::uo) is det.
release(_, head, _, !IO).
release(_VCS, tag, _Package, !IO).
%tags.tags(Tags, VCS, Package, !IO),
%tags.checkout(tag.latest(Tags), VCS, Package, !IO).
:- pred yn_prompt(string::in, bool::out, io::di, io::uo) is det.
yn_prompt(S, B, !IO) :-
io.write_string(S ++ " [y/n] ", !IO),
io.read_line_as_string(Res, !IO),
( if
Res = ok(Line),
to_lower(chomp(Line)) = Ans
then
( if Ans = "yes" ; Ans = "y" then
B = yes
else if Ans = "no" ; Ans = "n" then
B = no
else
io.write_string("Sorry, I didn't understand that.\n\n", !IO),
yn_prompt(S, B, !IO)
)
else
throw(Res)
).
:- pred choose(io.result(reviewed), list(reviewed), io, io).
:- mode choose(out, in, di, uo) is det.
choose(R, Ps, !IO) :-
io.format("Ambiguous result. Your choice? [1..%d, show, brief, q] ",
[i(length(Ps))], !IO),
io.read_line_as_string(Res, !IO),
(
Res = ok(Line),
to_lower(chomp(Line)) = Choice,
( if
to_int(Choice, N),
index1(Ps, N, R0)
then
R = ok(R0)
else if Choice = "show" then
show_choices(dump_package, 1, Ps, !IO),
choose(R, Ps, !IO)
else if Choice = "brief" then
show_choices(dump_brief_package, 1, Ps, !IO),
choose(R, Ps, !IO)
else if Choice = "q" then
R = eof
else
io.write_string("Sorry, I didn't understand that.\n", !IO),
choose(R, Ps, !IO)
)
;
Res = eof, R = eof
;
Res = error(E), R = error(E)
).
:- pred show_choices(func(reviewed) = string, int, list(reviewed), io, io).
:- mode show_choices(in, in, in, di, uo) is det.
show_choices(_, _, [], !IO) :- io.nl(!IO).
show_choices(Dump, N, [P | Ps], !IO) :-
io.format("[%d] %s", [i(N), s(Dump(P))], !IO),
show_choices(Dump, N + 1, Ps, !IO).