1#!/usr/bin/env bash
2# Call gclient and generate a flutter-engine source tarball if one does not
3# already exist.
4set -eu
5
6DL_DIR=
7DOT_GCLIENT=
8JOBS=
9SCRATCH_DIR=
10TARBALL_DL_PATH=
11VERSION=
12TARBALL_NAME=
13
14term_bold="$(tput smso 2>/dev/null || true)"
15term_reset="$(tput rmso 2>/dev/null || true)"
16
17# Print status messages in the same style Buildroot prints.
18message() {
19    printf "%s>>> flutter-engine %s %s%s\n" "${term_bold}" "${VERSION}" "${1}" "${term_reset}"
20}
21
22parse_opts() {
23    local o O opts
24
25    o='d:j:s:t:v:'
26    O='dot-gclient:,jobs:,scratch-dir:,tarball-dl-path:,version:'
27    opts="$(getopt -o "${o}" -l "${O}" -- "${@}")"
28    eval set -- "${opts}"
29
30    while [ ${#} -gt 0 ]; do
31        case "${1}" in
32          (-d|--dot-gclient)        DOT_GCLIENT="${2}"; shift 2;;
33          (-j|--jobs)               JOBS="${2}"; shift 2;;
34          (-s|--scratch-dir)        SCRATCH_DIR="${2}"; shift 2;;
35          (-t|--tarball-dl-path)    DL_DIR=$(dirname "${2}"); TARBALL_DL_PATH="${2}"; shift 2;;
36          (-v|--version)            VERSION="${2}"; TARBALL_NAME=flutter-"${VERSION}".tar.gz; shift 2;;
37          (--)                      shift; break;;
38        esac
39    done
40}
41
42prepare() {
43    rm -rf "${SCRATCH_DIR}"
44    mkdir -p "${SCRATCH_DIR}"
45    pushd "${SCRATCH_DIR}" >/dev/null
46}
47
48copy_dot_gclient() {
49    sed "s%!FLUTTER_VERSION!%${VERSION}%g" "${DOT_GCLIENT}" >.gclient
50}
51
52run_gclient() {
53    message "Downloading"
54    gclient.py \
55        sync \
56        --delete_unversioned_trees \
57        --no-history \
58        --reset \
59        --shallow \
60        -j"${JOBS}"
61}
62
63gen_tarball() {
64    message "Generating tarball"
65    mkdir -p "${DL_DIR}"
66    # There are two issues with the flutter-engine buildsystem:
67    # - it expects empty directories created by gclient.py to be present; that
68    #   means we can't use the mk_tar_gz helper method from support/download/helpers,
69    #   becasue it does not include emnpty directories;
70    # - it insists on having a full git repositoy, with .git et al., which means
71    #   we can't generate a reproducible archive anyway.
72    # So we jsut create a plain tarball.
73    ${TAR} -C "${SCRATCH_DIR}"/src -czf "${TARBALL_NAME}" .
74    mv "${TARBALL_NAME}" "${TARBALL_DL_PATH}"
75}
76
77cleanup() {
78    popd >/dev/null
79    rm -rf "${SCRATCH_DIR}"
80}
81
82main() {
83    parse_opts "${@}"
84    if [[ ! -e "${TARBALL_DL_PATH}" ]]; then
85        prepare
86        copy_dot_gclient
87        run_gclient
88        gen_tarball
89        cleanup
90    fi
91}
92
93main "${@}"
94