abcl-asdf.lisp 6.09 KB
Newer Older
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 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 :initarg :repository :initform "http://repo1.maven.org/maven2/") ;;; XXX unimplemented
18
   (resolved-classpath :initform nil :accessor resolved-classpath)
19 20
   (classname :initarg :classname :initform nil)
   (alternate-uri :initarg :alternate-uri :initform nil)
21 22
   ;; inherited from ASDF:COMPONENT ??? what are the CL semantics on overriding -- ME 2012-04-01
   #+nil   (version :initform nil)))
23 24 25 26

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

28

29
;;; We intercept compilation to ensure that load-op will succeed
30
(defmethod perform ((op compile-op) (c mvn))
31 32 33 34
  (unless (resolved-classpath c)
    (setf (resolved-classpath c)
          (abcl-asdf:resolve   
           (ensure-parsed-mvn c)))))
35

36
(defmethod perform ((operation load-op) (c mvn))
37 38 39
  (let ((resolved-classpath (resolved-classpath c)))
    (when (stringp resolved-classpath)
      (java:add-to-classpath (abcl-asdf:as-classpath resolved-classpath)))))
40 41 42

;;; A Maven URI has the form "mvn:group-id/artifact-id/version"
;;;
43 44 45 46
;;; 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.
;;;
47
;;; Currently we "stuff" the group-id/artifact-id into the 'name' and
48 49 50
;;; 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.
51 52 53
(defparameter *mvn-repositories* nil
  "A list of all Maven repositories encountered in the lifetime of this instance of the implementation.")

54 55 56 57 58 59 60
#+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)))

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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
      (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")
85 86
        (when repository
          (pushnew repository *mvn-repositories*))
87 88 89
        ;;; Always set path to normalized path "on the way out" to
        ;;; contain group-id/artifact-id/version
        ;;; TODO? record repository as well in path of component
90 91 92
        (setf path (format nil "~A/~A/~A" group-id artifact-id version))))
    component))

93 94 95
(eval-when (:compile-toplevel :load-toplevel :execute)
  (export `(mvn iri ensure-parsed-mvn group-id artifact-id version) 
          'asdf))
96 97 98 99 100 101

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

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

103
(in-package #:abcl-asdf)
104

105
(defgeneric resolve (something)
106
  (:documentation "Returns a string in JVM CLASSPATH format as entries delimited by classpath separator string."))
107

108
(defmethod resolve ((mvn-component asdf::mvn))
109 110
  "Resolve all runtime dependencies of MVN-COMPONENT.

111 112 113 114
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."
115
  (asdf:ensure-parsed-mvn mvn-component)
116 117 118 119 120
  (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))
        (classname (slot-value mvn-component 'asdf::classname))
        (alternate-uri (slot-value mvn-component 'asdf::alternate-uri))
121
        (repository (slot-value mvn-component 'asdf::repository))
122 123 124 125 126 127 128 129 130 131 132 133 134 135
        (version (if (slot-value mvn-component 'asdf::version)
                     (slot-value mvn-component 'asdf::version)
                     "LATEST")))
    (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)
136 137 138
        (resolve-dependencies group-id artifact-id
                              :version version
                              :repository repository)
139
        (if alternate-uri
140
            (values (pathname alternate-uri) alternate-uri) 
141
            (error "Failed to resolve MVN component name ~A." name)))))
142

143 144 145
(defmethod resolve ((uri pathname))
  (warn "Unimplemented."))

146
(defun as-classpath (classpath)
147
  "Break apart the JVM CLASSPATH string into a list of its consituents."
148 149
  (split-string classpath 
                (java:jfield "java.io.File" "pathSeparator")))
150

151 152 153 154
(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)
155
     :while j))