Skip to content

Commit eeaa064

Browse files
author
Konstantin Gredeskoul
committed
Adding auto-fix buildifier and auto-fix rubocop
* to bin/pre-commit file which is symlinked by bin/setup to .git/hooks/pre-commit. If, after running the formatters it's determined that some files have changed (using git status -s), it shows the diff between the two states and aborts the commit so that you have a chance to add fixed files. It does not automatically add anything to git because it would be a very large and often invalid assumption.
1 parent e8d462a commit eeaa064

File tree

6 files changed

+87
-33
lines changed

6 files changed

+87
-33
lines changed

BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")
22

33
buildifier(
44
name = "buildifier",
5-
exclude_patterns = [".git/**"],
5+
exclude_patterns = ["./.git/**/*"],
66
)
77

88
buildifier(
99
name = "buildifier-check",
10-
exclude_patterns = [".git/**"],
10+
exclude_patterns = ["./.git/**/*"],
1111
mode = "check",
1212
)

bin/ci

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@ export BAZEL_BUILD_OPTS="--curses=no --verbose_failures -j 15 --show_progress_ra
1414
export BAZEL_TEST_OPTS="--verbose_failures --test_output=streamed --test_verbose_timeout_warnings "
1515
export RUBY_VERSION=$(cat .ruby-version)
1616

17+
export BashMatic__Expr="
18+
source ${SHELL_INIT}
19+
[[ -f ${HOME}/.bashmatic/init.sh ]] && source ${HOME}/.bashmatic/init.sh
20+
set -e
21+
"
22+
1723
[[ -n ${DEBUG} ]] && set -x
1824

1925
trap exit 1 INT
2026

2127
# Runs Rubocop Tests
2228
test::rubocop() {
2329
/usr/bin/env bash -c "
24-
source ${SHELL_INIT}
25-
[[ -f ${HOME}/.bashmatic/init.sh ]] && source ${HOME}/.bashmatic/init.sh
26-
set -e
30+
${BashMatic__Expr}
2731
h2 'Running target: rubocop'
2832
bundle install --path=${BUNDLE_PATH}
2933
bundle exec rubocop -E -D .rubocop.yml --force-exclusion
@@ -33,22 +37,17 @@ test::rubocop() {
3337
# Runs Buildifier
3438
test::buildifier() {
3539
/usr/bin/env bash -c "
36-
source ${SHELL_INIT}
37-
[[ -f ${HOME}/.bashmatic/init.sh ]] && source ${HOME}/.bashmatic/init.sh
40+
${BashMatic__Expr}
3841
h2 'Running target: buildifier'
39-
set -e
4042
bazel run :buildifier-check
4143
"
4244
}
4345

4446
# Builds and runs workspace inside examples/simple_script
4547
test::simple-script() {
4648
/usr/bin/env bash -c "
47-
source ${SHELL_INIT}
48-
[[ -f ${HOME}/.bashmatic/init.sh ]] && source ${HOME}/.bashmatic/init.sh
49+
${BashMatic__Expr}
4950
h2 'Testing target: simple-script'
50-
set -e
51-
cd examples/simple_script
5251
bazel ${BAZEL_OPTS} info
5352
bazel ${BAZEL_OPTS} build ${BAZEL_BUILD_OPTS} -- //...
5453
bazel ${BAZEL_OPTS} test ${BAZEL_BUILD_OPTS} ${BAZEL_TEST_OPTS} -- //...
@@ -58,10 +57,8 @@ test::simple-script() {
5857

5958
test::workspace() {
6059
/usr/bin/env bash -c "
61-
source ${SHELL_INIT}
62-
[[ -f ${HOME}/.bashmatic/init.sh ]] && source ${HOME}/.bashmatic/init.sh
60+
${BashMatic__Expr}
6361
h2 'Testing target: workspace'
64-
set -e
6562
bazel ${BAZEL_OPTS} build ${BAZEL_BUILD_OPTS} -- //...
6663
bazel ${BAZEL_OPTS} test ${BAZEL_BUILD_OPTS} ${BAZEL_TEST_OPTS} -- //...
6764
"
@@ -70,10 +67,8 @@ test::workspace() {
7067
# Private
7168
test::bazel-info() {
7269
/usr/bin/env bash -c "
73-
source ${SHELL_INIT}
74-
[[ -f ${HOME}/.bashmatic/init.sh ]] && source ${HOME}/.bashmatic/init.sh
70+
${BashMatic__Expr}
7571
h2 'Testing target: bazel-info'
76-
set -e
7772
bazel ${BAZEL_OPTS} version
7873
bazel ${BAZEL_OPTS} info
7974
bazel ${BAZEL_OPTS} fetch --curses=no -- '//ruby/...'

bin/pre-commit

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#
33
# Main dependency shell script that installs BashMatic Library in ~/.bashmatic folder.
44

5-
export COLUMNS=90
5+
export COLUMNS=75
6+
67
set -e
78
# shellcheck disable=SC1091
89
source "bin/deps"
@@ -13,10 +14,68 @@ source "bin/deps"
1314
setup::main gems
1415
}
1516

16-
info "Running Rubocop, please wait..."
17-
run "rubocop -F -D || rubocop -a -D -P || true"
18-
run::set-next show-output-on
19-
run "rubocop -D -P"
17+
run::set-all abort-on-error
18+
19+
fix::rubocop() {
20+
h2::green "Running Rubocop, please wait..."
21+
run "rubocop || rubocop -a"
22+
inf "Rubocop completed."
23+
ok:
24+
}
25+
26+
# runs buildifer from the PATH if it exists, otherwise bazel target
27+
# we want pre-commit to be as fast as possible.
28+
fix::buildifier() {
29+
h2::green "Buildifier"
30+
if [[ -n $(command -v buildifier) ]]; then
31+
info "Running $(command -v buildifier).."
32+
run "find . -name 'BUILD*' -o -name 'WORKSPACE' -o -name '*.bzl' | grep -v '.git' | xargs buildifier -v"
33+
else
34+
info "Running ${bldylw}bazel run :buildifier..."
35+
run "bazel run :buildifier"
36+
fi
37+
}
38+
39+
__fix::cleanup() {
40+
h2::green "Cleaning up..."
41+
for f in "${__git_pre}" "${__git_post}"; do
42+
[[ -f ${f} ]] && run "rm -f ${f}"
43+
done
44+
}
45+
46+
export __git_pre="/tmp/rules-ruby-git-status-pre-commit.$$"
47+
export __git_post="/tmp/rules-ruby-git-status-post-commit.$$"
48+
49+
trap __fix::cleanup EXIT
50+
51+
fix() {
52+
# number of modified files
53+
local changes_before=$(git status -s | md5)
54+
55+
git status -s >"${__git_pre}"
56+
57+
fix::rubocop
58+
fix::buildifier
59+
60+
git status -s >"${__git_post}"
61+
62+
local changes_after=$(git status -s | md5)
63+
if [[ ${changes_before} != "${changes_after}" ]]; then
64+
hl::subtle "Git status -s output changed after pre-commit hook."
65+
info "Changes before pre-commit hook:"
66+
hr
67+
diff "${pre}" "${post}"
68+
hr
69+
echo
70+
info "ACTION: ${bldylw}Please add any respective files to the commit and retry."
71+
exit 1
72+
else
73+
hr
74+
info "No changes detected, commit will proceed."
75+
exit 0
76+
fi
77+
}
2078

21-
success "RuboCop ran successfully."
79+
fix "$@"
2280

81+
exit 0

bin/setup

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ setup::gems() {
1111
}
1212

1313
setup::git() {
14-
[[ -L .git/hooks/pre-commit ]] || run "ln -nfs bin/pre-commit .git/hooks/pre-commit"
14+
inf 'Installing git pre-commit hook'
15+
set -e
16+
[[ -L .git/hooks/pre-commit ]] || run "cd .git/hooks && ln -nfs ../../bin/pre-commit pre-commit"
17+
set +e
18+
ok:
1519
}
1620

1721
setup::install() {

bin/setup-darwin

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ __setup::brew-install-jdk() {
2525
}
2626

2727
__setup::brew-deps() {
28-
if ! brew list | grep -q xz; then
29-
"brew install xz"
30-
else
31-
info: "Found brew package xz."
32-
fi
28+
lib::brew::install::packages 'xz' 'ydiff'
3329
}
3430

3531
__setup::is-bazelisk-installed() {

ruby/private/toolchains/BUILD.runtime.tpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ package(default_visibility = ["//visibility:public"])
99
ruby_toolchain(
1010
name = "toolchain",
1111
interpreter = "//:ruby_bin",
12-
runtime = "//:runtime",
1312
rules_ruby_workspace = "{rules_ruby_workspace}",
13+
runtime = "//:runtime",
1414
# TODO(yugui) Extract platform info from RbConfig
1515
# exec_compatible_with = [],
1616
# target_compatible_with = [],
@@ -25,14 +25,14 @@ sh_binary(
2525
cc_import(
2626
name = "libruby",
2727
hdrs = glob({hdrs}),
28-
static_library = {static_library},
2928
shared_library = {shared_library},
29+
static_library = {static_library},
3030
)
3131

3232
cc_library(
3333
name = "headers",
34-
includes = {includes},
3534
hdrs = glob({hdrs}),
35+
includes = {includes},
3636
)
3737

3838
filegroup(

0 commit comments

Comments
 (0)