-
Notifications
You must be signed in to change notification settings - Fork 45
/
rails-project.el
82 lines (67 loc) · 2.75 KB
/
rails-project.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
;;; rails-project.el ---
;; Copyright (C) 2006 Dmitry Galinsky <dima dot exe at gmail dot com>
;; Authors: Dmitry Galinsky <dima dot exe at gmail dot com>,
;; Keywords: ruby rails languages oop
;; $URL: svn+ssh://rubyforge/var/svn/emacs-rails/trunk/rails.el $
;; $Id: rails.el 149 2007-03-29 15:07:49Z dimaexe $
;;; 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.
;;; Code:
(defun rails-project:root ()
"Return RAILS_ROOT if this file is a part of a Rails application,
else return nil"
(let ((curdir default-directory)
(max 10)
(found nil))
(while (and (not found) (> max 0))
(progn
(if (file-exists-p (concat curdir "config/environment.rb"))
(progn
(setq found t))
(progn
(setq curdir (concat curdir "../"))
(setq max (- max 1))))))
(if found (expand-file-name curdir))))
(defmacro* rails-project:with-root ((root) &body body)
"If you use `rails-project:root' or functions related on it
several times in a block of code, you can optimize your code by
using this macro. Also, blocks of code will be executed only if
rails-root exist.
(rails-project:with-root (root)
(foo root)
(bar (rails-core:file \"some/path\")))
"
`(let ((,root (rails-project:root)))
(when ,root
(flet ((rails-project:root () ,root))
,@body))))
(defmacro rails-project:in-root (&rest body)
"Set the default directory to the Rails root directory while
BODY is executed."
(let ((root (gensym)))
`(rails-project:with-root
(,root)
(let ((default-dir ,root))
,@body))))
(defmacro* rails-project:in-root-with-cd (&rest body)
(let ((root (gensym)))
`(rails-project:with-root (,root)
(in-directory (,root) ,@body))))
(defun rails-project:compile-in-root (command)
(rails-project:in-root-with-cd
(compile command)))
(defun rails-project:name ()
"Return the name of current Rails project."
(replace-regexp-in-string "^.*/\\(.*\\)/$" "\\1"
(directory-name (rails-project:root))))
(provide 'rails-project)