Skip to content

Instantly share code, notes, and snippets.

@greenboxal
Created July 24, 2020 14:17
Show Gist options
  • Save greenboxal/eb08b99aae28c3ad0f9a8b7ec8510385 to your computer and use it in GitHub Desktop.
Save greenboxal/eb08b99aae28c3ad0f9a8b7ec8510385 to your computer and use it in GitHub Desktop.
#!/bin/bash
ARGS=()
FIX_LOG=""
WORKSPACE_DIR=$(bazel info | grep "workspace: " | cut -d' ' -f2)
function usage() {
echo "Runs ESLint for Bazel projects"
echo
echo "Usage: "
echo "$0 [-f] [bazel targets] -- [bazel options]"
echo
echo -e " -f\t\t\tAttempt to fix errors"
echo
}
# Parse CLI options
while getopts "fh" OPTION; do
case ${OPTION} in
f)
FIX_LOG=$(mktemp)
# Store build event log so we can find the produced patches latter
ARGS=("${ARGS[@]}" "--cache_test_results=no" "--build_event_json_file=${FIX_LOG}")
;;
h)
usage
exit 0
;;
*)
usage
exit 10
;;
esac
done
# Remove parsed args from arglist
shift $((OPTIND - 1))
bazel test \
--build_tests_only \
--test_tag_filters=eslint_test \
--test_output=errors \
"${ARGS[@]}" "$@"
if [[ -f "${FIX_LOG}" ]]; then
JQ_SCRIPT='
map(select(.id | has("testResult")))
| map(.testResult.testActionOutput)
| flatten
| map(select(.name == "test.outputs__outputs.zip"))
| map(.uri )
| join("\n")'
echo
echo "Applying fixes..."
# Apply fixes
jq -r -s "${JQ_SCRIPT}" "${FIX_LOG}" |
sed -e 's/^file:\/\///' |
xargs -n1 unzip -p |
patch -p0 -d "${WORKSPACE_DIR}"
rm "${FIX_LOG}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment