diff options
author | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2005-02-15 17:44:03 +0000 |
---|---|---|
committer | Eelco Dolstra <eelco.dolstra@logicblox.com> | 2005-02-15 17:44:03 +0000 |
commit | cbdd91f2a667cb0452a7f3fbf9288be1571cbabf (patch) | |
tree | 13ca5e0165d651a6d813d812ca0ef68d6ef16a63 /pkgs/build-support | |
parent | aac8011c8bcbf0bb098d49a7acaefbfdf400ddaa (diff) |
* Some utility functions to make performing substitutions in text
files easier. Examples: substitute inputFile outputFile \ --replace "@bindir@" "$out/bin" \ --replace "@gcc@" "$GCC/bin/gcc" substitute inputFile outputFile --subst-var out (this is sugar for --replace "@out@" "$out") substituteInPlace file --replace a b (input and output are both `file'; the execute bit is preserved) svn path=/nixpkgs/trunk/; revision=2239
Diffstat (limited to 'pkgs/build-support')
-rw-r--r-- | pkgs/build-support/substitute/substitute.sh | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/pkgs/build-support/substitute/substitute.sh b/pkgs/build-support/substitute/substitute.sh new file mode 100644 index 0000000000000..bd318522cae1c --- /dev/null +++ b/pkgs/build-support/substitute/substitute.sh @@ -0,0 +1,39 @@ +substitute() { + input=$1 + output=$2 + + params=("$@") + + sedArgs=() + + for ((n = 2; n < ${#params[*]}; n += 1)); do + p=${params[$n]} + + if test "$p" = "--replace"; then + pattern=${params[$((n + 1))]} + replacement=${params[$((n + 2))]} + n=$((n + 2)) + sedArgs=("${sedArgs[@]}" "-e" "s^$pattern^$replacement^g") + fi + + if test "$p" = "--subst-var"; then + varName=${params[$((n + 1))]} + n=$((n + 1)) + sedArgs=("${sedArgs[@]}" "-e" "s^@${varName}@^${!varName}^g") + fi + + done + + sed "${sedArgs[@]}" < "$input" > "$output".tmp + if test -x "$output"; then + chmod +x "$output".tmp + fi + mv -f "$output".tmp "$output" +} + + +substituteInPlace() { + fileName="$1" + shift + substitute "$fileName" "$fileName" "$@" +} |