From 16a5913687b4dd2fbbf8ef4bd74fdee10319c6a0 Mon Sep 17 00:00:00 2001
From: David Gauchard <gauchard@laas.fr>
Date: Sun, 17 Feb 2019 22:57:52 +0100
Subject: [PATCH] prepare allman style

---
 tests/astyle_core.conf                        | 32 +++++++++++++++++++
 ...amples_style.conf => astyle_examples.conf} |  0
 tests/ci/style_check.sh                       |  6 ++--
 tests/examples_restyle.sh                     |  3 --
 tests/restyle-all.sh                          | 31 ++++++++++++++++++
 tests/restyle.sh                              | 19 +++++++++++
 6 files changed, 84 insertions(+), 7 deletions(-)
 create mode 100644 tests/astyle_core.conf
 rename tests/{examples_style.conf => astyle_examples.conf} (100%)
 delete mode 100755 tests/examples_restyle.sh
 create mode 100755 tests/restyle-all.sh
 create mode 100755 tests/restyle.sh

diff --git a/tests/astyle_core.conf b/tests/astyle_core.conf
new file mode 100644
index 0000000000..f9ee90adb6
--- /dev/null
+++ b/tests/astyle_core.conf
@@ -0,0 +1,32 @@
+# Code formatting rules for Arduino examples, taken from:
+#
+#   https://github.com/arduino/Arduino/blob/master/build/shared/examples_formatter.conf
+#
+
+mode=c
+lineend=linux
+style=allman
+
+# 4 spaces indentation
+indent=spaces=4
+
+# also indent macros
+#indent-preprocessor
+
+# indent classes, switches (and cases), comments starting at column 1
+indent-col1-comments
+
+# put a space around operators
+pad-oper
+
+# put a space after if/for/while
+pad-header
+
+# if you like one-liners, keep them
+keep-one-line-statements
+
+attach-closing-while
+unpad-paren
+pad-oper
+remove-comment-prefix
+add-braces
diff --git a/tests/examples_style.conf b/tests/astyle_examples.conf
similarity index 100%
rename from tests/examples_style.conf
rename to tests/astyle_examples.conf
diff --git a/tests/ci/style_check.sh b/tests/ci/style_check.sh
index d51d9c54cd..a6b3532426 100755
--- a/tests/ci/style_check.sh
+++ b/tests/ci/style_check.sh
@@ -4,10 +4,8 @@
 
 set -ev
 
-find $TRAVIS_BUILD_DIR/libraries -name '*.ino' -exec \
-    astyle \
-        --suffix=none \
-        --options=$TRAVIS_BUILD_DIR/tests/examples_style.conf {} \;
+org=$(cd ${0%/*}; pwd)
+${org}/../restyle.sh
 
 # Revert changes which astyle might have done to the submodules,
 # as we don't want to fail the build because of the 3rd party libraries
diff --git a/tests/examples_restyle.sh b/tests/examples_restyle.sh
deleted file mode 100755
index 9d56aa41a2..0000000000
--- a/tests/examples_restyle.sh
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/bin/bash
-cd $(cd ${0%/*}; pwd)
-astyle --options=examples_style.conf ../libraries/*/examples/*{,/*}/*.ino
diff --git a/tests/restyle-all.sh b/tests/restyle-all.sh
new file mode 100755
index 0000000000..e751f476de
--- /dev/null
+++ b/tests/restyle-all.sh
@@ -0,0 +1,31 @@
+#!/bin/sh
+
+set -e
+
+org=$(cd ${0%/*}; pwd)
+cd ${org}/..
+pwd
+test -d cores/esp8266
+test -d libraries
+
+# this warning question will be removed after restyle-all.sh is renamed to restyle.sh
+echo "This is dangerous if you have modified your local repository"
+echo "type iknowwhatido to continue"
+read ans
+test "$ans" = iknowwhatido || exit 1
+
+for d in cores/esp8266 libraries; do
+    for e in c cpp h; do
+        find $d -name "*.$e" -exec \
+            astyle \
+                --suffix=none \
+                --options=${org}/astyle_core.conf {} \;
+        done
+done
+
+for d in libraries; do
+    find $d -name "*.ino" -exec \
+        astyle \
+            --suffix=none \
+            --options=${org}/astyle_examples.conf {} \;
+done
diff --git a/tests/restyle.sh b/tests/restyle.sh
new file mode 100755
index 0000000000..513a891900
--- /dev/null
+++ b/tests/restyle.sh
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+set -e
+
+org=$(cd ${0%/*}; pwd)
+cd ${org}/..
+pwd
+test -d cores/esp8266
+test -d libraries
+
+# in a near future, restyle-all.sh will be renamed to restyle.sh
+# and will be checked against CI
+
+for d in libraries; do
+	find $d -name "*.ino" -exec \
+	    astyle \
+	        --suffix=none \
+                --options=${org}/astyle_examples.conf {} \;
+done