1 ;;; rails-ws.el --- functions for manadge application server
3 ;; Copyright (C) 2006 Dmitry Galinsky <dima dot exe at gmail dot com>
5 ;; Authors: Dmitry Galinsky <dima dot exe at gmail dot com>,
6 ;; Rezikov Peter <crazypit13 (at) gmail.com>
8 ;; Keywords: ruby rails languages oop
9 ;; $URL: svn+ssh://rubyforge/var/svn/emacs-rails/trunk/rails-ws.el $
10 ;; $Id: rails-ws.el 150 2007-03-29 20:48:17Z dimaexe $
14 ;; This program is free software; you can redistribute it and/or
15 ;; modify it under the terms of the GNU General Public License
16 ;; as published by the Free Software Foundation; either version 2
17 ;; of the License, or (at your option) any later version.
19 ;; This program is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this program; if not, write to the Free Software
26 ;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 (defcustom rails-ws
:port
"3000"
31 "Default web server port"
34 :tag
"Rails Server Port")
36 (defcustom rails-ws
:server-name
"http://localhost"
37 "Protocol and the hostname for web server or other rails server"
40 :tag
"Rails Server Default")
42 (defcustom rails-ws
:default-server-type
"mongrel"
43 "Web server to run Rails application."
46 :tag
"Rails Server Type")
48 (defvar rails-ws
:available-servers-list
(list "mongrel" "lighttpd" "webrick"))
49 (defvar rails-ws
:buffer-name
"*RWebServer*")
50 (defvar rails-ws
:process-environment nil
)
52 (defun rails-ws:default-server-type-p
(type)
53 (string= type rails-ws
:default-server-type
))
55 (defun rails-ws:switch-default-server-type
(type)
56 "Switch default server type to run."
57 (interactive (list (completing-read "Server type (use autocomplete): "
58 rails-ws
:available-servers-list
60 rails-ws
:default-server-type
)))
61 (setq rails-ws
:default-server-type type
)
62 (customize-save-variable 'rails-ws
:default-server-type rails-ws
:default-server-type
)
63 (message (concat "Switching to " (upcase type
) " as default server type")))
65 (defun rails-ws:running-p
()
66 "Return t if a WebServer process is running."
67 (if (get-buffer-process rails-ws
:buffer-name
) t nil
))
69 (defun rails-ws:sentinel-proc
(proc msg
)
70 (let ((env rails-ws
:process-environment
))
71 (when (memq (process-status proc
) '(exit signal
))
72 (setq rails-ws
:process-environment nil
)
73 (setq msg
(format "stopped (%s)" msg
)))
75 (replace-regexp-in-string "\n" ""
77 (capitalize rails-ws
:default-server-type
)
80 (defun rails-ws:start
(&optional env
)
81 "Start a server process with ENV environment if ENV is not set
82 using `rails-default-environment'."
83 (interactive (list (rails-read-enviroment-name)))
84 (rails-project:with-root
86 (let ((proc (get-buffer-process rails-ws
:buffer-name
)))
88 (message "Only one instance rails-ws allowed")
89 (let* ((default-directory root
)
90 (env (if env env rails-default-environment
))
92 (rails-cmd-proxy:start-process rails-ruby-command
95 (format "script/server %s -p %s -e %s"
96 rails-ws
:default-server-type
98 (set-process-sentinel proc
'rails-ws
:sentinel-proc
)
99 (setq rails-ws
:process-environment env
)
100 (message (format "%s (%s) starting with port %s"
101 (capitalize rails-ws
:default-server-type
)
105 (defun rails-ws:stop
()
106 "Stop the WebServer process."
108 (let ((proc (get-buffer-process rails-ws
:buffer-name
)))
109 (when proc
(kill-process proc t
))))
112 (defun rails-ws:start-default
()
113 "Start WebServer using the default environment defined in
114 `rails-default-environment'."
116 (rails-ws:start rails-default-environment
))
118 (defun rails-ws:start-development
()
120 (rails-ws:start
"development"))
122 (defun rails-ws:start-production
()
124 (rails-ws:start
"production"))
126 (defun rails-ws:start-test
()
128 (rails-ws:start
"test"))
130 (defun rails-ws:toggle-start-stop
()
131 "Toggle Rails WebServer start/stop with default environment."
133 (if (rails-ws:running-p
)
135 (rails-ws:start-default
)))
137 (defun rails-ws:print-status
()
140 (concat rails-ws
:default-server-type
141 " (" (if rails-ws
:process-environment
142 rails-ws
:process-environment
143 rails-default-environment
) ")"
145 (if (rails-ws:running-p
)
146 (concat "running on port " rails-ws
:port
)
149 ;;;;;;;;;; Open browser ;;;;;;;;;;
151 (defun rails-ws:open-browser
(&optional address
)
152 "Open a browser on the main page of the current Rails project
155 (let ((url (concat (concat rails-ws
:server-name
160 (message "Opening browser: %s" url
)
163 (defun rails-ws:open-browser-on-controller
(&optional controller action params
)
164 "Open browser on the controller/action/id for the current
168 (completing-read "Controller name: "
169 (list->alist
(rails-core:controllers t
)))
170 (read-from-minibuffer "Action name: ")
171 (read-from-minibuffer "Params: ")))
172 (when (string-not-empty controller
)
173 (rails-ws:open-browser
174 (concat (rails-core:file-by-class controller t
) "/"
175 (if (string-not-empty action
) (concat action
"/")) params
))))
177 (defun rails-ws:auto-open-browser
(ask-parameters?
)
178 "Autodetect the current action and open browser on it with.
179 Prefix the command to ask parameters for action."
181 (rails-project:with-root
183 (if (find (rails-core:buffer-type
) '(:view
:controller
))
184 (when-bind (controller (rails-core:current-controller
))
185 (rails-ws:open-browser-on-controller
186 controller
(rails-core:current-action
)
187 (when ask-parameters?
188 (read-from-minibuffer "Parameters: "))))
189 (message "You can auto-open browser only in view or controller"))))