Remove warning(), inlined into generate().
Also remove old unneeded tests.
cd "$pwd"
}
-# Write a warning to $1 to make clear it should not be modified. $2 is the
-# source for the generated file. Also print a message to stdout that the file
-# $1 was generated from $2 using the command $3 with options $4.
-warning() {
- echo "###################################" > $1
- echo "# WARNING! DO NOT EDIT THIS FILE! #" >> $1
- echo "###################################" >> $1
- echo >> $1
- echo "# It was generated from $2 on `date`." >> $1
- echo >> $1
-
- # Display given options if there were any (Zsh has a problem with $options
- # as variable name).
- option=
- if test -n "$4"; then
- option=" with options '$4'"
- fi
- # Write message to stdout.
- echo "$3: generating '$1' from '$2'$option"
-
- unset option
-}
-
-# Generate a file using several methods. A warning not to edit it is
-# automatically added to the created file and a message printed to stdout
-# through warning().
+# Generate a file from a source file using a given command. A warning not to
+# edit it is automatically added to the created file.
#
-# The following commands are possible; the file extension for the source file
-# in brackets.
+# Usage: generated() <file> <extension> <cmd..>
#
-# - m4 (.m4): pipe $2.m4 through m4 then write it to $2
-# - awk (.in): pipe $2.in through awk then write it to $2
-# - perl (.in): pipe $2.in through perl then write it to $2
-# - cat ($3): copy $2$3 to $2
+# If an empty extension is provided, the file is modified in-place (through a
+# temporary file).
generate() {
+ local file >/dev/null 2>&1 || true
+ local file_tmp >/dev/null 2>&1 || true
+ local extension >/dev/null 2>&1 || true
+
# Get command and target file.
- command="$1"
- file="$2"
- # Remove arguments from list.
+ file="$1"
+ extension="$2"
shift
shift
- # Set extension for the used commands. When cat is used $3 is used as
- # extension.
- if test x"$command" = xm4; then
- extension=.m4
- elif test x"$command" = xawk -o x"$command" = xperl; then
- extension=.in
- elif test x"$command" = xcat; then
- extension="$1" # is $3 in reality, $1 because of shifting
- shift
- # Print a warning and exit if an unsupported command is used.
+ if test -z "$extension"; then
+ file_tmp="$file.tmp"
else
- echo "generate(): command '$command' not supported!" >&2
- exit 1
+ # We only need this message if we generate a new file.
+ printf "%s: generating from '%s' (%s)\n" \
+ "$file" "$file$extension" "$1"
+
+ echo '###################################' >"$file"
+ echo '# WARNING! DO NOT EDIT THIS FILE! #' >>"$file"
+ echo '###################################' >>"$file"
+ echo >>"$file"
+ echo "# It was generated from $file$extension on `date`." >>"$file"
+ echo >>"$file"
+
+ file_tmp="$file"
fi
- # Add warning to file and write a message to stdout.
- warning "$file" "$file$extension" $command "$*"
# Generate $file from $file$extension using the given command.
- cat "$file$extension" | $command "$@" >> "$file"
+ "$@" <"$file$extension" >>"$file_tmp"
- unset command file
+ if test -z "$extension"; then
+ mv "$file_tmp" "$file"
+ fi
}
+++ /dev/null
-# Tests for lib.sh.
-
-# Copyright (C) 2009-2013 Simon Ruderich
-#
-# 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 3 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, see <http://www.gnu.org/licenses/>.
-
-
-. ../lib.sh
-
-
-# Tests for generate().
-echo "Simple test file for generate() using m4.
-include(../lib.m4)dnl
-IF(TEST,m4)
- m4
-FI
-IF(TEST,n4)
- n4
-FI" > tmp/test.m4
-generate m4 tmp/test -DTEST=m4
-# Remove the line with the current date as it changes every time.
-cat tmp/test | grep -v "It was generated from tmp/test.m4"
-
-# Test multiple arguments to generate().
-echo "Test with multiple arguments passed to generate().
-first: FIRST
-second: SECOND
-" > tmp/test-multiple.m4
-generate m4 tmp/test-multiple -DFIRST=first -DSECOND=second
-# Remove the line with the current date as it changes every time.
-cat tmp/test-multiple | grep -v "It was generated from tmp/test-multiple.m4"
-
-# Test multiple arguments with spaces to generate().
-echo "Test with multiple arguments with spaces passed to generate().
-first: FIRST
-second: SECOND
-" > tmp/test-multiple-spaces.m4
-generate m4 tmp/test-multiple-spaces -DFIRST="first with spaces" \
- -DSECOND="second with spaces"
-# Remove the line with the current date as it changes every time.
-cat tmp/test-multiple-spaces \
- | grep -v "It was generated from tmp/test-multiple-spaces.m4"
-
-# Test generate() using awk.
-echo "Simple test fiel for generate() using awk.
-first second
-" > tmp/test-awk.in
-generate awk tmp/test-awk '{ print $1 }'
-# Remove the line with the current date as it changes every time.
-cat tmp/test-awk | grep -v "It was generated from tmp/test-awk.in"
-
-# Test generate() using perl.
-echo "Simple test fiel for generate() using perl.
-first second
-" > tmp/test-perl.in
-generate perl tmp/test-perl -p -e 's/first/best/'
-# Remove the line with the current date as it changes every time.
-cat tmp/test-perl | grep -v "It was generated from tmp/test-perl.in"
-
-# Test generate() using cat.
-echo "Simple test fiel for generate() using cat (simple copy).
-
-...
-" > tmp/test-cat.real
-generate cat tmp/test-cat .real
-# Remove the line with the current date as it changes every time.
-cat tmp/test-cat | grep -v "It was generated from tmp/test-cat.real"
-
-# vim: ft=sh
+++ /dev/null
-m4: generating 'tmp/test' from 'tmp/test.m4' with options '-DTEST=m4'
-###################################
-# WARNING! DO NOT EDIT THIS FILE! #
-###################################
-
-
-Simple test file for generate() using m4.
- m4
-m4: generating 'tmp/test-multiple' from 'tmp/test-multiple.m4' with options '-DFIRST=first -DSECOND=second'
-###################################
-# WARNING! DO NOT EDIT THIS FILE! #
-###################################
-
-
-Test with multiple arguments passed to generate().
-first: first
-second: second
-
-m4: generating 'tmp/test-multiple-spaces' from 'tmp/test-multiple-spaces.m4' with options '-DFIRST=first with spaces -DSECOND=second with spaces'
-###################################
-# WARNING! DO NOT EDIT THIS FILE! #
-###################################
-
-
-Test with multiple arguments with spaces passed to generate().
-first: first with spaces
-second: second with spaces
-
-awk: generating 'tmp/test-awk' from 'tmp/test-awk.in' with options '{ print $1 }'
-###################################
-# WARNING! DO NOT EDIT THIS FILE! #
-###################################
-
-
-Simple
-first
-
-perl: generating 'tmp/test-perl' from 'tmp/test-perl.in' with options '-p -e s/first/best/'
-###################################
-# WARNING! DO NOT EDIT THIS FILE! #
-###################################
-
-
-Simple test fiel for generate() using perl.
-best second
-
-cat: generating 'tmp/test-cat' from 'tmp/test-cat.real'
-###################################
-# WARNING! DO NOT EDIT THIS FILE! #
-###################################
-
-
-Simple test fiel for generate() using cat (simple copy).
-
-...
-
+++ /dev/null
-#!/bin/sh
-
-# Runs all tests in this directory.
-
-# Copyright (C) 2009-2013 Simon Ruderich
-#
-# 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 3 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, see <http://www.gnu.org/licenses/>.
-
-
-# Get all test files.
-for file in *.test; do
- # Create temporary directory.
- mkdir -p tmp
-
- # Run the test file in sh and zsh and get its output.
- sh $file > $file.out.sh 2>&1
- zsh $file > $file.out.zsh 2>&1
-
- # Check if the output matches the expected one. If not abort with exit
- # code 1.
- diff -u $file.out $file.out.sh || exit 1
- diff -u $file.out $file.out.zsh || exit 1
-
- # Remove temporary directory.
- rm -rf tmp
-done
-
-# Remove all temporary files.
-rm *.out.sh
-rm *.out.zsh