-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
174 lines (142 loc) · 5.47 KB
/
Makefile
File metadata and controls
174 lines (142 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
###
## This Makefile is only here to make sure Task is installable
## Usage: `make` or `make TASK_VERSION=<version>`
###
ifneq ($(wildcard .env.dist),)
include .env.dist
endif
ifneq ($(wildcard .env),)
include .env
endif
.DEFAULT_GOAL:=install
OS?=$(eval OS:=$$(shell uname -s | tr '[:upper:]' '[:lower:]'))$(OS)
MACH?=$(eval MACH:=$$(shell uname -m))$(MACH)
ARCH?=$(eval ARCH:=$$(if $$(filter $$(MACH),x86_64),amd64)$$(if $$(filter $$(MACH),x86 i686 i386),386))$(ARCH)
DESTDIR?=.
PREFIX?=
BIN_DIR?=$(patsubst %/,%,$(DESTDIR))$(patsubst %/,%,$(PREFIX))/bin
CACHE_DIR?=$(patsubst %/,%,$(DESTDIR))$(patsubst %/,%,$(PREFIX))/.cache
TASK_VERSION?=
TASK_GITHUB_OWNER?=go-task
TASK_GITHUB_REPOSITORY?=task
TASK_GITHUB_TARBALL_FILENAME?=task_$(OS)_$(ARCH).tar.gz
TASK_GITHUB_CHECKSUM_FILENAME?=task_checksums.txt
TASK_GITHUB_RELEASES_BASE_URL?=https://github.com/$(TASK_GITHUB_OWNER)/$(TASK_GITHUB_REPOSITORY)/releases
TASK_GITHUB_DOWNLOAD_BASE_URL?=$(patsubst %/,%,$(TASK_GITHUB_RELEASES_BASE_URL))/download
TASK_GITHUB_TAG=$(eval TASK_GITHUB_TAG:=$$(or $$(filter-out latest,$$(TASK_VERSION)),$$(notdir $$(shell curl -Ls -o /dev/null -w %{url_effective} $$(patsubst %/,%,$$(TASK_GITHUB_RELEASES_BASE_URL))/latest))))$(TASK_GITHUB_TAG)
# $1 is the command
is_command=command -v "$(1)" >/dev/null
# $1 is the url, $2 is the local filename
download=\
if $(call is_command,curl); then \
curl -sSL "$(1)" -o "$(2)"; \
elif $(call is_command,wget); then \
wget -q -O "$(2)" "$(1)"; \
else \
exit 1; \
fi
# $1 is filename, returns hash
generate_sha256_hash=\
if $(call is_command,gsha256sum); then \
HASH=$$(gsha256sum "$(1)") || exit 1; \
elif $(call is_command,sha256sum); then \
HASH=$$(sha256sum "$(1)") || exit 1; \
elif $(call is_command,shasum); then \
HASH=$$(shasum -a 256 "$(1)" 2>/dev/null) || exit 1; \
elif $(call is_command,openssl); then \
HASH=$$(openssl dgst -sha256 -r "$(1)") || exit 1; \
else \
exit 1; \
fi; \
echo "$${HASH}" | cut -d ' ' -f 1
#. Check for non-POSIX dependencies - @see https://pubs.opengroup.org/onlinepubs/9699919799/idx/utilities.html
define check-extra-dependency
ifeq ($$(shell command -v $(1) || which $(1) 2>/dev/null),)
$$(error Please provide the command "$(1)")
endif
endef
$(foreach command,tar,$(eval $(call check-extra-dependency,$(command))))
###
## Directories
###
$(BIN_DIR) $(CACHE_DIR):
if test ! -d "$(@)"; then mkdir -p "$(@)"; fi
###
## Binaries
###
$(CACHE_DIR)/task/%/$(TASK_GITHUB_CHECKSUM_FILENAME): | $(CACHE_DIR)
if test ! -d "$(dir $(@))"; then mkdir -p "$(dir $(@))"; fi
$(call download,$(patsubst %/,%,$(TASK_GITHUB_DOWNLOAD_BASE_URL))/$(TASK_GITHUB_TAG)/$(TASK_GITHUB_CHECKSUM_FILENAME),$(@))
if test -f "$(@)"; then touch "$(@)"; fi
.PRECIOUS: $(CACHE_DIR)/task/%/$(TASK_GITHUB_CHECKSUM_FILENAME)
$(CACHE_DIR)/task/%/$(TASK_GITHUB_TARBALL_FILENAME): $(CACHE_DIR)/task/%/$(TASK_GITHUB_CHECKSUM_FILENAME) | $(CACHE_DIR)
if test ! -d "$(dir $(@))"; then mkdir -p "$(dir $(@))"; fi
$(call download,$(patsubst %/,%,$(TASK_GITHUB_DOWNLOAD_BASE_URL))/$(TASK_GITHUB_TAG)/$(TASK_GITHUB_TARBALL_FILENAME),$(@))
trap "test \"\$$?\" != \"0\" && rm -f \"$(<)\" && rm -f \"$(@)\" && ( rmdir -p \"$(dir $(@))\" 2>/dev/null )" EXIT; \
EXPECTED_HASH="$$(cat "$(<)" | grep "$(notdir $(@))" | cut -d ' ' -f 1)"; \
ACTUAL_HASH="$$($(call generate_sha256_hash,$(@)))"; \
if test "$${ACTUAL_HASH}" != "$${EXPECTED_HASH}"; then exit 1; fi
if test -f "$(@)"; then touch "$(@)"; fi
.PRECIOUS: $(CACHE_DIR)/task/%/$(TASK_GITHUB_TARBALL_FILENAME)
$(BIN_DIR)/task-%: $(CACHE_DIR)/task/%/$(TASK_GITHUB_TARBALL_FILENAME) | $(BIN_DIR)
tar -xvf "$(<)" task -O > "$(@)"
if test -f "$(@)"; then chmod +x "$(@)"; touch "$(@)"; fi
.PRECIOUS: $(BIN_DIR)/task-%
task: $(BIN_DIR)/task-$(TASK_GITHUB_TAG)
@true
.PHONY: task
task.remove:
if test -d "$(CACHE_DIR)/task"; then rm -rf "$(CACHE_DIR)/task"; fi
if test -d "$(CACHE_DIR)"; then find "$(CACHE_DIR)" -type d -empty -delete; fi
if test -d "$(BIN_DIR)"; then find "$(BIN_DIR)" -maxdepth 1 -type f -name 'task*' -delete; fi
if test -d "$(BIN_DIR)"; then find "$(BIN_DIR)" -type d -empty -delete; fi
.PHONY: task.remove
###
## Symlink
###
$(BIN_DIR)/task: $(BIN_DIR)/task-$(TASK_GITHUB_TAG)
if test -f "$(@)"; then rm -f "$(@)"; fi
if test -L "$(@)"; then rm -f "$(@)"; fi
ln -s "$(abspath $(<))" "$(@)"
$(@) --version
.PRECIOUS: $(BIN_DIR)/task
task-symlink: $(BIN_DIR)/task
@true
.PHONY: task-symlink
task-symlink.remove:
if test -f "$(BIN_DIR)/task"; then rm -f "$(BIN_DIR)/task"; fi
if test -L "$(BIN_DIR)/task"; then rm -f "$(BIN_DIR)/task"; fi
.PHONY: task-symlink.remove
###
## Alias
###
ALIAS_FILE?=
ifneq ($(ALIAS_FILE),)
.PRECIOUS: $(ALIAS_FILE)
endif
task-alias: | task
ifneq ($(ALIAS_FILE),)
if test ! -f "$(ALIAS_FILE)"; then touch "$(ALIAS_FILE)"; fi
if ! grep -q "^alias task=" "$(ALIAS_FILE)"; then \
echo 'alias task="'\''$(abspath $(BIN_DIR)/task-$(TASK_GITHUB_TAG))'\''"' >> "$(ALIAS_FILE)"; \
else \
sed -i 's#^alias task=.*$$#alias task="'\''$(abspath $(BIN_DIR)/task-$(TASK_GITHUB_TAG))'\''"#' "$(ALIAS_FILE)"; \
fi
grep -q "^alias task=" "$(ALIAS_FILE)"
. "$(ALIAS_FILE)" && '$(abspath $(BIN_DIR)/task-$(TASK_GITHUB_TAG))' --version
else
@true
endif
.PHONY: task-alias
task-alias.remove:
if test -f "$(ALIAS_FILE)" && grep -q "^alias task=" "$(ALIAS_FILE)"; then \
sed -i '\#^alias task=#d' "$(ALIAS_FILE)"; \
fi
.PHONY: task-alias.remove
###
## Installation
###
install: | task task-alias task-symlink; @true
.PHONY: install
clean: | task-alias.remove task-symlink.remove task.remove ; @true
.PHONY: clean