-
Notifications
You must be signed in to change notification settings - Fork 1
/
blox.el
272 lines (231 loc) · 9.62 KB
/
blox.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
;;; blox.el --- Interaction with Roblox tooling -*- lexical-binding: t; -*-
;; Copyright (C) 2021 Kenneth Loeffler
;; Author: Kenneth Loeffler <[email protected]>
;; Version: 0.4.0
;; Keywords: roblox, rojo, tools
;; URL: https://github.com/kennethloeffler/blox
;; Package-Requires: ((emacs "25.1"))
;; 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:
;; blox provides functions to run Roblox tooling such as Rojo (see
;; https://github.com/rojo-rbx/rojo) from Emacs.
;; blox doesn't bind any keys by itself; assuming `lua-mode' is
;; installed and loaded, a configuration might look something like
;; this:
;; (require 'blox)
;; (define-key lua-prefix-mode-map (kbd "s") #'blox-prompt-serve)
;; (define-key lua-prefix-mode-map (kbd "b") #'blox-prompt-build)
;; (define-key lua-prefix-mode-map (kbd "t") #'blox-test)
;; Or with `use-package':
;; (use-package blox
;; :bind (:map lua-prefix-mode-map
;; ("s" . blox-prompt-serve)
;; ("b" . blox-prompt-build)
;; ("t" . blox-test)))
;;; Code:
(defgroup blox nil
"Customization for Roblox tooling."
:prefix 'blox-
:group 'tools)
(defgroup blox-executables nil
"Names of executable files."
:group 'blox)
(defcustom blox-binary-output nil
"Whether to output binary place and model files."
:type 'boolean
:group 'blox)
(defcustom blox-default-project "default.project.json"
"The name of the Rojo project file to use by default."
:type 'string
:group 'blox)
(defcustom blox-test-project "test.project.json"
"The name of the project file to use for `blox-test'."
:type 'string
:group 'blox)
(defcustom blox-test-script "TestRunner.server.lua"
"The name of the Lua script file to use for `blox-test'."
:type 'string
:group 'blox)
(defcustom blox-rojo-executable "rojo"
"The name of the executable to use when running Rojo."
:type 'string
:group 'blox-executables)
(defcustom blox-run-in-roblox-executable "run-in-roblox.exe"
"The name of the executable to use when running run-in-roblox."
:type 'string
:group 'blox-executables)
(defun blox--save-some-lua-mode-buffers ()
"Prompt to save any unsaved `lua-mode' buffers."
(save-some-buffers nil (lambda ()
(eq major-mode 'lua-mode))))
(defun blox--echo (message-string command-name)
"Display MESSAGE-STRING prefixed with COMMAND-NAME in the echo area.
Removes any trailing newlines from MESSAGE-STRING."
(message "[%s]: %s"
command-name
(replace-regexp-in-string "\n\\'" "" message-string)))
(defun blox--echo-filter (command-name)
"Process filter to display process output in the echo area.
The filter prefixes any output with COMMAND-NAME and writes the
result to the process buffer, in addition to displaying it in the
echo area."
(lambda (proc string)
(with-current-buffer (process-buffer proc)
(let ((moving (= (point) (process-mark proc))))
(setq buffer-read-only nil)
(save-excursion
(goto-char (process-mark proc))
(insert string)
(set-marker (process-mark proc) (point)))
(if moving (goto-char (process-mark proc)))
(setq buffer-read-only t))
(blox--echo string command-name))))
(defun blox--run-in-roblox-sentinel (buffer)
"Process sentinel to call `display-buffer' on BUFFER.
Also enable `help-mode' in BUFFER and display a message in the
echo area."
(lambda (_process _event)
(with-current-buffer buffer
(help-mode)
(display-buffer buffer)
(blox--echo "Waiting for output from Roblox Studio...done"
"blox-run-in-roblox"))))
(defun blox--run-after-build-sentinel (script-path build-path)
"Process sentinel to run SCRIPT-PATH in BUILD-PATH once finished."
(lambda (_process event)
(if (equal event "finished\n")
(blox-run-in-roblox
script-path
build-path))))
(defun blox--kill-if-running-p (process-name)
"Prompt to kill the process buffer of PROCESS-NAME if it is running.
Return t if the answer is \"yes\" or if the process is not
running. Return nil if the process is running and the answer is
\"no\"."
(or (not (get-process process-name))
(kill-buffer (process-buffer (get-process process-name)))))
(defun blox--roblox-file-extension (project-path)
"Return the appropriate Roblox file extension for PROJECT-PATH."
(with-temp-buffer
(insert-file-contents project-path)
(concat (if (search-forward "DataModel" nil t)
".rbxl"
".rbxm")
(if blox-binary-output "" "x"))))
(defun blox--build-filename (project-path)
"Return the path of the build file corresponding to PROJECT-PATH."
(concat (file-name-sans-extension (file-name-base project-path))
(blox--roblox-file-extension project-path)))
(defun blox--find-file (filename)
"Use function `locate-dominating-file' to find FILENAME.
Start at `default-directory'. If the file cannot be located,
fall back to `default-directory' to make Rojo generate an error
message. Return the path to the file."
(concat (file-truename (or (locate-dominating-file default-directory filename)
default-directory))
filename))
(defun blox-rojo-serve (project-path)
"Serve the Rojo project at PROJECT-PATH."
(blox--save-some-lua-mode-buffers)
(if (blox--kill-if-running-p "*rojo-serve*")
(with-current-buffer (get-buffer-create "*rojo-serve*")
(help-mode)
(make-process
:name "*rojo-serve*"
:buffer (current-buffer)
:filter (blox--echo-filter "blox-rojo-serve")
:command
(list blox-rojo-executable "serve"
project-path)))))
(defun blox-rojo-build (project-path output-path &optional sentinel)
"Build the Rojo project at PROJECT-PATH to OUTPUT-PATH if provided.
If the function SENTINEL is provided, attach it to the Rojo
process. If OUTPUT-PATH is not provided, build to the project directory instead."
(blox--save-some-lua-mode-buffers)
(with-current-buffer (get-buffer-create "*rojo-build*")
(help-mode)
(make-process
:name "*rojo-build*"
:buffer (current-buffer)
:filter (blox--echo-filter "blox-rojo-build")
:sentinel sentinel
:command
(list blox-rojo-executable
"build" project-path
"--output" output-path))))
(defun blox-run-in-roblox (script-path build-path)
"Run the Lua script at SCRIPT-PATH in BUILD-PATH with run-in-roblox."
(if (blox--kill-if-running-p "*run-in-roblox*")
(with-current-buffer (get-buffer-create "*run-in-roblox*")
(blox--echo "Waiting for output from Roblox Studio..."
"blox-run-in-roblox")
(setq buffer-read-only nil)
(erase-buffer)
(setq buffer-read-only t)
(make-process
:name "*run-in-roblox*"
:buffer (current-buffer)
:sentinel (blox--run-in-roblox-sentinel (current-buffer))
:command
(list blox-run-in-roblox-executable
"--place" build-path
"--script" script-path)))))
(defun blox-prompt-serve ()
"Prompt to serve a Rojo project."
(interactive)
(blox-rojo-serve (read-file-name "Choose project: "
(or (vc-root-dir)
default-directory))))
(defun blox-prompt-build ()
"Prompt for a Rojo project and a path to build to."
(interactive)
(blox-rojo-build (read-file-name "Choose project path: "
(or (vc-root-dir)
default-directory))
(read-file-name "Choose output path: "
(or (vc-root-dir)
default-directory))))
(defun blox-prompt-build-and-run ()
"Prompt to build a Rojo project, then for a script to run in it.
The script and project files are expected to be under the same
directory. If this is not the case, abort and display a message
in the echo area."
(interactive)
(let* ((directory (or (vc-root-dir) default-directory))
(project-path (read-file-name "Choose project: " directory))
(script-path (read-file-name "Choose script: " directory))
(output-path (concat (file-name-directory script-path)
(blox--build-filename project-path))))
(blox-rojo-build
project-path
output-path
(blox--run-after-build-sentinel script-path
output-path))))
(defun blox-test ()
"Run `blox-test-script' in `blox-test-project' with run-in-roblox."
(interactive)
(let* ((test-project-path (blox--find-file blox-test-project))
(test-script-path (blox--find-file blox-test-script))
(output-path (concat
(file-name-directory test-script-path)
(blox--build-filename test-project-path))))
(blox-rojo-build
test-project-path
output-path
(blox--run-after-build-sentinel test-script-path
output-path))))
(provide 'blox)
;; Local variables:
;; indent-tabs-mode: nil
;; End:
;;; blox.el ends here