source: trunk/abcl/contrib/abcl-asdf/abcl-asdf.lisp @ 14629

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

ASDF:ENSURE-PARSED-MVN should be exported at compile time.

Contributed by Cyrus Harmon.

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