@@ -3,36 +3,51 @@ RUSTUP = rustup
3
3
TOOLCHAIN = $(shell cat rust-toolchain)
4
4
CARGO = $(RUSTUP ) run --install $(TOOLCHAIN ) cargo --color always
5
5
6
- NIGHTLY_TOOLCHAIN = " nightly-2019-07-31"
6
+ NIGHTLY_TOOLCHAIN = nightly-2019-07-31
7
7
CARGO_NIGHTLY = $(RUSTUP ) run --install $(NIGHTLY_TOOLCHAIN ) cargo --color always
8
8
9
9
GIT_HOOKS_PATH = ".githooks"
10
10
GIT_HOOKS = $(wildcard $(GIT_HOOKS_PATH ) /* )
11
11
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
+
12
20
default : init_git_hooks build format
13
21
14
- init_git_hooks : $( GIT_HOOKS )
22
+ init_git_hooks :
15
23
git config core.hooksPath $(GIT_HOOKS_PATH )
16
24
25
+ # # Dev environment
26
+
17
27
install_rust :
18
- $(RUSTUP ) toolchain list | grep -q $(TOOLCHAIN ) || $(RUSTUP ) install $(TOOLCHAIN )
28
+ ifeq (,$(findstring $(TOOLCHAIN ) ,$(INSTALLED_TOOLCHAINS ) ) )
29
+ $(RUSTUP) install $(TOOLCHAIN)
30
+ endif
19
31
20
32
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
24
36
25
37
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
27
41
28
42
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
30
46
31
47
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
36
51
37
52
# # User install
38
53
@@ -46,13 +61,6 @@ clean:
46
61
47
62
all : format build clippy test doc e2e_scripts
48
63
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
-
56
64
ci : check_format doc clippy test build e2e
57
65
58
66
build :
@@ -77,16 +85,42 @@ test:
77
85
doc :
78
86
$(CARGO ) doc
79
87
88
+ e2e : build
89
+ (cd ./api_tests; yarn install; yarn test)
90
+
80
91
check_format : check_rust_format check_toml_format check_ts_format
81
92
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
+
82
111
check_rust_format : install_rustfmt
112
+ ifneq (,$(STAGED_RUST_FILES ) )
83
113
$(CARGO_NIGHTLY) fmt -- --check
114
+ endif
84
115
85
116
check_toml_format : install_tomlfmt
117
+ ifneq (,$(STAGED_TOML_FILES ) )
86
118
$(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