forked from skeeto/javadoc-lookup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maven-fetch.el
93 lines (75 loc) · 3.54 KB
/
maven-fetch.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
;;; maven-fetch.el --- Fetch Javadoc artifacts from the Maven repository
;; This is free and unencumbered software released into the public domain.
;;; Commentary:
;; This code adds support to javadoc-lookup by automatically fetching
;; and indexing documentation artifacts from the Maven repository. It
;; requires Maven and the command-line unzip utility installed on your
;; system. If these are not in your $PATH, the variables
;; `maven-program-name' and `unzip-program-name' will need to be set.
;; An artifact is specified by a sequence (list or vector) of three
;; strings: [groupId artifactId version]. For example,
;; ["org.apache.commons" "commons-math3" "3.0"]
;; In your Emacs initialization file, call `javadoc-add-artifact' with
;; your desired artifacts. They are only downloaded, unzipped, and
;; indexed -- a slow process -- the very first time. Each startup
;; after that, Emacs will work entirely from a cache (fast).
;;; Code:
(require 'cl)
(require 'javadoc-lookup)
(defcustom maven-program-name "mvn"
"Path to the Maven executable."
:group 'external)
(defcustom unzip-program-name "unzip"
"Path to the unzip executable."
:group 'external)
(defvar maven-fetch-command
"org.apache.maven.plugins:maven-dependency-plugin:2.6:get"
"Long command name for the Maven dependency fetch plugin.")
(defun maven-fetch-artifact-jar (artifact)
"Return the cache filename for ARTIFACT."
(let ((file (format "%s-%s-javadoc.jar" (elt artifact 1) (elt artifact 2))))
(expand-file-name file (jdl/dir-truename javadoc-lookup-cache-dir))))
(defun maven-fetch-unpacked-path (artifact)
"Return the unpacked directory name for ARTIFACT."
(expand-file-name (format "%s/%s" (elt artifact 0) (elt artifact 1))
(jdl/dir-truename javadoc-lookup-cache-dir)))
(defun* maven-fetch (artifact)
"Use maven to fetch ARTIFACT into the cache directory,
returning true on success."
(let ((jarfile (maven-fetch-artifact-jar artifact)))
(if (file-exists-p jarfile)
(format "Artifact %s already downloaded" artifact)
(let ((artifact-arg "-Dartifact=%s:%s:%s:javadoc"))
(message "Maven is fetching %s ..." artifact)
(zerop
(call-process maven-program-name nil nil nil
maven-fetch-command
(format "-Ddest=%s" jarfile)
(apply #'format artifact-arg
(coerce artifact 'list))))))))
(defun maven-fetch-unpack (artifact)
"Unpack an artifact in the javadoc-lookup cache directory,
returning the destination directory. Throws an error on any failure."
(let* ((jarfile (maven-fetch-artifact-jar artifact))
(destdir (maven-fetch-unpacked-path artifact)))
(if (not (file-exists-p jarfile))
(error "Could not find artifact jar file: %s." jarfile)
(if (file-exists-p destdir)
destdir
(message "Unpacking %s ..." (file-name-nondirectory jarfile))
(mkdir destdir t)
(if (call-process unzip-program-name nil nil nil jarfile "-d" destdir)
destdir
(delete-directory destdir t)
(error "Failed to unpack %s" jarfile))))))
;;;###autoload
(defun javadoc-add-artifacts (&rest artifacts)
"Add Maven repository artifacts to the javadoc-lookup index.
An artifact is specified by a sequence of three strings:
[groupId artifactId version]."
(dolist (artifact artifacts)
(if (maven-fetch artifact)
(javadoc-add-roots (maven-fetch-unpack artifact))
(error "Failed to fetch %s" artifact))))
(provide 'maven-fetch)
;;; maven-fetch.el ends here