Skip to content
This repository was archived by the owner on Mar 23, 2021. It is now read-only.

Commit 6b13fb1

Browse files
Massively improve Makefile performance
Instead of running all the commands all the time, we conditionally define rules based on the dev's machine and the files that need to be committed.
1 parent 2d02c0b commit 6b13fb1

File tree

2 files changed

+57
-57
lines changed

2 files changed

+57
-57
lines changed

.githooks/pre-commit

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,4 @@
11
#!/usr/bin/env bash
22

3-
set -e # grep failure does not trigger exit.
3+
make check_format
44

5-
dry=0 # Set to 1 to enable dry run.
6-
7-
function check_rs() {
8-
if (( dry == 0 )); then
9-
make check_rust_format
10-
else
11-
echo "check_rs ran"
12-
fi
13-
}
14-
15-
function check_ts() {
16-
if (( dry == 0 )); then
17-
make check_ts_format
18-
else
19-
echo "check_ts ran"
20-
fi
21-
}
22-
23-
function check_cargo_toml() {
24-
if (( dry == 0 )); then
25-
make check_toml_format
26-
else
27-
echo "check_cargo_toml ran"
28-
fi
29-
}
30-
31-
git status --untracked-files=no --short | grep -E '.rs$' > /dev/null 2>&1 && check_rs
32-
git status --untracked-files=no --short | grep -E '.ts$|.json$|.yml$' > /dev/null 2>&1 && check_ts
33-
git status --untracked-files=no --short | grep -E '.toml$' > /dev/null 2>&1 && check_cargo_toml
34-
35-
exit 0

Makefile

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,48 @@ RUSTUP = rustup
33
TOOLCHAIN = $(shell cat rust-toolchain)
44
CARGO = $(RUSTUP) run --install $(TOOLCHAIN) cargo --color always
55

6-
NIGHTLY_TOOLCHAIN = "nightly-2019-07-31"
6+
NIGHTLY_TOOLCHAIN = nightly-2019-07-31
77
CARGO_NIGHTLY = $(RUSTUP) run --install $(NIGHTLY_TOOLCHAIN) cargo --color always
88

99
GIT_HOOKS_PATH = ".githooks"
1010
GIT_HOOKS = $(wildcard $(GIT_HOOKS_PATH)/*)
1111

12+
INSTALLED_TOOLCHAINS = $(shell $(RUSTUP) toolchain list)
13+
INSTALLED_COMPONENTS = $(shell $(RUSTUP) component list --installed --toolchain $(TOOLCHAIN))
14+
INSTALLED_NIGHTLY_COMPONENTS = $(shell $(RUSTUP) component list --installed --toolchain $(NIGHTLY_TOOLCHAIN))
15+
AVAILABLE_CARGO_COMMANDS = $(shell $(CARGO) --list)
16+
1217
default: init_git_hooks build format
1318

14-
init_git_hooks: $(GIT_HOOKS)
19+
init_git_hooks:
1520
git config core.hooksPath $(GIT_HOOKS_PATH)
1621

22+
## Dev environment
23+
1724
install_rust:
18-
$(RUSTUP) toolchain list | grep -q $(TOOLCHAIN) || $(RUSTUP) install $(TOOLCHAIN)
25+
ifeq (,$(findstring $(TOOLCHAIN),$(INSTALLED_TOOLCHAINS)))
26+
$(RUSTUP) install $(TOOLCHAIN)
27+
endif
1928

2029
install_rust_nightly:
21-
$(RUSTUP) toolchain list | grep -q $(NIGHTLY_TOOLCHAIN) || $(RUSTUP) install $(NIGHTLY_TOOLCHAIN)
22-
23-
## Dev environment
30+
ifeq (,$(findstring $(NIGHTLY_TOOLCHAIN),$(INSTALLED_TOOLCHAINS)))
31+
$(RUSTUP) install $(NIGHTLY_TOOLCHAIN)
32+
endif
2433

2534
install_clippy: install_rust
26-
$(RUSTUP) component list --installed --toolchain $(TOOLCHAIN) | grep -q clippy || $(RUSTUP) component add clippy --toolchain $(TOOLCHAIN)
35+
ifeq (,$(findstring clippy,$(INSTALLED_COMPONENTS)))
36+
$(RUSTUP) component add clippy --toolchain $(TOOLCHAIN)
37+
endif
2738

2839
install_rustfmt: install_rust_nightly
29-
$(RUSTUP) component list --installed --toolchain $(NIGHTLY_TOOLCHAIN) | grep -q rustfmt || $(RUSTUP) component add rustfmt --toolchain $(NIGHTLY_TOOLCHAIN)
40+
ifeq (,$(findstring rustfmt,$(INSTALLED_NIGHTLY_COMPONENTS)))
41+
$(RUSTUP) component add rustfmt --toolchain $(NIGHTLY_TOOLCHAIN)
42+
endif
3043

3144
install_tomlfmt: install_rust
32-
$(CARGO) --list | grep -q tomlfmt || $(CARGO) install cargo-tomlfmt
33-
34-
yarn_install:
35-
(cd ./api_tests; yarn install)
45+
ifeq (,$(findstring tomlfmt,$(AVAILABLE_CARGO_COMMANDS)))
46+
$(CARGO) install cargo-tomlfmt
47+
endif
3648

3749
## User install
3850

@@ -46,13 +58,6 @@ clean:
4658

4759
all: format build clippy test doc e2e_scripts
4860

49-
format: install_rustfmt install_tomlfmt yarn_install
50-
$(CARGO_NIGHTLY) fmt
51-
$(CARGO) tomlfmt -p Cargo.toml
52-
$(CARGO) tomlfmt -p cnd/Cargo.toml
53-
$(CARGO) tomlfmt -p libp2p-comit/Cargo.toml
54-
(cd ./api_tests; yarn run fix)
55-
5661
ci: check_format doc clippy test build e2e
5762

5863
build:
@@ -77,16 +82,42 @@ test:
7782
doc:
7883
$(CARGO) doc
7984

85+
e2e: build
86+
(cd ./api_tests; yarn install; yarn test)
87+
8088
check_format: check_rust_format check_toml_format check_ts_format
8189

90+
STAGED_FILES = $(shell git diff --staged --name-only)
91+
STAGED_RUST_FILES = $(filter %.rs,$(STAGED_FILES))
92+
STAGED_TOML_FILES = $(filter %.toml,$(STAGED_FILES))
93+
STAGED_TYPESCRIPT_FILES = $(filter %.ts %.json %.yml,$(STAGED_FILES))
94+
95+
format: install_rustfmt install_tomlfmt
96+
ifneq (,$(STAGED_RUST_FILES))
97+
$(CARGO_NIGHTLY) fmt
98+
endif
99+
ifneq (,$(STAGED_TOML_FILES))
100+
$(CARGO) tomlfmt -p Cargo.toml
101+
$(CARGO) tomlfmt -p cnd/Cargo.toml
102+
$(CARGO) tomlfmt -p libp2p-comit/Cargo.toml
103+
endif
104+
ifneq (,$(STAGED_TYPESCRIPT_FILES))
105+
(cd ./api_tests; yarn install; yarn run fix)
106+
endif
107+
82108
check_rust_format: install_rustfmt
109+
ifneq (,$(STAGED_RUST_FILES))
83110
$(CARGO_NIGHTLY) fmt -- --check
111+
endif
84112

85113
check_toml_format: install_tomlfmt
114+
ifneq (,$(STAGED_TOML_FILES))
86115
$(CARGO) tomlfmt -d -p Cargo.toml
87-
88-
check_ts_format: yarn_install
89-
(cd ./api_tests; yarn run check)
90-
91-
e2e: yarn_install
92-
(cd ./api_tests; yarn test)
116+
$(CARGO) tomlfmt -d -p cnd/Cargo.toml
117+
$(CARGO) tomlfmt -d -p libp2p-comit/Cargo.toml
118+
endif
119+
120+
check_ts_format:
121+
ifneq (,$(STAGED_TYPESCRIPT_FILES))
122+
(cd ./api_tests; yarn install; yarn run check)
123+
endif

0 commit comments

Comments
 (0)