-
Notifications
You must be signed in to change notification settings - Fork 1
/
fuel-scaffold.el
274 lines (229 loc) · 9.76 KB
/
fuel-scaffold.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
;;; fuel-scaffold.el -- interaction with tools.scaffold -*- lexical-binding: t -*-
;; Copyright (C) 2009 Jose Antonio Ortega Ruiz
;; See https://factorcode.org/license.txt for BSD license.
;; Author: Jose Antonio Ortega Ruiz <[email protected]>
;; Keywords: languages, fuel, factor
;; Start date: Sun Jan 11, 2009 18:40
;;; Comentary:
;; Utilities for creating new vocabulary files and other boilerplate.
;; Mainly, an interface to Factor's tools.scaffold.
;;; Code:
(require 'fuel-eval)
(require 'fuel-edit)
(require 'fuel-base)
(require 'factor-mode)
;;; Customisation:
;;;###autoload
(defgroup fuel-scaffold nil
"Options for FUEL's scaffolding."
:group 'fuel)
;;; Auxiliary functions:
(defun fuel-mode--code-file (kind &optional file)
(let* ((file (or file (buffer-file-name)))
(bn (file-name-nondirectory file)))
(and (string-match (format "\\(.+\\)-%s\\.factor$" kind) bn)
(expand-file-name (concat (match-string 1 bn) ".factor")
(file-name-directory file)))))
(defun fuel-mode--in-docs (&optional file)
(fuel-mode--code-file "docs" file))
(defun fuel-mode--in-tests (&optional file)
(fuel-mode--code-file "tests" file))
(defun fuel-scaffold--vocab-roots ()
(let ((cmd '(:fuel* (vocab-roots get)
"fuel" ("namespaces" "vocabs.loader"))))
(nth 1 (fuel-eval--send/wait cmd))))
(defun fuel-scaffold--dev-name ()
(or (let ((cmd '(:fuel* (developer-name get)
"fuel"
("namespaces" "tools.scaffold"))))
(fuel-eval--retort-result (fuel-eval--send/wait cmd)))
user-full-name
"Your name"))
(defun fuel-scaffold--first-vocab ()
(goto-char (point-min))
(re-search-forward factor-current-vocab-regex nil t))
(defsubst fuel-scaffold--vocab (file)
(with-current-buffer (find-file-noselect file)
(fuel-scaffold--first-vocab)
(factor-current-vocab)))
(defconst fuel-scaffold--tests-header-format
"! Copyright (C) %s %s
! See https://factorcode.org/license.txt for BSD license.
USING: %s tools.test ;
IN: %s
")
(defvar fuel-scaffold-test-autoinsert-p nil)
(defvar fuel-scaffold-help-autoinsert-p nil)
(defvar fuel-scaffold-help-header-only-p nil)
(defsubst fuel-scaffold--check-auto (var)
(and var (or (eq var 'always) (y-or-n-p "Insert template? "))))
(defun fuel-scaffold--tests (parent)
(when (and parent (fuel-scaffold--check-auto fuel-scaffold-test-autoinsert-p))
(let ((year (format-time-string "%Y"))
(name (fuel-scaffold--dev-name))
(vocab (fuel-scaffold--vocab parent)))
(insert (format fuel-scaffold--tests-header-format
year name vocab vocab))
t)))
(defsubst fuel-scaffold--create-docs (vocab)
(let ((cmd `(:fuel* (,vocab ,(fuel-scaffold--dev-name) fuel-scaffold-help)
"fuel")))
(fuel-eval--send/wait cmd)))
(defsubst fuel-scaffold--create-tests (vocab)
(let ((cmd `(:fuel* (,vocab ,(fuel-scaffold--dev-name) fuel-scaffold-tests)
"fuel")))
(fuel-eval--send/wait cmd)))
(defsubst fuel-scaffold--create-authors (vocab)
(let ((cmd `(:fuel* (,vocab ,(fuel-scaffold--dev-name)
fuel-scaffold-authors) "fuel")))
(fuel-eval--send/wait cmd)))
(defsubst fuel-scaffold--create-tags (vocab tags)
(let ((cmd `(:fuel* (,vocab ,tags fuel-scaffold-tags) "fuel")))
(fuel-eval--send/wait cmd)))
(defsubst fuel-scaffold--create-summary (vocab summary)
(let ((cmd `(:fuel* (,vocab ,summary fuel-scaffold-summary) "fuel")))
(fuel-eval--send/wait cmd)))
(defsubst fuel-scaffold--create-platforms (vocab platforms)
(let ((cmd `(:fuel* (,vocab ,platforms fuel-scaffold-platforms) "fuel")))
(fuel-eval--send/wait cmd)))
(defun fuel-scaffold--help (parent)
(when (and parent (fuel-scaffold--check-auto fuel-scaffold-help-autoinsert-p))
(let* ((ret (fuel-scaffold--create-docs (fuel-scaffold--vocab parent)))
(file (fuel-eval--retort-result ret)))
(when file
(revert-buffer t t t)
(when (and fuel-scaffold-help-header-only-p
(fuel-scaffold--first-vocab))
(delete-region (1+ (point)) (point-max))
(save-buffer))
(message "Inserting template ... done."))
(goto-char (point-min)))))
(defun fuel-scaffold--maybe-insert ()
(ignore-errors
(or (fuel-scaffold--tests (fuel-mode--in-tests))
(fuel-scaffold--help (fuel-mode--in-docs)))))
;;; User interface:
;;;###autoload
(defun fuel-scaffold-vocab (&optional other-window name-hint root-hint)
"Creates a directory in the given root for a new vocabulary and
adds source and authors.txt files. Prompts the user for optional summary,
tags, help, and test file creation.
You can configure `user-full-name' for the name to be inserted in
the generated files."
(interactive)
(let* ((name (read-string "Vocab name: " name-hint))
(root (completing-read "Vocab root: "
(fuel-scaffold--vocab-roots)
nil t (or root-hint "resource:")))
(summary (read-string "Vocab summary (empty for none): "))
(tags (read-string "Vocab tags (empty for none): "))
(platforms (read-string "Vocab platforms (empty for all): "))
(help (y-or-n-p "Scaffold help? "))
(tests (y-or-n-p "Scaffold tests? "))
(cmd `(:fuel* (,root ,name ,(fuel-scaffold--dev-name)
fuel-scaffold-vocab) "fuel"))
(ret (fuel-eval--send/wait cmd))
(file (fuel-eval--retort-result ret)))
(unless file
(error "Error creating vocab (%s)" (car (fuel-eval--retort-error ret))))
(when (not (equal "" summary))
(fuel-scaffold--create-summary name summary))
(when (not (equal "" tags))
(fuel-scaffold--create-tags name tags))
(when (not (equal "" platforms))
(fuel-scaffold--create-platforms name platforms))
(when help
(fuel-scaffold--create-docs name))
(when tests
(fuel-scaffold--create-tests name))
(if other-window (find-file-other-window file) (find-file file))
(goto-char (point-max))
name))
;;;###autoload
(defun fuel-scaffold-help (&optional arg)
"Creates, if it does not already exist, a help file with
scaffolded help for each word in the current vocabulary.
With prefix argument, ask for the vocabulary name. You can
configure `user-full-name' for the name to be
inserted in the generated file."
(interactive "P")
(let* ((vocab (or (and (not arg) (factor-current-vocab))
(fuel-completion--read-vocab nil)))
(ret (fuel-scaffold--create-docs vocab))
(file (fuel-eval--retort-result ret)))
(unless file
(error "Error creating help file: %s"
(car (fuel-eval--retort-error ret))))
(find-file file)))
;;;###autoload
(defun fuel-scaffold-tests (&optional arg)
"Creates, if it does not already exist, a tests file for the current
vocabulary.
With prefix argument, ask for the vocabulary name. You can
configure `user-full-name' for the name to be inserted in the
generated file."
(interactive "P")
(let* ((vocab (or (and (not arg) (factor-current-vocab))
(fuel-completion--read-vocab nil)))
(ret (fuel-scaffold--create-tests vocab))
(file (fuel-eval--retort-result ret)))
(unless file
(error "Error creating tests file: %s"
(car (fuel-eval--retort-error ret))))
(find-file file)))
(defun fuel-scaffold-authors (&optional arg)
"Creates, if it does not already exist, an authors file for the current
vocabulary.
With prefix argument, ask for the vocabulary name. You can
configure `user-full-name' for the name to be
inserted in the generated file."
(interactive "P")
(let* ((vocab (or (and (not arg) (factor-current-vocab))
(fuel-completion--read-vocab nil)))
(ret (fuel-scaffold--create-authors vocab))
(file (fuel-eval--retort-result ret)))
(unless file
(error "Error creating authors file: %s"
(car (fuel-eval--retort-error ret))))
(find-file file)))
(defun fuel-scaffold-tags (&optional arg)
"Creates, if it does not already exist, a tags file for the current
vocabulary."
(interactive "P")
(let* ((vocab (or (and (not arg) (factor-current-vocab))
(fuel-completion--read-vocab nil)))
(tags (read-string "Tags: "))
(ret (fuel-scaffold--create-tags vocab tags))
(file (fuel-eval--retort-result ret)))
(unless file
(error "Error creating tags file: %s"
(car (fuel-eval--retort-error ret))))
(find-file file)))
(defun fuel-scaffold-summary (&optional arg)
"Creates, if it does not already exist, a summary file for the current
vocabulary."
(interactive "P")
(let* ((vocab (or (and (not arg ) (factor-current-vocab))
(fuel-completion--read-vocab nil)))
(summary (read-string "Summary: "))
(ret (fuel-scaffold--create-summary vocab summary))
(file (fuel-eval--retort-result ret)))
(unless file
(error "Error creating summary file: %s"
(car (fuel-eval--retort-error ret))))
(find-file file)))
(defun fuel-scaffold-platforms (&optional arg)
"Creates, if it does not already exist, a platforms file for the current
vocabulary."
(interactive "P")
(let* ((vocab (or (and (not arg ) (factor-current-vocab))
(fuel-completion--read-vocab nil)))
(platforms (read-string "Platforms: "))
(ret (fuel-scaffold--create-platforms vocab platforms))
(file (fuel-eval--retort-result ret)))
(unless file
(error "Error creating platforms file: %s"
(car (fuel-eval--retort-error ret))))
(find-file file)))
(provide 'fuel-scaffold)
;;; fuel-scaffold.el ends here