jar-args.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/bash -e
  2. # Copyright 2017 Google Inc. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Script that takes a list of files on stdin and converts it to arguments to jar on stdout
  16. # Usage:
  17. # find $dir -type f | sort | jar-args.sh $dir > jar_args.txt
  18. # jar cf out.jar @jar_args.txt
  19. case $(uname) in
  20. Linux)
  21. extended_re=-r
  22. ;;
  23. Darwin)
  24. extended_re=-E
  25. ;;
  26. *) echo "unknown OS:" $(uname) >&2 && exit 1;;
  27. esac
  28. if [ "$1" == "--test" ]; then
  29. in=$(mktemp)
  30. expected=$(mktemp)
  31. out=$(mktemp)
  32. cat > $in <<EOF
  33. a
  34. a/b
  35. a/b/'
  36. a/b/"
  37. a/b/\\
  38. a/b/#
  39. a/b/a
  40. EOF
  41. cat > $expected <<EOF
  42. -C 'a' 'b'
  43. -C 'a' 'b/\\''
  44. -C 'a' 'b/"'
  45. -C 'a' 'b/\\\\'
  46. -C 'a' 'b/#'
  47. -C 'a' 'b/a'
  48. EOF
  49. cat $in | $0 a > $out
  50. if cmp $out $expected; then
  51. status=0
  52. echo "PASS"
  53. else
  54. status=1
  55. echo "FAIL"
  56. echo "got:"
  57. cat $out
  58. echo "expected:"
  59. cat $expected
  60. fi
  61. rm -f $in $expected $out
  62. exit $status
  63. fi
  64. # In order, the regexps:
  65. # - Strip $1/ from the beginning of each line, and everything from lines that just have $1
  66. # - Escape single and double quotes, '#', ' ', and '\'
  67. # - Prefix each non-blank line with -C $1
  68. sed ${extended_re} \
  69. -e"s,^$1(/|\$),," \
  70. -e"s,(['\\]),\\\\\1,g" \
  71. -e"s,^(.+),-C '$1' '\1',"