blob: 1422a6c65947d308491c3726626402dd29490bac [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 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
27set -euo pipefail
28
29directory_prefix="$1"; shift
30output="$1"; shift
31
32initial_pwd="$(pwd)"
33
Googlerb0172be2020-06-09 05:05:08 -070034tmp_dir=$(mktemp -d -t 'tmp_bazel_zip_files_XXXXXX')
iirina7aace9c2019-03-18 05:41:55 -070035trap "rm -fr $tmp_dir" EXIT
36tmp_zip="$tmp_dir/archive.zip"
37
38zip -j -q "$tmp_zip" "$@"
39
40mkdir -p "$tmp_dir/$directory_prefix"
41cd "$tmp_dir/$directory_prefix"
iirinae4ac6162019-04-08 01:37:20 -070042unzip -q -o "$tmp_zip"
iirina7aace9c2019-03-18 05:41:55 -070043rm -f "$tmp_zip"
44cd "$tmp_dir"
45zip -r -q "$tmp_zip" "$directory_prefix"
46
47cd "$initial_pwd"
48mv -f "$tmp_zip" "$output"
49rm -r "$tmp_dir"
50