forked from tomtt/emacs-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rails-scripts.el
345 lines (296 loc) · 14.4 KB
/
rails-scripts.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
;;; rails-scripts.el --- emacs-rails integraions with rails script/* scripts
;; Copyright (C) 2006 Dmitry Galinsky <dima dot exe at gmail dot com>
;; Authors: Dmitry Galinsky <dima dot exe at gmail dot com>,
;; Rezikov Peter <crazypit13 (at) gmail.com>
;; Keywords: ruby rails languages oop
;; $URL$
;; $Id$
;;; License
;; 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 2
;; 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, write to the Free Software
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
(eval-when-compile
(require 'inf-ruby)
(require 'ruby-mode))
(defvar rails-script:generators-list
'("controller" "model" "scaffold" "migration" "plugin" "mailer" "observer" "resource"))
(defvar rails-script:destroy-list rails-script:generators-list)
(defvar rails-script:generate-params-list
'("-f")
"Add parameters to script/generate.
For example -s to keep existing files and -c to add new files into svn.")
(defvar rails-script:destroy-params-list
'("-f")
"Add parameters to script/destroy.
For example -c to remove files from svn.")
(defvar rails-script:buffer-name "*ROutput*")
(defvar rails-script:running-script-name nil
"Currently running the script name")
(defvar rails-script:history (list))
(defvar rails-script:history-of-generate (list))
(defvar rails-script:history-of-destroy (list))
;; output-mode
(defconst rails-script:font-lock-ketwords
(list
'("^\\(\(in [^\)]+\)\\)$" 1 font-lock-builtin-face)
'(" \\(rm\\|rmdir\\) " 1 font-lock-warning-face)
'(" \\(missing\\|notempty\\|exists\\) " 1 font-lock-warning-face)
'(" \\(create\\|dependency\\) " 1 font-lock-function-name-face)))
(defconst rails-script:button-regexp
" \\(create\\) + \\([^ ]+\\.\\w+\\)")
(defvar rails-script:output-mode-ret-value nil)
(defvar rails-script:run-after-stop-hook nil)
(defvar rails-script:show-buffer-hook nil)
(defun rails-script:make-buttons (start end len)
(save-excursion
(let ((buffer-read-only nil))
(goto-char start)
(while (re-search-forward rails-script:button-regexp end t)
(make-button (match-beginning 2) (match-end 2)
:type 'rails-button
:rails:file-name (match-string 2))))))
(defun rails-script:popup-buffer (&optional do-not-scroll-to-top)
"Popup output buffer."
(unless (buffer-visible-p rails-script:buffer-name)
(display-buffer rails-script:buffer-name t))
(let ((win (get-buffer-window-list rails-script:buffer-name)))
(when win
(unless do-not-scroll-to-top
(mapcar #'(lambda(w) (set-window-point w 0)) win))
(shrink-window-if-larger-than-buffer
(get-buffer-window rails-script:buffer-name))
(run-hooks 'rails-script:show-buffer-hook))))
(defun rails-script:push-first-button ()
(let (file-name)
(with-current-buffer (get-buffer rails-script:buffer-name)
(let ((button (next-button 1)))
(when button
(setq file-name (button-get button :rails:file-name)))))
(when file-name
(rails-core:find-file-if-exist file-name))))
(defun rails-script:toggle-output-window ()
(interactive)
(let ((current (current-buffer))
(buf (get-buffer rails-script:buffer-name)))
(if buf
(if (buffer-visible-p rails-script:buffer-name)
(delete-windows-on buf)
(progn
(pop-to-buffer rails-script:buffer-name t t)
(pop-to-buffer current t t)
(shrink-window-if-larger-than-buffer
(get-buffer-window rails-script:buffer-name))
(run-hooks 'rails-script:show-buffer-hook)))
(message "No output window found. Try running a script or a rake task before."))))
(defun rails-script:setup-output-buffer ()
"Setup default variables and values for the output buffer."
(set (make-local-variable 'font-lock-keywords-only) t)
(make-local-variable 'font-lock-defaults)
(set (make-local-variable 'scroll-margin) 0)
(set (make-local-variable 'scroll-preserve-screen-position) nil)
(make-local-variable 'after-change-functions)
(rails-minor-mode t))
(define-derived-mode rails-script:output-mode fundamental-mode "ROutput"
"Major mode to Rails Script Output."
(rails-script:setup-output-buffer)
(setq font-lock-defaults '((rails-script:font-lock-ketwords) nil t))
(buffer-disable-undo)
(setq buffer-read-only t)
(rails-script:make-buttons (point-min) (point-max) (point-max))
(add-hook 'rails-script:run-after-stop-hook 'rails-script:popup-buffer t t)
(add-hook 'rails-script:run-after-stop-hook 'rails-script:push-first-button t t)
(add-hook 'after-change-functions 'rails-script:make-buttons nil t)
(run-hooks 'rails-script:output-mode-hook))
(defun rails-script:running-p ()
(get-buffer-process rails-script:buffer-name))
(defun rails-script:kill-script ()
"Kill the currently running rails script"
(interactive)
(let ((proc (rails-script:running-p)))
(if proc
(delete-process proc))))
(defun rails-script:sentinel-proc (proc msg)
(let* ((name rails-script:running-script-name)
(ret-val (process-exit-status proc))
(buf (get-buffer rails-script:buffer-name))
(ret-message (if (zerop ret-val) "successful" "failure")))
(with-current-buffer buf
(set (make-local-variable 'rails-script:output-mode-ret-value) ret-val))
(when (memq (process-status proc) '(exit signal))
(setq rails-script:running-script-name nil
msg (format "%s was stopped (%s)." name ret-message)))
(message (replace-regexp-in-string "\n" "" msg))
(with-current-buffer buf
(run-hooks 'rails-script:run-after-stop-hook))))
(defun rails-script:run (command parameters &optional buffer-major-mode mode-line-string)
"Run a Rails script COMMAND with PARAMETERS with
BUFFER-MAJOR-MODE and process-sentinel SENTINEL."
(unless (listp parameters)
(error "rails-script:run PARAMETERS must be the list"))
(rails-project:with-root
(root)
(save-some-buffers)
(let ((proc (rails-script:running-p)))
(if proc
(message "Only one instance rails-script allowed")
(let* ((default-directory root)
(proc (rails-cmd-proxy:start-process rails-script:buffer-name
rails-script:buffer-name
command
(strings-join " " parameters))))
(with-current-buffer (get-buffer rails-script:buffer-name)
(let ((buffer-read-only nil)
(win (get-buffer-window-list rails-script:buffer-name)))
(kill-region (point-min) (point-max)))
(if buffer-major-mode
(apply buffer-major-mode (list))
(rails-script:output-mode))
(add-hook 'after-change-functions 'rails-cmd-proxy:convert-buffer-from-remote nil t))
(set-process-coding-system proc 'utf-8-dos 'utf-8-dos)
(set-process-sentinel proc 'rails-script:sentinel-proc)
(setq rails-script:running-script-name
(if (= 1 (length parameters))
(format "%s %s" command (first parameters))
(format "%s %s" (first parameters) (first (cdr parameters)))))
(setq rails-ui:mode-line-script-name (or mode-line-string
command))
(message "Starting %s." rails-script:running-script-name))))))
;;;;;;;;;; Destroy stuff ;;;;;;;;;;
(defun rails-script:run-destroy (what &rest parameters)
"Run the destroy script using WHAT and PARAMETERS."
(rails-script:run rails-ruby-command
(append (list (format "script/destroy %s" what))
parameters
rails-script:destroy-params-list)))
(defun rails-script:destroy (what)
"Run destroy WHAT"
(interactive (rails-completing-read "What destroy" rails-script:destroy-list
'rails-script:history-of-destroy nil))
(let ((name (intern (concat "rails-script:destroy-"
(replace-regexp-in-string "_" "-" what)))))
(when (fboundp name)
(call-interactively name))))
(defmacro rails-script:gen-destroy-function (name &optional completion completion-arg)
(let ((func (intern (format "rails-script:destroy-%s" name)))
(param (intern (concat name "-name"))))
`(defun ,func (&optional ,param)
(interactive
(list (completing-read ,(concat "Destroy "
(replace-regexp-in-string "[^a-z0-9]" " " name)
": ")
,(if completion
`(list->alist
,(if completion-arg
`(,completion ,completion-arg)
`(,completion)))
nil))))
(when (string-not-empty ,param)
(rails-script:run-destroy ,(replace-regexp-in-string "-" "_" name) ,param)))))
(rails-script:gen-destroy-function "controller" rails-core:controllers t)
(rails-script:gen-destroy-function "model" rails-core:models)
(rails-script:gen-destroy-function "scaffold")
(rails-script:gen-destroy-function "migration" rails-core:migrations t)
(rails-script:gen-destroy-function "mailer" rails-core:mailers)
(rails-script:gen-destroy-function "plugin" rails-core:plugins)
(rails-script:gen-destroy-function "observer" rails-core:observers)
(rails-script:gen-destroy-function "resource")
;;;;;;;;;; Generators stuff ;;;;;;;;;;
(defun rails-script:run-generate (what &rest parameters)
"Run the generate script using WHAT and PARAMETERS."
(rails-script:run rails-ruby-command
(append (list (format "script/generate %s" what))
parameters
rails-script:generate-params-list)))
(defun rails-script:generate (what)
"Run generate WHAT"
(interactive (rails-completing-read "What generate" rails-script:generators-list
'rails-script:history-of-generate nil))
(let ((name (intern (concat "rails-script:generate-"
(replace-regexp-in-string "_" "-" what)))))
(when (fboundp name)
(call-interactively name))))
(defmacro rails-script:gen-generate-function (name &optional completion completion-arg)
(let ((func (intern (format "rails-script:generate-%s" name)))
(param (intern (concat name "-name"))))
`(defun ,func (&optional ,param)
(interactive
(list (completing-read ,(concat "Generate "
(replace-regexp-in-string "[^a-z0-9]" " " name)
": ")
,(if completion
`(list->alist
,(if completion-arg
`(,completion ,completion-arg)
`(,completion)))
nil))))
(when (string-not-empty ,param)
(rails-script:run-generate ,(replace-regexp-in-string "-" "_" name) ,param)))))
(defun rails-script:generate-controller (&optional controller-name actions)
"Generate a controller and open the controller file."
(interactive (list
(completing-read "Controller name (use autocomplete) : "
(list->alist (rails-core:controllers-ancestors)))
(read-string "Actions (or return to skip): ")))
(when (string-not-empty controller-name)
(rails-script:run-generate "controller" controller-name actions)))
(defun rails-script:generate-scaffold (&optional model-name controller-name actions)
"Generate a scaffold and open the controller file."
(interactive
"MModel name: \nMController (or return to skip): \nMActions (or return to skip): ")
(when (string-not-empty model-name)
(if (string-not-empty controller-name)
(rails-script:run-generate "scaffold" model-name controller-name actions)
(rails-script:run-generate "scaffold" model-name))))
(rails-script:gen-generate-function "model" rails-core:models-ancestors)
(rails-script:gen-generate-function "migration")
(rails-script:gen-generate-function "plugin")
(rails-script:gen-generate-function "mailer")
(rails-script:gen-generate-function "observer")
(rails-script:gen-generate-function "resource")
;;;;;;;;;; Rails create project ;;;;;;;;;;
(defun rails-script:create-project (dir)
"Create a new project in a directory named DIR."
(interactive "FNew Rails project directory: ")
(make-directory dir t)
(let ((default-directory (concat (expand-file-name dir) "/")))
(flet ((rails-project:root () default-directory))
(rails-script:run "rails" (list "--skip" (rails-project:root))))))
;;;;;;;;;; Shells ;;;;;;;;;;
(defun rails-script:run-interactive (name script &optional params)
"Run an interactive shell with SCRIPT in a buffer named
*rails-<project-name>-<name>*."
(rails-project:with-root
(root)
(let ((buffer-name (format "rails-%s-%s" (rails-project:name) name))
(script (rails-core:file script)))
(run-ruby-in-buffer buffer-name
script
params)
(setq ruby-buffer buffer-name))
(rails-minor-mode t)))
(defun rails-script:console (&optional environment)
"Run script/console. With prefix arg, prompts for environment."
(interactive (list
(and current-prefix-arg
(read-buffer "Environment: " rails-default-environment))))
(let* ((environment (or environment rails-default-environment))
(name (format "console at (%s)" environment))
(buffer (get-buffer (format "*rails-%s-%s*" (rails-project:name) name))))
(if buffer
(switch-to-buffer-other-window buffer)
(rails-script:run-interactive name
"script/console"
environment))))
(defun rails-script:breakpointer ()
"Run script/breakpointer."
(interactive)
(rails-script:run-interactive "breakpointer" "script/breakpointer"))
(provide 'rails-scripts)