|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Unified script to find boards for testing |
| 4 | +# Usage: |
| 5 | +# ./find_boards.sh all # Find all boards |
| 6 | +# ./find_boards.sh new <owner> <base_ref> # Find only new/modified boards |
| 7 | + |
| 8 | +# Function to create JSON matrix and set environment variables |
| 9 | +create_json_matrix_and_set_env() { |
| 10 | + local message_type=$1 # "Boards" or "New boards" |
| 11 | + shift # Remove the first argument (message_type) from the array |
| 12 | + local boards_array=("$@") |
| 13 | + |
| 14 | + # Sort the boards array alphabetically |
| 15 | + mapfile -t sorted_boards < <(printf '%s\n' "${boards_array[@]}" | sort) |
| 16 | + |
| 17 | + local board_count=${#sorted_boards[@]} |
| 18 | + |
| 19 | + echo "${message_type} found: $board_count" |
| 20 | + echo "BOARD-COUNT=$board_count" >> "$GITHUB_ENV" |
| 21 | + |
| 22 | + if [ "$board_count" -gt 0 ]; then |
| 23 | + json_matrix='[' |
| 24 | + local temp_count=$board_count |
| 25 | + for board in "${sorted_boards[@]}"; do |
| 26 | + json_matrix+='"'$board'"' |
| 27 | + if [ "$temp_count" -gt 1 ]; then |
| 28 | + json_matrix+="," |
| 29 | + fi |
| 30 | + temp_count=$((temp_count - 1)) |
| 31 | + done |
| 32 | + json_matrix+=']' |
| 33 | + |
| 34 | + echo "$json_matrix" |
| 35 | + echo "FQBNS=${json_matrix}" >> "$GITHUB_ENV" |
| 36 | + else |
| 37 | + echo "FQBNS=" >> "$GITHUB_ENV" |
| 38 | + fi |
| 39 | +} |
| 40 | + |
| 41 | +# Function to process board name and add to array |
| 42 | +process_board() { |
| 43 | + local board_name=$1 |
| 44 | + local boards_array_ref=$2 |
| 45 | + |
| 46 | + # skip esp32c2 as we dont build libs for it |
| 47 | + if [ "$board_name" == "esp32c2" ]; then |
| 48 | + echo "Skipping 'espressif:esp32:$board_name'" |
| 49 | + return |
| 50 | + fi |
| 51 | + |
| 52 | + eval "$boards_array_ref+=(\"espressif:esp32:$board_name\")" |
| 53 | + echo "Added 'espressif:esp32:$board_name' to array" |
| 54 | +} |
| 55 | + |
| 56 | +# Function to get all boards |
| 57 | +get_all_boards() { |
| 58 | + local boards_array=() |
| 59 | + local boards_list |
| 60 | + boards_list=$(grep '.tarch=' boards.txt) |
| 61 | + |
| 62 | + while read -r line; do |
| 63 | + local board_name |
| 64 | + board_name=$(echo "$line" | cut -d '.' -f1 | cut -d '#' -f1) |
| 65 | + process_board "$board_name" "boards_array" |
| 66 | + done <<< "$boards_list" |
| 67 | + |
| 68 | + create_json_matrix_and_set_env "Boards" "${boards_array[@]}" |
| 69 | +} |
| 70 | + |
| 71 | +# Function to get new/modified boards |
| 72 | +get_new_boards() { |
| 73 | + local owner_repository=$1 |
| 74 | + local base_ref=$2 |
| 75 | + |
| 76 | + if [ -z "$owner_repository" ] || [ -z "$base_ref" ]; then |
| 77 | + echo "Error: For 'new' mode, owner_repository and base_ref are required" |
| 78 | + echo "Usage: ./find_boards.sh new <owner_repository> <base_ref>" |
| 79 | + exit 1 |
| 80 | + fi |
| 81 | + |
| 82 | + # Download the boards.txt file from the base branch |
| 83 | + curl -L -o boards_base.txt https://raw.githubusercontent.com/"$owner_repository"/"$base_ref"/boards.txt |
| 84 | + |
| 85 | + # Compare boards.txt file in the repo with the modified file from PR |
| 86 | + local diff |
| 87 | + diff=$(diff -u boards_base.txt boards.txt) |
| 88 | + |
| 89 | + # Check if the diff is empty |
| 90 | + if [ -z "$diff" ]; then |
| 91 | + echo "No changes in boards.txt file" |
| 92 | + echo "FQBNS=" |
| 93 | + exit 0 |
| 94 | + fi |
| 95 | + |
| 96 | + # Extract added or modified lines (lines starting with '+' or '-') |
| 97 | + local modified_lines |
| 98 | + modified_lines=$(echo "$diff" | grep -E '^[+-][^+-]') |
| 99 | + |
| 100 | + # Print the modified lines for debugging |
| 101 | + echo "Modified lines:" |
| 102 | + echo "$modified_lines" |
| 103 | + |
| 104 | + local boards_array=() |
| 105 | + local previous_board="" |
| 106 | + |
| 107 | + # Extract board names from the modified lines, and add them to the boards_array |
| 108 | + while read -r line; do |
| 109 | + local board_name |
| 110 | + board_name=$(echo "$line" | cut -d '.' -f1 | cut -d '#' -f1) |
| 111 | + # remove + or - from the board name at the beginning |
| 112 | + board_name=${board_name#[-+]} |
| 113 | + if [ "$board_name" != "" ] && [ "$board_name" != "+" ] && [ "$board_name" != "-" ] && [ "$board_name" != "esp32_family" ]; then |
| 114 | + if [ "$board_name" != "$previous_board" ]; then |
| 115 | + process_board "$board_name" "boards_array" |
| 116 | + previous_board="$board_name" |
| 117 | + fi |
| 118 | + fi |
| 119 | + done <<< "$modified_lines" |
| 120 | + |
| 121 | + create_json_matrix_and_set_env "New boards" "${boards_array[@]}" |
| 122 | +} |
| 123 | + |
| 124 | +# Main script logic |
| 125 | +mode=$1 |
| 126 | + |
| 127 | +if [ "$mode" = "all" ]; then |
| 128 | + get_all_boards |
| 129 | +elif [ "$mode" = "new" ]; then |
| 130 | + get_new_boards "$2" "$3" |
| 131 | +else |
| 132 | + echo "Error: Invalid mode. Use 'all' or 'new'" |
| 133 | + echo "Usage:" |
| 134 | + echo " ./find_boards.sh all" |
| 135 | + echo " ./find_boards.sh new <owner_repository> <base_ref>" |
| 136 | + exit 1 |
| 137 | +fi |
0 commit comments