feat(analysis): add full toolchain to easely update ruleset

This commit is contained in:
Hugo Pointcheval 2023-10-09 21:44:23 +02:00
parent 14a5abd439
commit f2c33feb73
Signed by: hugo
GPG Key ID: 3AAC487E131E00BC
6 changed files with 371 additions and 143 deletions

View File

@ -1 +0,0 @@
2.5.0

View File

@ -1,142 +0,0 @@
#!/usr/bin/env sh
# Copyright (C) 2022 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/>.
usage="
usage:
$(basename "$0") <new_version> -- create new analyzer files.
where:
new_version: new version, for example \`2.4.0\`"
new=$1
if [ -z "$new" ]; then
echo "new_version cannot be null"
echo "${usage}"
exit 1
fi
SCRIPT_PATH="${BASH_SOURCE:-$0}"
ABS_SCRIPT_PATH="$(realpath "${SCRIPT_PATH}")"
ABS_DIRECTORY="$(dirname "${ABS_SCRIPT_PATH}")"
LATEST_VERSION="${ABS_DIRECTORY}/.latest_version"
# Read latest version
if [ -e "$LATEST_VERSION" ]; then
old=$(cat "$LATEST_VERSION")
echo "> latest package version is: $old"
echo "> new package version is: $new"
else
echo "latest version is not set"
echo "create a file named \`.latest_version\` in the root of the project"
exit 1
fi
LIB="${ABS_DIRECTORY}/lib/wyatt_analysis.dart"
OPTIONS="${ABS_DIRECTORY}/lib/analysis_options.yaml"
OLD_OPTIONS="${ABS_DIRECTORY}/lib/analysis_options.${old}.yaml"
NEW_OPTIONS="${ABS_DIRECTORY}/lib/analysis_options.${new}.yaml"
OPTIONS_FLUTTER="${ABS_DIRECTORY}/lib/analysis_options.flutter.yaml"
OLD_OPTIONS_FLUTTER="${ABS_DIRECTORY}/lib/analysis_options.flutter.${old}.yaml"
NEW_OPTIONS_FLUTTER="${ABS_DIRECTORY}/lib/analysis_options.flutter.${new}.yaml"
if [ ! -e "$OLD_OPTIONS" ]; then
echo "analysis_options.${old}.yaml doesn't exists"
echo "${usage}"
exit 1
fi
if [ ! -e "$OLD_OPTIONS_FLUTTER" ]; then
echo "analysis_options.flutter.${old}.yaml doesn't exists"
echo "${usage}"
exit 1
fi
if [ -e "$NEW_OPTIONS" ]; then
echo "analysis_options.${new}.yaml already exists"
echo "${usage}"
exit 1
fi
if [ -e "$NEW_OPTIONS_FLUTTER" ]; then
echo "analysis_options.flutter.${old}.yaml already exists"
echo "${usage}"
exit 1
fi
# Copy previous version files
cp "${OLD_OPTIONS}" "${NEW_OPTIONS}"
cp "${OLD_OPTIONS_FLUTTER}" "${NEW_OPTIONS_FLUTTER}"
# Search and replace old version string occurences in new files
sed -e "s/${old}/${new}/g" "${NEW_OPTIONS_FLUTTER}" > tempfile.tmp
mv -f tempfile.tmp "${NEW_OPTIONS_FLUTTER}"
sed -e "s/${old}/${new}/g" "${LIB}" > tempfile.tmp
mv -f tempfile.tmp "${LIB}"
sed -e "s/${old}/${new}/g" "README.md" > tempfile.tmp
mv -f tempfile.tmp "README.md"
echo "
# 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/>.
include: package:wyatt_analysis/analysis_options.${new}.yaml
" > "${OPTIONS}"
echo "
# 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/>.
include: package:wyatt_analysis/analysis_options.flutter.${new}.yaml
" > "${OPTIONS_FLUTTER}"
# Update latest version
echo "${new}" > "$LATEST_VERSION"
exit 0

View File

@ -0,0 +1,105 @@
#!/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/>.
# 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

View File

@ -0,0 +1,109 @@
#!/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

View File

@ -0,0 +1,98 @@
#!/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 arguments, first argument should be wyatt_analysis_version.
if [ $# -eq 0 ]; then
echo "Usage: $basename <wyatt_analysis_version>"
exit 1
fi
wyatt_analysis_version=$1
# 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
# Read latest version
latest_version=$(yq eval '.version' "${basepath}/../pubspec.yaml")
echo "> latest package version is: $latest_version"
echo "> new package version is: $wyatt_analysis_version"
# Create array with all files that need to exist
files=(
"${basepath}/../pubspec.yaml"
"${basepath}/../README.md"
"${basepath}/../lib/wyatt_analysis.dart"
"${basepath}/../lib/analysis_options.yaml"
"${basepath}/../lib/analysis_options.flutter.yaml"
"${basepath}/../lib/analysis_options.${latest_version}.yaml"
"${basepath}/../lib/analysis_options.flutter.${latest_version}.yaml"
)
# Check if all files exist
for file in "${files[@]}"; do
if [ ! -f "$file" ]; then
echo "File ${file} doesn't exists"
exit 1
fi
done
# Create new analysis_options files
echo "Creating new analysis_options files"
cp "${basepath}/../lib/analysis_options.${latest_version}.yaml" "${basepath}/../lib/analysis_options.${wyatt_analysis_version}.yaml"
cp "${basepath}/../lib/analysis_options.flutter.${latest_version}.yaml" "${basepath}/../lib/analysis_options.flutter.${wyatt_analysis_version}.yaml"
# Update version in new analysis_options files
echo "Updating version in new analysis_options files"
sed -i.bu "s/${latest_version}/${wyatt_analysis_version}/g" "${basepath}/../lib/analysis_options.${wyatt_analysis_version}.yaml"
rm "${basepath}/../lib/analysis_options.${wyatt_analysis_version}.yaml.bu"
sed -i.bu "s/${latest_version}/${wyatt_analysis_version}/g" "${basepath}/../lib/analysis_options.flutter.${wyatt_analysis_version}.yaml"
rm "${basepath}/../lib/analysis_options.flutter.${wyatt_analysis_version}.yaml.bu"
# Update version in pubspec.yaml
echo "Updating version in pubspec.yaml"
sed -i.bu "s/${latest_version}/${wyatt_analysis_version}/g" "${basepath}/../pubspec.yaml"
rm "${basepath}/../pubspec.yaml.bu"
# Update version in README.md
echo "Updating version in README.md"
sed -i.bu "s/${latest_version}/${wyatt_analysis_version}/g" "${basepath}/../README.md"
rm "${basepath}/../README.md.bu"
# Update version in wyatt_analysis.dart
echo "Updating version in wyatt_analysis.dart"
sed -i.bu "s/${latest_version}/${wyatt_analysis_version}/g" "${basepath}/../lib/wyatt_analysis.dart"
rm "${basepath}/../lib/wyatt_analysis.dart.bu"
# Update version in analysis_options.yaml
echo "Updating version in analysis_options.yaml"
sed -i.bu "s/${latest_version}/${wyatt_analysis_version}/g" "${basepath}/../lib/analysis_options.yaml"
rm "${basepath}/../lib/analysis_options.yaml.bu"
# Update version in analysis_options.flutter.yaml
echo "Updating version in analysis_options.flutter.yaml"
sed -i.bu "s/${latest_version}/${wyatt_analysis_version}/g" "${basepath}/../lib/analysis_options.flutter.yaml"
rm "${basepath}/../lib/analysis_options.flutter.yaml.bu"
exit 0

View File

@ -0,0 +1,59 @@
#!/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, 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
# 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"
echo "Sorting rules..."
# Sort the rules in "lint.rules" alphabetically.
yq eval '.linter.rules |= sort_by(.)' "$basepath/../lib/analysis_options.$wyatt_analysis_version.yaml" >"$basepath/../lib/analysis_options.$wyatt_analysis_version.yaml.tmp"
cp -f "$basepath/../lib/analysis_options.$wyatt_analysis_version.yaml.tmp" "$basepath/../lib/analysis_options.$wyatt_analysis_version.yaml"
rm -f "$basepath/../lib/analysis_options.$wyatt_analysis_version.yaml.tmp"
# Sort the rules in "lint.rules" alphabetically.
yq eval '.linter.rules |= sort_by(.)' "$basepath/../lib/analysis_options.flutter.$wyatt_analysis_version.yaml" >"$basepath/../lib/analysis_options.flutter.$wyatt_analysis_version.yaml.tmp"
cp -f "$basepath/../lib/analysis_options.flutter.$wyatt_analysis_version.yaml.tmp" "$basepath/../lib/analysis_options.flutter.$wyatt_analysis_version.yaml"
rm -f "$basepath/../lib/analysis_options.flutter.$wyatt_analysis_version.yaml.tmp"
exit 0