iirina | 7aace9c | 2019-03-18 05:41:55 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Copyright 2019 The Bazel Authors. All rights reserved. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # A script that zips all the inputs files under the given directory structure |
| 18 | # in the output zip file. |
| 19 | |
| 20 | # Usage: third_party/zip_files.sh directory_prefix output_zip [input_files] |
| 21 | # |
| 22 | # For example: third_party_zip_files.sh src/main/cpp my_archive.zip a.cc b.cc |
| 23 | # will create the archive my_archive.zip containing: |
| 24 | # src/main/cpp/a.cc |
| 25 | # src/main/cpp/b.cc |
| 26 | |
| 27 | set -euo pipefail |
| 28 | |
| 29 | directory_prefix="$1"; shift |
| 30 | output="$1"; shift |
| 31 | |
| 32 | initial_pwd="$(pwd)" |
| 33 | |
Googler | b0172be | 2020-06-09 05:05:08 -0700 | [diff] [blame] | 34 | tmp_dir=$(mktemp -d -t 'tmp_bazel_zip_files_XXXXXX') |
iirina | 7aace9c | 2019-03-18 05:41:55 -0700 | [diff] [blame] | 35 | trap "rm -fr $tmp_dir" EXIT |
| 36 | tmp_zip="$tmp_dir/archive.zip" |
| 37 | |
| 38 | zip -j -q "$tmp_zip" "$@" |
| 39 | |
| 40 | mkdir -p "$tmp_dir/$directory_prefix" |
| 41 | cd "$tmp_dir/$directory_prefix" |
iirina | e4ac616 | 2019-04-08 01:37:20 -0700 | [diff] [blame] | 42 | unzip -q -o "$tmp_zip" |
iirina | 7aace9c | 2019-03-18 05:41:55 -0700 | [diff] [blame] | 43 | rm -f "$tmp_zip" |
| 44 | cd "$tmp_dir" |
| 45 | zip -r -q "$tmp_zip" "$directory_prefix" |
| 46 | |
| 47 | cd "$initial_pwd" |
| 48 | mv -f "$tmp_zip" "$output" |
| 49 | rm -r "$tmp_dir" |
| 50 | |