Skip to content

Commit eb8de80

Browse files
committed
Port Empirical tidy enforcement
1 parent 4714e6c commit eb8de80

31 files changed

+629
-0
lines changed

.github/workflows/CI.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ jobs:
4949
tidy:
5050
runs-on: ubuntu-latest
5151
steps:
52+
- name: Install apt dependencies
53+
run: sudo apt-get install -y rename
5254
- uses: actions/checkout@v1
5355
- name: Set up Python 3.8
5456
uses: actions/setup-python@v2
@@ -63,6 +65,8 @@ jobs:
6365
- uses: editorconfig-checker/action-editorconfig-checker@main
6466
- name: Test editorconfig tidyness
6567
run: editorconfig-checker
68+
- name: Test tidy/ tidyness
69+
run: ./tidy/test_tidy.sh
6670

6771
deploy:
6872
needs: [coverage, test, tidy]

tidy/impl/alphabetize_includes.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# enforce use of GNU version of coreutils
5+
. ./tidy/util/enforce_gnu_utils.sh
6+
7+
TARGETS=$(find . -type f \( -name "*.hpp" -o -name "*.cpp" \) ! -path "./third-party/*" ! -path "./node_modules/*")
8+
9+
for filename in ${TARGETS}
10+
do
11+
12+
printf "."
13+
14+
# adapted from https://stackoverflow.com/a/6970681
15+
include_linenos=$(awk '/^#include .*/ {print FNR}' "${filename}")
16+
17+
# adapted from https://stackoverflow.com/a/26809816
18+
# and https://superuser.com/a/284192
19+
echo "${include_linenos}" | tr ' ' '\n' | awk 'NR==1{first=$1;last=$1;next} $1 == last+1 {last=$1;next} {print first,last;first=$1;last=first} END{print first,last}' | while read line ; do
20+
21+
# adapted from https://unix.stackexchange.com/a/11064
22+
read start stop <<< "${line}"
23+
24+
# adapted from https://stackoverflow.com/a/46018238
25+
# and https://stackoverflow.com/a/32723119
26+
# and https://unix.stackexchange.com/a/87748
27+
export LC_ALL=C
28+
echo "x" | ex -s -c "${start},${stop}!sort --stable --ignore-nonprinting --ignore-case --dictionary-order" $filename
29+
# see also https://www.gnu.org/software/coreutils/manual/html_node/sort-invocation.html
30+
31+
done
32+
done
33+
34+
echo
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# enforce use of GNU version of coreutils
5+
. ./tidy/util/enforce_gnu_utils.sh
6+
7+
# greq -qI tests if file is binary
8+
# adapted from https://stackoverflow.com/a/29689345
9+
TARGETS=$(find . -type f ! -path "./third-party/*" ! -path "./.git/*" ! -path "*/assets/*" ! -path "*.tar.gz" ! -path "*.jpg" ! -path "*.png" ! -path "*Makefile" ! -path "*Maketemplate*" ! -path "./node_modules/*" -exec grep -qI . {} ';' -print)
10+
11+
for filename in ${TARGETS}
12+
do
13+
sed -i -e '$a\' "${filename}"
14+
done

tidy/impl/generate_boilerplate.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
set -e
3+
4+
./tidy/impl/generate_boilerplate_headerguards.sh
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
for filename in $(cd include && find * -name '*.hpp' -type f); do
4+
5+
GUARD=$( sed "s/[^[:alnum:]]/_/g" <<< "${filename}" | tr [a-z] [A-Z] )
6+
NDEF_LINE="#ifndef ${GUARD}_INCLUDE"
7+
DEF_LINE="#define ${GUARD}_INCLUDE"
8+
ENDIF_LINE="#endif // ${NDEF_LINE}"
9+
10+
sed -i '1s/^.*$/'"#pragma once"'/' "include/${filename}"
11+
sed -i '2s/^.*$/'"${NDEF_LINE}"'/' "include/${filename}"
12+
sed -i '3s/^.*$/'"${DEF_LINE}"'/' "include/${filename}"
13+
sed -i '$ s,^.*$,'"${ENDIF_LINE}"',g' "include/${filename}"
14+
15+
done
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# enforce use of GNU version of coreutils
5+
. ./tidy/util/enforce_gnu_utils.sh
6+
7+
# license notice boilerplate for source files that aren't in include/
8+
9+
for filename in $(find . -name '*.cpp' -type f ! -path "./third-party/*") $(find . -name '*.hpp' -type f ! -path "./third-party/*" ! -path "./include/*"); do
10+
11+
# grow file up to at least 8 lines
12+
FILE_NUM_LINES="$(cat "${filename}" | wc -l)"
13+
for __ in $(seq "${FILE_NUM_LINES}" 1 6); do
14+
echo >> "${filename}"
15+
done
16+
17+
# stamp in expected boilerplate line-by-line
18+
# just like file docstrings, but don't require a brief
19+
# stamp in expected boilerplate line-by-line
20+
sed -i '1s|^.*$|/**|' "${filename}"
21+
sed -i '2s|^.*$| * @note This file is part of Empirical, https://github.com/devosoft/Empirical|' "${filename}"
22+
sed -i '3s|^.*$| * @copyright Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md|' "${filename}"
23+
# only match if a @date isn't currently in place
24+
# if we see @date, "break" (b) sed script for that line
25+
# adapted from https://stackoverflow.com/a/5334825
26+
# and https://stackoverflow.com/a/12178023
27+
# and https://stackoverflow.com/a/9053163
28+
sed -i "/^ \* @date /b; 4s/^.*\$/ * @date $(date +'%Y')/" "${filename}"
29+
sed -i '5s/^.*$/ */' "${filename}"
30+
sed -i "6s/^.*\$/ * @file $(basename "${filename}")/" "${filename}"
31+
# only match empty lines
32+
# add extra * to replace later with */ when constructing fresh
33+
sed -i '7s/^$/ */' "${filename}"
34+
35+
# close boilerplate file docstring
36+
# must accomodate possible additional content in docstring
37+
# so, search backwards from first blank line to place close */
38+
FILE_NUM_LINES="$(cat "${filename}" | wc -l)"
39+
ONE_PAST_LAST_LINE_NO="$((${FILE_NUM_LINES}+1))"
40+
# adapted from https://stackoverflow.com/a/66474842
41+
FIRST_BLANK_LINE_NO="$(awk '! NF { print NR; exit }' "${filename}")"
42+
# two cases: file does or does not have a blank line
43+
FIRST_EMPTY_LINE_NO="$(grep -q '^$' "${filename}" && echo "${FIRST_BLANK_LINE_NO}" || echo "${ONE_PAST_LAST_LINE_NO}")"
44+
DOCSTRING_CLOSE_LINE_NO="$((${FIRST_EMPTY_LINE_NO}-1))"
45+
# stamp */ into line preceding first empty line
46+
sed -i "${DOCSTRING_CLOSE_LINE_NO}s|^.*\$| */|" "${filename}"
47+
48+
49+
done
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# enforce use of GNU version of coreutils
5+
. ./tidy/util/enforce_gnu_utils.sh
6+
7+
# enforce availability of dependencies
8+
. ./tidy/util/enforce_dependency.sh rename
9+
10+
find ! -path "./third-party/*" ! -path "./.git/*" -type f | rename 's/\.h$/.hpp/'
11+
find ! -path "./third-party/*" ! -path "./.git/*" -type f | rename 's/\.c$/.cpp/'
12+
find ! -path "./third-party/*" ! -path "./.git/*" -type f | rename 's/\.cc$/.cpp/'

tidy/impl/partition_includes.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# enforce use of GNU version of coreutils
5+
. ./tidy/util/enforce_gnu_utils.sh
6+
7+
TARGETS=$(find . -type f \( -name "*.hpp" -o -name "*.cpp" \) ! -path "./third-party/*" ! -path "./node_modules/*")
8+
9+
for filename in ${TARGETS}
10+
do
11+
12+
# insert a blank line between #include <'s and #include "'s
13+
# adapted from https://www.unix.com/shell-programming-and-scripting/186893-how-insert-line-between-two-consecutive-lines-match-special-pattern.html
14+
awk -v ADD='' -v ANGLEINCLUDE='^#include <.+>$' -v QUOTEINCLUDE='^#include ".+"$' '/^.+$/ L { print L; if(((L ~ ANGLEINCLUDE) && ($0 ~ QUOTEINCLUDE)) || ((L ~ QUOTEINCLUDE) && ($0 ~ ANGLEINCLUDE)) ) print ADD }; { L=$0 } END { print L }' "${filename}" | awk 'NR!=1 {print}' > "${filename}.bak"
15+
mv "${filename}.bak" "${filename}"
16+
17+
# insert a blank line between #include "'s and #include "../'s
18+
# adapted from https://www.unix.com/shell-programming-and-scripting/186893-how-insert-line-between-two-consecutive-lines-match-special-pattern.html
19+
awk -v ADD='' -v PWDINCLUDE='^#include "[0-9a-zA-Z_].*"$' -v RELATIVEINCLUDE='^#include "../[0-9a-zA-Z_].*"$' '/^.+$/ L { print L; if(((L ~ PWDINCLUDE) && ($0 ~ RELATIVEINCLUDE)) || ((L ~ RELATIVEINCLUDE) && ($0 ~ PWDINCLUDE)) ) print ADD }; { L=$0 } END { print L }' "${filename}" | awk 'NR!=1 {print}' > "${filename}.bak"
20+
mv "${filename}.bak" "${filename}"
21+
22+
done
23+
24+
echo

tidy/impl/replace_tabs.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# enforce use of GNU version of coreutils
5+
. ./tidy/util/enforce_gnu_utils.sh
6+
7+
# greq -qI tests if file is binary
8+
# adapted from https://stackoverflow.com/a/29689345
9+
TARGETS=$(find . -type f ! -path "./third-party/*" ! -path "./.git/*" ! -path "*/assets/*" ! -path "*Makefile" ! -path "*Maketemplate*" ! -path "./node_modules/*" -exec grep -qI . {} ';' -print)
10+
11+
for filename in ${TARGETS}
12+
do
13+
sed -i 's/\t/ /g' "${filename}"
14+
done
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# enforce use of GNU version of coreutils
5+
. ./tidy/util/enforce_gnu_utils.sh
6+
7+
# enforce availability of dependencies
8+
. ./tidy/util/enforce_dependency.sh rename
9+
10+
find ! -path "./third-party/*" ! -path "./node_modules/*" -type d | grep '\s' | rename 's/\s/_/g' # do the directories first
11+
find ! -path "./third-party/*" ! -path "./node_modules/*" -type f | grep '\s' | rename 's/\s/_/g' # do the directories first

0 commit comments

Comments
 (0)