Skip to content

Commit

Permalink
Merge pull request #589 from IntersectMBO/improvements-to-prettify
Browse files Browse the repository at this point in the history
Improvements to prettify
  • Loading branch information
palas authored Jul 18, 2024
2 parents 13f4749 + b8e4f12 commit 0fd29b0
Showing 1 changed file with 39 additions and 20 deletions.
59 changes: 39 additions & 20 deletions scripts/devshell/prettify
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,33 @@ show_help() {
echo "Format Haskell source files using fourmolu and stylish-haskell."
echo ""
echo "Options:"
echo " -t, --tracked Format all tracked Haskell (*.hs) files in the repository"
echo " -s, --staged Format all staged Haskell (*.hs) files"
echo " -m, --modified Format all modified Haskell (*.hs) files, including staged and unstaged"
echo " -n, --not-staged Format all non-staged modified Haskell (*.hs) files"
echo " -h, --help Show this help message"
echo " -t, --tracked Format all tracked Haskell (*.hs) files in the repository"
echo " -s, --staged Format all staged Haskell (*.hs) files"
echo " -m, --modified Format all modified Haskell (*.hs) files, including staged and unstaged"
echo " -n, --not-staged Format all non-staged modified Haskell (*.hs) files"
echo " -p, --previous-commit Format all Haskell (*.hs) files modified before the last commit (HEAD~1)"
echo " -h, --help Show this help message"
}

# Function to run the formatting commands
run_format() {
top_level=$(git rev-parse --show-toplevel)
for file in "$@"; do
if [[ $file == *.hs ]]; then
if grep -qE '^#' "$file"; then
if grep -qE '^#' "$top_level/$file"; then
echo "$file contains CPP. Skipping."
else
echo "Formatting: $file"
fourmolu -q -i "$file"
fourmolu -q -i "$file"
stylish-haskell -i "$file"
fourmolu -q -i "$top_level/$file"
fourmolu -q -i "$top_level/$file"
stylish-haskell -i "$top_level/$file"
fi

fi
done
}

flag_passed="true"

# Parse command line arguments
case $1 in
-t|--tracked)
Expand All @@ -44,31 +47,47 @@ case $1 in
-n|--not-staged)
files=$(git diff --name-only --diff-filter=ACM '*.hs')
;;
-p|--previous-commit)
files=$(git diff --name-only --diff-filter=ACM HEAD~1 '*.hs')
;;
-h|--help)
show_help
exit 0
;;
*)
files="$@"
flag_passed="false"
;;
esac

if !(which stylish-haskell > /dev/null 2>&1); then
echo "ERROR: stylish-haskell is not available!"
echo -e
echo "Try entering the development shell with:"
echo " nix develop"
exit 1
if [[ $flag_passed == "true" ]] && [[ $# -gt 1 ]]; then
echo "ERROR: only one flag is allowed!"
echo -e
show_help
exit 1
fi

if !(which fourmolu > /dev/null 2>&1); then
echo "ERROR: fourmolu is not available!"
for file in $files; do
if [[ ! -a $file ]]; then
echo "ERROR: $file does not exist"
exit 1
if ![[ -f $file ]]; then
echo "ERROR: $file is not a regular file"
exit
fi
fi
done

for tool in stylish-haskell fourmolu
do
if !(which $tool > /dev/null 2>&1); then
echo "ERROR: $tool is not available!"
echo -e
echo "Try entering the development shell with:"
echo " nix develop"
exit 1
fi

fi
done

if [[ -z $files ]]; then
echo "No files to format!"
Expand Down

0 comments on commit 0fd29b0

Please sign in to comment.