David Burger | 234efd7 | 2020-02-26 17:34:47 -0700 | [diff] [blame] | 1 | #!/bin/bash |
Jack Neus | b8b00c4 | 2021-07-28 15:07:38 +0000 | [diff] [blame] | 2 | # Copyright 2020 The Chromium OS Authors. All rights reserved. |
David Burger | 234efd7 | 2020-02-26 17:34:47 -0700 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
Jack Neus | b8b00c4 | 2021-07-28 15:07:38 +0000 | [diff] [blame] | 6 | # Setup a project. |
| 7 | |
| 8 | # Exit if any command fails. |
| 9 | set -e |
| 10 | |
| 11 | function bail() { |
| 12 | RED='\033[38;5;9m' |
| 13 | NC='\033[0m' # No Color |
| 14 | echo -e "${RED}${1}${NC}" |
| 15 | exit 1 |
| 16 | } |
| 17 | |
| 18 | function prompt_continue() { |
| 19 | read -r -p "${1} (y/N) " answer |
| 20 | |
| 21 | if [[ "${answer^^}" != "Y" ]]; then |
| 22 | exit 0 |
| 23 | fi |
| 24 | } |
| 25 | |
| 26 | # Clone a repo and create a symlink a local manifest. |
| 27 | # |
| 28 | # Args: |
| 29 | # $1: URL to clone. |
| 30 | # $2: Path to clone to. |
| 31 | # $3: Path to symlink to. |
| 32 | function clone_manifest() { |
| 33 | [[ $# -eq 3 ]] || die "${FUNCNAME[0]}: takes three arguments" |
| 34 | |
| 35 | local clone_url=$1 |
| 36 | local clone_src=$2 |
| 37 | local symlink=$3 |
| 38 | |
| 39 | if [[ -d "${clone_src}" ]]; then |
| 40 | # If ${clone_src} is already present the user is likely running |
| 41 | # a second time when their first run failed. Users would do this |
| 42 | # when they found they didn't have adequate permissions on a first |
| 43 | # run. In this case we wipe the artifacts from the previous run and |
| 44 | # try again. |
| 45 | prompt_continue " |
| 46 | ${clone_src} appears to already exist. If you are |
| 47 | attempting to recover from a previous failed setup_project.sh attempt |
| 48 | this will attempt to fix it by removing it and resyncing. If you have |
| 49 | unsaved changes in ${clone_src} they will be lost. |
| 50 | Do you want to continue with the removal and resync?" |
| 51 | |
| 52 | echo "Found existing ${clone_src} checkout, removing." |
| 53 | rm -rf "${clone_src}" |
| 54 | rm -f "${symlink}" |
| 55 | fi |
| 56 | |
| 57 | # We only need the local_manifest.xml but have to clone to get it. |
| 58 | # Removing the rest of what we clone before we do the sync prevents |
| 59 | # a confusing error message from being shown to the user. The |
| 60 | # --force-sync below actually causes it to not be a problem, but we'd |
| 61 | # rather avoid the user having to interpret the error. |
| 62 | if [[ -z "${branch}" ]]; then |
| 63 | git clone "${clone_url}" "${clone_src}" |
| 64 | else |
| 65 | git clone "${clone_url}" "${clone_src}" --branch "${branch}" |
| 66 | fi |
| 67 | |
| 68 | find "${clone_src}" -mindepth 1 ! -name local_manifest.xml -exec rm -rf {} + |
| 69 | |
| 70 | if [[ ! -d "${local_manifests_dir}" ]]; then |
| 71 | mkdir -p "${local_manifests_dir}" |
| 72 | fi |
| 73 | |
| 74 | local_manifest="${clone_src}/local_manifest.xml" |
| 75 | if [[ ! -e "${local_manifest}" ]]; then |
| 76 | return 1 |
| 77 | fi |
| 78 | |
| 79 | ln -sr "${local_manifest}" "${symlink}" |
| 80 | } |
| 81 | |
| 82 | function usage() { |
| 83 | echo "Usage: $0 [options] <program> <project> |
| 84 | |
| 85 | Options: |
| 86 | -br, --branch Sync the project from the local manifest at the given branch. |
| 87 | --chipset Name of the chipset overlay to sync a local manifest from. |
| 88 | -h, --help This help output. |
| 89 | " >&2 |
| 90 | |
| 91 | exit 1 |
| 92 | } |
| 93 | |
| 94 | function main() { |
| 95 | while [[ $# -ne 0 ]]; do |
| 96 | case $1 in |
| 97 | -br|--branch) |
| 98 | readonly branch=$2 |
| 99 | shift |
| 100 | ;; |
| 101 | --chipset) |
| 102 | readonly chipset=$2 |
| 103 | shift |
| 104 | ;; |
| 105 | -h|--help) |
| 106 | usage |
| 107 | ;; |
| 108 | *) |
| 109 | ARGS+=( "$1" ) |
| 110 | ;; |
| 111 | esac |
| 112 | shift |
| 113 | done |
| 114 | set -- "${ARGS[@]}" "$@" |
| 115 | |
| 116 | if [[ $# -ne 2 ]]; then |
| 117 | usage |
| 118 | fi |
| 119 | |
| 120 | readonly program="${1}" |
| 121 | readonly project="${2}" |
| 122 | |
| 123 | prompt_continue " |
| 124 | If you are a googler and are working with an internal checkout you do |
| 125 | not need to run this script as you already have a full repo checkout. |
| 126 | Do you want to continue running this script?" |
| 127 | |
| 128 | # Move to this script's directory. |
| 129 | cd "$(dirname "$0")" |
| 130 | |
| 131 | readonly local_manifests_dir="../../.repo/local_manifests" |
| 132 | |
| 133 | # Clone local manifests from the program and project. Program local manifest |
| 134 | # is optional, project local manifest is required. |
| 135 | # |
| 136 | # Note that the symlinks include "_[program|project].xml" because the project |
| 137 | # name may be the same as the program name. |
| 138 | readonly prog_url="https://chrome-internal.googlesource.com/chromeos/program/${program}" |
| 139 | readonly prog_src="../../src/program/${program}" |
| 140 | readonly prog_symlink="${local_manifests_dir}/${program}_program.xml" |
| 141 | |
| 142 | if ! clone_manifest "${prog_url}" "${prog_src}" "${prog_symlink}"; then |
| 143 | echo "No program local manifest found in ${prog_url}, continuing." |
| 144 | fi |
| 145 | |
| 146 | readonly proj_url="https://chrome-internal.googlesource.com/chromeos/project/${program}/${project}" |
| 147 | readonly proj_src="../../src/project/${program}/${project}" |
| 148 | readonly proj_symlink="${local_manifests_dir}/${project}_project.xml" |
| 149 | |
| 150 | if ! clone_manifest "${proj_url}" "${proj_src}" "${proj_symlink}"; then |
| 151 | bail "Expected project local manifest in ${proj_url} does not exist, " \ |
| 152 | "exiting." |
| 153 | fi |
| 154 | |
| 155 | if [[ -n "${chipset}" ]]; then |
| 156 | readonly chip_url="https://chrome-internal.googlesource.com/chromeos/overlays/chipset-${chipset}-private" |
| 157 | readonly chip_src="../../private-overlays/chipset-${chipset}-private" |
| 158 | readonly chip_symlink="${local_manifests_dir}/${chipset}_chipset.xml" |
| 159 | if ! clone_manifest "${chip_url}" "${chip_src}" "${chip_symlink}"; then |
| 160 | bail "Expected chipset local manifest in ${chip_url} does not exist, " \ |
| 161 | "exiting." |
| 162 | fi |
| 163 | fi |
| 164 | |
| 165 | |
| 166 | echo "Local manifest setup complete, sync new projects with: |
| 167 | |
| 168 | repo sync --force-sync -j48" |
| 169 | } |
| 170 | |
| 171 | main "$@" |