maven-central.el
author Oleksandr Gavenko <gavenkoa@gmail.com>
Fri, 28 Feb 2014 16:38:33 +0200
changeset 1108 8e730890ac85
parent 1091 bbe864449f48
child 1109 09d967bff50f
permissions -rw-r--r--
maven.help - Run help:describe for plugin at point.

;;; 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)
      (beginning-of-buffer)
      (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)
      (beginning-of-buffer)
      (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*")

(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 -DgroupId=%s -DartifactId=%s" maven.command groupId artifactId)
       (switch-to-buffer maven.help-buffer-name)) )
    ))

;; (maven-central.last-version "junit" "junit")

(provide 'maven-central)

(provide 'maven-central)

;;; maven-central.el ends here