abcl-asdf.lisp 5.83 KB
Newer Older
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 8 9 10 11 12
(defclass iri (component) 
  ((schema :initform nil)
   (authority :initform nil)
   (path :initform nil)
   (query :initform nil)
   (fragment :initform nil)))
13

14
(defclass mvn (iri) 
15 16
  ((group-id :initarg :group-id :initform nil)
   (artifact-id :initarg :artifact-id :initform nil)
17
   (repository :initform "http://repo1.maven.org/maven2/") ;;; XXX unimplmented
18 19
   (classname :initarg :classname :initform nil)
   (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 24 25

#+nil
(defmethod find-component ((component iri) path)
  component)
26

27

28
;;; We intercept compilation to ensure that load-op will succeed
29
(defmethod perform ((op compile-op) (c mvn))
30 31
  (abcl-asdf:resolve   
   (ensure-parsed-mvn c)))
32
     
33
(defmethod perform ((operation load-op) (c mvn))
34 35 36 37
  (let ((resolved-path 
         (abcl-asdf:resolve (ensure-parsed-mvn c))))
    (when (stringp resolved-path)
      (java:add-to-classpath (abcl-asdf:as-classpath resolved-path)))))
38 39 40

;;; A Maven URI has the form "mvn:group-id/artifact-id/version"
;;;
41 42 43 44
;;; Sometimes people write "group-id:artifact-id:version" to refer to
;;; Maven artifacts.  One can use ABCL-ASDF:RESOLVE directly for
;;; serialized references to artifacts of this form.
;;;
45
;;; Currently we "stuff" the group-id/artifact-id into the 'name' and
46 47 48
;;; use the component 'version' for the version.  Parts of ASDF 
;;; *reallY* want ASDF:VERSION to be a triple of intergers, and never
;;; anything more, so that is part of the motivation behind this effort.
49 50 51
(defparameter *mvn-repositories* nil
  "A list of all Maven repositories encountered in the lifetime of this instance of the implementation.")

52 53 54 55 56 57 58
#+nil
(defmethod slot-missing ((class mvn) object slot-name operation &optional new-value)
  (setf (slot-value object slot-name) 
        (if new-value
            new-value
            nil)))

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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
      (let ((parsed (abcl-asdf::split-string name "/"))
            (asdf-version-p (slot-boundp component 'version))
            (default-version "LATEST"))
        (cond ((= (length parsed) 3)
               (setf 
                group-id (first parsed)
                artifact-id (second parsed)
                version (third parsed)))
              ((= (length parsed) 2)
               (setf 
                group-id (first parsed)
                artifact-id (second parsed)
                version (if asdf-version-p
                            version
                            default-version)))
              (t
               (error "Failed to construct a mvn reference from name '~A' and version '~A'"
                      name version)))
        (setf schema "mvn")
83
        (pushnew repository *mvn-repositories*)
84
        ;;; Always normalized path "on the way out" to contain group-id/artifact-id/version
85 86 87 88 89
        (setf path (format nil "~A/~A/~A" group-id artifact-id version))))
    component))

(export `(mvn iri ensure-parsed-mvn
              group-id artifact-id version) 'asdf)
90 91 92 93 94 95

(defmethod source-file-type ((component iri) (system system))
  nil)

(defmethod component-relative-pathname ((component iri))
  nil)
96

97
(in-package #:abcl-asdf)
98

99
(defgeneric resolve (something)
100
 (:documentation "Returns a string in JVM CLASSPATH format as entries delimited by classpath separator string."))
101

102
(defmethod resolve ((mvn-component asdf::mvn))
103 104
  "Resolve all runtime dependencies of MVN-COMPONENT.

105 106 107 108
Returns either a string in jvm classpath format as entries delimited
by classpath separator string or T.  If the value T is returned, it
denotes that current JVM already has already loaded a given class. Can possibly be a
single entry denoting a remote binary artifact."
109 110 111 112 113 114 115 116
  (macrolet ((aif (something consequence alternative))
             `(let ((it ,(something)))
                (if it
                    consequence
                    alternative)))
    (let ((name (slot-value mvn-component 'asdf::name))
          (group-id (slot-value mvn-component 'asdf::group-id))
          (artifact-id (slot-value mvn-component 'asdf::artifact-id))
117 118
          (classname (slot-value mvn-component 'asdf::classname))
          (alternate-uri (slot-value mvn-component 'asdf::alternate-uri))
119 120 121 122 123 124
          (version (let ((it (slot-value mvn-component 'asdf::version)))
                     (cond
                       ((not it)
                        it)
                       (t 
                        "LATEST")))))
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
      (handler-case 
          (when (and classname 
                     (jss:find-java-class classname))
            (warn "Not loading ~A from the network because ~A is present in classpath."
                  name classname)
            (return-from resolve t))
        (java:java-exception (e)
          (unless (java:jinstance-of-p (java:java-exception-cause e)
                                  "java.lang.ClassNotFoundException")
            (error "Unexpected Java exception~&~A.~&" e))
          (if (find-mvn)
              (resolve-dependencies group-id artifact-id version)
              (if alternate-uri
                  (values (namestring alternate-uri) alternate-uri)
                  (t 
                   (error "Failed to resolve MVN component name ~A." name)))))))))
141
  
142
(defun as-classpath (classpath)
143
  "Break apart the JVM CLASSPATH string into a list of its consituents."
144 145
  (split-string classpath 
                (java:jfield "java.io.File" "pathSeparator")))
146

147 148 149 150
(defun split-string (string split-char)
  (loop :for i = 0 :then (1+ j)
     :as j = (position split-char string :test #'string-equal :start i)
     :collect (subseq string i j)
151
     :while j))