|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | | -set -e |
| 3 | +# |
| 4 | +# Advanced Code Styling Check for getMaNGOS |
| 5 | +# Script written by Meltie2013 (https://github.com/Meltie2013) |
| 6 | +# |
| 7 | +# Copyright: 2025 |
| 8 | +# Website: https://getmangos.eu/ |
| 9 | +# |
| 10 | +# Script Type: Bash |
| 11 | +# |
| 12 | +# Co-authors Area: |
| 13 | +# |
| 14 | +# Note from the author: Anyone is free to use this script but is asked to keep the original author name and GitHub url in the header. |
| 15 | +# You can add co-authors to the header when the script is updated or changed for your project needs. Removing the original author |
| 16 | +# from the header, will result in a DMCA (take-down). |
| 17 | +# |
| 18 | +# This script is viewed under the GPL-2.0 license and shall not be changed unless authorized by the original author. |
| 19 | +# |
| 20 | + |
| 21 | +LOG_IGNORED_RULES=false # Set to true to print exception rule matches |
4 | 22 |
|
5 | | -echo "Starting Codestyling Script:" |
6 | 23 | echo |
| 24 | +echo "Checking For MaNGOS Coding Standards:" |
| 25 | +echo "Starting Codestyling Script:" |
7 | 26 |
|
8 | 27 | 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" |
| 28 | + # General whitespace/style rules |
| 29 | + ["[[:blank:]]$"]="Remove whitespace at the end of the lines" |
| 30 | + ["\t"]="Replace tabs with 4 spaces in the lines" |
| 31 | + |
| 32 | + # Exception patterns (ignored, but still shown in summary) |
| 33 | + ["^[[: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 (styling is valid) #ignore" |
| 34 | + ["^[[:blank:]]*\\{[[:blank:]]*\"(SOAP-ENV|SOAP-ENC|xsi|xsd|ns1)\"[[:blank:]]*,[[:blank:]]*\"(http://[^\"]+|urn:[^\"]+)\"(?:[[:blank:]]*,[[:blank:]]*\"(http://[^\"]+|urn:[^\"]+)\")?[[:blank:]]*\\}[[:blank:]]*,?[[:blank:]]*(//.*)?$"]="Namespace initializer list line (styling is valid) #ignore" |
| 35 | + ["^[[:blank:]]*[^[:blank:]]+[[:blank:]]*<<(.*\"[^\"]*[{}][^\"]*\".*)+[[:blank:]]*;?[[:blank:]]*$"]="Streamed brace output (styling is valid) #ignore" |
| 36 | + |
| 37 | + # Control TODO/FIXME rules |
| 38 | + ["//[[:blank:]]*(TODO|FIXME)[[:blank:]]*(?!:)"]="TODO/FIXME must include description" |
| 39 | + |
| 40 | + # Virtual destructor enforcement |
| 41 | + ["\\b(?:virtual[[:space:]]+)?~[[:alnum:]_]+[[:space:]]*\\([[:space:]]*\\)[[:space:]]*\\{[[:space:]]*\\}[[:space:]]*;?"]="Prefer '= default' for destructors instead of empty braces" |
| 42 | + |
| 43 | + # Control statements and braces |
| 44 | + ["^[[:blank:]]*(if|else if|else|do|for|while|switch|try|catch|class|struct|namespace|case)[[:blank:]]*(\(.*\))?[[:blank:]]*\{[[:blank:]]*\S"]="Opening brace must be on its own line after statement" |
| 45 | + ["^[[:blank:]]*[\w:<>\*&~]+[[:blank:]]+\w+::?\w*\(.*\)[[:blank:]]*(const)?[[:blank:]]*\{[[:blank:]]*\S"]="Function opening brace must be on its own line (no code after '{')" |
| 46 | + ["^\s*\S.*\}[[:blank:]]*\S.*$"]="Closing brace must be on its own line (no code before or after '}' on the line)" |
| 47 | + |
| 48 | + # Brace usage enforcement |
| 49 | + ["^[[:blank:]]*enum[[:blank:]]+(\\w+[[:blank:]]*)?\\{[[:blank:]]*[^}]*\\}[[:blank:]]*;"]="Enum opening brace must be on its own line and enum body properly formatted" |
| 50 | + |
| 51 | + # Spacing rules |
| 52 | + ["\\b(if|for|while|switch|else\\s+if)\\("]="Missing space between keyword and '('" |
| 53 | + |
| 54 | + # Bad coding practice enforcement |
| 55 | + ["^[[:blank:]]*using[[:blank:]]+namespace[[:blank:]]+std[[:blank:]]*;"]="Avoid using namespace std (prefer explicit qualifiers)" |
| 56 | + ["^[[:blank:]]*namespace[[:blank:]]*\\{"]="Anonymous namespace brace must be on its own line" |
| 57 | +) |
| 58 | + |
| 59 | +# Directories and files to exclude |
| 60 | +grep_exclude_args=( |
| 61 | + # Exclude Folders |
| 62 | + --exclude-dir="Eluna" |
| 63 | + --exclude-dir="Extractor_Binaries" |
| 64 | + --exclude-dir="MangosStrings_LanguageHGenerator" |
| 65 | + --exclude-dir="restart-scripts" |
| 66 | + |
| 67 | + # Exclude Files |
| 68 | + --exclude="CMakeLists.txt" |
| 69 | + --exclude="realmd.conf.dist.in" |
| 70 | + --exclude="mangosd.conf.dist.in" |
| 71 | + --exclude=".dockerignore" |
| 72 | + --exclude=".gitattributes" |
| 73 | + --exclude=".gitignore" |
| 74 | + --exclude=".gitmodules" |
11 | 75 | ) |
12 | 76 |
|
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 |
| 77 | +input_paths=("$@") |
| 78 | +if [[ ${#input_paths[@]} -eq 0 ]]; then |
| 79 | + input_paths=("src") |
| 80 | +fi |
| 81 | + |
| 82 | +hadError=0 |
| 83 | +declare -a triggeredDescriptions |
| 84 | +declare -a ignoredLines=() # To store all lines matched by #ignore rules |
| 85 | + |
| 86 | +# First pass: collect ignored lines (to exclude later from other rules) |
| 87 | +for check in "${!singleLineRegexChecks[@]}"; do |
| 88 | + ruleDesc="${singleLineRegexChecks[$check]}" |
| 89 | + if [[ "$ruleDesc" == *"#ignore"* ]]; then |
| 90 | + # Keep original description with #ignore intact |
| 91 | + origRuleDesc="$ruleDesc" |
| 92 | + # Create print-friendly description without #ignore |
| 93 | + printRuleDesc="${ruleDesc%%#*}" |
| 94 | + |
| 95 | + matches=$(grep -P -r -I -n "${grep_exclude_args[@]}" "${input_paths[@]}" -e "$check" 2>/dev/null) |
| 96 | + if [[ -n "$matches" ]]; then |
| 97 | + ignoredLines+=("$matches") |
| 98 | + triggeredDescriptions+=("$origRuleDesc") # Store original with #ignore for summary categorization |
| 99 | + |
| 100 | + if $LOG_IGNORED_RULES; then |
| 101 | + echo |
| 102 | + echo "== Exception Rule matched: $printRuleDesc ==" |
| 103 | + echo "$matches" |
| 104 | + fi |
| 105 | + fi |
20 | 106 | fi |
21 | 107 | done |
22 | 108 |
|
23 | | -# declare -A multiLineRegexChecks=( |
24 | | -# ["\n\n\n"]="Multiple blank lines detected, keep only one. Check the files above" |
25 | | -# ) |
| 109 | +# Flatten ignoredLines to a single pattern (line numbers + paths) for exclusion |
| 110 | +ignoredPattern=$(printf "%s\n" "${ignoredLines[@]}" | cut -d: -f1,2 | sort -u | tr '\n' '|' | sed 's/|$//') |
26 | 111 |
|
27 | | -# for check in ${!multiLineRegexChecks[@]}; do |
28 | | -# echo " Checking RegEx: '${check}'" |
| 112 | +# Second pass: check non-ignored rules, excluding ignored lines from output |
| 113 | +for check in "${!singleLineRegexChecks[@]}"; do |
| 114 | + ruleDesc="${singleLineRegexChecks[$check]}" |
| 115 | + if [[ "$ruleDesc" == *"#ignore"* ]]; then |
| 116 | + # Already handled in first pass, skip here |
| 117 | + continue |
| 118 | + fi |
| 119 | + |
| 120 | + matches=$(grep -P -r -I -n "${grep_exclude_args[@]}" "${input_paths[@]}" -e "$check" 2>/dev/null) |
| 121 | + |
| 122 | + # Filter out any ignored lines from matches |
| 123 | + if [[ -n "$matches" ]]; then |
| 124 | + # Remove lines already caught by ignored rules |
| 125 | + filteredMatches=$(printf "%s\n" "$matches" | grep -v -E "^($ignoredPattern):") |
| 126 | + |
| 127 | + if [[ -n "$filteredMatches" ]]; then |
| 128 | + # Skip lines with streamed or quoted braces |
| 129 | + filteredMatches=$(printf "%s\n" "$filteredMatches" | while IFS= read -r line; do |
| 130 | + # Extract just the code part from line (after filename:linenumber:) |
| 131 | + codePart=$(echo "$line" | cut -d: -f3-) |
29 | 132 |
|
30 | | -# if grep -Pzo -r -I ${check} src; then |
31 | | -# echo |
32 | | -# echo |
33 | | -# echo "${multiLineRegexChecks[$check]}" |
34 | | -# exit 1 |
35 | | -# fi |
36 | | -# done |
| 133 | + echo "$line" |
| 134 | + done) |
| 135 | +
|
| 136 | + if [[ -n "$filteredMatches" ]]; then |
| 137 | + echo |
| 138 | + echo "== Rule triggered: $ruleDesc ==" |
| 139 | + echo "$filteredMatches" |
| 140 | + triggeredDescriptions+=("$ruleDesc") |
| 141 | + hadError=1 |
| 142 | + fi |
| 143 | + fi |
| 144 | + fi |
| 145 | +done |
37 | 146 |
|
38 | 147 | echo |
39 | | -echo "Awesome! No issues..." |
| 148 | +echo "------------------------------------------" |
| 149 | +echo "Summary of Triggered Rules:" |
| 150 | +echo "------------------------------------------" |
| 151 | +
|
| 152 | +if [[ ${#triggeredDescriptions[@]} -eq 0 ]]; then |
| 153 | + echo "No style violations found." |
| 154 | +else |
| 155 | + declare -A seen=() |
| 156 | + exceptions=() |
| 157 | + violations=() |
| 158 | +
|
| 159 | + for desc in "${triggeredDescriptions[@]}"; do |
| 160 | + # Remove leading/trailing whitespace and outer quotes if present |
| 161 | + clean_desc="$(echo "$desc" | sed -E 's/^[[:space:]]+|[[:space:]]+$//g' | sed -E 's/^"(.*)"$/\1/')" |
| 162 | +
|
| 163 | + # Deduplicate |
| 164 | + [[ -n "${seen[$clean_desc]}" ]] && continue |
| 165 | + seen["$clean_desc"]=1 |
| 166 | +
|
| 167 | + # Check if the line ends with #ignore (with or without trailing space) |
| 168 | + if [[ "$desc" =~ [[:space:]]*#ignore$ ]]; then |
| 169 | + # Remove #ignore for printing |
| 170 | + exceptions+=("$(echo "$clean_desc" | sed -E 's/[[:space:]]*#ignore$//')") |
| 171 | + else |
| 172 | + violations+=("$clean_desc") |
| 173 | + fi |
| 174 | + done |
| 175 | +
|
| 176 | + echo "Exception Rules (Informational Only):" |
| 177 | + echo "----" |
| 178 | + if [[ ${#exceptions[@]} -eq 0 ]]; then |
| 179 | + echo "(none)" |
| 180 | + else |
| 181 | + for e in "${exceptions[@]}"; do |
| 182 | + echo "$e" |
| 183 | + done |
| 184 | + fi |
| 185 | +
|
| 186 | + echo "------------------------------------------" |
| 187 | + echo "Violations:" |
| 188 | + echo "----" |
| 189 | + if [[ ${#violations[@]} -eq 0 ]]; then |
| 190 | + echo "(none)" |
| 191 | + else |
| 192 | + for v in "${violations[@]}"; do |
| 193 | + echo "$v" |
| 194 | + done |
| 195 | + fi |
| 196 | +fi |
| 197 | +
|
| 198 | +exit $hadError |
0 commit comments