Saturday, December 08, 2012

emacs-grails-mode enhancements

I'm now using Emacs again as my main Anything (Text Editing, Mail Reading, Blogging, Chat, etc.).

I still use vi and IDEs randomly but I will dedicate more time to master Emacs (don't quote me on that...) and a bit of Lisp.

I was looking for some Emacs packages for Grails Development and I found emacs-grails-mode.
It's nice, but it doesn't seem to provide few things such as running a random Grails command against a project.

During development, most of the time I'm jumping between files using find-file-in-project. Occasionally, I use few functions provided by emacs-grails-mode to switch between files associated to Grails domain classes.

Then, whenever I want to run a Grails command, I switch to a dedicated shell buffer. I read the output and I jump back to the file(s) that I'm working on...

I'm no Lisp coder which is a bit sad from my perspective. If I were fluent with it, I could write couple of Emacs packages or improve few things for my personal taste.


Below is my first attempt to improve emacs-grails-mode. I will soon create a basic project on my corporate GitHub account and push the code there. The license will be GPL v3.

I added the following extensions to emacs-grails-mode.
  • Few functions to run some Grails commands from the project folder.
  • There are are also 3 functions to browse API docs, Wiki docs as well as the latest reference guide.


;; ================================
;; Extensions for grails-mode
;; Author : Yves Zoundi
;; Created : 08-Dec-2012
;; 
;; License: GNU GPL v3 (http://www.gnu.org/licenses/gpl-3.0.txt)
;; Sypnosis:
;;    Run grails command on a project
;;
;; TODO:
;;   - Add some easy keybindings like in Rinari
;;   - Would be nice if auto-completion was there
;;     (including dynamic methods)
;;   - Anything else that would be useful for Grails development
;; ================================
(require 'grails-mode)

;; --------------------------------
;; Main functions
;; --------------------------------
(defun grails/command (str)
  "Run a Grails command (Non interactive)"
  
  (project-ensure-current)
  (setq shell-command-initial-directory 
        (project-default-directory (project-current)))
  ;; store old directory
  (setq old-dir default-directory)

  (setq command-initial-directory 
        (expand-file-name shell-command-initial-directory))
  (cd command-initial-directory)
  
  ;; runs the grails command from the project directory
  (async-shell-command (concat "grails " str) nil nil)

  ;; restore previous directory
  (cd old-dir)
  )

 (defun grails/read-param-and-run (input-hint grails-command)
  "Read an input parameter and invoke a given Grails command"
  (setq grails-command-argument 
        (read-from-minibuffer input-hint))  

  (grails/command 
        (concat grails-command " " grails-command-argument))
 )

;; --------------------------------
;; General functions
;; --------------------------------
(defun grails/icommand ()
  "Enter a Grails command (Interactive)"
  (interactive)  
  (grails/read-param-and-run "Goal:" "")
  )

(defun grails/create-domain ()
  "Create a Grails Domain Class"
  (interactive)
  (grails/read-param-and-run 
   "Domain class:" "create-domain-class")
  )

(defun grails/create-controller ()
  "Create a Grails Controller"
  (interactive)
  (grails/read-param-and-run 
   "Controller Domain class:" "create-controller")
  )

(defun grails/create-service ()
  "Create a Grails Service"
  (interactive)
  (grails/read-param-and-run 
   "Service Domain class:" "create-service")
  )

(defun grails/create-taglib ()
  "Create a Grails Taglib"
  (interactive)
  (grails/read-param-and-run 
   "TagLib Name:" "create-tag-lib")
  )

;; --------------------------------
;; Plugin functions
;; --------------------------------
(defun grails/install-plugin ()
  "Install a Grails plugin"
  (interactive)
  (grails/read-param-and-run 
   "name optionalversion:" "install-plugin")
  )

(defun grails/uninstall-plugin ()
  "Uninstall a Grails plugin"
  (interactive)
  (grails/read-param-and-run 
   "Plugin Name:" "uninstall-plugin")
  )

(defun grails/package-plugin ()
  "Package a Grails plugin"
  (interactive)
  (grails/command "package-plugin")
  )

(defun grails/refresh-dependencies ()
  "Refresh Grails Dependencies"
  (interactive)
  (grails/command "refresh-dependencies")
  )

;; --------------------------------
;; Browse docs (api, wiki, guide)
;; --------------------------------
(defun grails/browse-wiki-docs ()
   "Browse the Wiki Documentation"
   (interactive)
   (browse-url "http://grails.org/Documentation")
)

(defun grails/browse-api-docs ()
   "Browse the API Documentation"
   (interactive)
   (browse-url "http://grails.org/doc/latest/api/")
)

(defun grails/browse-latest-guide ()
   "Browse the official Grails Guide"
   (interactive)
   (browse-url "http://grails.org/doc/latest/guide/single.html")
)

(provide 'emacs-grails-mode-ext)

No comments: