maven-central.el
changeset 1508 fad1a57bf25f
parent 1302 82d6e8bd0861
child 1509 f9b34ef28954
equal deleted inserted replaced
1507:6f939639c52a 1508:fad1a57bf25f
     3 ;;; Commentary:
     3 ;;; Commentary:
     4 ;;
     4 ;;
     5 
     5 
     6 (require 'json)
     6 (require 'json)
     7 (require 'url-handlers)
     7 (require 'url-handlers)
       
     8 (require 'maven)
     8 
     9 
     9 ;;; Code:
    10 ;;; Code:
    10 
    11 
    11 (defvar maven-central.buffer-name "*Maven Central*")
    12 (defvar maven-central.buffer-name "*Maven Central*")
    12 
    13 
    48 
    49 
    49 (defun maven-central.last-version (groupId artifactId)
    50 (defun maven-central.last-version (groupId artifactId)
    50   "Retrieve last package version."
    51   "Retrieve last package version."
    51   (url-retrieve (maven-central.last-version-url groupId artifactId) #'maven-central.last-version-callback))
    52   (url-retrieve (maven-central.last-version-url groupId artifactId) #'maven-central.last-version-callback))
    52 
    53 
    53 (defun maven-central.parse-pom-dependency-in-region (start end)
       
    54   (let (groupId artifactId version)
       
    55     (save-restriction
       
    56       (narrow-to-region start end)
       
    57       (goto-char start)
       
    58       (re-search-forward "<groupId>\\([^<]*\\)</groupId>" nil t)
       
    59       (setq groupId (match-string-no-properties 1))
       
    60       (goto-char start)
       
    61       (re-search-forward "<artifactId>\\([^<]*\\)</artifactId>" nil t)
       
    62       (setq artifactId (match-string-no-properties 1))
       
    63       (goto-char start)
       
    64       (re-search-forward "<version>\\([^<]*\\)</version>" nil t)
       
    65       (setq version (match-string-no-properties 1))
       
    66       (list groupId artifactId version)
       
    67       )))
       
    68 
       
    69 (defun maven-central.parse-pom-dependency (&optional point)
       
    70   (unless point
       
    71     (setq point (point)))
       
    72   (let (start end pos groupId artifactId version fs-o bs-o fs-c bs-c)
       
    73     (save-excursion
       
    74       (goto-char point)
       
    75       (unless (eq (char-after) ?<)
       
    76         (search-backward "<"))
       
    77       (setq point (point))
       
    78       (setq fs-o (re-search-forward "<\\(?:plugin\\|dependency\\)>" nil t))
       
    79       (when fs-o
       
    80         (setq fs-o (- fs-o 12)))
       
    81       (goto-char point)
       
    82       (setq bs-o (re-search-backward "<\\(?:plugin\\|dependency\\)>" nil t))
       
    83       (goto-char point)
       
    84       (setq fs-c (re-search-forward "</\\(?:plugin\\|dependency\\)>" nil t))
       
    85       (goto-char point)
       
    86       (setq bs-c (re-search-backward "</\\(?:plugin\\|dependency\\)>" nil t))
       
    87       (when bs-c
       
    88         (setq bs-c (+ bs-c 13)))
       
    89       (cond
       
    90        ((and fs-o (= fs-o point) fs-c)
       
    91         (maven-central.parse-pom-dependency-in-region fs-o fs-c))
       
    92        ((and bs-o (<= bs-o point) fs-c (<= point fs-c))
       
    93         (maven-central.parse-pom-dependency-in-region bs-o fs-c))
       
    94        ((and fs-o fs-c)
       
    95         (maven-central.parse-pom-dependency-in-region fs-o fs-c))
       
    96        ((and bs-o bs-c)
       
    97         (maven-central.parse-pom-dependency-in-region bs-o bs-c))
       
    98        (t
       
    99         (list nil nil nil)))
       
   100       )))
       
   101 ;; (pp (maven-central.parse-pom-dependency))
       
   102 
       
   103 ;;;###autoload
    54 ;;;###autoload
   104 (defun maven-central.last-version-from-pom ()
    55 (defun maven-central.last-version-from-pom ()
   105   (interactive)
    56   (interactive)
   106   (let (dependency groupId artifactId)
    57   (let (dependency groupId artifactId)
   107     (setq dependency (maven-central.parse-pom-dependency))
    58     (setq dependency (maven.parse-pom-dependency))
   108     (setq groupId (elt dependency 0))
    59     (setq groupId (elt dependency 0))
   109     (setq artifactId (elt dependency 1))
    60     (setq artifactId (elt dependency 1))
   110     (when (and groupId artifactId)
    61     (when (and groupId artifactId)
   111       (maven-central.last-version groupId artifactId))
    62       (maven-central.last-version groupId artifactId))
   112     ))
    63     ))
   113 
    64 
   114 ;;;###autoload
    65 ;;;###autoload
   115 (defun maven-central.versions-from-pom ()
    66 (defun maven-central.versions-from-pom ()
   116   (interactive)
    67   (interactive)
   117   (let (dependency groupId artifactId)
    68   (let (dependency groupId artifactId)
   118     (setq dependency (maven-central.parse-pom-dependency))
    69     (setq dependency (maven.parse-pom-dependency))
   119     (setq groupId (elt dependency 0))
    70     (setq groupId (elt dependency 0))
   120     (setq artifactId (elt dependency 1))
    71     (setq artifactId (elt dependency 1))
   121     (when (and groupId artifactId)
    72     (when (and groupId artifactId)
   122       (maven-central.versions groupId artifactId))
    73       (maven-central.versions groupId artifactId))
   123     ))
    74     ))
   124 
       
   125 (defvar maven.command "mvn")
       
   126 
       
   127 (defvar maven.help-buffer-name "*Maven Help*")
       
   128 
       
   129 ;;;###autoload
       
   130 (defun maven.help ()
       
   131   "Run help:describe for plugin at point."
       
   132   (interactive)
       
   133   (let (dependency groupId artifactId)
       
   134     (setq dependency (maven-central.parse-pom-dependency))
       
   135     (setq groupId (elt dependency 0))
       
   136     (setq artifactId (elt dependency 1))
       
   137     (if (not (and groupId artifactId))
       
   138         (message "Can't find `groupId' or `artifactId'")
       
   139       (shell-command
       
   140        (format "%s help:describe -Ddetail -DgroupId=%s -DartifactId=%s" maven.command groupId artifactId)
       
   141        (switch-to-buffer maven.help-buffer-name)) )
       
   142     ))
       
   143 
       
   144 ;;;###autoload
       
   145 (defun maven.effective-pom ()
       
   146   "Run help:effective-pom for plugin at point."
       
   147   (interactive)
       
   148   (shell-command
       
   149    (format "%s help:effective-pom" maven.command)
       
   150    (switch-to-buffer maven.help-buffer-name)) )
       
   151 
       
   152 ;;;###autoload
       
   153 (defun maven.effective-settings ()
       
   154   "Run help:effective-settings for plugin at point."
       
   155   (interactive)
       
   156   (shell-command
       
   157    (format "%s help:effective-settings" maven.command)
       
   158    (switch-to-buffer maven.help-buffer-name)) )
       
   159 
       
   160 ;;;###autoload
       
   161 (defun maven.dependency-tree ()
       
   162   "Run dependency:tree for plugin at point."
       
   163   (interactive)
       
   164   (shell-command
       
   165    (format "%s dependency:tree" maven.command)
       
   166    (switch-to-buffer maven.help-buffer-name)) )
       
   167 
       
   168 (defun maven.project-root ()
       
   169   "Root of project."
       
   170   (let ( (dir default-directory) (found nil) )
       
   171     (while (and (not found) (> (length dir) 5))
       
   172       (when (file-exists-p (concat dir "/pom.xml"))
       
   173         (setq found dir))
       
   174       (setq dir (expand-file-name (concat dir "/.."))))
       
   175     found))
       
   176 
       
   177 (defun maven.file-package ()
       
   178   "Return file package."
       
   179   (save-excursion
       
   180     (goto-char (point-min))
       
   181     (let (pkg cls)
       
   182       (re-search-forward "package +\\([[:alnum:]_.]+\\) *;" nil t)
       
   183       (setq pkg (match-string-no-properties 1))
       
   184       (re-search-forward "class\\s +\\([[:alnum:]_]+\\)\\(\\s \\|\n\\|implements [^{]*\\|extents [^{]*\\)*{" nil t)
       
   185       (setq cls (match-string-no-properties 1))
       
   186       (when (and pkg cls)
       
   187         (concat pkg "." cls))) ))
       
   188 
       
   189 ;;;###autoload
       
   190 (defun maven.run-file ()
       
   191   "Run exec:java for current file."
       
   192   (interactive)
       
   193   (let* ( (default-directory (concat (maven.project-root) "/")) )
       
   194     (if (not default-directory)
       
   195         (message "Can't find maven project root")
       
   196       (compilation-start
       
   197        (format "%s exec:java -Dexec.mainClass=%s" maven.command (maven.file-package))))))
       
   198 
       
   199 ;;;###autoload
       
   200 (defun maven.run-test ()
       
   201   "Run test -Dtest=... for current file."
       
   202   (interactive)
       
   203   (let* ( (default-directory (concat (maven.project-root) "/")) )
       
   204     (if (not default-directory)
       
   205         (message "Can't find maven project root")
       
   206       (compilation-start
       
   207        (format "%s test -Dtest=%s" maven.command (maven.file-package))))))
       
   208 
    75 
   209 ;; (maven-central.last-version "junit" "junit")
    76 ;; (maven-central.last-version "junit" "junit")
   210 
    77 
   211 (provide 'maven-central)
    78 (provide 'maven-central)
   212 
    79