Skip to content

Commit 63eec23

Browse files
Remove trailing newlines in process output, add blox-rojo-serve
- Remove the trailing newline from process output - Rename blox--project-build-filename, add blox-rojo-serve Also remove .project from the names of built place and model files, remove unnecessary arguments from read-file-name calls, and cd with the current buffer - Fix up docstrings - Rename some arguments, documentation
1 parent f9d4e6a commit 63eec23

File tree

1 file changed

+64
-59
lines changed

1 file changed

+64
-59
lines changed

blox.el

Lines changed: 64 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,17 @@
8686
(eq major-mode 'lua-mode))))
8787

8888
(defun blox--echo (message-string command-name)
89-
"Display MESSAGE-STRING formatted with COMMAND-NAME in the echo area."
90-
(message "[%s]: %s" command-name message-string))
91-
92-
(defun blox--echo-filter (command)
93-
"Return a filter that displays output from COMMAND in the echo area.
94-
Also write the output to the process buffer."
89+
"Display MESSAGE-STRING prefixed with COMMAND-NAME in the echo area.
90+
Removes any trailing newlines from MESSAGE-STRING."
91+
(message "[%s]: %s"
92+
command-name
93+
(replace-regexp-in-string "\n\\'" "" message-string)))
94+
95+
(defun blox--echo-filter (command-name)
96+
"Process filter to display process output in the echo area.
97+
The filter prefixes any output with COMMAND-NAME and writes the
98+
result to the process buffer, in addition to displaying it in the
99+
echo area."
95100
(lambda (proc string)
96101
(with-current-buffer (process-buffer proc)
97102
(let ((moving (= (point) (process-mark proc))))
@@ -102,11 +107,11 @@ Also write the output to the process buffer."
102107
(set-marker (process-mark proc) (point)))
103108
(if moving (goto-char (process-mark proc)))
104109
(setq buffer-read-only t))
105-
(blox--echo string command))))
110+
(blox--echo string command-name))))
106111

107112
(defun blox--run-in-roblox-sentinel (buffer)
108-
"Return a process sentinel to call `display-buffer' on BUFFER.
109-
Also turn on `help-mode' in BUFFER and display a message in the
113+
"Process sentinel to call `display-buffer' on BUFFER.
114+
Also enable `help-mode' in BUFFER and display a message in the
110115
echo area."
111116
(lambda (_process _event)
112117
(with-current-buffer buffer
@@ -116,17 +121,18 @@ echo area."
116121
"blox-run-in-roblox"))))
117122

118123
(defun blox--run-after-build-sentinel (script-path project-path)
119-
"Return a process sentinel to run SCRIPT-PATH in PROJECT-PATH."
124+
"Process sentinel to run SCRIPT-PATH in PROJECT-PATH once finished."
120125
(lambda (_process event)
121126
(if (equal event "finished\n")
122127
(blox-run-in-roblox
123128
script-path
124-
(blox--project-build-filename project-path)))))
129+
(blox--build-path project-path)))))
125130

126131
(defun blox--kill-if-running-p (process-name)
127132
"Prompt to kill the process if PROCESS-NAME is running.
128-
Return t if the answer is \"y\" or if the process is not running.
129-
Return nil if the process is running and the answer is \"n\"."
133+
Return t if the answer is \"yes\" or if the process is not
134+
running. Return nil if the process is running and the answer is
135+
\"no\"."
130136
(or (not (and (get-process process-name)
131137
(eq (process-status process-name) 'run)))
132138
(kill-buffer (process-buffer (get-process process-name)))))
@@ -139,49 +145,56 @@ Return nil if the process is running and the answer is \"n\"."
139145
".rbxlx"
140146
".rbxmx")))
141147

142-
(defun blox--project-build-filename (project-path)
148+
(defun blox--build-path (project-path)
143149
"Return the path of the build corresponding to PROJECT-PATH."
144-
(concat (file-name-sans-extension (file-name-nondirectory
145-
project-path))
150+
(concat (file-name-sans-extension (file-name-base project-path))
146151
(blox--roblox-file-extension project-path)))
147152

153+
(defun blox-rojo-serve (project-path)
154+
"Serve the Rojo project at PROJECT-PATH."
155+
(blox--save-some-lua-mode-buffers)
156+
(if (blox--kill-if-running-p "*rojo-serve*")
157+
(with-current-buffer (get-buffer-create "*rojo-serve*")
158+
(cd (or (vc-root-dir) default-directory))
159+
(help-mode)
160+
(make-process
161+
:name "*rojo-serve*"
162+
:buffer (get-buffer "*rojo-serve*")
163+
:filter (blox--echo-filter "blox-prompt-serve")
164+
:command
165+
(list blox-rojo-executable "serve" project-path)))))
166+
148167
(defun blox-rojo-build (project-path &optional sentinel)
149-
"Build the project at PROJECT-PATH and return the build's filename.
150-
If the function SENTINEL is provided, attach it to the rojo
168+
"Build the Rojo project at PROJECT-PATH.
169+
If the function SENTINEL is provided, attach it to the Rojo
151170
process."
152171
(blox--save-some-lua-mode-buffers)
153-
(let* ((previous-directory default-directory)
154-
(output (blox--project-build-filename project-path)))
172+
(with-current-buffer (get-buffer-create "*rojo-build*")
155173
(cd (file-name-directory project-path))
156-
(with-current-buffer (get-buffer-create "*rojo-build*")
157-
(help-mode)
158-
(make-process
159-
:name "*rojo-build*"
160-
:buffer (get-buffer "*rojo-build*")
161-
:filter (blox--echo-filter "blox-rojo-build")
162-
:sentinel sentinel
163-
:command
164-
(list blox-rojo-executable "build"
165-
(file-name-nondirectory project-path)
166-
"--output" output)))
167-
(cd previous-directory)))
168-
169-
(defun blox-run-in-roblox (script-path place-filename)
170-
"Run the Lua script at SCRIPT-PATH in PLACE-FILENAME with run-in-roblox.
171-
Both SCRIPT-PATH and PLACE-FILENAME must be under the same
174+
(help-mode)
175+
(make-process
176+
:name "*rojo-build*"
177+
:buffer (get-buffer "*rojo-build*")
178+
:filter (blox--echo-filter "blox-rojo-build")
179+
:sentinel sentinel
180+
:command
181+
(list
182+
blox-rojo-executable "build" (file-name-nondirectory
183+
project-path)
184+
"--output" (blox--build-path project-path)))))
185+
186+
(defun blox-run-in-roblox (script-path build-path)
187+
"Run the Lua script at SCRIPT-PATH in BUILD-PATH with run-in-roblox.
188+
Both SCRIPT-PATH and BUILD-PATH must have the same penultimate
172189
directory. If this is not the case, abort and display a message
173190
in the echo area."
174191
(if (blox--kill-if-running-p "*run-in-roblox*")
175-
;; We're fussing with the current directory and the locations of
176-
;; --place and --script here because some weirdness is happening
177-
;; when run-in-roblox is given absolute pathnames. This
178-
;; probably only happens when it's called from WSL?
179-
(if (not (locate-file place-filename
192+
(if (not (locate-file build-path
180193
(list (file-name-directory script-path))))
181194
(blox--echo
182195
"Script and place files must be under the same directory"
183196
"blox-run-in-roblox")
184-
(let ((previous-directory default-directory))
197+
(with-current-buffer (get-buffer-create "*run-in-roblox*")
185198
(cd (file-name-directory script-path))
186199
(blox--echo "Waiting for output from Roblox Studio..."
187200
"blox-run-in-roblox")
@@ -192,25 +205,17 @@ in the echo area."
192205
(get-buffer "*run-in-roblox*"))
193206
:command
194207
(list blox-run-in-roblox-executable
195-
"--place" place-filename
196-
"--script" (file-name-nondirectory script-path)))
197-
(cd previous-directory)))))
208+
"--place" (file-name-nondirectory build-path)
209+
"--script" (file-name-nondirectory script-path)))))))
198210

199211
(defun blox-prompt-serve ()
200-
"Prompt for a project file for Rojo to start serving."
212+
"Prompt to serve a Rojo project."
201213
(interactive)
202214
(blox--save-some-lua-mode-buffers)
203215
(if (blox--kill-if-running-p "*rojo-serve*")
204-
(let ((directory (or (vc-root-dir) default-directory)))
205-
(with-current-buffer (get-buffer-create "*rojo-serve*")
206-
(help-mode)
207-
(make-process
208-
:name "*rojo-serve*"
209-
:buffer (get-buffer "*rojo-serve*")
210-
:filter (blox--echo-filter "blox-prompt-serve")
211-
:command
212-
(list blox-rojo-executable "serve"
213-
(read-file-name "Choose project: " directory "")))))))
216+
(blox-rojo-serve (read-file-name "Choose project: "
217+
(or (vc-root-dir)
218+
default-directory)))))
214219

215220
(defun blox-prompt-build ()
216221
"Prompt to build a Rojo project."
@@ -234,14 +239,14 @@ in the echo area."
234239
(blox-rojo-build (concat project "default.project.json")))))
235240

236241
(defun blox-prompt-build-and-run ()
237-
"Prompt to build a Rojo project and a script to run in it.
242+
"Prompt to build a Rojo project, then for a script to run in it.
238243
The script and project files are expected to be under the same
239244
directory. If this is not the case, abort and display a message
240245
in the echo area."
241246
(interactive)
242247
(let* ((directory (or (vc-root-dir) default-directory))
243-
(project (read-file-name "Choose project: " directory ""))
244-
(script (read-file-name "Choose script " directory "")))
248+
(project (read-file-name "Choose project: " directory))
249+
(script (read-file-name "Choose script " directory)))
245250
(blox-rojo-build project
246251
(blox--run-after-build-sentinel script
247252
project))))

0 commit comments

Comments
 (0)