e2d04007c1b895958ccb884fed40ba18da972f86
[goals.git] / stdlib / prelude.gl
1 # Goals stdlib prelude.
2 # Copyright (C) 2019 Richard W.M. Jones
3 # Copyright (C) 2019 Red Hat Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 # This file is included first and automatically in all Goalfiles
20 # (unless you use --no-prelude).  It contains standard functions and
21 # tactics.
22
23 # The only tactic that ‘make’ has.
24 tactic *file (filename) = @{
25     # Rebuild if the target file doesn't exist at all.
26     test -f %filename || exit 99
27
28     # Otherwise rebuild if it is older than any dependency.
29     for f in %<; do
30         test %filename -ot "$f" && exit 99 ||:
31     done
32 }
33
34 # This is a simpler tactic than the above since it will
35 # rebuild if the file is missing, but not if it is older.
36 tactic *exists (filename) = @{
37     test -f %filename || exit 99
38 }
39
40 #----------------------------------------------------------------------
41 # Basic functions.
42
43 # Wrap list of strings in a call or tactic.
44 pure function wrap (wrapper, xs) = @{
45     echo '['
46     for x in %xs; do
47         echo %wrapper "( "
48         quoted_string "$x"
49         echo " ),"
50     done
51     echo ']'
52 }
53
54 #----------------------------------------------------------------------
55 # Text functions.
56
57 # Head of a list.
58 pure function head (xs) returning string = @{
59     for f in %xs; do
60         echo "$f"
61         exit 0
62     done
63 }
64
65 # Join two lists.
66 pure function join (xs, ys) returning strings = @{
67     for f in %xs %ys; do echo "$f"; done
68 }
69
70 # Sort + uniq a list.
71 pure function sort (xs) returning strings = @{
72     for f in %xs; do echo "$f"; done | sort -u
73 }
74
75 # Substitute.
76 pure function subst (from, to, text) returning string = @{
77     # We need to replace any / characters in ‘to’ with escaped ones.
78     to="$( echo -n %to | sed 's,/,\\/,g' )"
79     echo %text | sed -E s/%from/$to/g
80 }
81
82 # Tail of a list.
83 pure function tail (xs) returning strings = @{
84     drop=1
85     for f in %xs; do
86         if [ -z "$drop" ]; then echo "$f"; fi
87         drop=
88     done
89 }
90
91 #----------------------------------------------------------------------
92 # File functions.
93
94 # Base name.
95 pure function basename (name) returning string = @{
96     basename %name
97 }
98
99 # Directory name.
100 pure function dirname (name) returning string = @{
101     dirname %name
102 }
103
104 # File extension.
105 pure function extension (name) returning string = @{
106     name=%name
107     echo "${name##*.}"
108 }
109
110 # Real path.
111 function realpath (filename) returning string = @{
112     realpath -- %filename
113 }
114
115 # Expand a wildcard into a list of filenames.
116 #
117 # This function is probably not "pure" since it depends on the
118 # current working directory and also files may be created in
119 # the directory while goals is running which would affect the
120 # result.
121 function wildcard (wc) returning strings = @{
122     shopt -s nullglob
123     # Note that the substitution is quoted by goals, so to expand
124     # it we must assign it to a variable and then use it unquoted.
125     wc=%wc
126     for f in $wc; do echo "$f"; done
127 }