forked from larsyencken/marelle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.pl
55 lines (48 loc) · 1.19 KB
/
util.pl
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
%
% util.pl
% computer-deps
%
% Utility methods common to multiple deps.
%
expand_path(Path0, Path) :-
( atom_concat('~/', Suffix, Path0) ->
getenv('HOME', Home),
join([Home, '/', Suffix], Path)
;
Path = Path0
).
isfile(Path0) :-
expand_path(Path0, Path),
exists_file(Path).
isdir(Path0) :-
expand_path(Path0, Path),
exists_directory(Path).
make_executable(Path) :-
sh(['chmod a+x ', Path]).
curl(Source, Dest) :-
sh(['curl -s -o ', Dest, ' ', Source]).
% sformat(+S0, +Vars, -S) is semidet.
% String interpolation, where {} is replaced by an argument in the list.
% Will fail if the number of {} is not the same as the number of vars passed
% in.
%
% sformat('Hello ~a!', ['Bob'], 'Hello Bob!').
%
sformat(S0, Vars, S) :-
atomic_list_concat(Parts, '~a', S0),
( length(Vars, N), N1 is N + 1, length(Parts, N1) ->
true
;
throw('wrong number of arguments in interpolation')
),
interleave(Parts, Vars, S1),
atomic_list_concat(S1, '', S).
interleave(Xs, Ys, Zs) :-
( Ys = [] ->
Zs = Xs
;
Ys = [Y|Yr],
Xs = [X|Xr],
Zs = [X, Y|Zr],
interleave(Xr, Yr, Zr)
).