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 the content of the inputs zip files under a given directory |
| 18 | # structure in the output zip file. "-" can be passed if no top-level directory |
| 19 | # structure is required. |
| 20 | |
| 21 | # Usage: third_party/merge_zip_files.sh directory_prefix output_zip [input_zip_files] |
| 22 | # |
| 23 | # For example, if we have the following zips and their content: |
| 24 | # a.zip: |
| 25 | # dir1/a1.cc |
| 26 | # a2.cc |
| 27 | # b.zip: |
| 28 | # dir2/b1.cc |
| 29 | # b2.cc |
| 30 | # |
| 31 | # third_party_zip_files.sh src/main/cpp my_archive.zip a.zip b.zip |
| 32 | # will create the archive my_archive.zip containing: |
| 33 | # src/main/cpp/a2.cc |
| 34 | # src/main/cpp/b2.cc |
| 35 | # src/main/cpp/dir1/a1.cc |
| 36 | # src/main/cpp/dir2/b1.cc |
| 37 | # |
| 38 | # third_party_zip_files.sh - my_archive.zip a.zip b.zip |
| 39 | # will create the archive my_archive.zip containing: |
| 40 | # a2.cc |
| 41 | # b2.cc |
| 42 | # dir1/a1.cc |
| 43 | # dir2/b1.cc |
| 44 | |
| 45 | set -euo pipefail |
| 46 | |
| 47 | directory_prefix="$1"; shift |
| 48 | output="$1"; shift |
| 49 | |
| 50 | initial_pwd="$(pwd)" |
| 51 | |
| 52 | tmp_dir=$(mktemp -d -t 'tmp_bazel_zip_files_XXXXX') |
| 53 | trap "rm -fr $tmp_dir" EXIT |
| 54 | tmp_zip="$tmp_dir/archive.zip" |
| 55 | |
| 56 | if [[ "$directory_prefix" == "-" ]]; then |
| 57 | for curr_zip in "$@" |
| 58 | do |
iirina | e4ac616 | 2019-04-08 01:37:20 -0700 | [diff] [blame] | 59 | unzip -q -o "$curr_zip" -d "$tmp_dir" |
iirina | 7aace9c | 2019-03-18 05:41:55 -0700 | [diff] [blame] | 60 | done |
| 61 | |
| 62 | cd "$tmp_dir" |
| 63 | zip -9 -r -q "$tmp_zip" "." |
| 64 | else |
| 65 | mkdir -p "$tmp_dir/$directory_prefix" |
| 66 | for curr_zip in "$@" |
| 67 | do |
iirina | e4ac616 | 2019-04-08 01:37:20 -0700 | [diff] [blame] | 68 | unzip -q -o "$curr_zip" -d "$tmp_dir/$directory_prefix" |
iirina | 7aace9c | 2019-03-18 05:41:55 -0700 | [diff] [blame] | 69 | done |
| 70 | |
| 71 | cd "$tmp_dir" |
| 72 | zip -9 -r -q "$tmp_zip" "$directory_prefix" |
| 73 | fi |
| 74 | |
| 75 | cd "$initial_pwd" |
| 76 | mv -f "$tmp_zip" "$output" |