source: LMDZ6/trunk/tools/format_code.sh @ 6023

Last change on this file since 6023 was 6023, checked in by evignon, 6 days ago

update du script format_code pour avoir des codes lmdz bien indentes
et avec un format "propre"

  • Property svn:executable set to *
File size: 4.2 KB
Line 
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
22files_to_process="$1"
23
24# Check installation of findent and fprettify
25#------------------------------------------------------------------
26if command -v findent >/dev/null 2>&1; then
27    echo "findent has been installed"
28    findent --version
29else
30    echo "findent has not been installed"
31    echo "please install it with sudo apt-get install findent"
32fi
33
34
35if command -v fprettify >/dev/null 2>&1; then
36    echo "fprettify has been installed."
37    fprettify --version
38else
39    echo "fprettify has not been installed."
40    echo "please install it with: pip install fprettify"
41fi
42
43# write the .fprettify configuration file
44#------------------------------------------------------------------
45FILENAME=".fprettify"
46
47# check if the .fprettify exists
48if [ -f "$FILENAME" ]; then
49    echo "the file $FILENAME already exists"
50else
51echo "I create the file $FILENAME "
52
53# create the file
54cat << 'EOF' > "$FILENAME"
55# =========================
56# fprettify configuration
57# =========================
58
59# Indentation (number of spaces)
60indent = 3
61
62# Maximum line length
63line-length = 200
64
65# Indent internal blocks (do, if, select case…)
66indent-blocks = true
67
68# Add whitespace around operators (=, +, -, *, /, etc.)
69whitespace = true
70
71# Keyword normalization (lowercase or uppercase)
72case = [2,2,2,2]
73
74# Reformat declarations (type :: variable)
75reformat-declarations = true
76
77# Align variable declarations
78align-declarations = true
79
80# Align consecutive assignments
81align-assignments = true
82
83# Align arguments in multi-line calls
84align-arguments = true
85
86# Preserve comments as-is
87preserve-comments = true
88
89# Do not modify string literals
90preserve-strings = true
91
92# Reformat line continuations (&)
93reformat-continuations = true
94
95# Reformat USE statements
96reformat-use-statements = true
97
98# Allow inline formatting control
99# (via ! fprettify: off / on)
100enable-inline-control = true
101EOF
102fi
103
104# formatting step
105#------------------------------------------------------------------
106# Array to store files that could not be processed
107failures=()
108
109# Function that applies findent and fprettify to a file
110# with checks on errors and warnings
111
112process_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
155for 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
164done
165
166# Summary report
167echo
168echo "========== SUMMARY =========="
169if [ ${#failures[@]} -eq 0 ]; then
170    echo "All files were processed successfully."
171else
172    echo "The following files could NOT be processed:"
173    for f in "${failures[@]}"; do
174        echo " - $f"
175    done
176fi
177
178
Note: See TracBrowser for help on using the repository browser.