blob: 4dedc6780be44764f781608767e947f69f388ff4 [file] [log] [blame]
#!/bin/bash
# Copyright 2023 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#### CATEGORY=Other
### Create pb.zip suitable for flashing based on product_bundles.json file.
## The created zip file will default to ${pwd}/pb.zip
## usage: fx create-pb-zip [-b <product_bundle>] [-o <output>]
## -o Output location of built zip file
## -b Name of product bundle to zip
set -o errexit
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"/lib/vars.sh
fx-config-read
main() {
local output="$(pwd)"
local product_name=""
local product_bundle_path=""
local product_bundles_path="${FUCHSIA_BUILD_DIR}/product_bundles.json"
while [[ $# -ge 1 ]]; do
case "$1" in
-o)
shift
output=$(realpath "$1")
;;
-b)
shift
product_name="$1"
product_bundle_path=$(fx-command-run jq -r ".[] | select(.name==\"${product_name}\") | .path" ${product_bundles_path})
if [[ $product_bundle_path == "" ]]; then
fx-error "Invalid product bundle $product_name"
exit 1
fi
;;
esac
shift
done
if [[ -d $output ]]; then
output="${output}/pb.zip"
fi
if [[ ! -d ${FUCHSIA_BUILD_DIR}/${product_bundle_path} ]]; then
fx-error "Product bundle $product_name not found at path ${FUCHSIA_BUILD_DIR}/${product_bundle_path//\"}"
exit 1
fi
if [[ -z "${product_name}" ]]; then
product_bundle_path="$(fx-command-run get-main-pb-path)"
fi
fx-info "Start creating ${output} ..."
(
cd "${FUCHSIA_BUILD_DIR}/${product_bundle_path}/.."
local tmpfile_path="${FUCHSIA_BUILD_DIR}/tmp-pb-zip-$$"
# get the list of files needed for flashing
result=$(fx-command-run ffx --machine json product get-artifacts product_bundle -r -g flash)
echo "$result" | fx-command-run jq -r ".ok.paths[]" > "$tmpfile_path"
echo "product_bundle.json" >> "$tmpfile_path"
sed -i -e 's/^/product_bundle\//' "$tmpfile_path"
zip pb.zip -@ < "$tmpfile_path"
rm -rf "$tmpfile_path"
if [[ $output != "$(pwd)/pb.zip" ]]; then
mv -f pb.zip "${output}"
fi
)
fx-info "Done! Output: ${output}"
}
main "$@"