Colin Cross | b852a58 | 2017-08-10 17:58:12 -0700 | [diff] [blame^] | 1 | #!/bin/bash -e |
| 2 | |
| 3 | # Script that takes a list of files on stdin and converts it to arguments to jar on stdout |
| 4 | # Usage: |
| 5 | # find $dir -type f | sort | jar-args.sh $dir > jar_args.txt |
| 6 | # jar cf out.jar @jar_args.txt |
| 7 | |
| 8 | case $(uname) in |
| 9 | Linux) |
| 10 | extended_re=-r |
| 11 | ;; |
| 12 | Darwin) |
| 13 | extended_re=-E |
| 14 | ;; |
| 15 | *) echo "unknown OS:" $(uname) >&2 && exit 1;; |
| 16 | esac |
| 17 | |
| 18 | if [ "$1" == "--test" ]; then |
| 19 | in=$(mktemp) |
| 20 | expected=$(mktemp) |
| 21 | out=$(mktemp) |
| 22 | cat > $in <<EOF |
| 23 | a |
| 24 | a/b |
| 25 | a/b/' |
| 26 | a/b/" |
| 27 | a/b/\\ |
| 28 | a/b/# |
| 29 | a/b/a |
| 30 | EOF |
| 31 | cat > $expected <<EOF |
| 32 | |
| 33 | -C 'a' 'b' |
| 34 | -C 'a' 'b/\\'' |
| 35 | -C 'a' 'b/"' |
| 36 | -C 'a' 'b/\\\\' |
| 37 | -C 'a' 'b/#' |
| 38 | -C 'a' 'b/a' |
| 39 | EOF |
| 40 | cat $in | $0 a > $out |
| 41 | |
| 42 | if cmp $out $expected; then |
| 43 | status=0 |
| 44 | echo "PASS" |
| 45 | else |
| 46 | status=1 |
| 47 | echo "FAIL" |
| 48 | echo "got:" |
| 49 | cat $out |
| 50 | echo "expected:" |
| 51 | cat $expected |
| 52 | fi |
| 53 | rm -f $in $expected $out |
| 54 | exit $status |
| 55 | fi |
| 56 | |
| 57 | # In order, the regexps: |
| 58 | # - Strip $1/ from the beginning of each line, and everything from lines that just have $1 |
| 59 | # - Escape single and double quotes, '#', ' ', and '\' |
| 60 | # - Prefix each non-blank line with -C $1 |
| 61 | sed ${extended_re} \ |
| 62 | -e"s,^$1(/|\$),," \ |
| 63 | -e"s,(['\\]),\\\\\1,g" \ |
| 64 | -e"s,^(.+),-C '$1' '\1'," |