datahaven/operator/scripts/sort-cargo-deps.sh
Gonza Montiel e16420f266
feat(ci): add script to sort cargo deps (#62)
This PR introduces a new script in `./operator/scripts` that allows us
to sort cargo dependencies alphabetically, it is based in my
[gist](https://gist.github.com/gonzamontiel/b4594c62685175f99760442ad2e2dd98).

I modified it so we can run it both in `--fix` and `--check` modes.

To sort a single cargo file, you can do:

```
./scripts/sort-cargo-deps.sh  /path/to/Cargo.toml
```

Btw, make sure you are in the operator folder and you have exec
permissions:

```
cd operator
chmod +x ./scripts/sort-cargo-deps.sh
```
But what I recommend is that you format every Cargo.toml just in case,
like this:
```
find . -name "Cargo.toml" -exec ./scripts/sort-cargo-deps.sh {} \;
```

The CI will run 

```
find . -name "Cargo.toml" -exec ./scripts/sort-cargo-deps.sh {} check \;
```

---------

Co-authored-by: Facundo Farall <37149322+ffarall@users.noreply.github.com>
2025-05-06 17:22:01 +00:00

95 lines
No EOL
2.7 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
# Use first argument as file path, default to Cargo.toml
FILE="${1:-Cargo.toml}"
MODE="${2:-fix}" # "fix" (default) or "check"
# Check if file exists
if [[ ! -f "$FILE" ]]; then
echo "Error: File '$FILE' not found"
exit 1
fi
TMP_FILE="$(mktemp)"
SECTION_REGEX='^\[.*dependencies.*\]$'
process_file() {
local in_section=0
local section_content=""
local current_group=""
local current_comment=""
while IFS= read -r line || [[ -n "$line" ]]; do
# Handle section headers
if [[ "$line" =~ $SECTION_REGEX ]]; then
if [[ -n "$section_content" ]]; then
echo -n "$section_content" | LC_ALL=C sort -f
fi
in_section=1
section_content=""
current_group=""
echo "$line"
continue
fi
# Inside a dependencies section
if [[ $in_section -eq 1 ]]; then
# New section starts
if [[ "$line" =~ ^\[[^]]+\] ]]; then
if [[ -n "$section_content" ]]; then
echo -n "$section_content" | LC_ALL=C sort -f
fi
in_section=0
section_content=""
current_group=""
echo "$line"
# Empty line - flush current group
elif [[ -z "$line" ]]; then
if [[ -n "$section_content" ]]; then
echo -n "$section_content" | LC_ALL=C sort -f
echo
fi
section_content=""
current_group=""
# Comment line - start new group
elif [[ "$line" =~ ^[[:space:]]*# ]]; then
if [[ -n "$section_content" && "$current_group" != "$line" ]]; then
echo -n "$section_content" | LC_ALL=C sort -f
section_content=""
fi
current_group="$line"
section_content="$line"$'\n'
# Dependency line
else
if [[ -z "$current_group" ]]; then
current_group="default"
fi
section_content+="$line"$'\n'
fi
else
echo "$line"
fi
done < "$FILE"
if [[ -n "$section_content" ]]; then
echo -n "$section_content" | LC_ALL=C sort -f
fi
}
process_file > "$TMP_FILE"
if [[ "$MODE" == "check" ]]; then
if ! diff -q "$TMP_FILE" "$FILE" > /dev/null; then
echo "Error: $FILE is not sorted. Please run the script to sort it."
diff "$FILE" "$TMP_FILE"
rm "$TMP_FILE"
exit 1
else
echo "Check passed: $FILE is properly sorted."
rm "$TMP_FILE"
fi
else
mv "$TMP_FILE" "$FILE"
fi