/ emacs.d / rails / rails-rake.el
rails-rake.el
 1  ;;; rails-rake.el --- emacs-rails integraions with rake tasks.
 2  
 3  ;; Copyright (C) 2006 Dmitry Galinsky <dima dot exe at gmail dot com>
 4  
 5  ;; Authors: Dmitry Galinsky <dima dot exe at gmail dot com>,
 6  
 7  ;; Keywords: ruby rails languages oop
 8  ;; $URL: svn+ssh://rubyforge/var/svn/emacs-rails/trunk/rails-scripts.el $
 9  ;; $Id: rails-scripts.el 117 2007-03-25 23:37:37Z dimaexe $
10  
11  ;;; License
12  
13  ;; This program is free software; you can redistribute it and/or
14  ;; modify it under the terms of the GNU General Public License
15  ;; as published by the Free Software Foundation; either version 2
16  ;; of the License, or (at your option) any later version.
17  
18  ;; This program is distributed in the hope that it will be useful,
19  ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20  ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  ;; GNU General Public License for more details.
22  
23  ;; You should have received a copy of the GNU General Public License
24  ;; along with this program; if not, write to the Free Software
25  ;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  
27  (eval-when-compile
28    (require 'rails-scripts))
29  
30  (defvar rails-rake:history (list))
31  
32  (defvar rails-rake:tasks-regexp "^rake \\([^ ]*\\).*# \\(.*\\)"
33    "Regexp to match tasks list in `rake --tasks` output.")
34  
35  (defun rails-rake:create-tasks-cache (file-name)
36    "Create a cache file from rake --tasks output."
37    (let ((tasks (loop for str in (split-string (rails-cmd-proxy:shell-command-to-string "rake --tasks") "\n")
38                       for task = (when (string-not-empty str)
39                                    (string=~ rails-rake:tasks-regexp str $1))
40                       when task collect task)))
41      (write-string-to-file file-name (prin1-to-string tasks))
42      tasks))
43  
44  (defun rails-rake:list-of-tasks ()
45    "Return all available tasks and create tasks cache file."
46    (rails-project:in-root
47     (let* ((cache-file (rails-core:file "tmp/.tasks-cache")))
48       (if (file-exists-p cache-file)
49           (read-from-file cache-file)
50         (rails-rake:create-tasks-cache cache-file)))))
51  
52  (defun rails-rake:list-of-tasks-without-tests ()
53    "Return available tasks without test actions."
54    (when-bind
55     (tasks (rails-rake:list-of-tasks))
56     (sort (delete* nil
57                    (mapcar
58                     #'(lambda (it) (if (string=~ "^test\\($\\|:\\)" it t) nil it))
59                     (rails-rake:list-of-tasks))
60                    :if 'null)
61           'string<)))
62  
63  (defun rails-rake:task (task &optional major-mode)
64    "Run a Rake task in RAILS_ROOT with MAJOR-MODE."
65    (interactive (rails-completing-read "What task run" (rails-rake:list-of-tasks-without-tests)
66                                        'rails-rake:history nil))
67    (when task
68      (rails-script:run "rake" (list task) major-mode)))
69  
70  (defun rails-rake:migrate (&optional version)
71    "Run the db:migrate task"
72    (interactive)
73    (rails-rake:task
74     (concat
75      "db:migrate"
76      (typecase version
77        (integer (format " VERSION=%.3i" version))
78        (string (format " VERSION=%s" version))))))
79  
80  (defun rails-rake:migrate-to-version (version)
81    "Run migrate with VERSION."
82    (interactive (rails-completing-read "Version of migration"
83                                        (rails-core:migration-versions t)
84                                        nil
85                                        t))
86    (when version
87      (rails-rake:migrate version)))
88  
89  (defun rails-rake:migrate-to-prev-version ()
90    "Migrate to a previous version."
91    (interactive)
92    (let ((versions (rails-core:migration-versions t)))
93      (rails-rake:migrate
94       (when (< 2  (length versions))
95         (nth 1 versions)))))
96  
97  (provide 'rails-rake)