-
Notifications
You must be signed in to change notification settings - Fork 2
/
apprentice-project.el
267 lines (223 loc) · 10.9 KB
/
apprentice-project.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
;;; apprentice-project.el --- API to identify Elixir mix projects.
;; Copyright © 2014-2017 Samuel Tonini
;; Copyright © 2022 Fermin MF
;; Author: Samuel Tonini <tonini.samuel@gmail.com
;; Maintainer: Fermin MF <fmfs@posteo.net>
;; This file is not part of GNU Emacs.
;; This program 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 of the License, 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.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; API to identify Elixir mix projects.
;;; Code:
(require 'cl-lib)
(defgroup apprentice-project nil
"API to identify Elixir mix projects."
:prefix "apprentice-help-"
:group 'apprentice)
(defcustom apprentice-project-elixir-source-dir ""
"Path to the elixir source code."
:type 'string
:group 'alchemist-project)
(defconst apprentice-project-mix-project-indicator "mix.exs"
"File which indicates the root directory of an Elixir Mix project.")
;;TODO: Make something with this indicator
(defconst apprentice-project-hex-pkg-indicator ".hex"
"File which indicates the root directory of an Elixir Hex package.")
(defvar apprentice-project-root-path-cache nil
"Variable which holds the cached project root path.")
(defun apprentice-project-elixir-p ()
"Return non-nil if `default-directory' is inside the Elixir source codebase."
(stringp (apprentice-project-elixir-root)))
(defun apprentice-project-elixir-root (&optional dir)
"Return root directory of the Elixir source DIR."
(if (and (not (or
(null apprentice-project-elixir-source-dir)
(string= "" apprentice-project-elixir-source-dir)))
(string-prefix-p (expand-file-name apprentice-project-elixir-source-dir)
(expand-file-name default-directory)))
apprentice-project-elixir-source-dir
nil))
(defun apprentice-project-p ()
"Return non-nil if `default-directory' is inside an Elixir Mix project."
(stringp (apprentice-project-root)))
(defun apprentice-project-top-level-dir-p (dir)
"Return non-nil if DIR is the top level directory."
(equal dir (file-name-directory (directory-file-name dir))))
(defun apprentice-project--find-closest-identity-file (path identity)
"Find the closest IDENTITY file to the current buffer PATH."
(let ((root (locate-dominating-file path identity)))
(when root
(file-truename root))))
(defun apprentice-project--mix-find-umbrella-root (dir)
(let* ((potential-umbrella-root-parts (butlast (split-string dir "/apps/")))
(potential-umbrella-root (mapconcat #'identity potential-umbrella-root-parts ""))
(umbrella-app-root (apprentice-project--find-closest-identity-file
potential-umbrella-root
apprentice-project-mix-project-indicator)))
(or umbrella-app-root (apprentice-project--find-closest-identity-file dir apprentice-project-mix-project-indicator))))
(cl-defun apprentice-project-root (&optional (dir (or default-directory buffer-file-name)))
"Return root directory of the current Elixir Mix project.
It starts walking the directory tree to find the Elixir Mix root directory
from `default-directory'.If DIR is non-nil it starts walking the
directory from there instead."
(if (and apprentice-project-root-path-cache
(string-prefix-p apprentice-project-root-path-cache
(expand-file-name default-directory)))
apprentice-project-root-path-cache
(if (apprentice-project-top-level-dir-p dir)
nil
(if-let ((umbrella
(and apprentice-mix-start-in-umbrella
(string-match-p (regexp-quote "apps") dir))))
(setf apprentice-project-root-path-cache (apprentice-project--mix-find-umbrella-root dir))
(if-let ((root (or (apprentice-project--find-closest-identity-file dir apprentice-project-mix-project-indicator))))
(setf apprentice-project-root-path-cache root)
nil)))))
(defun apprentice-project-root-or-default-dir ()
"Return the current Elixir mix project root or `default-directory'."
(let* ((project-root (apprentice-project-root))
(dir (if project-root
project-root
default-directory)))
dir))
(defun apprentice-project-toggle-file-and-tests-other-window ()
"Toggle between a file and its test in other window."
(interactive)
(if (apprentice-utils-test-file-p)
(apprentice-project-open-file-for-current-tests 'find-file-other-window)
(apprentice-project-open-tests-for-current-file 'find-file-other-window)))
(defun apprentice-project-toggle-file-and-tests ()
"Toggle between a file and its test in the current window."
(interactive)
(if (apprentice-utils-test-file-p)
(apprentice-project-open-file-for-current-tests 'find-file)
(apprentice-project-open-tests-for-current-file 'find-file)))
(defun apprentice-project-file-under-test (file directory)
"Return the file which are tested by FILE.
DIRECTORY is the place where the file under test is located."
(let* ((filename (file-relative-name file (apprentice-project-root)))
(filename (replace-regexp-in-string "^test" directory filename))
(filename (replace-regexp-in-string "_test\.exs$" "\.ex" filename)))
(concat (apprentice-project-root) filename)))
(defun apprentice-project-open-file-for-current-tests (opener)
"Visit the implementation file for the current buffer with OPENER."
(let* ((filename (apprentice-project-file-under-test (buffer-file-name) "web"))
(filename (if (file-exists-p filename)
filename
(apprentice-project-file-under-test (buffer-file-name) "lib"))))
(funcall opener filename)))
(defun apprentice-project-open-tests-for-current-file (opener)
"Visit the test file for the current buffer with OPENER."
(let* ((filename (file-relative-name (buffer-file-name) (apprentice-project-root)))
(filename (replace-regexp-in-string "^lib/" "test/" filename))
(filename (replace-regexp-in-string "^apps/\\(.*\\)/lib/" "apps/\\1/test/" filename))
(filename (replace-regexp-in-string "^web/" "test/" filename))
(filename (replace-regexp-in-string "^apps/\\(.*\\)/web/" "apps/\\1/test/" filename))
(filename (replace-regexp-in-string "\.ex$" "_test\.exs" filename))
(filename (format "%s/%s" (apprentice-project-root) filename)))
(if (file-exists-p filename)
(funcall opener filename)
(if (y-or-n-p "No test file found; create one now?")
(apprentice-project--create-test-for-current-file
filename (current-buffer))
(message "No test file found.")))))
(defun apprentice-project--create-test-for-current-file (filename buffer)
"Create and populates a test module, FILENAME, for the code in BUFFER.
The module name given to the test module is determined from the name of the
first module defined in BUFFER."
(let* ((directory-name (file-name-directory filename))
(module-name (apprentice-project--grok-module-name buffer))
(test-module-name (concat module-name "Test")))
(unless (file-exists-p directory-name)
(make-directory (file-name-directory filename) t))
(apprentice-project--insert-test-boilerplate
(find-file-other-window filename) test-module-name)))
(defun apprentice-project--grok-module-name (buffer)
"Determine the name of the first module defined in BUFFER."
(save-excursion
(with-current-buffer buffer
(goto-char (point-min))
(re-search-forward "defmodule\\s-\\(.+?\\)\\s-?,?\\s-do")
(match-string 1))))
(defun apprentice-project--insert-test-boilerplate (buffer module)
"Insert ExUnit boilerplate for MODULE in BUFFER.
Point is left in a convenient location."
(with-current-buffer buffer
(insert "defmodule " module " do\n"
" use ExUnit.Case\n\n\n"
"end\n")
(goto-char (point-min))
(beginning-of-line 4)
(indent-according-to-mode)))
(defun apprentice-project-run-tests-for-current-file ()
"Run the tests related to the current file."
(interactive)
(apprentice-project-open-tests-for-current-file 'apprentice-mix-test-file))
(defun apprentice-project-create-file ()
"Create a file under lib/ in the current project.
The newly created buffer is filled with a module definition based
on the file name."
(interactive)
(let ((root (apprentice-project-root)))
(if (not root)
(message "You're not in a Mix project")
(let* ((lib-path (concat root "lib/"))
(abs-path (read-file-name "New file in lib/: " lib-path))
(abs-path (apprentice-utils-add-ext-to-path-if-not-present abs-path ".ex"))
(relative-path (file-relative-name abs-path lib-path)))
(if (file-readable-p abs-path)
(message "%s already exists" relative-path)
(make-directory (file-name-directory abs-path) t)
(find-file abs-path)
(insert "defmodule "
(apprentice-utils-path-to-module-name relative-path)
" do\n"
" \n"
"end\n")
(goto-char (point-min))
(beginning-of-line 2)
(back-to-indentation))))))
(defun apprentice-project-name ()
"Return the name of the current Elixir Mix project."
(if (apprentice-project-p)
(car (last (split-string (apprentice-project-root) "/") 2))
""))
(defun apprentice-project-find-files (root directory)
"Open DIRECTORY inside ROOT and prompt for a file."
(let* ((files (apprentice-project-read-dir root directory))
(file (completing-read (format "[%s] %s: " (apprentice-project-name) directory) files)))
(find-file (expand-file-name file root))))
(defun apprentice-project-read-dir (root directory)
"Return all files in DIRECTORY and use ROOT as `default-directory'."
(let ((default-directory root))
(mapcar (lambda (file) (file-relative-name file root))
(apprentice-project--files-from directory))))
(defun apprentice-project--files-from (directory)
"Internal function that return all files from DIRECTORY."
(when directory
(cl-loop for d in (directory-files directory t)
if (file-directory-p d)
nconc (unless (or (equal (file-relative-name d directory) "..")
(equal (file-relative-name d directory) "."))
(apprentice-project--files-from d))
else nconc (list d))))
(defun apprentice-project-find-dir (directory)
(unless (apprentice-project-p)
(error "Could not find an Elixir Mix project root"))
(apprentice-project-find-files (apprentice-project-root) directory))
(defun apprentice-project-find-lib ()
(interactive)
(apprentice-project-find-dir "lib"))
(defun apprentice-project-find-test ()
(interactive)
(apprentice-project-find-dir "test"))
(provide 'apprentice-project)
;;; apprentice-project.el ends here