Initial Commit
[temp.git] / site-lisp / rails / rails-ws.el
blob9a1a2a77024b0dfef500b1bef5bb3f0524684170
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 $
12 ;;; License
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.
28 ;;; Code:
30 (defcustom rails-ws:port "3000"
31 "Default web server port"
32 :group 'rails
33 :type 'string
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"
38 :group 'rails
39 :type 'string
40 :tag "Rails Server Default")
42 (defcustom rails-ws:default-server-type "mongrel"
43 "Web server to run Rails application."
44 :group 'rails
45 :type 'string
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
59 nil t
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)))
74 (message
75 (replace-regexp-in-string "\n" ""
76 (format "%s - %s"
77 (capitalize rails-ws:default-server-type)
78 msg)))))
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
85 (root)
86 (let ((proc (get-buffer-process rails-ws:buffer-name)))
87 (if proc
88 (message "Only one instance rails-ws allowed")
89 (let* ((default-directory root)
90 (env (if env env rails-default-environment))
91 (proc
92 (rails-cmd-proxy:start-process rails-ruby-command
93 rails-ws:buffer-name
94 rails-ruby-command
95 (format "script/server %s -p %s -e %s"
96 rails-ws:default-server-type
97 rails-ws:port env))))
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)
103 rails-ws:port)))))))
105 (defun rails-ws:stop ()
106 "Stop the WebServer process."
107 (interactive)
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'."
115 (interactive)
116 (rails-ws:start rails-default-environment))
118 (defun rails-ws:start-development ()
119 (interactive)
120 (rails-ws:start "development"))
122 (defun rails-ws:start-production ()
123 (interactive)
124 (rails-ws:start "production"))
126 (defun rails-ws:start-test ()
127 (interactive)
128 (rails-ws:start "test"))
130 (defun rails-ws:toggle-start-stop ()
131 "Toggle Rails WebServer start/stop with default environment."
132 (interactive)
133 (if (rails-ws:running-p)
134 (rails-ws:stop)
135 (rails-ws:start-default)))
137 (defun rails-ws:print-status ()
138 (interactive)
139 (message
140 (concat rails-ws:default-server-type
141 " (" (if rails-ws:process-environment
142 rails-ws:process-environment
143 rails-default-environment) ")"
144 " is "
145 (if (rails-ws:running-p)
146 (concat "running on port " rails-ws:port)
147 "stopped"))))
149 ;;;;;;;;;; Open browser ;;;;;;;;;;
151 (defun rails-ws:open-browser (&optional address)
152 "Open a browser on the main page of the current Rails project
153 server."
154 (interactive)
155 (let ((url (concat (concat rails-ws:server-name
157 rails-ws:port
159 address ))))
160 (message "Opening browser: %s" url)
161 (browse-url url)))
163 (defun rails-ws:open-browser-on-controller (&optional controller action params)
164 "Open browser on the controller/action/id for the current
165 file."
166 (interactive
167 (list
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."
180 (interactive "P")
181 (rails-project:with-root
182 (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"))))
191 (provide 'rails-ws)