Last active
July 31, 2023 10:36
-
-
Save ThaiDat/1f7107376b0f95507808165df965782b to your computer and use it in GitHub Desktop.
The highly-customizable fast-and-simple bash script that will do various text-processing operations on files in a certain directory. It is useful for code formatting at the repository level and can be used in continuous integration and deployment (CI/CD) pipelines.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CL_FILE_EXTENSIONS: The script will apply on files with given extensions | |
if [ -z ${CL_FILE_EXTENSIONS+x} ]; then | |
CL_FILE_EXTENSIONS=(txt md json xml yaml yml sh bat) | |
fi | |
# CL_IGNORED_SUB_DIR: The script will ignore files in given directories | |
if [ -z ${CL_IGNORED_SUB_DIR+x} ]; then | |
CL_IGNORED_SUB_DIR=(.git) | |
fi | |
# CL_TARGET_DIR: The directory that the scripts will work on. | |
if [ -z ${CL_TARGET_DIR+x} ]; then | |
CL_TARGET_DIR="${1:-$(pwd)}" | |
fi | |
# CL_VERBOSE: Set to 1 to print log | |
if [ -z ${CL_VERBOSE+x} ]; then | |
CL_VERBOSE=0 | |
fi | |
# Prepare parameters for find command to ignore directories in CL_IGNORED_SUB_DIR | |
CL_PRUNE_DIRS=() | |
for cl_dir in "${CL_IGNORED_SUB_DIR[@]}"; do | |
CL_PRUNE_DIRS+=("-type" "f" "-name" "$cl_dir" "-prune" "-o") | |
done | |
# Prepare parameters for find command to include given extensions in CL_FILE_EXTENSIONS | |
CL_INCLUDE_FILES=() | |
for cl_ext in "${CL_FILE_EXTENSIONS[@]}"; do | |
CL_INCLUDE_FILES+=("-type" "f" "-name" "*.${cl_ext}" "-o") | |
done | |
for cl_file in $(find "$CL_TARGET_DIR" "${CL_PRUNE_DIRS[@]}" "${CL_INCLUDE_FILES[@]}" -false); do | |
# Verbose | |
if [ ${CL_VERBOSE} == 1 ]; then | |
echo "${cl_file}" | |
fi | |
# Remove trailing whitespaces and tab characters | |
sed -i 's/[ \t]*$//' "$cl_file" | |
# Remove empty lines at the beginning | |
sed -i '/./,$!d' "$cl_file" | |
# Remove empty lines at the end | |
sed -i -Ez '$ s/\n+$//' "$cl_file" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment