bin/check_newline_eof
#!/usr/bin/env bash
# Finds files without final newlines
# Pass "-f" to also fix those files
#
# Always have a new line marker at the end of every file comprised of text.
# In a POSIX system a file missing a final new line is technically not a text file.
# Some tools will not parse them, or parse them in unexpected ways.
# See:
# Official POSIX Standard: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206
# Discussion of "why newline at EOF?": https://stackoverflow.com/q/729692/213191
# Primary Source: https://stackoverflow.com/a/67426395/213191
# Author: ppar, https://stackoverflow.com/users/9983387/ppar
# Documentation & theory behind `-not \( -path ... \)`: https://stackoverflow.com/a/69830768/213191
# Author: Gabriel Staples, https://stackoverflow.com/users/4561887/gabriel-staples
# License: CC BY-SA 4.0, https://creativecommons.org/licenses/by-sa/4.0/
fix_flag="$([ "$1" == "-f" ] && echo -true || echo -false)"
(test $(find . \
-type f \
-not \( -name "*.css" \) \
-not \( -name "*.scss" \) \
-not \( -name "*.csv" \) \
-not \( -name "*.json" \) \
-not \( -name "VERSION*" \) \
-not \( -path "*/.bundle/*" -prune \) \
-not \( -path "*/.git/*" -prune \) \
-not \( -path "*/.idea/*" -prune \) \
-not \( -path "*/.vscode/*" -prune \) \
-not \( -path "*/checksums/*" -prune \) \
-not \( -path "*/coverage/*" -prune \) \
-not \( -path "*/doc/*" -prune \) \
-not \( -path "*/log/*" -prune \) \
-not \( -path "*/node_modules/*" -prune \) \
-not \( -path "*/public/*" -prune \) \
-not \( -path "*/results/*" -prune \) \
-not \( -path "*/test-results/*" -prune \) \
-not \( -path "*/tmp/*" -prune \) \
-not \( -path "*/vendor/*" -prune \) \
-exec sh -c 'file -b "{}" | grep -q text' \; \
-exec sh -c '[ "$(tail -c 1 "{}" | od -An -a | tr -d "[:space:]")" != "nl" ]' \; \
-print \
$fix_flag \
-exec sh -c 'echo >> "{}"' \; | tee pb_ruby_newlines_missing.txt | wc -l) -eq "0" && \
rm pb_ruby_newlines_missing.txt) || \
(cat pb_ruby_newlines_missing.txt && rm pb_ruby_newlines_missing.txt && exit 1)