1 # Goals stdlib prelude.
2 # Copyright (C) 2019 Richard W.M. Jones
3 # Copyright (C) 2019 Red Hat Inc.
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.
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.
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.
19 # This file is included first and automatically in all Goalfiles
20 # (unless you use --no-prelude). It contains standard functions and
23 # The only predicate that ‘make’ has.
24 predicate is-file (filename) = @{
25 # Rebuild if the target file doesn't exist at all.
26 test -f %filename || exit 99
28 # Otherwise rebuild if it is older than any dependency.
30 test %filename -ot "$f" && exit 99 ||:
34 # This is a simpler predicate than the above since it will
35 # rebuild if the file is missing, but not if it is older.
36 predicate is-file-exists (filename) = @{
37 test -f %filename || exit 99
40 #----------------------------------------------------------------------
43 function error (msg) = @{
48 # Wrap list of strings in a call or predicate.
49 pure function wrap (wrapper, xs) = @{
59 #----------------------------------------------------------------------
62 # Filter a list by regexp.
63 pure function filter (pat, xs) returning strings = @{
64 for f in %xs; do echo "$f"; done | grep -E -- %pat
67 # Filter out a list by regexp.
68 pure function filter-out (pat, xs) returning strings = @{
69 for f in %xs; do echo "$f"; done | grep -v -E -- %pat
73 pure function head (xs) returning string = @{
81 pure function join (xs, ys) returning strings = @{
82 for f in %xs %ys; do echo "$f"; done
85 # Last element of a list.
86 pure function last (xs) returning string = @{
93 # n'th element of a list.
94 pure function nth (n, xs) returning string = @{
98 if [ $i -eq %n ]; then r="$f"; fi
104 # Sort + uniq a list.
105 pure function sort (xs) returning strings = @{
106 for f in %xs; do echo "$f"; done | sort -u
109 # Split a string into a list.
110 # https://superuser.com/a/1066541
111 pure function split (s) returning strings = @{
113 eval 'for f in '$s'; do echo "$f"; done'
117 pure function subst (from, to, text) returning string = @{
118 # We need to replace any / characters in ‘to’ with escaped ones.
119 to="$( echo -n %to | sed 's,/,\\/,g' )"
120 echo %text | sed -E s/%from/$to/g
124 pure function tail (xs) returning strings = @{
127 if [ -z "$drop" ]; then echo "$f"; fi
132 #----------------------------------------------------------------------
136 pure function basename (name) returning string = @{
141 pure function dirname (name) returning string = @{
146 pure function extension (name) returning string = @{
152 function read (filename) returning string = @{
156 # Read a file as a list of lines.
157 function readlines (filename) returning strings = @{
162 function realpath (filename) returning string = @{
163 realpath -- %filename
166 # Expand a wildcard into a list of filenames.
168 # This function is probably not "pure" since it depends on the
169 # current working directory and also files may be created in
170 # the directory while goals is running which would affect the
172 function wildcard (wc) returning strings = @{
174 # Note that the substitution is quoted by goals, so to expand
175 # it we must assign it to a variable and then use it unquoted.
177 for f in $wc; do echo "$f"; done