David Burger | 234efd7 | 2020-02-26 17:34:47 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | # Setup a project. |
| 7 | |
David Burger | 234efd7 | 2020-02-26 17:34:47 -0700 | [diff] [blame] | 8 | function bail() { |
| 9 | echo "${1}" |
| 10 | exit 1 |
| 11 | } |
| 12 | |
David Burger | ba59a3e | 2020-04-14 12:08:08 -0600 | [diff] [blame] | 13 | function prompt_continue() { |
| 14 | read -p "${1} (y/N) " answer |
| 15 | |
| 16 | if [[ "${answer^^}" != "Y" ]]; then |
| 17 | exit 0 |
| 18 | fi |
| 19 | } |
| 20 | |
David Burger | 234efd7 | 2020-02-26 17:34:47 -0700 | [diff] [blame] | 21 | function usage() { |
David Burger | 7b40838 | 2020-04-03 14:01:28 -0600 | [diff] [blame] | 22 | echo "Usage: $0 <program> [<project>]" >&2 |
| 23 | echo " for setups with a single repo for the program with" >&2 |
| 24 | echo " projects in subdirectories, only the program argument" >&2 |
| 25 | echo " is provided." >&2 |
David Burger | 234efd7 | 2020-02-26 17:34:47 -0700 | [diff] [blame] | 26 | exit 1 |
| 27 | } |
| 28 | |
David Burger | ba59a3e | 2020-04-14 12:08:08 -0600 | [diff] [blame] | 29 | # Exit if any command fails. |
| 30 | set -e |
David Burger | 234efd7 | 2020-02-26 17:34:47 -0700 | [diff] [blame] | 31 | |
David Burger | ba59a3e | 2020-04-14 12:08:08 -0600 | [diff] [blame] | 32 | prompt_continue " |
| 33 | If you are a googler and are working with an internal checkout you do |
| 34 | not need to run this script as you already have a full repo checkout. |
| 35 | Do you want to continue running this script?" |
Sean McAllister | 01931d1 | 2020-03-12 10:34:40 -0600 | [diff] [blame] | 36 | |
| 37 | # Move to this script's directory. |
| 38 | cd "$(dirname "$0")" |
| 39 | |
David Burger | 573a050 | 2020-03-31 13:54:21 -0600 | [diff] [blame] | 40 | readonly program="${1}" |
David Burger | 7b40838 | 2020-04-03 14:01:28 -0600 | [diff] [blame] | 41 | # Will be empty if this is a single program repository project. |
David Burger | 573a050 | 2020-03-31 13:54:21 -0600 | [diff] [blame] | 42 | readonly project="${2}" |
David Burger | 234efd7 | 2020-02-26 17:34:47 -0700 | [diff] [blame] | 43 | |
David Burger | 573a050 | 2020-03-31 13:54:21 -0600 | [diff] [blame] | 44 | readonly local_manifests_dir="../../.repo/local_manifests" |
David Burger | 234efd7 | 2020-02-26 17:34:47 -0700 | [diff] [blame] | 45 | |
David Burger | 7b40838 | 2020-04-03 14:01:28 -0600 | [diff] [blame] | 46 | if [[ -z "${project}" ]]; then |
| 47 | readonly clone_url="https://chrome-internal.googlesource.com/chromeos/program/${program}" |
| 48 | readonly clone_src="../../src/program/${program}" |
| 49 | readonly symlink="${local_manifests_dir}/${program}.xml" |
| 50 | else |
| 51 | readonly clone_url="https://chrome-internal.googlesource.com/chromeos/project/${program}/${project}" |
| 52 | readonly clone_src="../../src/project/${program}/${project}" |
| 53 | readonly symlink="${local_manifests_dir}/${project}.xml" |
| 54 | fi |
| 55 | |
| 56 | if [[ -d "${clone_src}" ]]; then |
| 57 | # If ${clone_src} is already present the user is likely running |
David Burger | 573a050 | 2020-03-31 13:54:21 -0600 | [diff] [blame] | 58 | # a second time when their first run failed. Users would do this |
| 59 | # when they found they didn't have adequate permissions on a first |
| 60 | # run. In this case we wipe the artifacts from the previous run and |
| 61 | # try again. |
David Burger | ba59a3e | 2020-04-14 12:08:08 -0600 | [diff] [blame] | 62 | prompt_continue " |
| 63 | ${clone_src} appears to already exist. If you are |
| 64 | attempting to recover from a previous failed setup_project.sh attempt |
| 65 | this will attempt to fix it by removing it and resyncing. If you have |
| 66 | unsaved changes in ${clone_src} they will be lost. |
| 67 | Do you want to continue with the removal and resync?" |
| 68 | |
David Burger | 7b40838 | 2020-04-03 14:01:28 -0600 | [diff] [blame] | 69 | echo "Founding existing ${clone_src} checkout, removing." |
| 70 | rm -rf "${clone_src}" |
David Burger | 573a050 | 2020-03-31 13:54:21 -0600 | [diff] [blame] | 71 | rm -f "${symlink}" |
David Burger | 234efd7 | 2020-02-26 17:34:47 -0700 | [diff] [blame] | 72 | fi |
| 73 | |
David Burger | 99263c1 | 2020-04-04 09:43:10 -0600 | [diff] [blame] | 74 | # We only need the local_manifest.xml but have to clone to get it. |
| 75 | # Removing the rest of what we clone before we do the sync prevents |
| 76 | # a confusing error message from being shown to the user. The |
| 77 | # --force-sync below actually causes it to not be a problem, but we'd |
| 78 | # rather avoid the user having to interpret the error. |
David Burger | 7b40838 | 2020-04-03 14:01:28 -0600 | [diff] [blame] | 79 | git clone "${clone_url}" "${clone_src}" |
David Burger | 99263c1 | 2020-04-04 09:43:10 -0600 | [diff] [blame] | 80 | find "${clone_src}" -mindepth 1 ! -name local_manifest.xml -exec rm -rf {} + |
David Burger | 234efd7 | 2020-02-26 17:34:47 -0700 | [diff] [blame] | 81 | |
David Burger | 234efd7 | 2020-02-26 17:34:47 -0700 | [diff] [blame] | 82 | if [[ ! -d "${local_manifests_dir}" ]]; then |
| 83 | mkdir -p "${local_manifests_dir}" |
| 84 | fi |
| 85 | |
David Burger | 7b40838 | 2020-04-03 14:01:28 -0600 | [diff] [blame] | 86 | local_manifest="${clone_src}/local_manifest.xml" |
David Burger | 234efd7 | 2020-02-26 17:34:47 -0700 | [diff] [blame] | 87 | if [[ ! -e "${local_manifest}" ]]; then |
| 88 | bail "Expected local manifest ${local_manifest} does not exist, exiting." |
| 89 | fi |
| 90 | |
| 91 | ln -sr "${local_manifest}" "${symlink}" |
| 92 | |
| 93 | repo sync --force-sync -j48 |