mylisp/maven-central.el
changeset 1666 06937ff1ec5f
parent 1599 665bd30beaae
equal deleted inserted replaced
1665:3685e2321a9b 1666:06937ff1ec5f
       
     1 ;;; maven-central.el --- Maven central auxility
       
     2 
       
     3 ;;; Commentary:
       
     4 ;;
       
     5 
       
     6 (require 'json)
       
     7 (require 'url-handlers)
       
     8 (require 'maven)
       
     9 
       
    10 ;;; Code:
       
    11 
       
    12 (defvar maven-central/buffer-name "*Maven Central*")
       
    13 
       
    14 (defconst maven-central/search-url "http://search.maven.org/solrsearch/select?wt=json&rows=20")
       
    15 
       
    16 (defun maven-central/versions-url (groupId artifactId)
       
    17   (concat maven-central/search-url "&core=gav&q=g:" (url-hexify-string groupId) (url-hexify-string " AND ") "a:" (url-hexify-string artifactId)))
       
    18 ;; (maven-central/versions-url "junit" "junit")
       
    19 
       
    20 (defun maven-central/versions-callback (status)
       
    21   (let ( (buffer (current-buffer)) json )
       
    22     (with-temp-buffer
       
    23       (url-insert buffer)
       
    24       (goto-char (point-min))
       
    25       (setq json (json-read))
       
    26       ;; (switch-to-buffer maven-central/buffer-name)
       
    27       ;; (pp json)
       
    28       (message "Available versions: %s" (mapconcat (lambda (json) (cdr (assq 'v json))) (cdr (assq 'docs (cdr (assoc 'response json)))) ", "))
       
    29       )))
       
    30 
       
    31 (defun maven-central/versions (groupId artifactId)
       
    32   "Retrieve available versions."
       
    33   (url-retrieve (maven-central/versions-url groupId artifactId) #'maven-central/versions-callback))
       
    34 
       
    35 (defun maven-central/last-version-url (groupId artifactId)
       
    36   (concat maven-central/search-url "&q=g:" (url-hexify-string groupId) (url-hexify-string " AND ") "a:" (url-hexify-string artifactId)))
       
    37 ;; (maven-central/versions-url "junit" "junit")
       
    38 
       
    39 (defun maven-central/last-version-callback (status)
       
    40   (let ( (buffer (current-buffer)) json )
       
    41     (with-temp-buffer
       
    42       (url-insert buffer)
       
    43       (goto-char (point-min))
       
    44       (setq json (json-read))
       
    45       ;; (switch-to-buffer maven-central/buffer-name)
       
    46       ;; (pp json)
       
    47       (message "Latest version: %s" (cdr (assq 'latestVersion (elt (cdr (assq 'docs (cdr (assoc 'response json)))) 0))))
       
    48       )))
       
    49 
       
    50 (defun maven-central/last-version (groupId artifactId)
       
    51   "Retrieve last package version."
       
    52   (url-retrieve (maven-central/last-version-url groupId artifactId) #'maven-central/last-version-callback))
       
    53 
       
    54 ;;;###autoload
       
    55 (defun maven-central-last-version-from-pom ()
       
    56   (interactive)
       
    57   (let (dependency groupId artifactId)
       
    58     (setq dependency (maven/parse-pom-dependency))
       
    59     (setq groupId (elt dependency 0))
       
    60     (setq artifactId (elt dependency 1))
       
    61     (when (and groupId artifactId)
       
    62       (maven-central/last-version groupId artifactId))
       
    63     ))
       
    64 
       
    65 ;;;###autoload
       
    66 (defun maven-central-versions-from-pom ()
       
    67   (interactive)
       
    68   (let (dependency groupId artifactId)
       
    69     (setq dependency (maven/parse-pom-dependency))
       
    70     (setq groupId (elt dependency 0))
       
    71     (setq artifactId (elt dependency 1))
       
    72     (when (and groupId artifactId)
       
    73       (maven-central/versions groupId artifactId))
       
    74     ))
       
    75 
       
    76 (defun ivy/parse-dependency ()
       
    77   (let ( end groupId artifactId )
       
    78     (save-excursion
       
    79       (if (looking-at "[^>]*<dependency ")
       
    80           (progn (search-forward "<") (backward-char))
       
    81         (search-backward "<dependency " nil t))
       
    82       (when (re-search-forward "<dependency [^/]*/>")
       
    83         (setq end (point))
       
    84         (search-backward "<")
       
    85         (save-restriction
       
    86           (narrow-to-region (point) end)
       
    87           (goto-char (point-min))
       
    88           (when (re-search-forward "\\s-org\\s-*=\\s-*['\"]\\([^'\"]+\\)['\"]" nil t)
       
    89             (setq groupId (match-string 1)))
       
    90           (goto-char (point-min))
       
    91           (when (re-search-forward "\\s-name\\s-*=\\s-*['\"]\\([^'\"]+\\)['\"]" nil t)
       
    92             (setq artifactId (match-string 1)))
       
    93           )))
       
    94     (list groupId artifactId)))
       
    95 
       
    96 ;;;###autoload
       
    97 (defun maven-central-last-version-from-ivy ()
       
    98   (interactive)
       
    99   (let (dependency groupId artifactId)
       
   100     (setq dependency (ivy/parse-dependency))
       
   101     (setq groupId (elt dependency 0))
       
   102     (setq artifactId (elt dependency 1))
       
   103     (when (and groupId artifactId)
       
   104       (maven-central/last-version groupId artifactId))
       
   105     ))
       
   106 
       
   107 ;;;###autoload
       
   108 (defun maven-central-versions-from-ivy ()
       
   109   (interactive)
       
   110   (let (dependency groupId artifactId)
       
   111     (setq dependency (ivy/parse-dependency))
       
   112     (setq groupId (elt dependency 0))
       
   113     (setq artifactId (elt dependency 1))
       
   114     (when (and groupId artifactId)
       
   115       (maven-central/versions groupId artifactId))
       
   116     ))
       
   117 
       
   118 (defun maven-central/parse-gradle-dependency ()
       
   119   (let ( groupId artifactId )
       
   120     (save-excursion
       
   121       (goto-char (line-beginning-position))
       
   122       (when (re-search-forward "\\([\"']\\)\\([a-zA-Z0-9.-]+\\):\\([a-zA-Z0-9.-]+\\):?[^\"']*\\1" (line-end-position) t)
       
   123         (setq groupId (match-string 2))
       
   124         (setq artifactId (match-string 3))
       
   125         (list groupId artifactId)))))
       
   126 
       
   127 ;;;###autoload
       
   128 (defun maven-central-last-version-from-gradle ()
       
   129   (interactive)
       
   130   (let (dependency groupId artifactId)
       
   131     (setq dependency (maven-central/parse-gradle-dependency))
       
   132     (setq groupId (elt dependency 0))
       
   133     (setq artifactId (elt dependency 1))
       
   134     (when (and groupId artifactId)
       
   135       (maven-central/last-version groupId artifactId))
       
   136     ))
       
   137 
       
   138 ;;;###autoload
       
   139 (defun maven-central-versions-from-gradle ()
       
   140   (interactive)
       
   141   (let (dependency groupId artifactId)
       
   142     (setq dependency (maven-central/parse-gradle-dependency))
       
   143     (setq groupId (elt dependency 0))
       
   144     (setq artifactId (elt dependency 1))
       
   145     (when (and groupId artifactId)
       
   146       (maven-central/versions groupId artifactId))
       
   147     ))
       
   148 
       
   149 (defun maven-central/open (groupId artifactId)
       
   150   "Open in Maven Central WEB page info for package."
       
   151   (browse-url (format "https://mvnrepository.com/artifact/%s/%s" groupId artifactId)))
       
   152 
       
   153 ;;;###autoload
       
   154 (defun maven-central-open-from-gradle ()
       
   155   (interactive)
       
   156   (let (dependency groupId artifactId)
       
   157     (setq dependency (maven-central/parse-gradle-dependency))
       
   158     (setq groupId (elt dependency 0))
       
   159     (setq artifactId (elt dependency 1))
       
   160     (when (and groupId artifactId)
       
   161       (maven-central/open groupId artifactId))
       
   162     ))
       
   163 
       
   164 ;; (maven-central/last-version "junit" "junit")
       
   165 
       
   166 (provide 'maven-central)
       
   167 
       
   168 ;;; maven-central.el ends here