lint.mk 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # BEGIN: lint-install -makefile lint.mk .
  2. # http://github.com/tinkerbell/lint-install
  3. .PHONY: lint
  4. lint: _lint
  5. LINT_ARCH := $(shell uname -m)
  6. LINT_OS := $(shell uname)
  7. LINT_OS_LOWER := $(shell echo $(LINT_OS) | tr '[:upper:]' '[:lower:]')
  8. LINT_ROOT := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
  9. # shellcheck and hadolint lack arm64 native binaries: rely on x86-64 emulation
  10. ifeq ($(LINT_OS),Darwin)
  11. ifeq ($(LINT_ARCH),arm64)
  12. LINT_ARCH=x86_64
  13. endif
  14. endif
  15. LINTERS :=
  16. FIXERS :=
  17. SHELLCHECK_VERSION ?= v0.8.0
  18. SHELLCHECK_BIN := out/linters/shellcheck-$(SHELLCHECK_VERSION)-$(LINT_ARCH)
  19. $(SHELLCHECK_BIN):
  20. mkdir -p out/linters
  21. rm -rf out/linters/shellcheck-*
  22. 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 -
  23. mv out/linters/shellcheck-$(SHELLCHECK_VERSION)/shellcheck $@
  24. rm -rf out/linters/shellcheck-$(SHELLCHECK_VERSION)/shellcheck
  25. LINTERS += shellcheck-lint
  26. shellcheck-lint: $(SHELLCHECK_BIN)
  27. $(SHELLCHECK_BIN) $(shell find . -name "*.sh")
  28. FIXERS += shellcheck-fix
  29. shellcheck-fix: $(SHELLCHECK_BIN)
  30. $(SHELLCHECK_BIN) $(shell find . -name "*.sh") -f diff | { read -t 1 line || exit 0; { echo "$$line" && cat; } | git apply -p2; }
  31. GOLANGCI_LINT_CONFIG := $(LINT_ROOT)/.golangci.yml
  32. GOLANGCI_LINT_VERSION ?= v1.43.0
  33. GOLANGCI_LINT_BIN := out/linters/golangci-lint-$(GOLANGCI_LINT_VERSION)-$(LINT_ARCH)
  34. $(GOLANGCI_LINT_BIN):
  35. mkdir -p out/linters
  36. rm -rf out/linters/golangci-lint-*
  37. curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b out/linters $(GOLANGCI_LINT_VERSION)
  38. mv out/linters/golangci-lint $@
  39. LINTERS += golangci-lint-lint
  40. golangci-lint-lint: $(GOLANGCI_LINT_BIN)
  41. find . -name go.mod -execdir "$(GOLANGCI_LINT_BIN)" run -c "$(GOLINT_CONFIG)" \;
  42. FIXERS += golangci-lint-fix
  43. golangci-lint-fix: $(GOLANGCI_LINT_BIN)
  44. find . -name go.mod -execdir "$(GOLANGCI_LINT_BIN)" run -c "$(GOLINT_CONFIG)" --fix \;
  45. YAMLLINT_VERSION ?= 1.26.3
  46. YAMLLINT_ROOT := out/linters/yamllint-$(YAMLLINT_VERSION)
  47. YAMLLINT_BIN := $(YAMLLINT_ROOT)/dist/bin/yamllint
  48. $(YAMLLINT_BIN):
  49. mkdir -p out/linters
  50. rm -rf out/linters/yamllint-*
  51. curl -sSfL https://github.com/adrienverge/yamllint/archive/refs/tags/v$(YAMLLINT_VERSION).tar.gz | tar -C out/linters -zxf -
  52. cd $(YAMLLINT_ROOT) && pip3 install --target dist .
  53. LINTERS += yamllint-lint
  54. yamllint-lint: $(YAMLLINT_BIN)
  55. PYTHONPATH=$(YAMLLINT_ROOT)/dist $(YAMLLINT_ROOT)/dist/bin/yamllint .
  56. .PHONY: _lint $(LINTERS)
  57. _lint: $(LINTERS)
  58. .PHONY: fix $(FIXERS)
  59. fix: $(FIXERS)
  60. # END: lint-install -makefile lint.mk .