X-Git-Url: https://ruderich.org/simon/gitweb/?p=config%2Fdotfiles.git;a=blobdiff_plain;f=lib.sh;fp=lib.sh;h=72040244499bd585ebfbbd6a2edac57048331ddc;hp=5abd27898400e44bbaed2c14ec4a45ce80b8f480;hb=a82683769e49aba668f48c8cbe6d8efd9d940b4b;hpb=8348ecd5e6759f9551fac0431a45c3bdffc36956 diff --git a/lib.sh b/lib.sh index 5abd278..7204024 100644 --- a/lib.sh +++ b/lib.sh @@ -138,67 +138,45 @@ link() { 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() # -# - 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 }