maven-central.el
changeset 1082 964519e5a5ac
child 1091 bbe864449f48
equal deleted inserted replaced
1081:37ddc669849c 1082:964519e5a5ac
       
     1 ;;; maven-central.el --- Maven central auxility
       
     2 
       
     3 ;;; Commentary:
       
     4 ;;
       
     5 
       
     6 (require 'url-handlers)
       
     7 
       
     8 ;;; Code:
       
     9 
       
    10 (defvar maven-central.buffer-name "*Maven Central*")
       
    11 
       
    12 (defconst maven-central.search-url "http://search.maven.org/solrsearch/select?wt=json&rows=20")
       
    13 
       
    14 (defun maven-central.callback1 (status)
       
    15   (let ( (buffer (current-buffer)) )
       
    16     (switch-to-buffer maven-central.buffer-name)
       
    17     (url-insert buffer)))
       
    18 
       
    19 (defun maven-central.versions-url (groupId artifactId)
       
    20   (concat maven-central.search-url "&core=gav&q=g:" (url-hexify-string groupId) (url-hexify-string " AND ") "a:" (url-hexify-string artifactId)))
       
    21 ;; (maven-central.versions-url "junit" "junit")
       
    22 
       
    23 (defun maven-central.versions-callback (status)
       
    24   (let ( (buffer (current-buffer)) json )
       
    25     (with-temp-buffer
       
    26       (url-insert buffer)
       
    27       (beginning-of-buffer)
       
    28       (setq json (json-read))
       
    29       ;; (switch-to-buffer maven-central.buffer-name)
       
    30       ;; (pp json)
       
    31       (message "Available versions: %s" (mapconcat (lambda (json) (cdr (assq 'v json))) (cdr (assq 'docs (cdr (assoc 'response json)))) ", "))
       
    32       )))
       
    33 
       
    34 (defun maven-central.versions (groupId artifactId)
       
    35   "Retrieve available versions."
       
    36   (url-retrieve (maven-central.versions-url groupId artifactId) #'maven-central.versions-callback))
       
    37 
       
    38 (defun maven-central.last-version-url (groupId artifactId)
       
    39   (concat maven-central.search-url "&q=g:" (url-hexify-string groupId) (url-hexify-string " AND ") "a:" (url-hexify-string artifactId)))
       
    40 ;; (maven-central.versions-url "junit" "junit")
       
    41 
       
    42 (defun maven-central.last-version-callback (status)
       
    43   (let ( (buffer (current-buffer)) json )
       
    44     (with-temp-buffer
       
    45       (url-insert buffer)
       
    46       (beginning-of-buffer)
       
    47       (setq json (json-read))
       
    48       ;; (switch-to-buffer maven-central.buffer-name)
       
    49       ;; (pp json)
       
    50       (message "Latest version: %s" (cdr (assq 'latestVersion (elt (cdr (assq 'docs (cdr (assoc 'response json)))) 0))))
       
    51       )))
       
    52 
       
    53 (defun maven-central.last-version (groupId artifactId)
       
    54   "Retrieve last package version."
       
    55   (url-retrieve (maven-central.last-version-url groupId artifactId) #'maven-central.last-version-callback))
       
    56 
       
    57 (defun maven-central.parse-pom-dependency-in-region (start end)
       
    58   (let (groupId artifactId version)
       
    59     (save-restriction
       
    60       (narrow-to-region start end)
       
    61       (goto-char start)
       
    62       (re-search-forward "<groupId>\\([^<]*\\)</groupId>" nil t)
       
    63       (setq groupId (match-string-no-properties 1))
       
    64       (goto-char start)
       
    65       (re-search-forward "<artifactId>\\([^<]*\\)</artifactId>" nil t)
       
    66       (setq artifactId (match-string-no-properties 1))
       
    67       (goto-char start)
       
    68       (re-search-forward "<version>\\([^<]*\\)</version>" nil t)
       
    69       (setq version (match-string-no-properties 1))
       
    70       (list groupId artifactId version)
       
    71       )))
       
    72 
       
    73 (defun maven-central.parse-pom-dependency (&optional point)
       
    74   (unless point
       
    75     (setq point (point)))
       
    76   (let (start end pos groupId artifactId version fs-o bs-o fs-c bs-c)
       
    77     (save-excursion
       
    78       (goto-char point)
       
    79       (unless (eq (char-after) ?<)
       
    80         (search-backward "<"))
       
    81       (setq point (point))
       
    82       (setq fs-o (re-search-forward "<\\(?:plugin\\|dependency\\)>" nil t))
       
    83       (when fs-o
       
    84         (setq fs-o (- fs-o 12)))
       
    85       (goto-char point)
       
    86       (setq bs-o (re-search-backward "<\\(?:plugin\\|dependency\\)>" nil t))
       
    87       (goto-char point)
       
    88       (setq fs-c (re-search-forward "</\\(?:plugin\\|dependency\\)>" nil t))
       
    89       (goto-char point)
       
    90       (setq bs-c (re-search-backward "</\\(?:plugin\\|dependency\\)>" nil t))
       
    91       (when bs-c
       
    92         (setq bs-c (+ bs-c 13)))
       
    93       (cond
       
    94        ((and fs-o (= fs-o point) fs-c)
       
    95         (maven-central.parse-pom-dependency-in-region fs-o fs-c))
       
    96        ((and bs-o (<= bs-o point) fs-c (<= point fs-c))
       
    97         (maven-central.parse-pom-dependency-in-region bs-o fs-c))
       
    98        ((and fs-o fs-c)
       
    99         (maven-central.parse-pom-dependency-in-region fs-o fs-c))
       
   100        ((and bs-o bs-c)
       
   101         (maven-central.parse-pom-dependency-in-region bs-o bs-c))
       
   102        (t
       
   103         (list nil nil nil)))
       
   104       )))
       
   105 
       
   106 (defun maven-central.parse-test (&optional point)
       
   107   (interactive)
       
   108   (pp (maven-central.parse-pom-dependency)) )
       
   109 
       
   110 ;;;###autoload 
       
   111 (defun maven-central.last-version-from-pom ()
       
   112   (interactive)
       
   113   (let (dependency groupId artifactId)
       
   114     (setq dependency (maven-central.parse-pom-dependency))
       
   115     (setq groupId (elt dependency 0))
       
   116     (setq artifactId (elt dependency 1))
       
   117     (when (and groupId artifactId)
       
   118       (maven-central.last-version groupId artifactId))
       
   119     ))
       
   120 
       
   121 ;;;###autoload 
       
   122 (defun maven-central.versions-from-pom ()
       
   123   (interactive)
       
   124   (let (dependency groupId artifactId)
       
   125     (setq dependency (maven-central.parse-pom-dependency))
       
   126     (setq groupId (elt dependency 0))
       
   127     (setq artifactId (elt dependency 1))
       
   128     (when (and groupId artifactId)
       
   129       (maven-central.versions groupId artifactId))
       
   130     ))
       
   131 
       
   132 ;; (maven-central.last-version "junit" "junit")
       
   133 
       
   134 (provide 'maven-central)
       
   135 
       
   136 (provide 'maven-central)
       
   137 
       
   138 ;;; maven-central.el ends here