Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 73 additions & 66 deletions src/beautify_script.sh
Original file line number Diff line number Diff line change
@@ -1,93 +1,100 @@
#!/usr/bin/env bash

# Script Name: beautify_script.sh
# Description: This script will format shell scripts using Beautysh and then perform a ShellCheck analysis.
# Usage: beautify_script.sh [--check] path
# --check - When specified, script will only check if formatting is needed without actually formatting
# path - The path can be a directory or a single file.
# Description: Formats shell scripts using Beautysh and analyzes them with ShellCheck.
# Usage: beautify_script.sh [--check] <path>
# --check - Only check if formatting is needed, do not modify files.
# <path> - Directory or file to process.
# Example: ./beautify_script.sh ./my_directory

set -euo pipefail

# Color codes for output
RED="$(tput setaf 1)"
GREEN="$(tput setaf 2)"
YELLOW="$(tput setaf 3)"
CYAN="$(tput setaf 6)"
RESET="$(tput sgr0)"

status=0

format() {
local filepath=$1
local checkonly=$2
local origfile
local fmtfile
log() {
local color="$1"; shift
echo -e "${color}$*${RESET}"
}

if [[ $filepath == *.sh ]]; then
echo "Processing $filepath"
check_tools() {
command -v beautysh >/dev/null 2>&1 || { log "$RED" "[ERROR] beautysh is not installed."; exit 1; }
command -v shellcheck >/dev/null 2>&1 || { log "$RED" "[ERROR] shellcheck is not installed."; exit 1; }
}

if [[ $checkonly -ne 1 ]]; then
if ! beautysh "$filepath"; then
echo "An error occurred while formatting $filepath."
status=1
fi
format_file() {
local file="$1"
local checkonly="$2"
log "$CYAN" "Processing: $file"

if ! shellcheck --exclude=SC1091,SC2001 "$filepath"; then
echo "ShellCheck reported issues in $filepath."
status=1
fi
if [[ $checkonly -eq 1 ]]; then
# Check formatting
if ! diff -q <(beautysh - < "$file") "$file" >/dev/null; then
log "$YELLOW" "[NEEDS FORMAT] $file"
status=1
fi
# ShellCheck analysis
if ! shellcheck --exclude=SC1091,SC2001 "$file"; then
log "$RED" "[SHELLCHECK FAIL] $file"
status=1
fi
else
# Format in place
if ! beautysh "$file"; then
log "$RED" "[FORMAT ERROR] $file"
status=1
else
origfile=$(cat "$filepath")
fmtfile=$(beautysh - < "$filepath")
if [[ "$origfile" != "$fmtfile" ]]; then
echo "$filepath requires formatting"
status=1
fi

if ! shellcheck --exclude=SC1091,SC2001 "$filepath"; then
echo "ShellCheck reported issues in $filepath."
status=1
fi
log "$GREEN" "[FORMATTED] $file"
fi
# ShellCheck analysis
if ! shellcheck --exclude=SC1091,SC2001 "$file"; then
log "$RED" "[SHELLCHECK FAIL] $file"
status=1
else
log "$GREEN" "[SHELLCHECK PASS] $file"
fi
fi
}

# Check for the necessary tools before starting
if ! command -v beautysh > /dev/null; then
echo "beautysh is not installed. Please install it to format shell scripts."
exit 1
fi

if ! command -v shellcheck > /dev/null; then
echo "shellcheck is not installed. Please install it to perform shell script analysis."
exit 1
fi

main() {
local checkonly=0

if [[ $1 == "--check" ]]; then
checkonly=1
shift
fi

if [ $# -eq 0 ]; then
echo "Error: No arguments provided."
echo "Usage: beautify_script.sh [--check] path"
echo " --check - When specified, script will only check if formatting is needed without actually formatting"
echo " path - The path can be a directory or a single file."
echo "Example: ./beautify_script.sh ./my_directory"
exit 1
fi

process_path() {
local path="$1"

local checkonly="$2"
if [ -d "$path" ]; then
while IFS= read -r -d '' file
do
format "$file" "$checkonly"
done < <(find "$path" -name '*.sh' -print0)
find "$path" -type f -name '*.sh' -print0 | while IFS= read -r -d '' file; do
format_file "$file" "$checkonly"
done
elif [ -f "$path" ]; then
format "$path" "$checkonly"
format_file "$path" "$checkonly"
else
echo "Error: '$path' is not a valid path!"
log "$RED" "[ERROR] '$path' is not a valid file or directory."
exit 1
fi
}

main() {
check_tools
local checkonly=0
if [[ $# -eq 0 ]]; then
log "$RED" "[ERROR] No path provided."
echo "Usage: beautify_script.sh [--check] <path>"
exit 1
fi
if [[ $1 == "--check" ]]; then
checkonly=1
shift
fi
process_path "$1" "$checkonly"
if [[ $status -eq 1 ]]; then
log "$RED" "\nSome files require formatting or have ShellCheck issues."
exit 1
else
log "$GREEN" "\nAll files are properly formatted and pass ShellCheck."
fi
}

Expand Down
7 changes: 6 additions & 1 deletion src/remove_trailing_whitespaces.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ remove_trailing_whitespaces() {

echo "Checking each line of ${file} for trailing whitespaces..."

touch "${file}".tmp
if [[ $checkonly -eq 0 ]]; then
touch "${file}".tmp
fi

while IFS= read -r line; do
if [[ $line == *[[:space:]] ]]; then
Expand All @@ -37,6 +39,9 @@ remove_trailing_whitespaces() {
if [[ $checkonly -eq 0 ]]; then
mv "${file}".tmp "${file}"
echo "Done!"
else
# Clean up any .tmp file that may have been created accidentally
[[ -f "${file}".tmp ]] && rm -f "${file}".tmp
fi
}

Expand Down
Loading