Skip to content

Commit 6cf3d00

Browse files
authored
Merge pull request #50 from bazelruby/kig/buildifer-git-hook
Adding auto-fix buildifier and auto-fix rubocop
2 parents e8d462a + 5fd72dd commit 6cf3d00

7 files changed

Lines changed: 187 additions & 43 deletions

File tree

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
)

README.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,16 +436,51 @@ To get the initial stuff setup required by this repo, please run the script:
436436
bin/setup
437437
```
438438

439-
Whenever you'll commit something, a pre-commit hook will run as well.
439+
This script does a couple of very useful things, including setting up a git commit
440+
hook that performs rubocop fixes and buildifier fixes of the Bazel build files.
441+
442+
You can run partial setup comamnds like so:
443+
444+
```bash
445+
❯ bin/setup -h
446+
447+
USAGE:
448+
bin/setup [ gems | git-hook | help | install | main | no-git-hook ]
449+
450+
DESCRIPTION:
451+
Runs full setup without any arguments.
452+
453+
Accepts one argument — one of the actions that typically run
454+
as part of setup.
455+
456+
For instance, to perform full setup:
457+
458+
bin/setup
459+
460+
Or, to run only one of the sub-functions (actions), pass
461+
it as an argument:
462+
463+
bin/setup help
464+
bin/setup no-git-hook
465+
466+
etc.
467+
```
468+
469+
Whenever you'll commit something, a pre-commit hook will run as well. If it
470+
finds anything that needs fixing, it will attempt to fix it. If the resulting
471+
git state is different than before the commit, the commit is aborted and the user
472+
should add any auto-fixed modifications to the list of staged files for commit.
440473

441474
### Running Tests
442475

443-
We have a handy script you can use to run all tests:
476+
We have a pretty useful script you can use to run all tests in the repo that run on CI:
444477

445478
```bash
446479
bin/ci
447480
```
448481

482+
On a MacBook Pro it takes about 3 minutes to run.
483+
449484
## Copyright
450485

451486
© 2018-2019 Yuki Yugui Sonoda & BazelRuby Authors

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: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,68 @@
11
#!/usr/bin/env bash
2-
2+
#
3+
#
4+
#
35
set -e
46
# shellcheck disable=SC1091
57
source "bin/deps"
68

9+
__setup::actions() {
10+
local sep="${1:-', '}"
11+
printf "${bldylw}$(lib::array::join "${sep}" $(lib::util::functions-matching setup::))"
12+
}
13+
14+
setup::help() {
15+
printf "
16+
${bldpur}USAGE:${clr}
17+
bin/setup [ $(__setup::actions " | ")${clr} ]
18+
19+
${bldpur}DESCRIPTION:${clr}
20+
Runs full setup without any arguments.
21+
22+
Accepts one argument — one of the actions that typically run
23+
as part of setup.
24+
25+
For instance, to perform full setup:
26+
27+
${bldylw}bin/setup${clr}
28+
29+
Or, to run only one of the sub-functions (actions), pass
30+
it as an argument:
31+
32+
${bldylw}bin/setup help${clr}
33+
${bldylw}bin/setup no-git-hook${clr}
34+
35+
etc.
36+
"
37+
}
38+
39+
40+
741
setup::gems() {
842
for gem in rubocop relaxed-rubocop rubocop-performance; do
943
lib::gem::install ${gem}
1044
done
1145
}
1246

13-
setup::git() {
14-
[[ -L .git/hooks/pre-commit ]] || run "ln -nfs bin/pre-commit .git/hooks/pre-commit"
47+
setup::no-git-hook() {
48+
set -e
49+
[[ -L .git/hooks/pre-commit ]] && {
50+
info 'Removing git commit hook...'
51+
run "rm -f .git/hooks/pre-commit"
52+
echo
53+
}
54+
set +e
55+
}
56+
57+
setup::git-hook() {
58+
set -e
59+
if [[ ! -L .git/hooks/pre-commit ]]; then
60+
info 'Installing git pre-commit hook'
61+
run "cd .git/hooks && ln -nfs ../../bin/pre-commit pre-commit && cd -"
62+
else
63+
info: "git pre-commit hook is already installed."
64+
fi
65+
set +e
1566
}
1667

1768
setup::install() {
@@ -32,27 +83,35 @@ setup::install() {
3283
/usr/bin/env bash bin/show-env || true
3384
fi
3485
else
35-
error "Operating system $(uname -s) is not currently supported." >&2
86+
error "Operating system ${os} is not currently supported." >&2
3687
fi
3788
echo
3889
return 0
3990
}
4091

4192
setup::main() {
4293
local action="$1"
94+
[[ "${action}" == "-h" || ${action} == "--help" ]] && action="help"
4395
local func="setup::${action}"
4496

45-
if lib::util::is-a-function "${func}"; then
46-
h2 "Executing partial setup for action ${bldylw}${action}"
47-
shift
48-
${func} "$@"
97+
if [[ -n ${action} ]] ; then
98+
if lib::util::is-a-function "${func}"; then
99+
[[ ${action} != "help" ]] && h2 "Executing partial setup for action ${bldylw}${action}"
100+
shift
101+
${func} "$@"
102+
else
103+
h1 "Invalid action provided." "Valid actions are: $(__setup::actions)"
104+
exit 1
105+
fi
49106
else
50107
set +e
51108
h2 "Installing required development dependencies for working with rules_ruby and Bazel."
52109
setup::gems
53-
[[ -z ${CI} ]] && setup::git
110+
[[ -z ${CI} ]] && setup::git-hook
54111
setup::install "$@"
55112
fi
56113
}
57114

58115
setup::main "$@"
116+
117+

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)