|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# exit on error, unset variables, print commands, fail on pipe errors |
| 4 | +set -euxo pipefail |
| 5 | + |
| 6 | +# How to test this script, run it with the required environment variables: |
| 7 | +# 1. A modified file from the root, but not the package.json or package-lock.json: |
| 8 | +# ALL_CHANGED_FILES="README.md" ./.github/scripts/changed-modules.sh |
| 9 | +# Expected output: [], as no module should be built |
| 10 | +# |
| 11 | +# 2. The package.json or package-lock.json are modified: |
| 12 | +# ALL_CHANGED_FILES="package.json" ./.github/scripts/changed-modules.sh |
| 13 | +# Expected output: all modules, as the dependencies have been modified |
| 14 | +# ALL_CHANGED_FILES="package-lock.json" ./.github/scripts/changed-modules.sh |
| 15 | +# Expected output: all modules, as the dependencies have been modified |
| 16 | +# |
| 17 | +# 3. A file in the testcontainers module is modified: |
| 18 | +# ALL_CHANGED_FILES="packages/testcontainers/a.txt" ./.github/scripts/changed-modules.sh |
| 19 | +# Expected output: all modules, as the core has been modified |
| 20 | +# |
| 21 | +# 4. A file in a module is modified: |
| 22 | +# ALL_CHANGED_FILES="packages/modules/arangodb/a.txt" ./.github/scripts/changed-modules.sh |
| 23 | +# Expected output: [arangodb], only |
| 24 | +# |
| 25 | +# 5. Three files in three different modules are modified: |
| 26 | +# ALL_CHANGED_FILES="packages/modules/arangodb/a.txt packages/modules/cassandra/b.txt packages/modules/chromadb/c.txt" ./.github/scripts/changed-modules.sh |
| 27 | +# Expected output: [arangodb, cassandra, chromadb] |
| 28 | +# |
| 29 | +# 6. Core files and module files are modified: |
| 30 | +# ALL_CHANGED_FILES="packages/testcontainers/a.txt packages/modules/chromadb/b.txt" ./.github/scripts/changed-modules.sh |
| 31 | +# Expected output: all modules, as the core has been modified |
| 32 | +# |
| 33 | +# 7. This script is modified: |
| 34 | +# ALL_CHANGED_FILES=".github/scripts/changed-modules.sh" ./.github/scripts/changed-modules.sh |
| 35 | +# Expected output: all modules, as the build script has been modified |
| 36 | +# |
| 37 | +# 8. A .github file is modified: |
| 38 | +# ALL_CHANGED_FILES=".github/release-drafter.yml" ./.github/scripts/changed-modules.sh |
| 39 | +# Expected output: [] |
| 40 | +# |
| 41 | +# 9. A excluded module is modified: |
| 42 | +# ALL_CHANGED_FILES="packages/modules/couchbase/a.txt" ./.github/scripts/changed-modules.sh |
| 43 | +# Expected output: [] |
| 44 | +# |
| 45 | +# There is room for improvement in this script. For example, it could detect if the changes applied to the docs or the .github dirs, and then do not include any module in the list. |
| 46 | +# But then we would need to verify the CI scripts to ensure that the job receives the correct modules to build. |
| 47 | + |
| 48 | +# ROOT_DIR is the root directory of the repository. |
| 49 | +readonly ROOT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd) |
| 50 | + |
| 51 | +# define an array of directories that won't be included in the list |
| 52 | +readonly excluded_modules=(".devcontainer" ".vscode" ".husky" "docs" ".github/ISSUE_TEMPLATE") |
| 53 | + |
| 54 | +# define an array of files that won't be included in the list |
| 55 | +# Get all files in the root directory except package.json and package-lock.json |
| 56 | +# Create array of excluded files by finding all files in root dir except package.json and package-lock.json |
| 57 | +excluded_files=("${ROOT_DIR}/.github/release-drafter.yml") |
| 58 | +while IFS= read -r file; do |
| 59 | + excluded_files+=("\"${file}\"") |
| 60 | +done < <(find "${ROOT_DIR}" -maxdepth 1 -type f -not -name "package.json" -not -name "package-lock.json") |
| 61 | + |
| 62 | +# define an array of modules that won't be part of the build |
| 63 | +readonly no_build_modules=("couchbase") |
| 64 | + |
| 65 | +# modules is an array that will store the paths of all the modules in the repository. |
| 66 | +modules=() |
| 67 | + |
| 68 | +# Find all package.json files in the repository, building a list of all the available modules. |
| 69 | +# The list of modules is stored in the modules array, but the testcontainers-node module is excluded |
| 70 | +# as it is not a module. |
| 71 | +for packageJSONFile in $(find "${ROOT_DIR}" -name "package.json" -not -path "*/node_modules/*"); do |
| 72 | + name=$(basename "$(dirname "${packageJSONFile}")") |
| 73 | + if [[ "${name}" != "testcontainers-node" ]]; then |
| 74 | + modules+=("\"${name}\"") |
| 75 | + fi |
| 76 | +done |
| 77 | + |
| 78 | +# sort modules array |
| 79 | +IFS=$'\n' modules=($(sort <<<"${modules[*]}")) |
| 80 | +unset IFS |
| 81 | + |
| 82 | +# Get the list of modified files, retrieved from the environment variable ALL_CHANGED_FILES. |
| 83 | +# On CI, this value will come from a Github Action retrieving the list of modified files from the pull request. |
| 84 | +readonly modified_files=${ALL_CHANGED_FILES[@]} |
| 85 | + |
| 86 | +# Initialize variables |
| 87 | +modified_modules=() |
| 88 | + |
| 89 | +# Check the modified files and determine which modules to build, following these rules: |
| 90 | +# - if the modified files contain any file in the root module, include all modules in the list |
| 91 | +# - if the modified files only contain files in one of the modules, include that module in the list |
| 92 | +for file in $modified_files; do |
| 93 | + # check if the file is in one of the excluded files |
| 94 | + for exclude_file in ${excluded_files[@]}; do |
| 95 | + # Remove quotes from exclude_file for comparison |
| 96 | + clean_exclude_file=$(echo $exclude_file | tr -d '"') |
| 97 | + if [[ "${ROOT_DIR}/${file}" == "${clean_exclude_file}" ]]; then |
| 98 | + # if the file is in the excluded files, skip the rest of the loop. |
| 99 | + # Execution continues at the loop control of the 2nd enclosing loop. |
| 100 | + continue 2 |
| 101 | + fi |
| 102 | + done |
| 103 | + |
| 104 | + if [[ $file == packages/modules/* ]]; then |
| 105 | + module_name=$(echo $file | cut -d'/' -f3) |
| 106 | + if [[ ! " ${modified_modules[@]} " =~ " ${module_name} " ]]; then |
| 107 | + modified_modules+=("\"$module_name\"") |
| 108 | + fi |
| 109 | + else |
| 110 | + # a file from the core module (packages/testcontainers) is modified, so include all modules in the list and stop the loop |
| 111 | + # check if the file is in one of the excluded modules |
| 112 | + for exclude_module in ${excluded_modules[@]}; do |
| 113 | + if [[ $file == $exclude_module/* ]]; then |
| 114 | + # continue skips to the next iteration of an enclosing for, select, until, or while loop in a shell script. |
| 115 | + # Execution continues at the loop control of the nth enclosing loop, in this case two levels up. |
| 116 | + continue 2 |
| 117 | + fi |
| 118 | + done |
| 119 | + |
| 120 | + modified_modules=${modules[@]} |
| 121 | + break |
| 122 | + fi |
| 123 | +done |
| 124 | + |
| 125 | +# print all modules with this format: |
| 126 | +# each module will be enclosed in double quotes |
| 127 | +# each module will be separated by a comma |
| 128 | +# the entire list will be enclosed in square brackets |
| 129 | +# the list will be sorted and unique |
| 130 | +sorted_unique_modules=($(echo "${modified_modules[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')) |
| 131 | + |
| 132 | +# remove modules that won't be part of the build from the list |
| 133 | +filtered_modules=() |
| 134 | +for module in "${sorted_unique_modules[@]}"; do |
| 135 | + skip=false |
| 136 | + for no_build_module in "${no_build_modules[@]}"; do |
| 137 | + if [[ ${module} == \"${no_build_module}\" ]]; then |
| 138 | + skip=true |
| 139 | + break |
| 140 | + fi |
| 141 | + done |
| 142 | + if [[ $skip == false ]]; then |
| 143 | + filtered_modules+=(${module}) |
| 144 | + fi |
| 145 | +done |
| 146 | +sorted_unique_modules=("${filtered_modules[@]}") |
| 147 | + |
| 148 | +echo "["$(IFS=,; echo "${sorted_unique_modules[*]}" | sed 's/ /,/g')"]" |
0 commit comments