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

Commit 0a075af

Browse files
Merge #1727
1727: Massively improve Makefile performance r=mergify[bot] a=thomaseizinger 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. Co-authored-by: Thomas Eizinger <[email protected]>
2 parents 2d02c0b + 8b1b7ba commit 0a075af

File tree

2 files changed

+60
-57
lines changed

2 files changed

+60
-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: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,51 @@ 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+
17+
# All our targets go into .PHONY because none of them actually create files
18+
.PHONY: init_git_hooks default install_rust install_rust_nightly install_clippy install_rustfmt install_tomlfmt install clean all ci build clippy test doc e2e check_format format check_rust_format check_toml_format check_ts_format
19+
1220
default: init_git_hooks build format
1321

14-
init_git_hooks: $(GIT_HOOKS)
22+
init_git_hooks:
1523
git config core.hooksPath $(GIT_HOOKS_PATH)
1624

25+
## Dev environment
26+
1727
install_rust:
18-
$(RUSTUP) toolchain list | grep -q $(TOOLCHAIN) || $(RUSTUP) install $(TOOLCHAIN)
28+
ifeq (,$(findstring $(TOOLCHAIN),$(INSTALLED_TOOLCHAINS)))
29+
$(RUSTUP) install $(TOOLCHAIN)
30+
endif
1931

2032
install_rust_nightly:
21-
$(RUSTUP) toolchain list | grep -q $(NIGHTLY_TOOLCHAIN) || $(RUSTUP) install $(NIGHTLY_TOOLCHAIN)
22-
23-
## Dev environment
33+
ifeq (,$(findstring $(NIGHTLY_TOOLCHAIN),$(INSTALLED_TOOLCHAINS)))
34+
$(RUSTUP) install $(NIGHTLY_TOOLCHAIN)
35+
endif
2436

2537
install_clippy: install_rust
26-
$(RUSTUP) component list --installed --toolchain $(TOOLCHAIN) | grep -q clippy || $(RUSTUP) component add clippy --toolchain $(TOOLCHAIN)
38+
ifeq (,$(findstring clippy,$(INSTALLED_COMPONENTS)))
39+
$(RUSTUP) component add clippy --toolchain $(TOOLCHAIN)
40+
endif
2741

2842
install_rustfmt: install_rust_nightly
29-
$(RUSTUP) component list --installed --toolchain $(NIGHTLY_TOOLCHAIN) | grep -q rustfmt || $(RUSTUP) component add rustfmt --toolchain $(NIGHTLY_TOOLCHAIN)
43+
ifeq (,$(findstring rustfmt,$(INSTALLED_NIGHTLY_COMPONENTS)))
44+
$(RUSTUP) component add rustfmt --toolchain $(NIGHTLY_TOOLCHAIN)
45+
endif
3046

3147
install_tomlfmt: install_rust
32-
$(CARGO) --list | grep -q tomlfmt || $(CARGO) install cargo-tomlfmt
33-
34-
yarn_install:
35-
(cd ./api_tests; yarn install)
48+
ifeq (,$(findstring tomlfmt,$(AVAILABLE_CARGO_COMMANDS)))
49+
$(CARGO) install cargo-tomlfmt
50+
endif
3651

3752
## User install
3853

@@ -46,13 +61,6 @@ clean:
4661

4762
all: format build clippy test doc e2e_scripts
4863

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-
5664
ci: check_format doc clippy test build e2e
5765

5866
build:
@@ -77,16 +85,42 @@ test:
7785
doc:
7886
$(CARGO) doc
7987

88+
e2e: build
89+
(cd ./api_tests; yarn install; yarn test)
90+
8091
check_format: check_rust_format check_toml_format check_ts_format
8192

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

85116
check_toml_format: install_tomlfmt
117+
ifneq (,$(STAGED_TOML_FILES))
86118
$(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)
119+
$(CARGO) tomlfmt -d -p cnd/Cargo.toml
120+
$(CARGO) tomlfmt -d -p libp2p-comit/Cargo.toml
121+
endif
122+
123+
check_ts_format:
124+
ifneq (,$(STAGED_TYPESCRIPT_FILES))
125+
(cd ./api_tests; yarn install; yarn run check)
126+
endif

0 commit comments

Comments
 (0)