| 1 | #!/bin/bash |
|---|
| 2 | #----------------------------------------------------------------- |
|---|
| 3 | # This script formats the LMDZ fortran routines with |
|---|
| 4 | # a common and unified format |
|---|
| 5 | # it works in 1 or 2 steps: |
|---|
| 6 | # 1/an optional preprocessing step with findent (if_findent) |
|---|
| 7 | # (more robust for code structuration) |
|---|
| 8 | # 2/a formatting step with fprettify using a .fprettify format file |
|---|
| 9 | # |
|---|
| 10 | # if findent or fprettify displays an error or warning message, |
|---|
| 11 | # the file is not processed |
|---|
| 12 | # how to use: |
|---|
| 13 | # ./format_code.sh "file1 file2 file3" |
|---|
| 14 | # v1: November 2025 |
|---|
| 15 | #----------------------------------------------------------------- |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | # List of files to process |
|---|
| 19 | #------------------------------------------------------------------ |
|---|
| 20 | #files_to_process=( $(find ../libf/*/*blowing* -maxdepth 0 -type f ! -xtype l \( -name "*.f90" -o -name "*.F90" \)) ) |
|---|
| 21 | |
|---|
| 22 | files_to_process="$1" |
|---|
| 23 | |
|---|
| 24 | # Check installation of findent and fprettify |
|---|
| 25 | #------------------------------------------------------------------ |
|---|
| 26 | if command -v findent >/dev/null 2>&1; then |
|---|
| 27 | echo "findent has been installed" |
|---|
| 28 | findent --version |
|---|
| 29 | else |
|---|
| 30 | echo "findent has not been installed" |
|---|
| 31 | echo "please install it with sudo apt-get install findent" |
|---|
| 32 | fi |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | if command -v fprettify >/dev/null 2>&1; then |
|---|
| 36 | echo "fprettify has been installed." |
|---|
| 37 | fprettify --version |
|---|
| 38 | else |
|---|
| 39 | echo "fprettify has not been installed." |
|---|
| 40 | echo "please install it with: pip install fprettify" |
|---|
| 41 | fi |
|---|
| 42 | |
|---|
| 43 | # write the .fprettify configuration file |
|---|
| 44 | #------------------------------------------------------------------ |
|---|
| 45 | FILENAME=".fprettify" |
|---|
| 46 | |
|---|
| 47 | # check if the .fprettify exists |
|---|
| 48 | if [ -f "$FILENAME" ]; then |
|---|
| 49 | echo "the file $FILENAME already exists" |
|---|
| 50 | else |
|---|
| 51 | echo "I create the file $FILENAME " |
|---|
| 52 | |
|---|
| 53 | # create the file |
|---|
| 54 | cat << 'EOF' > "$FILENAME" |
|---|
| 55 | # ========================= |
|---|
| 56 | # fprettify configuration |
|---|
| 57 | # ========================= |
|---|
| 58 | |
|---|
| 59 | # Indentation (number of spaces) |
|---|
| 60 | indent = 3 |
|---|
| 61 | |
|---|
| 62 | # Maximum line length |
|---|
| 63 | line-length = 200 |
|---|
| 64 | |
|---|
| 65 | # Indent internal blocks (do, if, select case…) |
|---|
| 66 | indent-blocks = true |
|---|
| 67 | |
|---|
| 68 | # Add whitespace around operators (=, +, -, *, /, etc.) |
|---|
| 69 | whitespace = true |
|---|
| 70 | |
|---|
| 71 | # Keyword normalization (lowercase or uppercase) |
|---|
| 72 | case = [2,2,2,2] |
|---|
| 73 | |
|---|
| 74 | # Reformat declarations (type :: variable) |
|---|
| 75 | reformat-declarations = true |
|---|
| 76 | |
|---|
| 77 | # Align variable declarations |
|---|
| 78 | align-declarations = true |
|---|
| 79 | |
|---|
| 80 | # Align consecutive assignments |
|---|
| 81 | align-assignments = true |
|---|
| 82 | |
|---|
| 83 | # Align arguments in multi-line calls |
|---|
| 84 | align-arguments = true |
|---|
| 85 | |
|---|
| 86 | # Preserve comments as-is |
|---|
| 87 | preserve-comments = true |
|---|
| 88 | |
|---|
| 89 | # Do not modify string literals |
|---|
| 90 | preserve-strings = true |
|---|
| 91 | |
|---|
| 92 | # Reformat line continuations (&) |
|---|
| 93 | reformat-continuations = true |
|---|
| 94 | |
|---|
| 95 | # Reformat USE statements |
|---|
| 96 | reformat-use-statements = true |
|---|
| 97 | |
|---|
| 98 | # Allow inline formatting control |
|---|
| 99 | # (via ! fprettify: off / on) |
|---|
| 100 | enable-inline-control = true |
|---|
| 101 | EOF |
|---|
| 102 | fi |
|---|
| 103 | |
|---|
| 104 | # formatting step |
|---|
| 105 | #------------------------------------------------------------------ |
|---|
| 106 | # Array to store files that could not be processed |
|---|
| 107 | failures=() |
|---|
| 108 | |
|---|
| 109 | # Function that applies findent and fprettify to a file |
|---|
| 110 | # with checks on errors and warnings |
|---|
| 111 | |
|---|
| 112 | process_file() { |
|---|
| 113 | local f="$1" |
|---|
| 114 | local tmp |
|---|
| 115 | tmp=$(mktemp) || return 1 |
|---|
| 116 | |
|---|
| 117 | local patterns="error" |
|---|
| 118 | local err |
|---|
| 119 | local ret |
|---|
| 120 | |
|---|
| 121 | cp "$f" "$tmp" |
|---|
| 122 | |
|---|
| 123 | # Step 1: findent |
|---|
| 124 | if_findent=0 |
|---|
| 125 | if [ "$if_findent" -eq 1 ]; then |
|---|
| 126 | rm "$tmp" |
|---|
| 127 | err=$(findent < "$f" 2>&1) |
|---|
| 128 | ret=$? |
|---|
| 129 | if [ $ret -ne 0 ] || echo "$err" | grep -Eqi "$patterns"; then |
|---|
| 130 | echo "findent detected ERROR in $f" |
|---|
| 131 | echo $err |
|---|
| 132 | #rm -f "$tmp" |
|---|
| 133 | return 1 |
|---|
| 134 | fi |
|---|
| 135 | findent < "$f" > "$tmp" 2>/dev/null |
|---|
| 136 | fi |
|---|
| 137 | |
|---|
| 138 | # Step 2: fprettify |
|---|
| 139 | err=$(fprettify "$tmp" 2>&1) |
|---|
| 140 | ret=$? |
|---|
| 141 | if [ $ret -ne 0 ] || echo "$err" | grep -Eqi "$patterns"; then |
|---|
| 142 | echo "fprettify detected ERROR in $f" |
|---|
| 143 | echo "$err" |
|---|
| 144 | #rm -f "$tmp" |
|---|
| 145 | return 1 |
|---|
| 146 | fi |
|---|
| 147 | |
|---|
| 148 | # Step 3: replace original file |
|---|
| 149 | mv "$tmp" "$f" |
|---|
| 150 | return 0 |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | # Loop over all the files that we want to process |
|---|
| 155 | for f in "${files_to_process[@]}"; do |
|---|
| 156 | echo "Processing $f ..." |
|---|
| 157 | |
|---|
| 158 | if process_file "$f"; then |
|---|
| 159 | echo "ok" |
|---|
| 160 | else |
|---|
| 161 | echo "failure" |
|---|
| 162 | failures+=("$f") |
|---|
| 163 | fi |
|---|
| 164 | done |
|---|
| 165 | |
|---|
| 166 | # Summary report |
|---|
| 167 | echo |
|---|
| 168 | echo "========== SUMMARY ==========" |
|---|
| 169 | if [ ${#failures[@]} -eq 0 ]; then |
|---|
| 170 | echo "All files were processed successfully." |
|---|
| 171 | else |
|---|
| 172 | echo "The following files could NOT be processed:" |
|---|
| 173 | for f in "${failures[@]}"; do |
|---|
| 174 | echo " - $f" |
|---|
| 175 | done |
|---|
| 176 | fi |
|---|
| 177 | |
|---|
| 178 | |
|---|