From: Richard W.M. Jones Date: Sun, 12 Jan 2020 19:51:56 +0000 (+0000) Subject: stdlib: Implement dirname(), basename() and extension() functions. X-Git-Tag: v'0.2'~28 X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=9607fb2d34d679e14040f9132625a53f1de5200a;p=goals.git stdlib: Implement dirname(), basename() and extension() functions. --- diff --git a/docs/Goalfile.pod b/docs/Goalfile.pod index 9731cb1..75ae4fc 100644 --- a/docs/Goalfile.pod +++ b/docs/Goalfile.pod @@ -14,6 +14,30 @@ Goalfile - introduction, tutorial, and reference for writing goal files =head2 Standard Functions +=head3 basename (path) + +For example: + + basename ("dir/file.ext") ⇒ "file.ext" + +Returns the filename part of the path. + +=head3 dirname (path) + +For example: + + dirname ("dir/file.ext") ⇒ "dir" + +Returns the directory part of the path. + +=head3 extension (filename) + +For example: + + extension ("dir/file.ext") ⇒ "ext" + +Returns the filename extension. + =head3 sort (list) For example: diff --git a/stdlib/prelude.gl b/stdlib/prelude.gl index 8315b6f..d496d71 100644 --- a/stdlib/prelude.gl +++ b/stdlib/prelude.gl @@ -69,6 +69,22 @@ pure function subst (from, to, text) returning string = @{ #---------------------------------------------------------------------- # File functions. +# Base name. +pure function basename (name) returning string = @{ + basename %name +} + +# Directory name. +pure function dirname (name) returning string = @{ + dirname %name +} + +# File extension. +pure function extension (name) returning string = @{ + name=%name + echo "${name##*.}" +} + # Expand a wildcard into a list of filenames. # # This function is probably not "pure" since it depends on the diff --git a/tests/10-function-basename.gl b/tests/10-function-basename.gl new file mode 100644 index 0000000..4ec2c58 --- /dev/null +++ b/tests/10-function-basename.gl @@ -0,0 +1,23 @@ +# Goals test. +# Copyright (C) 2020 Richard W.M. Jones +# Copyright (C) 2020 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +# Test the basename function. + +let s = basename ("dir/basename.ext") + +goal all = { echo %s } diff --git a/tests/10-function-basename.sh b/tests/10-function-basename.sh new file mode 100755 index 0000000..5d42783 --- /dev/null +++ b/tests/10-function-basename.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# Goals test. +# Copyright (C) 2020 Richard W.M. Jones +# Copyright (C) 2020 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +set -e + +goals -f 10-function-basename.gl > 10-function-basename.out +test "$(cat 10-function-basename.out)" = "basename.ext" +rm 10-function-basename.out diff --git a/tests/10-function-dirname.gl b/tests/10-function-dirname.gl new file mode 100644 index 0000000..9fdd22b --- /dev/null +++ b/tests/10-function-dirname.gl @@ -0,0 +1,23 @@ +# Goals test. +# Copyright (C) 2020 Richard W.M. Jones +# Copyright (C) 2020 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +# Test the dirname function. + +let s = dirname ("dir/basename.ext") + +goal all = { echo %s } diff --git a/tests/10-function-dirname.sh b/tests/10-function-dirname.sh new file mode 100755 index 0000000..6af6e70 --- /dev/null +++ b/tests/10-function-dirname.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# Goals test. +# Copyright (C) 2020 Richard W.M. Jones +# Copyright (C) 2020 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +set -e + +goals -f 10-function-dirname.gl > 10-function-dirname.out +test "$(cat 10-function-dirname.out)" = "dir" +rm 10-function-dirname.out diff --git a/tests/10-function-extension.gl b/tests/10-function-extension.gl new file mode 100644 index 0000000..167f85e --- /dev/null +++ b/tests/10-function-extension.gl @@ -0,0 +1,23 @@ +# Goals test. +# Copyright (C) 2020 Richard W.M. Jones +# Copyright (C) 2020 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +# Test the extension function. + +let s = extension ("dir/basename.ext") + +goal all = { echo %s } diff --git a/tests/10-function-extension.sh b/tests/10-function-extension.sh new file mode 100755 index 0000000..66786d3 --- /dev/null +++ b/tests/10-function-extension.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# Goals test. +# Copyright (C) 2020 Richard W.M. Jones +# Copyright (C) 2020 Red Hat Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +set -e + +goals -f 10-function-extension.gl > 10-function-extension.out +test "$(cat 10-function-extension.out)" = "ext" +rm 10-function-extension.out