Skip to content

Commit 68ca126

Browse files
committed
Add tool specific lint/fix targets
This commit breaks out the lint/fix commands to their own targets that make can run concurrently (in parallel or not too!). The benefit of this is that we can run `make --keep-going` and all the targets will be run regardless of exit status (while still getting an error exit if any of them errored). This gives us the benefit of seeing *all* lint issues in one go instead of stopping at the first errored linter. This Makefile is also just plain nicer in my opinion. Everything to do with per file linter is in just one chunk in the file instead of multiple. We can easily see all the per-$file code. This also means we only have to mess within the file-type's block to add/remove linters instead of in multiple locations (block, _lint, and fix). Signed-off-by: Manuel Mendez <[email protected]>
1 parent 3179413 commit 68ca126

File tree

2 files changed

+69
-17
lines changed

2 files changed

+69
-17
lines changed

Makefile

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ ifeq ($(LINT_OS),Darwin)
1717
endif
1818
endif
1919

20+
LINTERS :=
21+
FIXERS :=
22+
2023
SHELLCHECK_VERSION ?= v0.7.2
2124
SHELLCHECK_BIN := out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)
2225
$(SHELLCHECK_BIN):
@@ -25,13 +28,27 @@ $(SHELLCHECK_BIN):
2528
curl -sSfL https://github.com/koalaman/shellcheck/releases/download/$(SHELLCHECK_VERSION)/shellcheck-$(SHELLCHECK_VERSION).$(LINT_OS_LOWER).$(LINT_ARCH).tar.xz | tar -C out/linters -xJf -
2629
mv out/linters/shellcheck-$(SHELLCHECK_VERSION)/shellcheck $@
2730
rm -rf out/linters/shellcheck-$(SHELLCHECK_VERSION)/shellcheck
31+
32+
LINTERS += shellcheck-lint
33+
shellcheck-lint: $(SHELLCHECK_BIN)
34+
$(SHELLCHECK_BIN) $(shell find . -name "*.sh")
35+
36+
FIXERS += shellcheck-fix
37+
shellcheck-fix: $(SHELLCHECK_BIN)
38+
$(SHELLCHECK_BIN) $(shell find . -name "*.sh") -f diff | { read -t 1 line || exit 0; { echo "$$line" && cat; } | git apply -p2; }
39+
2840
HADOLINT_VERSION ?= v2.7.0
2941
HADOLINT_BIN := out/linters/hadolint-$(HADOLINT_VERSION)-$(LINT_ARCH)
3042
$(HADOLINT_BIN):
3143
mkdir -p out/linters
3244
rm -rf out/linters/hadolint-*
3345
curl -sfL https://github.com/hadolint/hadolint/releases/download/v2.6.1/hadolint-$(LINT_OS)-$(LINT_ARCH) > $@
3446
chmod u+x $@
47+
48+
LINTERS += hadolint-lint
49+
hadolint-lint: $(HADOLINT_BIN)
50+
$(HADOLINT_BIN) $(shell find . -name "*Dockerfile")
51+
3552
GOLANGCI_LINT_CONFIG := $(LINT_ROOT)/.golangci.yml
3653
GOLANGCI_LINT_VERSION ?= v1.42.1
3754
GOLANGCI_LINT_BIN := out/linters/golangci-lint-$(GOLANGCI_LINT_VERSION)-$(LINT_ARCH)
@@ -40,6 +57,15 @@ $(GOLANGCI_LINT_BIN):
4057
rm -rf out/linters/golangci-lint-*
4158
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b out/linters $(GOLANGCI_LINT_VERSION)
4259
mv out/linters/golangci-lint $@
60+
61+
LINTERS += golangci-lint-lint
62+
golangci-lint-lint: $(GOLANGCI_LINT_BIN)
63+
$(GOLANGCI_LINT_BIN) run
64+
65+
FIXERS += golangci-lint-fix
66+
golangci-lint-fix: $(GOLANGCI_LINT_BIN)
67+
$(GOLANGCI_LINT_BIN) run --fix
68+
4369
YAMLLINT_VERSION ?= 1.26.3
4470
YAMLLINT_ROOT := out/linters/yamllint-$(YAMLLINT_VERSION)
4571
YAMLLINT_BIN := $(YAMLLINT_ROOT)/dist/bin/yamllint
@@ -48,16 +74,15 @@ $(YAMLLINT_BIN):
4874
rm -rf out/linters/yamllint-*
4975
curl -sSfL https://github.com/adrienverge/yamllint/archive/refs/tags/v$(YAMLLINT_VERSION).tar.gz | tar -C out/linters -zxf -
5076
cd $(YAMLLINT_ROOT) && pip3 install --target dist .
51-
.PHONY: _lint
52-
_lint: $(SHELLCHECK_BIN) $(HADOLINT_BIN) $(GOLANGCI_LINT_BIN) $(YAMLLINT_BIN)
53-
$(GOLANGCI_LINT_BIN) run
54-
$(HADOLINT_BIN) $(shell find . -name "*Dockerfile")
55-
$(SHELLCHECK_BIN) $(shell find . -name "*.sh")
77+
78+
LINTERS += yamllint-lint
79+
yamllint-lint: $(YAMLLINT_BIN)
5680
PYTHONPATH=$(YAMLLINT_ROOT)/dist $(YAMLLINT_ROOT)/dist/bin/yamllint .
5781

58-
.PHONY: fix
59-
fix: $(SHELLCHECK_BIN) $(GOLANGCI_LINT_BIN)
60-
$(GOLANGCI_LINT_BIN) run --fix
61-
$(SHELLCHECK_BIN) $(shell find . -name "*.sh") -f diff | { read -t 1 line || exit 0; { echo "$$line" && cat; } | git apply -p2; }
82+
.PHONY: _lint $(LINTERS)
83+
_lint: $(LINTERS)
84+
85+
.PHONY: fix $(FIXERS)
86+
fix: $(FIXERS)
6287

6388
# END: lint-install .

Makefile.tmpl

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ ifeq ($(LINT_OS),Darwin)
1717
endif
1818
endif
1919

20+
LINTERS :=
21+
FIXERS :=
22+
2023
{{ if .Shell -}}
2124
SHELLCHECK_VERSION ?= v0.7.2
2225
SHELLCHECK_BIN := out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)
@@ -26,6 +29,15 @@ $(SHELLCHECK_BIN):
2629
curl -sSfL https://github.com/koalaman/shellcheck/releases/download/$(SHELLCHECK_VERSION)/shellcheck-$(SHELLCHECK_VERSION).$(LINT_OS_LOWER).$(LINT_ARCH).tar.xz | tar -C out/linters -xJf -
2730
mv out/linters/shellcheck-$(SHELLCHECK_VERSION)/shellcheck $@
2831
rm -rf out/linters/shellcheck-$(SHELLCHECK_VERSION)/shellcheck
32+
33+
LINTERS += shellcheck-lint
34+
shellcheck-lint: $(SHELLCHECK_BIN)
35+
{{ .LintCommands.shellcheck }}
36+
37+
FIXERS += shellcheck-fix
38+
shellcheck-fix: $(SHELLCHECK_BIN)
39+
{{ .FixCommands.shellcheck }}
40+
2941
{{ end -}}
3042

3143
{{ if .Dockerfile -}}
@@ -36,6 +48,11 @@ $(HADOLINT_BIN):
3648
rm -rf out/linters/hadolint-*
3749
curl -sfL https://github.com/hadolint/hadolint/releases/download/v2.6.1/hadolint-$(LINT_OS)-$(LINT_ARCH) > $@
3850
chmod u+x $@
51+
52+
LINTERS += hadolint-lint
53+
hadolint-lint: $(HADOLINT_BIN)
54+
{{ .LintCommands.hadolint }}
55+
3956
{{ end -}}
4057

4158
{{ if .Go -}}
@@ -47,6 +64,15 @@ $(GOLANGCI_LINT_BIN):
4764
rm -rf out/linters/golangci-lint-*
4865
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b out/linters $(GOLANGCI_LINT_VERSION)
4966
mv out/linters/golangci-lint $@
67+
68+
LINTERS += golangci-lint-lint
69+
golangci-lint-lint: $(GOLANGCI_LINT_BIN)
70+
{{ index .LintCommands "golangci-lint" }}
71+
72+
FIXERS += golangci-lint-fix
73+
golangci-lint-fix: $(GOLANGCI_LINT_BIN)
74+
{{ index .FixCommands "golangci-lint" }}
75+
5076
{{ end -}}
5177

5278
{{ if .YAML -}}
@@ -58,16 +84,17 @@ $(YAMLLINT_BIN):
5884
rm -rf out/linters/yamllint-*
5985
curl -sSfL https://github.com/adrienverge/yamllint/archive/refs/tags/v$(YAMLLINT_VERSION).tar.gz | tar -C out/linters -zxf -
6086
cd $(YAMLLINT_ROOT) && pip3 install --target dist .
87+
88+
LINTERS += yamllint-lint
89+
yamllint-lint: $(YAMLLINT_BIN)
90+
{{ .LintCommands.yamllint }}
91+
6192
{{ end -}}
6293

63-
.PHONY: _lint
64-
_lint: {{ if .Shell }}$(SHELLCHECK_BIN) {{ end }}{{ if .Dockerfile }}$(HADOLINT_BIN) {{ end }}{{ if .Go}}$(GOLANGCI_LINT_BIN) {{ end }}{{ if .YAML}}$(YAMLLINT_BIN){{ end }}
65-
{{- range $k, $v := .LintCommands }}
66-
{{ $v }}{{ end}}
94+
.PHONY: _lint $(LINTERS)
95+
_lint: $(LINTERS)
6796

68-
.PHONY: fix
69-
fix: {{ if .Shell }}$(SHELLCHECK_BIN) {{ end }}{{ if .Go}}$(GOLANGCI_LINT_BIN){{ end }}
70-
{{- range $k, $v := .FixCommands }}
71-
{{ $v }}{{ end}}
97+
.PHONY: fix $(FIXERS)
98+
fix: $(FIXERS)
7299

73100
# END: lint-install {{.Args}}

0 commit comments

Comments
 (0)