source: tags/1.3.0/contrib/abcl-asdf/abcl-asdf.lisp

Last change on this file was 14631, checked in by Mark Evenson, 10 years ago

abcl-asdf: cache Maven dependency resolution to avoid repeated lookups.

Instead of calling ABCL-ASDF:RESOLVE in both the ASDF COMPILE-OP and
LOAD-OP, we now cache the result of invocation in COMPILE-OP and add
this value in the LOAD-OP phase.

Contributed by Cyrus Harmon.

File size: 5.8 KB
Line 
1;;;; The ABCL specific overrides in ASDF. 
2;;;;
3;;;; Done separately from asdf.lisp for stability.
4(require :asdf)
5(in-package :asdf)
6
7(defclass iri (component) 
8  ((schema :initform nil)
9   (authority :initform nil)
10   (path :initform nil)
11   (query :initform nil)
12   (fragment :initform nil)))
13
14(defclass mvn (iri) 
15  ((group-id :initarg :group-id :initform nil)
16   (artifact-id :initarg :artifact-id :initform nil)
17   (repository :initform "http://repo1.maven.org/maven2/") ;;; XXX unimplemented
18   (resolved-classpath :initform nil :accessor resolved-classpath)
19   (classname :initarg :classname :initform nil)
20   (alternate-uri :initarg :alternate-uri :initform nil)
21   ;; inherited from ASDF:COMPONENT ??? what are the CL semantics on overriding -- ME 2012-04-01
22   #+nil   (version :initform nil)))
23
24#+nil
25(defmethod find-component ((component iri) path)
26  component)
27
28
29;;; We intercept compilation to ensure that load-op will succeed
30(defmethod perform ((op compile-op) (c mvn))
31  (unless (resolved-classpath c)
32    (setf (resolved-classpath c)
33          (abcl-asdf:resolve   
34           (ensure-parsed-mvn c)))))
35
36(defmethod perform ((operation load-op) (c mvn))
37  (let ((resolved-classpath (resolved-classpath c)))
38    (when (stringp resolved-classpath)
39      (java:add-to-classpath (abcl-asdf:as-classpath resolved-classpath)))))
40
41;;; A Maven URI has the form "mvn:group-id/artifact-id/version"
42;;;
43;;; Sometimes people write "group-id:artifact-id:version" to refer to
44;;; Maven artifacts.  One can use ABCL-ASDF:RESOLVE directly for
45;;; serialized references to artifacts of this form.
46;;;
47;;; Currently we "stuff" the group-id/artifact-id into the 'name' and
48;;; use the component 'version' for the version.  Parts of ASDF
49;;; *reallY* want ASDF:VERSION to be a triple of intergers, and never
50;;; anything more, so that is part of the motivation behind this effort.
51(defparameter *mvn-repositories* nil
52  "A list of all Maven repositories encountered in the lifetime of this instance of the implementation.")
53
54#+nil
55(defmethod slot-missing ((class mvn) object slot-name operation &optional new-value)
56  (setf (slot-value object slot-name) 
57        (if new-value
58            new-value
59            nil)))
60
61(defun ensure-parsed-mvn (component)
62  (with-slots (name group-id artifact-id
63                    version schema path repository) 
64      component
65    (when (null asdf::artifact-id) 
66      (let ((parsed (abcl-asdf::split-string name "/"))
67            (asdf-version-p (slot-boundp component 'version))
68            (default-version "LATEST"))
69        (cond ((= (length parsed) 3)
70               (setf 
71                group-id (first parsed)
72                artifact-id (second parsed)
73                version (third parsed)))
74              ((= (length parsed) 2)
75               (setf 
76                group-id (first parsed)
77                artifact-id (second parsed)
78                version (if asdf-version-p
79                            version
80                            default-version)))
81              (t
82               (error "Failed to construct a mvn reference from name '~A' and version '~A'"
83                      name version)))
84        (setf schema "mvn")
85        (pushnew repository *mvn-repositories*)
86        ;;; Always normalized path "on the way out" to contain group-id/artifact-id/version
87        (setf path (format nil "~A/~A/~A" group-id artifact-id version))))
88    component))
89
90(eval-when (:compile-toplevel :load-toplevel :execute)
91  (export `(mvn iri ensure-parsed-mvn group-id artifact-id version) 
92          'asdf))
93
94(defmethod source-file-type ((component iri) (system system))
95  nil)
96
97(defmethod component-relative-pathname ((component iri))
98  nil)
99
100(in-package #:abcl-asdf)
101
102(defgeneric resolve (something)
103  (:documentation "Returns a string in JVM CLASSPATH format as entries delimited by classpath separator string."))
104
105(defmethod resolve ((mvn-component asdf::mvn))
106  "Resolve all runtime dependencies of MVN-COMPONENT.
107
108Returns either a string in jvm classpath format as entries delimited
109by classpath separator string or T.  If the value T is returned, it
110denotes that current JVM already has already loaded a given class. Can possibly be a
111single entry denoting a remote binary artifact."
112  (asdf:ensure-parsed-mvn mvn-component)
113  (let ((name (slot-value mvn-component 'asdf::name))
114        (group-id (slot-value mvn-component 'asdf::group-id))
115        (artifact-id (slot-value mvn-component 'asdf::artifact-id))
116        (classname (slot-value mvn-component 'asdf::classname))
117        (alternate-uri (slot-value mvn-component 'asdf::alternate-uri))
118        (version (if (slot-value mvn-component 'asdf::version)
119                     (slot-value mvn-component 'asdf::version)
120                     "LATEST")))
121    (handler-case 
122        (when (and classname 
123                   (jss:find-java-class classname))
124          (warn "Not loading ~A from the network because ~A is present in classpath."
125                name classname)
126          (return-from resolve t))
127      (java:java-exception (e)
128        (unless (java:jinstance-of-p (java:java-exception-cause e)
129                                     "java.lang.ClassNotFoundException")
130          (error "Unexpected Java exception~&~A.~&" e))))
131    (if (find-mvn)
132        (resolve-dependencies group-id artifact-id version)
133        (if alternate-uri
134            (values (namestring alternate-uri) alternate-uri) 
135      (error "Failed to resolve MVN component name ~A." name)))))
136
137(defmethod resolve ((uri pathname))
138  (warn "Unimplemented."))
139
140(defun as-classpath (classpath)
141  "Break apart the JVM CLASSPATH string into a list of its consituents."
142  (split-string classpath 
143                (java:jfield "java.io.File" "pathSeparator")))
144
145(defun split-string (string split-char)
146  (loop :for i = 0 :then (1+ j)
147     :as j = (position split-char string :test #'string-equal :start i)
148     :collect (subseq string i j)
149     :while j))
Note: See TracBrowser for help on using the repository browser.