1#!/bin/bash
2#
3# Copyright 2025 The Android Open Source Project
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#
17set -euo pipefail
18
19usage() {
20  cat << EOF
21Usage: $0 [baseCommit] [tasksToRun] [--runOnDependentProjects]
22
23Optional arguments:
24  baseCommit                Commit hash to use for the :listAffectedProjects task.
25                            If not provided, the task will run with the last merge commit as the baseCommit.
26  tasksToRun                Comma-separated list of Gradle tasks to run for each affected project.
27                            If not provided, defaults to 'buildOnServer'.
28  --runOnDependentProjects  If specified, the tasks will also run on dependent projects.
29                            Enabling this flag will increase the processing time.
30
31Examples:
32  1. Run with a base commit (e.g. HEAD~1) and a specified list of tasks:
33     ./development/validate_changes.sh HEAD~1 bOS,allHostTests
34
35  2. Run with a base commit, a specified list of tasks, and enable running on dependent projects:
36     ./development/validate_changes.sh HEAD~1 bOS,allHostTests --runOnDependentProjects
37
38  3. Run with default base commit (last merge commit), specified tasks, and enabling dependent projects:
39     ./development/validate_changes.sh "" bOS,allHostTests --runOnDependentProjects
40
41Options:
42  --help                  Display this help message and exit.
43EOF
44}
45
46if [[ "${1:-}" == "--help" ]]; then
47  usage
48  exit 0
49fi
50
51# ANSI color codes for error messages
52RED='\033[0;31m'
53NC='\033[0m'
54
55# Parse arguments
56RUN_ON_DEPENDENT_PROJECTS="false"
57ARGS=()
58
59while [[ $# -gt 0 ]]; do
60  case "$1" in
61    --runOnDependentProjects)
62      RUN_ON_DEPENDENT_PROJECTS="true"
63      shift
64      ;;
65    --help)
66      usage
67      exit 0
68      ;;
69    *)
70      ARGS+=("$1")
71      shift
72      ;;
73  esac
74done
75
76BASE_COMMIT="${ARGS[0]:-}"
77TASKS_TO_RUN="${ARGS[1]:-bOS}"
78
79if [[ "$RUN_ON_DEPENDENT_PROJECTS" != "true" ]]; then
80  echo -e "${RED}Dependent projects will NOT be captured. Running with --runOnDependentProjects will\
81 run tasks on dependent projects, but it may take more time.${NC}"
82fi
83
84REPO_ROOT=$(git rev-parse --show-toplevel)
85cd "$REPO_ROOT" || { echo "Error: Unable to change directory to repository root"; exit 1; }
86
87OUTPUT_FILE="$REPO_ROOT/../../out/androidx/build/changedProjects.txt"
88
89gradle_cmd=(./gradlew :listAffectedProjects)
90if [[ -n "$BASE_COMMIT" ]]; then
91  gradle_cmd+=("--baseCommit=$BASE_COMMIT")
92fi
93gradle_cmd+=("--tasksToRun=$TASKS_TO_RUN")
94if [[ "$RUN_ON_DEPENDENT_PROJECTS" == "true" ]]; then
95  gradle_cmd+=("--runOnDependentProjects=true")
96fi
97
98echo "Running: ${gradle_cmd[*]}"
99"${gradle_cmd[@]}"
100
101if [[ ! -f "$OUTPUT_FILE" ]]; then
102  echo -e "${RED}Output file '$OUTPUT_FILE' not found. Exiting.${NC}"
103  exit 1
104fi
105
106TASKS=$(<"$OUTPUT_FILE")
107
108if [[ -z "$TASKS" ]]; then
109  echo -e "${RED}No tasks found in '$OUTPUT_FILE'. Nothing to execute.${NC}"
110  exit 0
111fi
112
113read -r -a tasks_array <<< "$TASKS"
114
115echo "Running: ./gradlew "${tasks_array[@]}""
116PROJECT_PREFIX=: ANDROIDX_PROJECTS=ALL ./gradlew --strict "${tasks_array[@]}"
117