| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | |
| 3 | set -euo pipefail |
| 4 | |
| 5 | base_sha=${1:?base sha required} |
| 6 | head_sha=${2:?head sha required} |
| 7 | |
| 8 | mode=full |
| 9 | reason="changed files require the full suite" |
| 10 | declare -a plugin_dirs=() |
| 11 | declare -a targets=() |
| 12 | declare -a plugin_paths=() |
| 13 | |
| 14 | fatalscip-shell shell shell-project 0.0.0 fatal().() { |
| 15 | printfscip-shell system . . printf. 'error: %s\n' "$1" >&2 |
| 16 | exit 1 |
| 17 | } |
| 18 | |
| 19 | is_plugin_treescip-shell shell shell-project 0.0.0 is_plugin_tree().() { |
| 20 | case "$1" in |
| 21 | src/filter/*|src/mixer2/*|src/mixer3/*) return 0 ;; |
| 22 | *) return 1 ;; |
| 23 | esac |
| 24 | } |
| 25 | |
| 26 | plugin_dir_from_pathscip-shell shell shell-project 0.0.0 plugin_dir_from_path().() { |
| 27 | local path=$1 |
| 28 | IFS=/scip-shell filesystem . . `/`. read -r top category name _ <<< "$path" |
| 29 | if [[ "$top" == "src" && -n "${category:-}" && -n "${name:-}" ]]; then |
| 30 | printfscip-shell system . . printf. '%s/%s/%s\n' "$top" "$category" "$name" |
| 31 | return 0 |
| 32 | fi |
| 33 | return 1 |
| 34 | } |
| 35 | |
| 36 | contains_dirscip-shell shell shell-project 0.0.0 contains_dir().() { |
| 37 | local needle=$1 |
| 38 | shift || truescip-shell system . . true. |
| 39 | local item |
| 40 | for item in "$@"; do |
| 41 | if [[ "$item" == "$needle" ]]; then |
| 42 | return 0 |
| 43 | fi |
| 44 | done |
| 45 | return 1 |
| 46 | } |
| 47 | |
| 48 | dir_exists_in_commitscip-shell shell shell-project 0.0.0 dir_exists_in_commit().() { |
| 49 | local commit=$1 |
| 50 | local path=$2 |
| 51 | gitscip-shell system . . git. cat-file -e "${commit}:${path}" 2>/dev/nullscip-shell filesystem . . `/dev/null`. |
| 52 | } |
| 53 | |
| 54 | parent_cmake_registers_pluginscip-shell shell shell-project 0.0.0 parent_cmake_registers_plugin().() { |
| 55 | local commit=$1 |
| 56 | local plugin_dir=$2 |
| 57 | local category=${plugin_dir#src/} |
| 58 | category=${category%%/*} |
| 59 | local plugin_name=${plugin_dir##*/} |
| 60 | local parent_cmake="src/${category}/CMakeLists.txt" |
| 61 | |
| 62 | local parent_text |
| 63 | parent_text=$(gitscip-shell system . . git. show "${commit}:${parent_cmake}" 2>/dev/nullscip-shell filesystem . . `/dev/null`.) || { |
| 64 | return 1 |
| 65 | } |
| 66 | |
| 67 | printfscip-shell system . . printf. '%s\n' "$parent_text" | grepscip-shell system . . grep. -Eq "^[[:space:]]*add_subdirectory[[:space:]]*\\([[:space:]]*${plugin_name}[[:space:]]*\\)" |
| 68 | } |
| 69 | |
| 70 | extract_target_namescip-shell shell shell-project 0.0.0 extract_target_name().() { |
| 71 | local commit=$1 |
| 72 | local plugin_dir=$2 |
| 73 | local plugin_name=${plugin_dir##*/} |
| 74 | local cmake_file="${plugin_dir}/CMakeLists.txt" |
| 75 | |
| 76 | local cmake_text |
| 77 | cmake_text=$(gitscip-shell system . . git. show "${commit}:${cmake_file}" 2>/dev/nullscip-shell filesystem . . `/dev/null`.) || { |
| 78 | return 1 |
| 79 | } |
| 80 | |
| 81 | local target |
| 82 | target=$(printfscip-shell system . . printf. '%s\n' "$cmake_text" | sedscip-shell system . . sed. -nE 's/^[[:space:]]*set[[:space:]]*\([[:space:]]*TARGET[[:space:]]+([^[:space:])]+).*/\1/p' | headscip-shell system . . head. -n1) |
| 83 | if [[ -n "$target" ]]; then |
| 84 | printfscip-shell system . . printf. '%s\n' "$target" |
| 85 | return 0 |
| 86 | fi |
| 87 | |
| 88 | target=$(printfscip-shell system . . printf. '%s\n' "$cmake_text" | sedscip-shell system . . sed. -nE 's/^[[:space:]]*add_library[[:space:]]*\(([[:alnum:]_.+-]+)[[:space:]]+MODULE.*/\1/p' | headscip-shell system . . head. -n1) |
| 89 | if [[ -n "$target" ]]; then |
| 90 | printfscip-shell system . . printf. '%s\n' "$target" |
| 91 | return 0 |
| 92 | fi |
| 93 | |
| 94 | printfscip-shell system . . printf. '%s\n' "$plugin_name" |
| 95 | } |
| 96 | |
| 97 | while IFS=$'\t' read -r status path new_path; do |
| 98 | [[ -n "${status:-}" ]] || continue |
| 99 | |
| 100 | if [[ "$status" == A* ]] && is_plugin_treescip-shell shell shell-project 0.0.0 is_plugin_tree(). "$path"; then |
| 101 | plugin_dir=$(plugin_dir_from_pathscip-shell shell shell-project 0.0.0 plugin_dir_from_path(). "$path") || truescip-shell system . . true. |
| 102 | if [[ -n "${plugin_dir:-}" ]] && dir_exists_in_commitscip-shell shell shell-project 0.0.0 dir_exists_in_commit(). "$base_sha" "$plugin_dir"; then |
| 103 | reason="PR adds files inside existing plugin directory: $plugin_dir" |
| 104 | plugin_dirs=() |
| 105 | break |
| 106 | fi |
| 107 | |
| 108 | if [[ -n "${plugin_dir:-}" ]] && ! contains_dirscip-shell shell shell-project 0.0.0 contains_dir(). "$plugin_dir" "${plugin_dirs[@]}"; then |
| 109 | plugin_dirs+=("$plugin_dir") |
| 110 | fi |
| 111 | continue |
| 112 | fi |
| 113 | |
| 114 | case "$path" in |
| 115 | src/filter/CMakeLists.txt|src/mixer2/CMakeLists.txt|src/mixer3/CMakeLists.txt) |
| 116 | continue |
| 117 | ;; |
| 118 | *) |
| 119 | reason="PR touches non-new-plugin files: $path" |
| 120 | plugin_dirs=() |
| 121 | break |
| 122 | ;; |
| 123 | esac |
| 124 | done < <(gitscip-shell system . . git. diff --name-status --find-renames "$base_sha" "$head_sha") |
| 125 | |
| 126 | if [[ ${#plugin_dirs[@]} -gt 0 ]]; then |
| 127 | mode=targeted |
| 128 | reason="PR only adds new plugin directories" |
| 129 | |
| 130 | for plugin_dir in "${plugin_dirs[@]}"; do |
| 131 | if ! parent_cmake_registers_pluginscip-shell shell shell-project 0.0.0 parent_cmake_registers_plugin(). "$head_sha" "$plugin_dir"; then |
| 132 | fatalscip-shell shell shell-project 0.0.0 fatal(). "parent CMakeLists.txt does not register ${plugin_dir}. Add add_subdirectory(${plugin_dir##*/}) to the category CMakeLists.txt." |
| 133 | fi |
| 134 | |
| 135 | target=$(extract_target_namescip-shell shell shell-project 0.0.0 extract_target_name(). "$head_sha" "$plugin_dir") || { |
| 136 | mode=full |
| 137 | reason="could not resolve target for $plugin_dir" |
| 138 | targets=() |
| 139 | plugin_paths=() |
| 140 | break |
| 141 | } |
| 142 | |
| 143 | plugin_paths+=("build/${plugin_dir}/${target}.so") |
| 144 | targets+=("$target") |
| 145 | done |
| 146 | fi |
| 147 | |
| 148 | printfscip-shell system . . printf. 'mode=%s\n' "$mode" |
| 149 | printfscip-shell system . . printf. 'reason=%s\n' "$reason" |
| 150 | |
| 151 | if [[ "$mode" == "targeted" ]]; then |
| 152 | { |
| 153 | printfscip-shell system . . printf. 'targets<<EOF\n' |
| 154 | printfscip-shell system . . printf. '%s\n' "${targets[*]}" |
| 155 | printfscip-shell system . . printf. 'EOF\n' |
| 156 | printfscip-shell system . . printf. 'plugin_paths<<EOF\n' |
| 157 | printfscip-shell system . . printf. '%s\n' "${plugin_paths[*]}" |
| 158 | printfscip-shell system . . printf. 'EOF\n' |
| 159 | printfscip-shell system . . printf. 'plugin_dirs<<EOF\n' |
| 160 | printfscip-shell system . . printf. '%s\n' "${plugin_dirs[*]}" |
| 161 | printfscip-shell system . . printf. 'EOF\n' |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | printfscip-shell system . . printf. 'targets=\n' |
| 166 | printfscip-shell system . . printf. 'plugin_paths=\n' |
| 167 | printfscip-shell system . . printf. 'plugin_dirs=\n' |
| 168 | } |
| 169 | fi |