-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathudeps_workspace.sh
More file actions
executable file
·96 lines (81 loc) · 2.69 KB
/
udeps_workspace.sh
File metadata and controls
executable file
·96 lines (81 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash
# Checks for dependencies in the cargo workspace that are
# not listed in any crate's Cargo.toml
WORKSPACE_TOML="Cargo.toml"
TMP_FILE=".unused_deps_found"
# Check Cargo.toml exists
if [[ ! -f "$WORKSPACE_TOML" ]]; then
echo "❌ Error: $WORKSPACE_TOML not found in the current directory: $(pwd)"
echo "👉 Please run this script from the root of your Cargo workspace."
exit 1
fi
# Check if [workspace.dependencies] section exists
if ! grep -q '^\[workspace.dependencies\]' "$WORKSPACE_TOML"; then
echo "ℹ️ No [workspace.dependencies] section found in $WORKSPACE_TOML."
echo "✅ Nothing to check. Exiting."
exit 0
fi
echo "🔍 Scanning [workspace.dependencies] in $WORKSPACE_TOML..."
# Extract lines after the section until the next section, clean comments and whitespace
deps=()
in_section=false
while IFS= read -r line; do
if [[ "$line" =~ ^\[.*\] ]]; then
if $in_section; then break; fi
if [[ "$line" == "[workspace.dependencies]" ]]; then in_section=true; fi
continue
fi
if $in_section; then
# Strip comments
clean_line=$(echo "$line" | sed 's/#.*//' | xargs)
# Skip empty lines
[[ -z "$clean_line" ]] && continue
# Extract the key before '='
dep_name=$(echo "$clean_line" | cut -d '=' -f 1 | xargs)
deps+=("$dep_name")
fi
done < "$WORKSPACE_TOML"
# Check if any deps were found
if [[ ${#deps[@]} -eq 0 ]]; then
echo "⚠️ [workspace.dependencies] section is present but contains no dependencies."
exit 0
fi
echo "📦 Found ${#deps[@]} declared workspace dependencies:"
printf ' - %s\n' "${deps[@]}"
echo
# Search for each dep
for dep in "${deps[@]}"; do
echo "🔎 Checking usage of '$dep'..."
found=false
# Find all Cargo.toml files EXCEPT the root workspace one
cargo_tomls=$(find . -name "Cargo.toml" ! -path "./$WORKSPACE_TOML")
# Search each for the dependency name
for toml in $cargo_tomls; do
if grep -q -w "$dep" "$toml"; then
found=true
break
fi
done
# Search in all Cargo.toml files (excluding the workspace one)
if grep -rq "$dep" . --include "Cargo.toml" --exclude "$WORKSPACE_TOML"; then
found=true
fi
if ! $found; then
echo "❌ Unused: $dep"
echo "$dep" >> "$TMP_FILE"
else
echo "✅ Used"
fi
done
# Summary
if [[ -s "$TMP_FILE" ]]; then
echo ""
echo "💡 The following workspace dependencies appear unused:"
cat "$TMP_FILE"
echo ""
echo "✂️ You can now manually remove them from [workspace.dependencies] in $WORKSPACE_TOML."
rm "$TMP_FILE"
else
echo ""
echo "✅ No unused workspace dependencies found!"
fi