Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to prettify #589

Merged
merged 2 commits into from
Jul 18, 2024
Merged
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
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
Loading