maven.el
author Oleksandr Gavenko <gavenkoa@gmail.com>
Sat, 25 Mar 2017 02:12:56 +0300
changeset 1508 fad1a57bf25f
parent 1302 maven-central.el@82d6e8bd0861
child 1510 96a2dcb07960
permissions -rw-r--r--
Move maven handling logic from maven-central.el to separate file.

;;; maven.el --- Maven build project helpers.

;;; Commentary:
;;

;;; Code:

(defvar maven.command "mvn")

(defvar maven.help-buffer-name "*Maven Help*")

(defun maven.parse-pom-dependency-in-region (start end)
  (let (groupId artifactId version)
    (save-restriction
      (narrow-to-region start end)
      (goto-char start)
      (re-search-forward "<groupId>\\([^<]*\\)</groupId>" nil t)
      (setq groupId (match-string-no-properties 1))
      (goto-char start)
      (re-search-forward "<artifactId>\\([^<]*\\)</artifactId>" nil t)
      (setq artifactId (match-string-no-properties 1))
      (goto-char start)
      (re-search-forward "<version>\\([^<]*\\)</version>" nil t)
      (setq version (match-string-no-properties 1))
      (list groupId artifactId version)
      )))

(defun maven.parse-pom-dependency (&optional point)
  (unless point
    (setq point (point)))
  (let (start end pos groupId artifactId version fs-o bs-o fs-c bs-c)
    (save-excursion
      (goto-char point)
      (unless (eq (char-after) ?<)
        (search-backward "<"))
      (setq point (point))
      (setq fs-o (re-search-forward "<\\(?:plugin\\|dependency\\)>" nil t))
      (when fs-o
        (setq fs-o (- fs-o 12)))
      (goto-char point)
      (setq bs-o (re-search-backward "<\\(?:plugin\\|dependency\\)>" nil t))
      (goto-char point)
      (setq fs-c (re-search-forward "</\\(?:plugin\\|dependency\\)>" nil t))
      (goto-char point)
      (setq bs-c (re-search-backward "</\\(?:plugin\\|dependency\\)>" nil t))
      (when bs-c
        (setq bs-c (+ bs-c 13)))
      (cond
       ((and fs-o (= fs-o point) fs-c)
        (maven.parse-pom-dependency-in-region fs-o fs-c))
       ((and bs-o (<= bs-o point) fs-c (<= point fs-c))
        (maven.parse-pom-dependency-in-region bs-o fs-c))
       ((and fs-o fs-c)
        (maven.parse-pom-dependency-in-region fs-o fs-c))
       ((and bs-o bs-c)
        (maven.parse-pom-dependency-in-region bs-o bs-c))
       (t
        (list nil nil nil)))
      )))
;; (pp (maven.parse-pom-dependency))

;;;###autoload
(defun maven.help ()
  "Run help:describe for plugin at point."
  (interactive)
  (let (dependency groupId artifactId)
    (setq dependency (maven.parse-pom-dependency))
    (setq groupId (elt dependency 0))
    (setq artifactId (elt dependency 1))
    (if (not (and groupId artifactId))
        (message "Can't find `groupId' or `artifactId'")
      (shell-command
       (format "%s help:describe -Ddetail -DgroupId=%s -DartifactId=%s" maven.command groupId artifactId)
       (switch-to-buffer maven.help-buffer-name)) )
    ))

;;;###autoload
(defun maven.effective-pom ()
  "Run help:effective-pom for plugin at point."
  (interactive)
  (shell-command
   (format "%s help:effective-pom" maven.command)
   (switch-to-buffer maven.help-buffer-name)) )

;;;###autoload
(defun maven.effective-settings ()
  "Run help:effective-settings for plugin at point."
  (interactive)
  (shell-command
   (format "%s help:effective-settings" maven.command)
   (switch-to-buffer maven.help-buffer-name)) )

;;;###autoload
(defun maven.dependency-tree ()
  "Run dependency:tree for plugin at point."
  (interactive)
  (shell-command
   (format "%s dependency:tree" maven.command)
   (switch-to-buffer maven.help-buffer-name)) )

(defun maven.project-root ()
  "Root of project."
  (let ( (dir default-directory) (found nil) )
    (while (and (not found) (> (length dir) 5))
      (when (file-exists-p (concat dir "/pom.xml"))
        (setq found dir))
      (setq dir (expand-file-name (concat dir "/.."))))
    found))

(defun maven.file-package ()
  "Return file package."
  (save-excursion
    (goto-char (point-min))
    (let (pkg cls)
      (re-search-forward "package +\\([[:alnum:]_.]+\\) *;" nil t)
      (setq pkg (match-string-no-properties 1))
      (re-search-forward "class\\s +\\([[:alnum:]_]+\\)\\(\\s \\|\n\\|implements [^{]*\\|extents [^{]*\\)*{" nil t)
      (setq cls (match-string-no-properties 1))
      (when (and pkg cls)
        (concat pkg "." cls))) ))

;;;###autoload
(defun maven.run-file ()
  "Run exec:java for current file."
  (interactive)
  (let* ( (default-directory (concat (maven.project-root) "/")) )
    (if (not default-directory)
        (message "Can't find maven project root")
      (compilation-start
       (format "%s exec:java -Dexec.mainClass=%s" maven.command (maven.file-package))))))

;;;###autoload
(defun maven.run-test ()
  "Run test -Dtest=... for current file."
  (interactive)
  (let* ( (default-directory (concat (maven.project-root) "/")) )
    (if (not default-directory)
        (message "Can't find maven project root")
      (compilation-start
       (format "%s test -Dtest=%s" maven.command (maven.file-package))))))

(provide 'maven)

;;; maven.el ends here