diff options
author | Franck Cuny <franckcuny@gmail.com> | 2016-02-19 16:26:05 -0800 |
---|---|---|
committer | Franck Cuny <franckcuny@gmail.com> | 2016-02-19 16:26:05 -0800 |
commit | 4f687f22f6656e38c76158ed91216a70822fd9f3 (patch) | |
tree | 7c62d954ca2749fa9c1538907806fe044b7f3adb | |
parent | [emacs] Add a function to jump to a BUILD file. (diff) | |
download | emacs.d-4f687f22f6656e38c76158ed91216a70822fd9f3.tar.gz |
[emacs] More code related to pants
Rename the function `jump-to-build-file` to `find-build-file`. Add a function to run a target from the build file. This function searches for the BUILD file, and if one is found, will propose a list of targets to run (for now, only the "binary" goal is supported).
-rw-r--r-- | emacs.d/core/core-bindings.el | 4 | ||||
-rw-r--r-- | emacs.d/core/core-defun.el | 52 |
2 files changed, 46 insertions, 10 deletions
diff --git a/emacs.d/core/core-bindings.el b/emacs.d/core/core-bindings.el index a9d1b4d..53bc847 100644 --- a/emacs.d/core/core-bindings.el +++ b/emacs.d/core/core-bindings.el @@ -8,6 +8,8 @@ (global-set-key (kbd "s-N") 'fcuny/switch-to-scratch) -(global-set-key (kbd "C-c t") 'fcuny/jump-to-build-file) +(global-set-key (kbd "C-c b") 'fcuny/find-build-file) + +(global-set-key (kbd "C-c r") 'fcuny/build-run-target) (provide 'core-bindings) diff --git a/emacs.d/core/core-defun.el b/emacs.d/core/core-defun.el index be7899f..6544815 100644 --- a/emacs.d/core/core-defun.el +++ b/emacs.d/core/core-defun.el @@ -39,33 +39,67 @@ (ansi-term (getenv "SHELL"))) (switch-to-buffer-other-window "*ansi-term*"))) -;; jump to the BUILD file +;; this functions are to make it easy to work with `pants' +(defvar fcuny/build-command "cd ~/src/source && ./pants --no-colors" + "Command to use to execute the target") + (defvar fcuny/build-file "BUILD" "Name of the file containing our build targets") -(defun fcuny/find-root-directory-for-build-file (file) - "Find the root." +(defun fcuny/--find-directory-containing-build-file (file) + "Find the directory containing the build file." (let ((root nil) try) (while (not (or root (null file) (string-match locate-dominating-stop-dir-regexp file))) (setq try (if (stringp fcuny/build-file) - (file-exists-p (expand-file-name fcuny/build-file file)) - (message "found file exists -> %s" file))) + (file-exists-p (expand-file-name fcuny/build-file file)))) (cond (try (setq root file)) ((equal file (setq file (file-name-directory (directory-file-name file)))) (setq file nil)))) (and root (expand-file-name (file-name-as-directory root))))) -(defun fcuny/jump-to-build-file () - "Open the BUILD file in a buffer if it exists." +(defun fcuny/--build-action (target) + (compile (format "%s binary %s" fcuny/build-command target))) + +(defun fcuny/--build-target-list (file) + "Generate a list of existing targets" + (let ((build-command (format "%s list %s:" fcuny/build-command file)) + targets target) + (with-temp-buffer + (insert + (shell-command-to-string build-command)) + (goto-char (point-min)) + (while (re-search-forward "^\\(.+\\)$" nil t) + (setq target (match-string 1)) + (unless (or (save-excursion + (goto-char (match-beginning 0)) + (forward-line -1) + (looking-at "^# Not a target:")) + (string-match "^\\." target)) + (push target targets)))) + (helm + (helm :sources + `((name . "Targets") + (candidates . ,targets) + (action . fcuny/build-action)))))) + +(defun fcuny/find-build-file () + "Find the build file and if it exists, open it." (interactive) - (let ((build-file (fcuny/find-root-directory-for-build-file (file-name-directory (buffer-file-name))))) - (message "found a file %s" build-file) + (let ((build-file (fcuny/find-directory-containing-build-file (file-name-directory (buffer-file-name))))) (if build-file (find-file (concat build-file fcuny/build-file)) (error "Could not find %s" fcuny/build-file)))) +(defun fcuny/build-run-target () + "List the targets for a BUILD file." + (interactive) + (let ((build-file (fcuny/find-root-directory-for-build-file (file-name-directory (buffer-file-name))))) + (if build-file + (fcuny/build-target-list build-file) + (error "Could not find %s" fcuny/build-file)))) + (provide 'core-defun) |