#!/usr/bin/env bash # Copyright (C) 2023 WYATT GROUP # Please see the AUTHORS file for details. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # This script scrapes the latest version of the ruleset from the # official Dart repository and tests it against the latest version of # the Wyatt Analysis ruleset. basename=$(basename "$0") basepath=$(dirname "$0") official_ruleset_url="https://raw.githubusercontent.com/dart-lang/sdk/main/pkg/linter/example/all.yaml" red=$(tput setaf 1) blue=$(tput setaf 4) yellow=$(tput setaf 3) reset=$(tput sgr0) # Check for dependencies. if ! command -v yq &>/dev/null; then echo "yq could not be found. Please install it and try again." echo "https://github.com/mikefarah/yq" exit 1 fi # Check for arguments, if no arguments read latest version from pubspec.yaml. if [ $# -eq 0 ]; then wyatt_analysis_version=$(yq eval '.version' "${basepath}/../pubspec.yaml") else wyatt_analysis_version=$1 fi # Read latest version wyatt_analysis_version=$(yq eval '.version' "${basepath}/../pubspec.yaml") # Check if the version of the Wyatt Analysis ruleset is valid by checking if the file exists. if [ ! -f "$basepath/../lib/analysis_options.$wyatt_analysis_version.yaml" ]; then echo "Wyatt Analysis version $wyatt_analysis_version could not be found." exit 1 fi echo "Found Wyatt Analysis version $wyatt_analysis_version" # Generate temporary directory. tmp_dir=$(mktemp -d) # Download the official ruleset. curl -s "$official_ruleset_url" >"$tmp_dir/official.yaml" # List yaml elements in the official ruleset, "linter.rules" and save it in an array. official_rules=($(yq eval '.linter.rules | .[]' "$tmp_dir/official.yaml")) echo "Finding differences between the official ruleset and the Wyatt Analysis ruleset..." echo "This may take a while..." # Merges the wyatt dart analysis ruleset with the wyatt flutter analysis ruleset. yq ea '. as $item ireduce ({}; . *+ $item)' "$basepath/../lib/analysis_options.$wyatt_analysis_version.yaml" "$basepath/../lib/analysis_options.flutter.$wyatt_analysis_version.yaml" >"$tmp_dir/wyatt.yaml" # List yaml elements in the Wyatt Analysis ruleset, "linter.rules" and save it in an array. wyatt_rules=($(yq eval '.linter.rules | .[]' "$tmp_dir/wyatt.yaml")) # Read merged files, and list all ignored rules (starting with a # -). ignored_rules=($(grep -oP '(?<=# - ).*' "$tmp_dir/wyatt.yaml")) echo "Found $(echo "${official_rules[@]}" | wc -w) rules in the official ruleset." echo "Found $(echo "${wyatt_rules[@]}" | wc -w) rules in the Wyatt Analysis ruleset." echo "Found $(echo "${ignored_rules[@]}" | wc -w) ignored rules in the Wyatt Analysis ruleset." # Iterate over the official ruleset. for rule in "${official_rules[@]}"; do # Check if the rule is in the Wyatt Analysis ruleset. if ! grep -q "$rule" "$tmp_dir/wyatt.yaml"; then echo "${red}+ $rule${reset} (https://dart.dev/tools/linter-rules/$rule)" fi done # Iterate over the Wyatt Analysis ruleset and print the rules that are not in the official ruleset. for rule in "${wyatt_rules[@]}"; do # Check if the rule is in the official ruleset. if ! grep -q "$rule" "$tmp_dir/official.yaml"; then echo "${blue}- $rule${reset} (https://dart.dev/tools/linter-rules/$rule)" fi done # Iterate over the ignored rules and print them. for rule in "${ignored_rules[@]}"; do echo "${yellow}~ $rule${reset} (https://dart.dev/tools/linter-rules/$rule)" done # Remove temporary directory. rm -rf "$tmp_dir" exit 0