Cookbook: add an example of using default.do for text processing.

This commit is contained in:
Avery Pennarun 2018-11-26 13:10:29 -05:00
commit 3b305edc7e
18 changed files with 377 additions and 28 deletions

View file

@ -0,0 +1,5 @@
date
version
include/version.h
version.py
test.txt

View file

@ -0,0 +1,2 @@
redo-ifchange test.txt version.py \
test.py include/version.h

View file

@ -0,0 +1,2 @@
rm -f date version include/version.h \
version.py test.txt *~ .*~

View file

@ -0,0 +1,3 @@
date +'%Y-%m-%d' >$3
redo-always
redo-stamp <$3

View file

@ -0,0 +1,25 @@
# $1 is the target name, eg. test.txt
# $2 in the same as $1. We'll talk about
# that in a later example.
# $3 is the temporary output file we should
# create. If this script is successful,
# redo will atomically replace $1 with $3.
if [ -e "$1.in" ]; then
# if a .in file exists, then do some
# text substitution.
#
# Remember, the user asks redo to build
# a particular *target* name. It's the .do
# file's job to figure out what source file(s)
# to use to generate the target.
redo-ifchange "$1.in" version date
read VERSION <version
read DATE <date
sed -e "s/%VERSION%/$VERSION/g" \
-e "s/%DATE%/$DATE/g" \
<$1.in >$3
else
echo "$0: Fatal: don't know how to build '$1'" >&2
exit 99
fi

View file

@ -0,0 +1,8 @@
// C/C++ header file identifying the current version
#ifndef __VERSION_H
#define __VERSION_H
#define VERSION "%VERSION%"
#define DATE "%DATE%"
#endif // __VERSION_H

View file

@ -0,0 +1,6 @@
#!/usr/bin/env python
"""Test program for auto-generated version.py"""
import version
print('Version %r has build date %r'
% (version.VERSION, version.DATE))

View file

@ -0,0 +1,2 @@
This is the documentation for MyProgram version
%VERSION%. It was generated on %DATE%.

View file

@ -0,0 +1,7 @@
# Try to get a version number from git, if possible.
if ! git describe >$3; then
echo "$0: Falling back to static version." >&2
echo 'UNKNOWN' >$3
fi
redo-always
redo-stamp <$3

View file

@ -0,0 +1,3 @@
# python module identifying the current version
VERSION='%VERSION%'
DATE='%DATE%'