Move elisp tuning upper to get better support for debugging when init
file has errors.
;;; maven-central.el --- Maven central auxility
;;; Commentary:
;;
(require 'json)
(require 'url-handlers)
;;; Code:
(defvar maven-central.buffer-name "*Maven Central*")
(defconst maven-central.search-url "http://search.maven.org/solrsearch/select?wt=json&rows=20")
(defun maven-central.callback1 (status)
(let ( (buffer (current-buffer)) )
(switch-to-buffer maven-central.buffer-name)
(url-insert buffer)))
(defun maven-central.versions-url (groupId artifactId)
(concat maven-central.search-url "&core=gav&q=g:" (url-hexify-string groupId) (url-hexify-string " AND ") "a:" (url-hexify-string artifactId)))
;; (maven-central.versions-url "junit" "junit")
(defun maven-central.versions-callback (status)
(let ( (buffer (current-buffer)) json )
(with-temp-buffer
(url-insert buffer)
(goto-char (point-min))
(setq json (json-read))
;; (switch-to-buffer maven-central.buffer-name)
;; (pp json)
(message "Available versions: %s" (mapconcat (lambda (json) (cdr (assq 'v json))) (cdr (assq 'docs (cdr (assoc 'response json)))) ", "))
)))
(defun maven-central.versions (groupId artifactId)
"Retrieve available versions."
(url-retrieve (maven-central.versions-url groupId artifactId) #'maven-central.versions-callback))
(defun maven-central.last-version-url (groupId artifactId)
(concat maven-central.search-url "&q=g:" (url-hexify-string groupId) (url-hexify-string " AND ") "a:" (url-hexify-string artifactId)))
;; (maven-central.versions-url "junit" "junit")
(defun maven-central.last-version-callback (status)
(let ( (buffer (current-buffer)) json )
(with-temp-buffer
(url-insert buffer)
(goto-char (point-min))
(setq json (json-read))
;; (switch-to-buffer maven-central.buffer-name)
;; (pp json)
(message "Latest version: %s" (cdr (assq 'latestVersion (elt (cdr (assq 'docs (cdr (assoc 'response json)))) 0))))
)))
(defun maven-central.last-version (groupId artifactId)
"Retrieve last package version."
(url-retrieve (maven-central.last-version-url groupId artifactId) #'maven-central.last-version-callback))
(defun maven-central.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-central.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-central.parse-pom-dependency-in-region fs-o fs-c))
((and bs-o (<= bs-o point) fs-c (<= point fs-c))
(maven-central.parse-pom-dependency-in-region bs-o fs-c))
((and fs-o fs-c)
(maven-central.parse-pom-dependency-in-region fs-o fs-c))
((and bs-o bs-c)
(maven-central.parse-pom-dependency-in-region bs-o bs-c))
(t
(list nil nil nil)))
)))
(defun maven-central.parse-test (&optional point)
(interactive)
(pp (maven-central.parse-pom-dependency)) )
;;;###autoload
(defun maven-central.last-version-from-pom ()
(interactive)
(let (dependency groupId artifactId)
(setq dependency (maven-central.parse-pom-dependency))
(setq groupId (elt dependency 0))
(setq artifactId (elt dependency 1))
(when (and groupId artifactId)
(maven-central.last-version groupId artifactId))
))
;;;###autoload
(defun maven-central.versions-from-pom ()
(interactive)
(let (dependency groupId artifactId)
(setq dependency (maven-central.parse-pom-dependency))
(setq groupId (elt dependency 0))
(setq artifactId (elt dependency 1))
(when (and groupId artifactId)
(maven-central.versions groupId artifactId))
))
(defvar maven.command "mvn")
(defvar maven.help-buffer-name "*Maven Help*")
;;;###autoload
(defun maven.help ()
"Run help:describe for plugin at point."
(interactive)
(let (dependency groupId artifactId)
(setq dependency (maven-central.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)) )
))
(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)) )
(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)) )
(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))))))
;; (maven-central.last-version "junit" "junit")
(provide 'maven-central)
(provide 'maven-central)
;;; maven-central.el ends here