Commit 65e99a07 authored by Pádraig Ó Conbhuí's avatar Pádraig Ó Conbhuí
Browse files

Added tools for listing files in the project, and for formatting them.

parent 5f2c041e
#!/usr/bin/env bash
# List the suffixes that will be formatted in subdirectory recursion.
suffixes=( .cpp .hpp )
#
# Parse optional arguments
#
# Set defaults for the optional arguments
CLANG_FORMAT_EXECUTABLE=$(which clang-format)
VERBOSE=
# Loop over arguments
while :
do
case "$1" in
--clang-format=*)
CLANG_FORMAT_EXECUTABLE="$(echo "$1" | sed 's/--clang-format=//')"
shift
;;
--verbose)
VERBOSE="--verbose"
shift
;;
--help)
echo
echo "Usage: format.sh [OPTIONS] [FILE|DIRECTORY ...]"
echo
echo "Options:"
echo " --clang-format=CLANG_FORMAT_EXECUTABLE"
echo " Set the clang-format executable to use."
echo
echo " --verbose"
echo " Enable verbose output"
echo
echo "Examples:"
echo " format.sh"
echo " format.sh /path/to/file.cpp"
echo " format.sh --clang-format=my-clang-format file.cpp"
echo " format.sh /path/to/src"
echo " format.sh file_1.cpp file_2.cpp src/"
echo
exit 0
;;
*)
break
;;
esac
done
echo_verbose() {
if [[ -n "${VERBOSE}" ]]
then
echo >&2 "$@"
fi
}
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
#
# If given exactly one file argument, parse it.
#
if [[ $# -eq 1 ]] && [[ -f "$1" ]]
then
target_file=$1
# Only format files with the correct suffix
if [[ -z "$("${script_dir}"/list_files.sh "${suffixes[@]}" "${target_file}")" ]]
then
echo >&2 "Error: File suffix not one of: \"${suffixes[@]}\""
echo >&2 " File: ${target_file}"
exit 1
fi
echo_verbose "Formatting: ${target_file}"
# Make a temporary file that deletes itself on script exit
tmp_base="$(basename ${target_file}).XXXXXX"
tmpfile="$(mktemp "${tmp_base}")"
trap 'rm -f "${tmpfile}"' EXIT
# Run clang-format!
"${CLANG_FORMAT_EXECUTABLE}" "${target_file}" > "${tmpfile}"
# If the output file is of size zero, an error occurred, abort
if [[ ! -s "${tmpfile}" ]]
then
echo >&2 "Error: failed to format file!"
exit 1
fi
# If the formatted and unformatted files are the same, don't
# touch the original
if ! cmp "${tmpfile}" "${target_file}"
then
# Using cat instead of mv to preserve permissions etc.
cat "${tmpfile}" > "${target_file}"
fi
exit 0
fi
# On some / no file arguments, run the formatter over all the files.
"${script_dir}"/list_files.sh "${suffixes[@]}" "$@" \
| xargs -n 1 "$0" --clang-format="${CLANG_FORMAT_EXECUTABLE}" ${VERBOSE}
#!/usr/bin/env bash
# Project subdirectories to search for files in
subdirectories=( "src" )
if [[ $# -eq 0 ]]
then
echo "Usage: list_files.sh SUFFIX [...] [FILE|DIRECTORY ...]"
echo
echo "Examples:"
echo " list_files.sh .hpp"
echo " list_files.sh .cpp src/"
echo " list_files.sh .cpp path/to/file.cpp"
echo
echo "Suffixes are differentiated from files and directories by simply not"
echo "being existing files or directories. Therefore, this command can"
echo "only be used to list existing files or directories."
exit 1
fi
# Loop over arguments, pop off non-existant files/directories as suffixes.
declare -a suffixes
while [[ $# -gt 0 ]]
do
if [[ -f "$1" ]] || [[ -d "$1" ]]
then
break
fi
suffixes=( "${suffixes[@]}" "$1" )
shift
done
#
# Put the script into the project directory, assuming it has been placed in
# a subdirectory. e.g. project/tools/list_files.sh
#
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
project_dir="$(cd "${script_dir}" && cd .. && pwd)"
#
# List all the files.
# The `find` command isn't strictly guaranteed to work in all bash environments.
# It's broken in e.g. the Cmder terminal on Windows.
#
# If no file or directory arguments have been supplied, list all the available
# suffixes in the project subdirectories
if [[ "$#" -eq 0 ]]
then
cd "${project_dir}"
for suffix in "${suffixes[@]}"
do
for subdirectory in "${subdirectories[@]}"
do
pushd "${subdirectory}" >/dev/null
find "${PWD}" -iname '*'"${suffix}"
popd > /dev/null
done
done
exit 0
fi
# If files or directories have been passed, look in there.
for file_or_directory in "$@"
do
for suffix in "${suffixes[@]}"
do
find "${file_or_directory}" -iname '*'"${suffix}"
done
exit 0
done
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment