abcl-asdf.lisp 5.66 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
21
   ;; inherited from ASDF:COMPONENT ??? what are the CL semantics on overriding -- ME 2012-04-01
   #+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
        (setf path (format nil "~A/~A/~A" group-id artifact-id version))))
    component))

88
89
90
(eval-when (:compile-toplevel :load-toplevel :execute)
  (export `(mvn iri ensure-parsed-mvn group-id artifact-id version) 
          'asdf))
91
92
93
94
95
96

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

(defmethod component-relative-pathname ((component iri))
  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
105
  "Resolve all runtime dependencies of MVN-COMPONENT.

106
107
108
109
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."
110
  (asdf:ensure-parsed-mvn mvn-component)
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  (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))
        (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)
        (resolve-dependencies group-id artifact-id version)
        (if alternate-uri
132
133
            (values (namestring alternate-uri) alternate-uri) 
	    (error "Failed to resolve MVN component name ~A." name)))))
134

135
136
137
(defmethod resolve ((uri pathname))
  (warn "Unimplemented."))

138
(defun as-classpath (classpath)
139
  "Break apart the JVM CLASSPATH string into a list of its consituents."
140
141
  (split-string classpath 
                (java:jfield "java.io.File" "pathSeparator")))
142

143
144
145
146
(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)
147
     :while j))