110 lines
4.3 KiB
Bash
Executable File

#!/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 <https://www.gnu.org/licenses/>.
basename=$(basename "$0")
basepath=$(dirname "$0")
# 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,
# it should be the version of the old ruleset that you want to compare with the new ruleset.
if [ $# -eq 0 ]; then
echo "Please provide the version of the old ruleset that you want to compare with the new ruleset."
exit 1
fi
# Read the version of the old ruleset from the arguments.
old_wyatt_analysis_version=$1
# Read the version of the new ruleset from pubspec.yaml.
new_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.$old_wyatt_analysis_version.yaml" ]; then
echo "Wyatt Analysis version $old_wyatt_analysis_version could not be found."
exit 1
fi
if [ ! -f "$basepath/../lib/analysis_options.$new_wyatt_analysis_version.yaml" ]; then
echo "Wyatt Analysis version $new_wyatt_analysis_version could not be found."
exit 1
fi
echo "Found Wyatt Analysis version $old_wyatt_analysis_version and $new_wyatt_analysis_version"
# Generate temporary directory.
tmp_dir=$(mktemp -d)
# Merges the wyatt dart analysis ruleset with the wyatt flutter analysis ruleset.
yq ea '. as $item ireduce ({}; . *+ $item)' "$basepath/../lib/analysis_options.$old_wyatt_analysis_version.yaml" "$basepath/../lib/analysis_options.flutter.$old_wyatt_analysis_version.yaml" >"$tmp_dir/old_wyatt.yaml"
yq ea '. as $item ireduce ({}; . *+ $item)' "$basepath/../lib/analysis_options.$new_wyatt_analysis_version.yaml" "$basepath/../lib/analysis_options.flutter.$new_wyatt_analysis_version.yaml" >"$tmp_dir/new_wyatt.yaml"
# List yaml elements in the old ruleset, "linter.rules" and save it in an array.
old_wyatt_rules=($(yq eval '.linter.rules | .[]' "$tmp_dir/old_wyatt.yaml"))
# List yaml elements in the new ruleset, "linter.rules" and save it in an array.
new_wyatt_rules=($(yq eval '.linter.rules | .[]' "$tmp_dir/new_wyatt.yaml"))
# List all the rules that are in the old ruleset but not in the new ruleset.
removed_rules=($(comm -23 <(printf '%s\n' "${old_wyatt_rules[@]}" | sort) <(printf '%s\n' "${new_wyatt_rules[@]}" | sort)))
# List all the rules that are in the new ruleset but not in the old ruleset.
added_rules=($(comm -13 <(printf '%s\n' "${old_wyatt_rules[@]}" | sort) <(printf '%s\n' "${new_wyatt_rules[@]}" | sort)))
# Generate markdown file.
echo "## ${new_wyatt_analysis_version}" >"$tmp_dir/CHANGELOG.md"
echo "" >>"$tmp_dir/CHANGELOG.md"
# Iterate over the added rules and print them in the markdown file.
for rule in "${added_rules[@]}"; do
echo " - **ADDED** $rule (https://dart.dev/tools/linter-rules/$rule)" >>"$tmp_dir/CHANGELOG.md"
done
# Iterate over the removed rules and print them in the markdown file.
for rule in "${removed_rules[@]}"; do
echo " - **REMOVED** $rule (https://dart.dev/tools/linter-rules/$rule)" >>"$tmp_dir/CHANGELOG.md"
done
echo "" >>"$tmp_dir/CHANGELOG.md"
# Print the contents of the markdown file.
cat "$tmp_dir/CHANGELOG.md"
# Ask the user if they want to add the changes to the CHANGELOG.md file.
read -p "Do you want to add the changes to the CHANGELOG.md file? (y/n) " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
# Add the changes to the top of the CHANGELOG.md file.
cat "$tmp_dir/CHANGELOG.md" "$basepath/../CHANGELOG.md" >"$tmp_dir/CHANGELOG.md.tmp"
cp -f "$tmp_dir/CHANGELOG.md.tmp" "$basepath/../CHANGELOG.md"
rm -f "$tmp_dir/CHANGELOG.md.tmp"
fi
# Remove temporary directory.
rm -rf "$tmp_dir"
exit 0