mylisp/maven-central.el
changeset 1666 06937ff1ec5f
parent 1599 665bd30beaae
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mylisp/maven-central.el	Sat Jan 02 00:33:04 2021 +0200
@@ -0,0 +1,168 @@
+;;; maven-central.el --- Maven central auxility
+
+;;; Commentary:
+;;
+
+(require 'json)
+(require 'url-handlers)
+(require 'maven)
+
+;;; 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/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))
+
+;;;###autoload
+(defun maven-central-last-version-from-pom ()
+  (interactive)
+  (let (dependency groupId artifactId)
+    (setq dependency (maven/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/parse-pom-dependency))
+    (setq groupId (elt dependency 0))
+    (setq artifactId (elt dependency 1))
+    (when (and groupId artifactId)
+      (maven-central/versions groupId artifactId))
+    ))
+
+(defun ivy/parse-dependency ()
+  (let ( end groupId artifactId )
+    (save-excursion
+      (if (looking-at "[^>]*<dependency ")
+          (progn (search-forward "<") (backward-char))
+        (search-backward "<dependency " nil t))
+      (when (re-search-forward "<dependency [^/]*/>")
+        (setq end (point))
+        (search-backward "<")
+        (save-restriction
+          (narrow-to-region (point) end)
+          (goto-char (point-min))
+          (when (re-search-forward "\\s-org\\s-*=\\s-*['\"]\\([^'\"]+\\)['\"]" nil t)
+            (setq groupId (match-string 1)))
+          (goto-char (point-min))
+          (when (re-search-forward "\\s-name\\s-*=\\s-*['\"]\\([^'\"]+\\)['\"]" nil t)
+            (setq artifactId (match-string 1)))
+          )))
+    (list groupId artifactId)))
+
+;;;###autoload
+(defun maven-central-last-version-from-ivy ()
+  (interactive)
+  (let (dependency groupId artifactId)
+    (setq dependency (ivy/parse-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-ivy ()
+  (interactive)
+  (let (dependency groupId artifactId)
+    (setq dependency (ivy/parse-dependency))
+    (setq groupId (elt dependency 0))
+    (setq artifactId (elt dependency 1))
+    (when (and groupId artifactId)
+      (maven-central/versions groupId artifactId))
+    ))
+
+(defun maven-central/parse-gradle-dependency ()
+  (let ( groupId artifactId )
+    (save-excursion
+      (goto-char (line-beginning-position))
+      (when (re-search-forward "\\([\"']\\)\\([a-zA-Z0-9.-]+\\):\\([a-zA-Z0-9.-]+\\):?[^\"']*\\1" (line-end-position) t)
+        (setq groupId (match-string 2))
+        (setq artifactId (match-string 3))
+        (list groupId artifactId)))))
+
+;;;###autoload
+(defun maven-central-last-version-from-gradle ()
+  (interactive)
+  (let (dependency groupId artifactId)
+    (setq dependency (maven-central/parse-gradle-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-gradle ()
+  (interactive)
+  (let (dependency groupId artifactId)
+    (setq dependency (maven-central/parse-gradle-dependency))
+    (setq groupId (elt dependency 0))
+    (setq artifactId (elt dependency 1))
+    (when (and groupId artifactId)
+      (maven-central/versions groupId artifactId))
+    ))
+
+(defun maven-central/open (groupId artifactId)
+  "Open in Maven Central WEB page info for package."
+  (browse-url (format "https://mvnrepository.com/artifact/%s/%s" groupId artifactId)))
+
+;;;###autoload
+(defun maven-central-open-from-gradle ()
+  (interactive)
+  (let (dependency groupId artifactId)
+    (setq dependency (maven-central/parse-gradle-dependency))
+    (setq groupId (elt dependency 0))
+    (setq artifactId (elt dependency 1))
+    (when (and groupId artifactId)
+      (maven-central/open groupId artifactId))
+    ))
+
+;; (maven-central/last-version "junit" "junit")
+
+(provide 'maven-central)
+
+;;; maven-central.el ends here