|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | | -set -e |
4 | | - |
| 3 | +echo |
| 4 | +echo "Checking For MaNGOS Coding Standards:" |
5 | 5 | echo "Starting Codestyling Script:" |
6 | 6 | echo |
7 | 7 |
|
8 | 8 | declare -A singleLineRegexChecks=( |
9 | | - ["[[:blank:]]$"]="Remove whitespace at the end of the lines above" |
10 | | - ["\t"]="Replace tabs with 4 spaces in the lines above" |
| 9 | + # Exception patterns (ignore initializer-style brace lines) |
| 10 | + ["^[[:blank:]]*\\{\\s*\".*?\"\\s*,\\s*\\w+\\s*,\\s*(true|false)\\s*,\\s*&[a-zA-Z_][\\w:]*::[a-zA-Z_]\\w*\\s*,\\s*\".*?\"\\s*,\\s*(NULL|nullptr)\\s*\\},?[[:blank:]]*$"]="Well-formed initializer list line (valid) #ignore" |
| 11 | + |
| 12 | + # General whitespace/style rules |
| 13 | + ["[[:blank:]]$"]="Remove whitespace at the end of the lines" |
| 14 | + ["\t"]="Replace tabs with 4 spaces in the lines" |
| 15 | + |
| 16 | + # Control statements and braces |
| 17 | + ["^[[:blank:]]*(if|else if|else|for|while|switch)[[:blank:]]*\(.*\)[[:blank:]]*\{[[:blank:]]*\S"]="Opening brace must be on its own line after control statement" |
| 18 | + ["^[[:blank:]]*[\w:<>\*&~]+[[:blank:]]+\w+::?\w*\(.*\)[[:blank:]]*(const)?[[:blank:]]*\{[[:blank:]]*\S"]="Function opening brace must be on its own line (no code after '{')" |
| 19 | + ["^\s*\S.*\}[[:blank:]]*\S.*$"]="Closing brace must be on its own line (no code before or after '}' on the line)" |
| 20 | + |
| 21 | + # Brace usage enforcement |
| 22 | + ["^[[:blank:]]*if[[:blank:]]*\(.*\)[[:blank:]]*$"]="if statement must use braces" |
| 23 | + ["^[[:blank:]]*else[[:blank:]]*$"]="else statement must use braces" |
| 24 | + |
| 25 | + # Spacing rules |
| 26 | + ["\\b(if|for|while|switch|else\\s+if)\\("]="Missing space between keyword and '('" |
| 27 | + ["\\)(\\{)"]="Missing space before '{'" |
| 28 | +) |
| 29 | + |
| 30 | +# Ignore directories |
| 31 | +grep_exclude_args=( |
| 32 | + --exclude-dir="Eluna" |
| 33 | + --exclude-dir="Extractor_Binaries" |
| 34 | + --exclude-dir="MangosStrings_LanguageHGenerator" |
| 35 | + --exclude-dir="restart-scripts" |
| 36 | + --exclude="CMakeLists.txt" |
11 | 37 | ) |
12 | 38 |
|
13 | | -for check in ${!singleLineRegexChecks[@]}; do |
14 | | - echo " Checking RegEx: '${check}'" |
15 | | - |
16 | | - if grep -P -r -I -n ${check} src; then |
17 | | - echo |
18 | | - echo "${singleLineRegexChecks[$check]}" |
19 | | - exit 1 |
| 39 | +# Accept multiple input paths |
| 40 | +input_paths=("$@") |
| 41 | +if [[ ${#input_paths[@]} -eq 0 ]]; then |
| 42 | + input_paths=("src") # fallback |
| 43 | +fi |
| 44 | + |
| 45 | +hadError=0 |
| 46 | +declare -a triggeredDescriptions |
| 47 | + |
| 48 | +for check in "${!singleLineRegexChecks[@]}"; do |
| 49 | + ruleDesc="${singleLineRegexChecks[$check]}" |
| 50 | + |
| 51 | + # If it's a fully ignorable rule, skip early |
| 52 | + if [[ "$ruleDesc" == *"#ignore"* ]]; then |
| 53 | + continue |
20 | 54 | fi |
21 | | -done |
22 | 55 |
|
23 | | -# declare -A multiLineRegexChecks=( |
24 | | -# ["\n\n\n"]="Multiple blank lines detected, keep only one. Check the files above" |
25 | | -# ) |
| 56 | + matches=$(grep -P -r -I -n "${grep_exclude_args[@]}" "${input_paths[@]}" -e "$check" 2>/dev/null) |
26 | 57 |
|
27 | | -# for check in ${!multiLineRegexChecks[@]}; do |
28 | | -# echo " Checking RegEx: '${check}'" |
| 58 | + if [[ -n "$matches" ]]; then |
| 59 | + # Efficiently filter initializer list matches |
| 60 | + filteredMatches=$(printf "%s\n" "$matches" | grep -Pv '^[[:blank:]]*\{\s*".*?"\s*,\s*\w+\s*,\s*(true|false)\s*,\s*&[a-zA-Z_][\w:]*::[a-zA-Z_]\w*\s*,\s*".*?"\s*,\s*(NULL|nullptr)\s*\},?[[:blank:]]*$') |
29 | 61 |
|
30 | | -# if grep -Pzo -r -I ${check} src; then |
31 | | -# echo |
32 | | -# echo |
33 | | -# echo "${multiLineRegexChecks[$check]}" |
34 | | -# exit 1 |
35 | | -# fi |
36 | | -# done |
| 62 | + if [[ -n "$filteredMatches" ]]; then |
| 63 | + echo |
| 64 | + echo "== Rule triggered: ${ruleDesc%%#*} ==" |
| 65 | + echo "$filteredMatches" |
| 66 | + triggeredDescriptions+=("${ruleDesc%%#*}") |
| 67 | + hadError=1 |
| 68 | + fi |
| 69 | + fi |
| 70 | +done |
37 | 71 |
|
38 | 72 | echo |
39 | | -echo "Awesome! No issues..." |
| 73 | +echo "------------------------------------------" |
| 74 | +echo "Summary of Triggered Rules:" |
| 75 | +echo "------------------------------------------" |
| 76 | + |
| 77 | +if [[ ${#triggeredDescriptions[@]} -eq 0 ]]; then |
| 78 | + echo "No style violations found." |
| 79 | +else |
| 80 | + for rule in "${triggeredDescriptions[@]}"; do |
| 81 | + echo "$rule" |
| 82 | + done |
| 83 | +fi |
| 84 | + |
| 85 | +exit $hadError |
0 commit comments