|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 4 | +# or more contributor license agreements. Licensed under the Elastic License; |
| 5 | +# you may not use this file except in compliance with the Elastic License. |
| 6 | +# |
| 7 | + |
| 8 | +# Reformats the driver code, using Artistic Style, to ensure consistency. |
| 9 | + |
| 10 | + |
| 11 | +# By default, format the driver source code (no arguments are received); |
| 12 | +# else append the arguments to the pre-set formating parameters |
| 13 | +if [ $# -lt 1 ] ; then |
| 14 | + ARGS=`git rev-parse --show-toplevel`/driver/*.[ch] |
| 15 | +else |
| 16 | + ARGS=${@:1} |
| 17 | +fi |
| 18 | + |
| 19 | +# |
| 20 | +# Ensure astyle is available and is of the right version to have all below |
| 21 | +# parameters supported. |
| 22 | +# |
| 23 | +if ! which -s astyle ; then |
| 24 | + echo "ERROR: The astyle code formatter is not available. Exiting." |
| 25 | + exit 1 |
| 26 | +fi |
| 27 | + |
| 28 | +REQUIRED_ASTYLE_VERSION=3.1 |
| 29 | +FOUND_ASTYLE_VERSION=$(expr "`astyle --version`" : ".* \([0-9].[0-9]\(.[0-9]\)\{0,1\}\)") |
| 30 | + |
| 31 | +if [ -z "${FOUND_ASTYLE_VERSION}" ] ; then |
| 32 | + echo "ERROR: Required astyle version ${REQUIRED_ASTYLE_VERSION} not found." |
| 33 | + echo " Could not determine astyle version." |
| 34 | + exit 2 |
| 35 | +fi |
| 36 | + |
| 37 | +if [ "${REQUIRED_ASTYLE_VERSION}" != "${FOUND_ASTYLE_VERSION}" ] ; then |
| 38 | + echo "ERROR: Required astyle version ${REQUIRED_ASTYLE_VERSION} not found." |
| 39 | + echo " Detected astyle version ${FOUND_ASTYLE_VERSION}" |
| 40 | + exit 3 |
| 41 | +fi |
| 42 | + |
| 43 | +# A10: "One True Brace Style" uses linux braces and adds braces to unbraced |
| 44 | +# one line conditional statements. (and apparently loops too) |
| 45 | +# k3 : align-pointer=name |
| 46 | +# T : Indent using all tab characters, if possible. |
| 47 | +# xC : break a line if the code exceeds # characters. |
| 48 | +# xU : Indent, instead of align, continuation lines following lines that |
| 49 | +# contain an opening paren '(' or an assignment '='. |
| 50 | +# xt : Set the continuation indent for a line that ends with an opening paren |
| 51 | +# '(' or an assignment '='. |
| 52 | +# w : Indent multi-line preprocessor definitions ending with a backslash. |
| 53 | +# S : Indent 'switch' blocks so that the 'case X:' statements are indented in |
| 54 | +# the switch block |
| 55 | +# xL : will cause the logical conditionals to be placed last on the previous |
| 56 | +# line. |
| 57 | +# xW : Indent preprocessor blocks at brace level zero and immediately within a |
| 58 | +# namespace. |
| 59 | +# xf : |
| 60 | +# xh : Attach the return type to the function name (in deFinition and |
| 61 | +# declaration). |
| 62 | +# n : Do not retain a backup of the original file. |
| 63 | +# --dry-run -Q : only output file that would be formatted |
| 64 | +ASTYLE_PARAMS="-A10 -k3 -T -xC79 -xU -w -S -xt -xL -xf -xh -n" |
| 65 | +astyle $ASTYLE_PARAMS $ARGS |
| 66 | + |
0 commit comments