-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
256 lines (215 loc) · 8.36 KB
/
Makefile
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/make -f
# Install/Uninstall make script for vim-utilities/build-vim-makefile
# Copyright (C) 2021 S0AndS0
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# Lambda-like functions
#
path_append = $(strip $(1))$(strip $(__PATH_SEPARATOR__))$(strip $(2))
#
# Make variables to satisfy conventions
#
NAME = build-vim-makefile
VERSION = 0.0.1
PKG_NAME = $(NAME)-$(VERSION)
#
# Make variables that readers &/or maintainers may wish to modify
#
SCRIPT_NAME := build-vim-makefile
MAN_PATH := $(firstword $(subst :, ,$(shell manpath)))
MAN_DIR_NAME := man1
GIT_BRANCH := main
GIT_REMOTE := origin
ifeq '$(shell id -u)' '0'
INSTALL_DIRECTORY := /usr/local/sbin
COMPLETION_DIR := $(shell pkg-config --variable=completionsdir bash-completion)
else
COMPLETION_DIR := $(shell echo "$${BASH_COMPLETION_USER_DIR:-$${HOME}/.local/share/bash-completion}")
INSTALL_DIRECTORY := $(HOME)/bin
endif
#
# Make variables set upon run-time
#
## Note ':=' is to avoid late binding that '=' entails
## Attempt to detect Operating System
ifeq '$(findstring :,$(PATH))' ';'
__OS__ := Windows
else
__OS__ := $(shell uname 2>/dev/null || echo 'Unknown')
__OS__ := $(patsubst CYGWIN%,Cygwin,$(__OS__))
__OS__ := $(patsubst MSYS%,MSYS,$(__OS__))
__OS__ := $(patsubst MINGW%,MSYS,$(__OS__))
endif
ifeq '$(__OS__)' 'Windows'
__PATH_SEPARATOR__ := \\
else
__PATH_SEPARATOR__ := /
endif
## Obtain directory path that this Makefile lives in
ROOT_DIRECTORY_PATH := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
ROOT_DIRECTORY_NAME := $(notdir $(patsubst %$(__PATH_SEPARATOR__),%,$(ROOT_DIRECTORY_PATH)))
#
# Override variables via optional configuration file
#
CONFIG_PATH := $(call path_append, $(ROOT_DIRECTORY_PATH), .config-make)
ifneq ("$(wildcard $(CONFIG_PATH))", "")
include $(CONFIG_PATH)
endif
#
# Make variables built from components
#
MAKE_PATH := $(call path_append, $(ROOT_DIRECTORY_PATH), Makefile)
SCRIPT__SOURCE_PATH := $(call path_append, $(ROOT_DIRECTORY_PATH), $(SCRIPT_NAME))
SCRIPT__INSTALL_PATH := $(call path_append, $(INSTALL_DIRECTORY), $(SCRIPT_NAME))
MAN__SOURCE_DIR := $(call path_append, $(ROOT_DIRECTORY_PATH), $(MAN_DIR_NAME))
MAN__INSTALL_DIR := $(call path_append, $(MAN_PATH), $(MAN_DIR_NAME))
GIT_MODULES_PATH := $(call path_append, $(ROOT_DIRECTORY_PATH), .gitmodules)
#
# Make targets and settings
#
.ONESHELL: install uninstall
.PHONY: clean config install uninstall upgrade git-pull link-script unlink-script man link-manual unlink-manual completion link-completion unlink-completion list
.SILENT: clean config install uninstall upgrade git-pull link-script unlink-script man link-manual unlink-manual completion link-completion unlink-completion list
clean: SHELL := /bin/bash
clean: ## Removes configuration file
[[ -f "$(CONFIG_PATH)" ]] && {
rm -v "$(CONFIG_PATH)"
}
config: SHELL := /bin/bash
config: ## Writes configuration file
tee "$(CONFIG_PATH)" 1>/dev/null <<EOF
SCRIPT_NAME = $(SCRIPT_NAME)
INSTALL_DIRECTORY = $(INSTALL_DIRECTORY)
__OS__ = $(__OS__)
__PATH_SEPARATOR__ = $(__PATH_SEPARATOR__)
MAN_PATH = $(MAN_PATH)
MAN_DIR_NAME = $(MAN_DIR_NAME)
GIT_BRANCH = $(GIT_BRANCH)
GIT_REMOTE = $(GIT_REMOTE)
COMPLETION_DIR = $(COMPLETION_DIR)
# vim: filetype=make
EOF
install: ## Runs targets -> link-script link-manual link-completion
install: | link-script link-manual link-completion
uninstall: ## Runs targets -> unlink-script unlink-manual unlink-completion
uninstall: | unlink-script unlink-manual unlink-completion
upgrade: ## Runs targets -> uninstall git-pull install
upgrade: | uninstall git-pull install
git-pull: SHELL := /bin/bash
git-pull: ## Pulls updates from default upstream Git remote
cd "$(ROOT_DIRECTORY_PATH)"
git pull $(GIT_REMOTE) $(GIT_BRANCH)
[[ -f "$(GIT_MODULES_PATH)" ]] && {
git submodule update --init --merge --recursive
}
link-script: SHELL := /bin/bash
link-script: ## Symbolically links to project script
if [[ -L "$(SCRIPT__INSTALL_PATH)" ]]; then
printf >&2 'Link already exists -> %s\n' "$(SCRIPT__INSTALL_PATH)"
elif [[ -f "$(SCRIPT__INSTALL_PATH)" ]]; then
printf >&2 'Error link target is a file -> %s\n' "$(SCRIPT__INSTALL_PATH)"
else
ln -sv "$(SCRIPT__SOURCE_PATH)" "$(SCRIPT__INSTALL_PATH)"
fi
unlink-script: SHELL := /bin/bash
unlink-script: ## Removes symbolic links to project script
if [[ -L "$(SCRIPT__INSTALL_PATH)" ]]; then
rm -v "$(SCRIPT__INSTALL_PATH)"
elif [[ -f "$(SCRIPT__INSTALL_PATH)" ]]; then
printf >&2 'Error link target is a file -> %s\n' "$(SCRIPT__INSTALL_PATH)"
else
printf >&2 'No link to remove at -> %s\n' "$(SCRIPT__INSTALL_PATH)"
fi
man: SHELL := /bin/bash
man: ## Builds manual pages via `help2man` command
if [[ -d "$(MAN__SOURCE_DIR)" ]]; then
help2man "$(SCRIPT__SOURCE_PATH)" --output="$(call path_append, $(MAN__SOURCE_DIR), $(NAME)).1" --no-info
fi
link-manual: SHELL := /bin/bash
link-manual: ## Symbolically links project manual page(s)
if ! [[ -d "$(MAN__SOURCE_DIR)" ]]; then
printf >&2 'No manual entries found at -> %s\n' "$(MAN__SOURCE_DIR)"
exit 0
fi
while read -r _page; do
if [[ -L "$(MAN__INSTALL_DIR)$(__PATH_SEPARATOR__)$${_page}" ]]; then
printf >&2 'Link already exists -> %s\n' "$(MAN__INSTALL_DIR)$(__PATH_SEPARATOR__)$${_page}"
else
ln -sv "$(MAN__SOURCE_DIR)$(__PATH_SEPARATOR__)$${_page}" "$(MAN__INSTALL_DIR)$(__PATH_SEPARATOR__)$${_page}"
fi
done < <(ls "$(MAN__SOURCE_DIR)")
unlink-manual: SHELL := /bin/bash
unlink-manual: ## Removes symbolic links to project manual page(s)
if ! [[ -d "$(MAN__SOURCE_DIR)" ]]; then
printf >&2 'No manual entries found at -> %s\n' "$(MAN__SOURCE_DIR)"
exit 0
fi
while read -r _page; do
if [[ -L "$(MAN__INSTALL_DIR)$(__PATH_SEPARATOR__)$${_page}" ]]; then
rm -v "$(MAN__INSTALL_DIR)$(__PATH_SEPARATOR__)$${_page}"
else
printf >&2 'No manual page found at -> %s\n' "$(MAN__INSTALL_DIR)$(__PATH_SEPARATOR__)$${_page}"
fi
done < <(ls "$(MAN__SOURCE_DIR)")
completion: SHELL := /bin/bash
completion: ## Builds tab completion Bash configuration file for build-vim-makefile
"help-to-complete" --executable "$(ROOT_DIRECTORY_PATH)/build-vim-makefile" --completion-dir "$(ROOT_DIRECTORY_PATH)/bash-completion" --clobber
link-completion: SHELL := /bin/bash
link-completion: ## Links tab completion Bash configuration file for build-vim-makefile
if ! [[ -d "$(COMPLETION_DIR)" ]]; then
printf >&2 'No completion directory found at -> %s\n' "$(COMPLETION_DIR)"
exit 1
fi
while read -r _completion; do
if [[ -L "$(MAN__INSTALL_DIR)$(__PATH_SEPARATOR__)$${_page}" ]]; then
printf >&2 'Link already exists -> %s\n' "$(COMPLETION_DIR)$(__PATH_SEPARATOR__)$${_completion}"
else
ln -sv "$(ROOT_DIRECTORY_PATH)/bash-completion/$${_completion}" "$(COMPLETION_DIR)$(__PATH_SEPARATOR__)$${_completion}"
fi
done < <(ls "$(ROOT_DIRECTORY_PATH)/bash-completion")
unlink-completion: SHELL := /bin/bash
unlink-completion: ## Removes project symbolic link(s) for Bash tab completion
if ! [[ -d "$(COMPLETION_DIR)" ]]; then
printf >&2 'No completion directory found at -> %s\n' "$(COMPLETION_DIR)"
exit 1
fi
while read -r _completion; do
if [[ -L "$(MAN__INSTALL_DIR)$(__PATH_SEPARATOR__)$${_page}" ]]; then
rm "$(COMPLETION_DIR)$(__PATH_SEPARATOR__)$${_completion}"
else
printf >&2 'No link found at -> %s\n' "$(COMPLETION_DIR)$(__PATH_SEPARATOR__)$${_completion}"
fi
done < <(ls "$(ROOT_DIRECTORY_PATH)/bash-completion")
list: SHELL := /bin/bash
list: ## Lists available make commands
gawk 'BEGIN {
delete matched_lines
}
{
if ($$0 ~ "^[a-z0-9A-Z-]{1,32}: [#]{1,2}[[:print:]]*$$") {
matched_lines[length(matched_lines)] = $$0
}
}
END {
print "## Make Commands for $(NAME) ##\n"
for (k in matched_lines) {
split(matched_lines[k], line_components, ":")
gsub(" ## ", " ", line_components[2])
print line_components[1]
print line_components[2]
if ((k + 1) != length(matched_lines)) {
print
}
}
}' "$(MAKE_PATH)"