Created
February 23, 2012 12:06
-
-
Save antoniogarrote/1892560 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; The script assumes you are using bash. | |
;; | |
;; You can run any of these commands from a buffer visiting a file within a Ruby project: | |
;; | |
;; cap-tasks -> lists all capistrano tasks | |
;; cap-deploy | |
;; cap -> asks the user the name of a task and executes it, asking for more input if it detects | |
;; capistrano is outputing something finished with ':' (like Password:). | |
(defun chomp (str) | |
"Chomp leading and tailing whitespace from STR." | |
(while (string-match "\\`\n+\\|^\\s-+\\|\\s-+$\\|\n+\\'" str) | |
(setq str (replace-match "" t t str))) | |
str) | |
(defun get-string-from-file (file-path) | |
(chomp (with-temp-buffer | |
(insert-file-contents file-path) | |
(buffer-string)))) | |
(defun rvm-binary () | |
(if (file-exists-p (expand-file-name "~/.rvm/bin/rvm")) | |
(concat "&& source ~/.bashrc && source ~/.bash_profile && PATH=$PATH:" (expand-file-name "~/.rvm/bin")) | |
"&& source ~/.bashrc && source ~/.bash_profile")) | |
(defun select-ruby (base-dir) | |
(let ((maybe-rvmrc-file (concat (expand-file-name base-dir) ".rvmrc"))) | |
(if (file-exists-p maybe-rvmrc-file) | |
(concat " && " (get-string-from-file maybe-rvmrc-file)) | |
""))) | |
(defun current-directory () | |
(let* ((current-file (buffer-file-name))) | |
(file-name-directory current-file))) | |
(defun look-for-cap (directory) | |
(let ((directory (expand-file-name directory))) | |
(if (string= directory "/") | |
(error "Could not find Capfile or capfile, aborting") | |
(if (file-exists-p (concat directory "Capfile")) | |
directory | |
(if (file-exists-p (concat directory "capfile")) | |
directory | |
(look-for-cap (file-name-directory (concat directory "../")))))))) | |
(defun look-for-cap-current () | |
(look-for-cap (current-directory))) | |
(defun cap-tasks () | |
"Lists all the capistrano tasks in the current project" | |
(interactive) | |
(let ((cap-directory (look-for-cap-current)) | |
(cap-buffer (get-buffer-create "*cap*"))) | |
(switch-to-buffer-other-window cap-buffer) | |
(erase-buffer) | |
(insert (concat "cd " cap-directory | |
(rvm-binary) | |
(select-ruby cap-directory) | |
" && cap -vT") ) | |
(condition-case ex | |
(call-process-shell-command (concat "cd " cap-directory | |
(rvm-binary) | |
(select-ruby cap-directory) | |
" && cap -vT") | |
nil cap-buffer) | |
('error (call-process-shell-command (concat "cd " cap-directory | |
(rvm-binary) | |
(select-ruby cap-directory) | |
" && bundle exec cap -vT") | |
nil cap-buffer))))) | |
(defun cap-deploy () | |
"Runs cap deploy" | |
(interactive) | |
(let ((cap-directory (look-for-cap-current))) | |
(setq cap-buffer (get-buffer-create "*cap*")) | |
(switch-to-buffer-other-window cap-buffer) | |
(erase-buffer) | |
(insert (concat "DEPLOYING from " cap-directory " NOW!!! \n\n")) | |
(let ((cap-process (condition-case ex | |
(start-process-shell-command "cap-process" nil (concat "cd " cap-directory | |
(rvm-binary) | |
(select-ruby cap-directory) | |
" && cap deploy")) | |
('error (start-process-shell-command "cap-process" nil (concat "cd " cap-directory | |
(rvm-binary) | |
(select-ruby cap-directory) | |
" && bundle exec cap deploy")))))) | |
(set-process-filter cap-process (lambda (process output) | |
(with-buffer cap-buffer | |
(progn | |
(goto-char (point-max)) | |
(insert output) | |
(if (string-match "Password:" output) | |
(let ((passwd (read-from-minibuffer "Deploy password:"))) | |
(progn | |
(process-send-string process passwd) | |
(process-send-string process "\r\n"))))))))))) | |
(defun cap (task) | |
"Runs a capistrano task" | |
(interactive "sCap task to run:") | |
(let ((cap-directory (look-for-cap-current))) | |
(setq cap-buffer (get-buffer-create "*cap*")) | |
(switch-to-buffer-other-window cap-buffer) | |
(erase-buffer) | |
(insert (concat "RUNNING " task " from " cap-directory " NOW!!! \n\n")) | |
(let ((cap-process (condition-case ex | |
(start-process-shell-command "cap-process" nil (concat "cd " cap-directory | |
(rvm-binary) | |
(select-ruby cap-directory) | |
" && cap " task)) | |
('error (start-process-shell-command "cap-process" nil (concat "cd " cap-directory | |
(rvm-binary) | |
(select-ruby cap-directory) | |
" && bundle exec cap " task)))))) | |
(set-process-filter cap-process (lambda (process output) | |
(with-buffer cap-buffer | |
(progn | |
(goto-char (point-max)) | |
(insert output) | |
(if (equalp (car (last (string-to-list output))) ?\:) | |
(let ((argument (read-from-minibuffer "User input:"))) | |
(progn | |
(process-send-string process argument) | |
(process-send-string process "\r\n"))))))))))) | |
(provide 'capify) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment