Skip to content

Commit 3fc70de

Browse files
refactor: abstract bin check and brew install directives
DRY
1 parent 3e5ad1c commit 3fc70de

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

Makefile

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/usr/bin/make -f
2+
3+
.DEFAULT_GOAL := help
4+
5+
.ONESHELL:
6+
7+
# ENV VARS
8+
export SHELL := $(shell which sh)
9+
export UNAME := $(shell uname -s)
10+
export ASDF_VERSION := v0.13.1
11+
12+
# check commands and OS
13+
ifeq ($(UNAME), Darwin)
14+
export XCODE := $(shell xcode-select -p 2>/dev/null)
15+
export HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK := 1
16+
else ifeq ($(UNAME), Linux)
17+
include /etc/os-release
18+
endif
19+
20+
# colors
21+
GREEN := $(shell tput -Txterm setaf 2)
22+
YELLOW := $(shell tput -Txterm setaf 3)
23+
WHITE := $(shell tput -Txterm setaf 7)
24+
CYAN := $(shell tput -Txterm setaf 6)
25+
RESET := $(shell tput -Txterm sgr0)
26+
27+
# Usage: $(call check_bin,command_name)
28+
define check_bin
29+
$(shell command -v $(1) 2>/dev/null)
30+
endef
31+
32+
# Usage: $(call brew_install,package_name)
33+
# For packages where binary name differs from package name, add a mapping in the case statement
34+
define brew_install
35+
@if [ "${UNAME}" = "Darwin" ] || [ "${UNAME}" = "Linux" ]; then \
36+
binary_name=""; \
37+
case "$(1)" in \
38+
"go-task") binary_name="task" ;; \
39+
*) binary_name="$(1)" ;; \
40+
esac; \
41+
if [ -z "$(call check_bin,$$binary_name)" ]; then \
42+
echo "Installing $(1)..."; \
43+
brew install $(1); \
44+
else \
45+
echo "$(1) already installed."; \
46+
fi \
47+
else \
48+
echo "$(1) not supported."; \
49+
fi
50+
endef
51+
52+
# targets
53+
.PHONY: all
54+
all: help asdf xcode brew jq pre-commit sccache task ## run all targets
55+
56+
xcode: ## install xcode command line tools
57+
ifeq ($(UNAME), Darwin)
58+
@if [ -z "${XCODE}" ]; then \
59+
echo "Installing Xcode command line tools..."; \
60+
xcode-select --install; \
61+
else \
62+
echo "xcode already installed."; \
63+
fi
64+
else
65+
@echo "xcode not supported."
66+
endif
67+
68+
brew: xcode ## install homebrew
69+
ifeq ($(UNAME), Darwin)
70+
@if [ -z "$(call check_bin,brew)" ]; then \
71+
echo "Installing Homebrew..."; \
72+
/bin/bash -c "$$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"; \
73+
else \
74+
echo "brew already installed."; \
75+
fi
76+
else ifeq ($(UNAME), Linux)
77+
@if [ -z "$(call check_bin,brew)" ] && [ "${ID_LIKE}" = "debian" ]; then \
78+
echo "Installing Homebrew..."; \
79+
/bin/bash -c "$$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"; \
80+
else \
81+
echo "brew already installed."; \
82+
fi
83+
else
84+
@echo "brew not supported."
85+
endif
86+
87+
asdf: xcode ## install asdf
88+
@if [ -z "$(call check_bin,asdf)" ]; then \
89+
echo "Installing asdf..."; \
90+
git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch ${ASDF_VERSION}; \
91+
echo "To use asdf, add the following to your shell rc (.bashrc/.zshrc):"; \
92+
echo "export PATH=\"$$HOME/.asdf/shims:$$PATH\""; \
93+
echo ". $$HOME/.asdf/asdf.sh"; \
94+
echo ". $$HOME/.asdf/completions/asdf.bash"; \
95+
else \
96+
echo "asdf already installed."; \
97+
fi
98+
99+
jq: brew ## install jq
100+
$(call brew_install,jq)
101+
102+
pre-commit: brew ## install pre-commit
103+
$(call brew_install,pre-commit)
104+
105+
sccache: brew ## install sccache
106+
$(call brew_install,sccache)
107+
108+
task: brew ## install taskfile
109+
$(call brew_install,go-task)
110+
111+
install: xcode asdf brew jq pre-commit sccache task ## install dependencies
112+
113+
help: ## show this help
114+
@echo ''
115+
@echo 'Usage:'
116+
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
117+
@echo ''
118+
@echo 'Targets:'
119+
@awk 'BEGIN {FS = ":.*?## "} { \
120+
if (/^[a-zA-Z_-]+:.*?##.*$$/) {printf " ${YELLOW}%-20s${GREEN}%s${RESET}\n", $$1, $$2} \
121+
else if (/^## .*$$/) {printf " ${CYAN}%s${RESET}\n", substr($$1,4)} \
122+
}' $(MAKEFILE_LIST)

0 commit comments

Comments
 (0)