-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmakefile-executor.el
274 lines (222 loc) · 9.97 KB
/
makefile-executor.el
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
;;; makefile-executor.el --- Commands for conveniently running makefile targets -*- lexical-binding: t -*-
;; Copyright (C) 2017 Olivia Thiderman
;; Author: Olivia Thiderman <[email protected]>
;; URL: https://github.com/Olivia5k/makefile-executor.el
;; Package-Version: 20230224
;; Version: 0.3.0
;; Package-Requires: ((emacs "27.1") (dash "2.11.0") (f "0.11.0") (s "1.10.0"))
;; Keywords: processes
;; This file is not part of GNU Emacs.
;;; License:
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; 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 General Public License for more details.
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; A set of tools aimed at working with Makefiles on a project level.
;;
;; Currently available:
;; - Interactively selecting a make target and running it.
;; Bound to 'C-c C-e' when 'makefile-executor-mode' is enabled.
;; - Re-running the last execution. We usually run things in
;; Makefiles many times after all! Bound to '`C-c C-c'` in `makefile-mode` when
;; 'makefile-executor-mode'` is enabled.
;; - Running a makefile target in a dedicated buffer. Useful when
;; starting services and other long-running things! Bound to
;; '`C-c C-d'` in `makefile-mode` when 'makefile-executor-mode'` is enabled.
;; - Calculation of variables et.c.; $(BINARY) will show up as what it
;; evaluates to.
;; - Via `project.el', execution from any buffer in a project.
;; If more than one makefile is found, an interactive prompt for one is shown.
;; If `projectile' is installed, this is added to the `projectile-commander' on the 'm' key.
;;
;; To enable it, use the following snippet to add the hook into 'makefile-mode':
;;
;; (add-hook 'makefile-mode-hook 'makefile-executor-mode)
;;
;;; Code:
(require 'compile)
(require 'dash)
(require 'f)
(require 'make-mode)
(require 's)
(require 'projectile nil t)
(require 'project)
(defvar makefile-executor-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-e") 'makefile-executor-execute-target)
(define-key map (kbd "C-c C-c") 'makefile-executor-execute-last)
(define-key map (kbd "C-c C-d") 'makefile-executor-execute-dedicated-buffer)
map)
"Keymap for `makefile-executor-mode'.")
(define-minor-mode makefile-executor-mode
"Turn `makefile-executor' mode on if ARG is positive, off otherwise.
Bindings in `makefile-mode':
\\{makefile-executor-mode-map}"
:global nil
:lighter " executor"
:keymap makefile-executor-mode-map)
(defvar makefile-executor-special-target "emacs--makefile--list")
(setq makefile-executor-cache (make-hash-table :test 'equal))
(defgroup makefile-executor nil
"Conveniently running Makefile targets."
:group 'convenience
:prefix "makefile-executor-")
(defcustom makefile-executor-projectile-style 'makefile-executor-execute-project-target
"Decides what to do when executing from `projectile-commander'."
:type '(choice
(const :tag "Always choose target"
makefile-executor-execute-project-target)
(const :tag "Run most recently executed target"
makefile-executor-execute-last)))
(defcustom makefile-executor-ignore "vendor/"
"Regexp of paths that should be filtered when looking for Makefiles."
:type 'string)
(defun makefile-executor-get-targets (filename)
"Return a list of all the targets of a Makefile.
To list them in a computed manner, print the Makefile database
using `make -p` and process the output.
Optional argument FILENAME defaults to current buffer."
(s-split "\n"
(shell-command-to-string
;; Based on http://stackoverflow.com/a/26339924/983746
(format "make -pRrq -f %s -C %s : 2>/dev/null | awk -v RS= -F: '/(^|\\n)# Files(\\n|$)/,/(^|\\n)# Finished Make data base/ {if ($1 !~ \"^[#.]\") {print $1}}' | sort | grep -E -v -e '^[^[:alnum:]]'"
(shell-quote-argument filename)
(shell-quote-argument (file-name-directory filename))))
t))
(defun makefile-executor-select-target (&optional filename)
"Prompt the user for a Makefile target.
If there is only one, it is returned immediately."
(let ((targets (makefile-executor-get-targets (or filename (buffer-file-name)))))
(if (= (length targets) 1)
(car targets)
(completing-read "target: " targets))))
;;;###autoload
(defun makefile-executor-execute-target (filename &optional target)
"Execute a Makefile target from FILENAME.
FILENAME defaults to current buffer."
(interactive
(list (file-truename buffer-file-name)))
(let ((target (or target (makefile-executor-select-target filename))))
(makefile-executor-store-cache filename target)
(compile (format "make -f %s -C %s %s"
(shell-quote-argument filename)
(shell-quote-argument (file-name-directory filename))
target))))
(defun makefile-executor-store-cache (filename target)
"Store the FILENAME and TARGET in the cache.
If you are in a project, this uses `project-root'. If
not, it uses the current filename."
(puthash (makefile-executor--project-root filename)
(list filename target)
makefile-executor-cache))
(defun makefile-executor-get-cache ()
"Gets the cache for the current project or Makefile.
If you are in a project, this uses `project-root'. If
not, just use the current filename."
(gethash (makefile-executor--project-root (file-truename buffer-file-name))
makefile-executor-cache))
(defun makefile-executor-makefile-p (f)
(and (s-contains? "makefile" (s-downcase f))
(not (string-match makefile-executor-ignore f))))
(defun makefile-executor-get-makefiles ()
(-filter 'makefile-executor-makefile-p
(project-files (project-current))))
(defun makefile-executor--project-root (&optional otherwise)
(if (project-current)
(project-root (project-current))
otherwise))
;;;###autoload
(defun makefile-executor-execute-project-target ()
"Choose a Makefile target from all of the Makefiles in the project.
If there are several Makefiles, a prompt to select one of them is shown.
If so, the parent directory of the closest Makefile is added
as initial input for convenience in executing the most relevant Makefile."
(interactive)
(let* ((files (makefile-executor-get-makefiles)))
(when (= (length files) 0)
(user-error "No makefiles found in this project"))
(makefile-executor-execute-target
;; If there is just the one file, return that one immediately
(if (= (length files) 1)
(car files)
;; If there are more, do a completing read, and use the
;; closest makefile in the project as an initial input, if
;; possible.
(completing-read "Makefile: " files nil t
(makefile-executor--initial-input))))))
(defun makefile-executor--initial-input ()
"Return the Makefile closest to the current buffer.
If none can be found, returns empty string."
;; Get the dominating file dir so we can use that as initial input
;; This means that if we are in a large project with a lot
;; of Makefiles, the closest one will be the initial suggestion.
(let* ((bn (or (buffer-file-name) default-directory))
(fn (or (locate-dominating-file bn "Makefile")
(locate-dominating-file bn "makefile")))
(root (makefile-executor--project-root nil))
(relpath (file-relative-name (or fn root) root)))
;; If we are at the root, we don't need the initial
;; input. If we have it as `./`, the Makefile at
;; the root will not be selectable, which is confusing. Hence, we
;; remove that
(unless fn (user-error "No Makefiles found in project"))
(if (not (s-equals? relpath "./"))
relpath
"")))
;;;###autoload
(defun makefile-executor-execute-dedicated-buffer (filename &optional target)
"Runs a makefile target in a dedicated compile buffer.
The dedicated buffer will be named \"*<target>*\". If
`projectile' is installed and the makefile is in a project the
project name will be prepended to the dedicated buffer name."
(interactive (list
(buffer-file-name)
nil))
(let* ((target (or target (makefile-executor-select-target filename)))
(buffer-name
(if (and (featurep 'projectile) (projectile-project-p))
(format "*%s-%s*" (projectile-project-name) target)
(format "*%s*" target))))
(makefile-executor-execute-target filename target)
(with-current-buffer (get-buffer "*compilation*")
(rename-buffer buffer-name))))
;;;###autoload
(defun makefile-executor-execute-last (arg)
"Execute the most recently executed Makefile target.
If none is set, prompt for it using
`makefile-executor-execute-project-target'. If the universal
argument is given, always prompt."
(interactive "P")
(let ((targets (makefile-executor-get-cache)))
(if (or arg (not targets))
(if (project-current)
(makefile-executor-execute-project-target)
(makefile-executor-execute-target (buffer-file-name)))
(makefile-executor-execute-target
(car targets)
(cadr targets)))))
;;;###autoload
(defun makefile-executor-goto-makefile ()
"Interactively choose a Makefile to visit."
(interactive)
(let ((makefiles (makefile-executor-get-makefiles)))
(find-file
(if (= 1 (length makefiles))
(car makefiles)
(completing-read "Makefile: " makefiles)))))
;; This is so that the library is useful even if one does not have
;; `projectile' installed.
(when (featurep 'projectile)
(def-projectile-commander-method ?m
"Execute makefile targets in project."
(funcall makefile-executor-projectile-style)))
(provide 'makefile-executor)
;;; makefile-executor.el ends here