Skip to content

Commit c2ff0be

Browse files
build (Makefile): move binary check logic to parent function
Simplifies function calls to check_bin
1 parent 273641d commit c2ff0be

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

Makefile

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

0 commit comments

Comments
 (0)