blob: c148a9a44d9f00061c43223335df8b4cb4f520d9 [file] [log] [blame]
iirina7aace9c2019-03-18 05:41:55 -07001#!/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
45set -euo pipefail
46
47directory_prefix="$1"; shift
48output="$1"; shift
49
50initial_pwd="$(pwd)"
51
52tmp_dir=$(mktemp -d -t 'tmp_bazel_zip_files_XXXXX')
53trap "rm -fr $tmp_dir" EXIT
54tmp_zip="$tmp_dir/archive.zip"
55
56if [[ "$directory_prefix" == "-" ]]; then
57 for curr_zip in "$@"
58 do
iirinae4ac6162019-04-08 01:37:20 -070059 unzip -q -o "$curr_zip" -d "$tmp_dir"
iirina7aace9c2019-03-18 05:41:55 -070060 done
61
62 cd "$tmp_dir"
63 zip -9 -r -q "$tmp_zip" "."
64else
65 mkdir -p "$tmp_dir/$directory_prefix"
66 for curr_zip in "$@"
67 do
iirinae4ac6162019-04-08 01:37:20 -070068 unzip -q -o "$curr_zip" -d "$tmp_dir/$directory_prefix"
iirina7aace9c2019-03-18 05:41:55 -070069 done
70
71 cd "$tmp_dir"
72 zip -9 -r -q "$tmp_zip" "$directory_prefix"
73fi
74
75cd "$initial_pwd"
76mv -f "$tmp_zip" "$output"