Last active
September 27, 2024 11:51
-
-
Save apoorv-2204/c3b57882ee6cbf869928721e36657a7b to your computer and use it in GitHub Desktop.
Bash Scrip to sort elixir module aliases Alphabetically , when adopting credo rules
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
#!/bin/bash | |
# This script will recursively find all .ex and .exs files | |
# and sort the alias groups within each file alphabetically. | |
find . -type f \( -name "*.ex" -o -name "*.exs" \) | while read -r file; do | |
# Read the file into an array | |
mapfile -t lines < "$file" | |
in_alias_group=false | |
alias_group_start=0 | |
alias_group_lines=() | |
i=0 | |
while [ $i -lt ${#lines[@]} ]; do | |
line="${lines[$i]}" | |
trimmed_line="$(echo "$line" | sed 's/^[[:space:]]*//')" | |
if [ -z "$trimmed_line" ]; then | |
if [ "$in_alias_group" = true ]; then | |
# End of alias group | |
in_alias_group=false | |
# Process the alias_group_lines | |
# Separate alias lines and other lines (comments) | |
alias_lines=() | |
other_lines=() | |
for l in "${alias_group_lines[@]}"; do | |
t="$(echo "$l" | sed 's/^[[:space:]]*//')" | |
if [[ "$t" =~ ^alias ]]; then | |
alias_lines+=("$l") | |
else | |
other_lines+=("$l") | |
fi | |
done | |
# Sort alias_lines while preserving indentation | |
IFS=$'\n' sorted_alias_lines=($(printf "%s\n" "${alias_lines[@]}" | sort -k2)) | |
# Reconstruct the alias group | |
new_alias_group=("${sorted_alias_lines[@]}" "${other_lines[@]}") | |
# Replace lines in the original array | |
alias_group_end=$((i - 1)) | |
lines=("${lines[@]:0:$alias_group_start}" "${new_alias_group[@]}" "${lines[@]:$((alias_group_end + 1))}") | |
# Adjust i | |
i=$((alias_group_start + ${#new_alias_group[@]})) | |
alias_group_lines=() | |
fi | |
i=$((i + 1)) | |
elif [[ "$trimmed_line" =~ ^alias ]]; then | |
if [ "$in_alias_group" = false ]; then | |
in_alias_group=true | |
alias_group_start=$i | |
alias_group_lines=() | |
fi | |
alias_group_lines+=("${lines[$i]}") | |
i=$((i + 1)) | |
else | |
if [ "$in_alias_group" = true ]; then | |
# End of alias group | |
in_alias_group=false | |
# Process the alias_group_lines | |
alias_lines=() | |
other_lines=() | |
for l in "${alias_group_lines[@]}"; do | |
t="$(echo "$l" | sed 's/^[[:space:]]*//')" | |
if [[ "$t" =~ ^alias ]]; then | |
alias_lines+=("$l") | |
else | |
other_lines+=("$l") | |
fi | |
done | |
IFS=$'\n' sorted_alias_lines=($(printf "%s\n" "${alias_lines[@]}" | sort -k2)) | |
new_alias_group=("${sorted_alias_lines[@]}" "${other_lines[@]}") | |
alias_group_end=$((i - 1)) | |
lines=("${lines[@]:0:$alias_group_start}" "${new_alias_group[@]}" "${lines[@]:$((alias_group_end + 1))}") | |
i=$((alias_group_start + ${#new_alias_group[@]})) | |
alias_group_lines=() | |
else | |
i=$((i + 1)) | |
fi | |
fi | |
done | |
# Handle the case where the file ends with an alias group | |
if [ "$in_alias_group" = true ]; then | |
in_alias_group=false | |
alias_lines=() | |
other_lines=() | |
for l in "${alias_group_lines[@]}"; do | |
t="$(echo "$l" | sed 's/^[[:space:]]*//')" | |
if [[ "$t" =~ ^alias ]]; then | |
alias_lines+=("$l") | |
else | |
other_lines+=("$l") | |
fi | |
done | |
IFS=$'\n' sorted_alias_lines=($(printf "%s\n" "${alias_lines[@]}" | sort -k2)) | |
new_alias_group=("${sorted_alias_lines[@]}" "${other_lines[@]}") | |
alias_group_end=$((i - 1)) | |
lines=("${lines[@]:0:$alias_group_start}" "${new_alias_group[@]}" "${lines[@]:$((alias_group_end + 1))}") | |
fi | |
# Write the updated lines back to the file | |
printf "%s\n" "${lines[@]}" > "$file" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment