Skip to content

Instantly share code, notes, and snippets.

@alexheretic
Last active February 1, 2024 12:31
Show Gist options
  • Save alexheretic/fc74e164ac7ae887519594f77abfe4a0 to your computer and use it in GitHub Desktop.
Save alexheretic/fc74e164ac7ae887519594f77abfe4a0 to your computer and use it in GitHub Desktop.
Brute force check for ununsed Cargo.toml dependencies
#!/usr/bin/env bash
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
set -euo pipefail
yellow='\e[93m'
reset='\e[0m'
print_help() {
echo "find-cargo-unused-dependencies: Brute force check for ununsed Cargo.toml dependencies"
echo
echo "By default recursively checks all child Cargo.toml manifests"
echo
echo "Usage:"
echo " find-cargo-unused-dependencies [path/to/specific/Cargo.toml]"
}
case "${1:-}" in -h|--help|help) print_help; exit 1;; esac
manifest_path="${1:-}"
if [ -z "$manifest_path" ]; then
# recursively check all manifests in child dirs
find . -type f -name 'Cargo.toml' -exec sh -c "
echo \"==> checking \$1\" >&2
${BASH_SOURCE[0]} \$1
" sh {} \;
exit 0
fi
if [ ! -f "$manifest_path" ]; then
echo "$manifest_path not found" >&2
exit 1
fi
if [[ $manifest_path != *"Cargo.toml" ]]; then
echo "$manifest_path not a cargo manifest" >&2
exit 2
fi
# args to run `cargo check` with
check_args=(--all-targets --all-features)
if grep -q '\[workspace\]' "$manifest_path"; then
check_args+=(--workspace)
fi
cd "$(dirname "$manifest_path")"
echo "Running initial cargo check..." >&2
if ! cargo check "${check_args[@]}"; then
echo "cargo check must work initially to allow dependency checking" >&2
exit 3
fi
# Comment out $1 and see if the project still compiles
try_without_manifest_line() {
if [[ "$1" != *'='* ]]; then return; fi
echo -n "- trying without $(echo "$1" | cut -d'=' -f1) ..." >&2
local sed_escaped
sed_escaped="$(echo "$1" | sed -e 's/[]\/$*.^[]/\\&/g')"
sed -e "/^$sed_escaped/ s/^#*/#/" -i "./Cargo.toml"
if cargo check "${check_args[@]}" -q >/dev/null 2>&1; then
printf "%b not required%b\n" "$yellow" "$reset"
else
sed -e "/^#$sed_escaped/ s/^#//" -i "./Cargo.toml"
echo " needed"
fi
}
manifest_section=""
while IFS="" read -r p || [ -n "$p" ]
do
if [ -z "$p" ] || [[ "$p" == '#'* ]]; then continue; fi
if [[ $p == '['*']' ]]; then
manifest_section="$p"
elif [ "$manifest_section" = "[dependencies]" ] \
|| [ "$manifest_section" = "[dev-dependencies]" ] \
|| [ "$manifest_section" = "[build-dependencies]" ]
then
try_without_manifest_line "$p"
fi
done < "./Cargo.toml"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment