el-get-core.el 14.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
;;; el-get-core.el --- Manage the external elisp bits and pieces you depend upon
;;
;; Copyright (C) 2010-2011 Dimitri Fontaine
;;
;; Author: Dimitri Fontaine <dim@tapoueh.org>
;; URL: http://www.emacswiki.org/emacs/el-get
;; GIT: https://github.com/dimitri/el-get
;; Licence: WTFPL, grab your copy here: http://sam.zoy.org/wtfpl/
;;
;; This file is NOT part of GNU Emacs.
;;
;; Install
;;     Please see the README.asciidoc file from the same distribution

;;; Commentary:
;;
;; el-get-core provides basic el-get API, intended for developpers of el-get
;; and its methods.  See the methods directory for implementation of them.
;;

(require 'dired)
(require 'cl)            ; needed for `remove-duplicates'
(require 'simple)        ; needed for `apply-partially'
(require 'bytecomp)
(require 'autoload)

(defun el-get-verbose-message (format &rest arguments)
  (when el-get-verbose (apply 'message format arguments)))

30 31 32 33

;;
;; el-get-methods support, those are like backends.
;;
34 35 36 37 38
(defvar el-get-methods nil
  "Register methods that el-get can use to fetch and update a given package.

The methods list is a PLIST, each entry has a method name
property which value is another PLIST, which must contain values
39
for :install, :install-hook, :update, :remove and :checksum
40 41 42 43
properties. Those should be the elisp functions to call for doing
the named package action in the given method.")

(defun el-get-register-method (name install update remove
44
				    &optional install-hook remove-hook compute-checksum)
45
  "Register the method for backend NAME, with given functions"
46
  (let ((def (list :install install :update update :remove remove)))
47 48 49
    (when install-hook     (setq def (append def (list :install-hook install-hook))))
    (when remove-hook      (setq def (append def (list :remove-hook remove-hook))))
    (when compute-checksum (setq def (append def (list :compute-checksum compute-checksum))))
50
    (setq el-get-methods (plist-put el-get-methods name def))))
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80


;;
;; "Fuzzy" data structure handling
;;
;; In el-get-sources, single elements are often allowed instead of a
;; list, and strings and symbols are often interchangeable.
;; Presumably it's easier for users who don't use the customization
;; interface to write such structures as raw elisp.
;;
;;;  "Fuzzy" data structure conversion utilities
(defun el-get-as-string (symbol-or-string)
  "If STRING-OR-SYMBOL is already a string, return it.  Otherwise
convert it to a string and return that."
  (if (stringp symbol-or-string) symbol-or-string
    (symbol-name symbol-or-string)))

(defun el-get-as-symbol (string-or-symbol)
  "If STRING-OR-SYMBOL is already a symbol, return it.  Otherwise
convert it to a symbol and return that."
  (if (symbolp string-or-symbol) string-or-symbol
      (intern string-or-symbol)))

(defun el-get-as-list (element-or-list)
  "If ELEMENT-OR-LIST is already a list, return it.  Otherwise
returning a list that contains it (and only it)."
  (if (listp element-or-list) element-or-list
      (list element-or-list)))

81 82 83 84 85 86
(defun el-get-list-of-strings-p (obj)
  (or (null obj)
      (and (consp obj)
           (stringp (car obj))
           (el-get-list-of-strings-p (cdr obj)))))

87 88 89 90 91 92
(defun el-get-source-name (source)
  "Return the package name (stringp) given an `el-get-sources'
entry."
  (if (symbolp source) (symbol-name source)
    (format "%s" (plist-get source :name))))

93 94 95 96

;;
;; Common support bits
;;
97 98 99 100 101 102 103 104 105
(defun el-get-rmdir (package &rest ignored)
  "Just rm -rf the package directory. If it is a symlink, delete it."
  (let* ((pdir (el-get-package-directory package)))
    (cond ((file-symlink-p pdir)
           (delete-file pdir))
          ((file-directory-p pdir)
           (delete-directory pdir 'recursive))
          ((file-exists-p pdir)
           (delete-file pdir)))))
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150


;;
;; Some tools
;;
(defun el-get-duplicates (list)
  "Return duplicates found in list."
  (loop with dups and once
	for elt in list
	if (member elt once) collect elt into dups
	else collect elt into once
	finally return dups))

(defun el-get-flatten (arg)
  "Return a version of ARG as a one-level list

 (el-get-flatten 'x) => '(x)
 (el-get-flatten '(a (b c (d)) e)) => '(a b c d e)"
  (if (listp arg)
      (apply 'append (mapcar 'el-get-flatten arg))
    (list arg)))

(defun el-get-load-path (package)
  "Return the list of absolute directory names to be added to
`load-path' by the named PACKAGE."
  (let* ((source   (el-get-package-def package))
	 (el-path  (el-get-flatten (or (plist-get source :load-path) ".")))
         (pkg-dir (el-get-package-directory package)))
    (mapcar (lambda (p) (expand-file-name p pkg-dir)) el-path)))

(defun el-get-method (method-name action)
  "Return the function to call for doing action (e.g. install) in
given method."
  (let* ((method  (intern (concat ":" (format "%s" method-name))))
	 (actions (plist-get el-get-methods method)))
    (plist-get actions action)))

(defun el-get-check-init ()
  "Check that we can run el-get."
  (unless (file-directory-p el-get-dir)
    (make-directory el-get-dir)))

(defun el-get-package-directory (package)
  "Return the absolute directory name of the named PACKAGE."
  (file-name-as-directory
151 152
   (expand-file-name (el-get-as-string package)
		     (expand-file-name el-get-dir))))
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

(defun el-get-add-path-to-list (package list path)
  "(add-to-list LIST PATH) checking for path existence within
given package directory."
  (let* ((pdir     (el-get-package-directory package))
	 (fullpath (expand-file-name (or path ".") pdir)))
    (unless (file-directory-p fullpath)
      (error "el-get could not find directory `%s' for package %s, at %s"
	     path package fullpath))
    (add-to-list list fullpath)))

(defun el-get-package-exists-p (package)
  "Return true only when the given package name is either a
directory or a symlink in el-get-dir."
  (let ((pdir (el-get-package-directory package)))
    ;; seems overkill as file-directory-p will always be true
    (or (file-directory-p pdir)
	(file-symlink-p   pdir))))

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

;;
;; el-get-reload API functions
;;
(defun el-get-package-files (package)
  "Return a list of files loaded from PACKAGE's directory."
  (loop with pdir = (file-truename (el-get-package-directory package))
        with regexp = (format "^%s" (regexp-quote (file-name-as-directory (expand-file-name pdir))))
        for (f . nil) in load-history
        when (and (stringp f) (string-match-p regexp (file-truename f)))
        collect (if (string-match-p "\\.elc?$" f)
                    (file-name-sans-extension f)
                  f)))

(defun el-get-package-features (package)
  "Return a list of features provided by files in PACKAGE."
  (loop with pdir = (file-truename (el-get-package-directory package))
        with regexp = (format "^%s" (regexp-quote (file-name-as-directory (expand-file-name pdir))))
        for (f . l) in load-history
        when (and (stringp f) (string-match-p regexp (file-truename f)))
        nconc (loop for i in l
                    when (and (consp i) (eq (car i) 'provide))
                    collect (cdr i))))
195

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

;;
;; call-process-list utility
;;
(defun el-get-start-process-list-sentinel (proc change)
  "When proc has exited and was successful, chain next command."
  (when (eq (process-status proc) 'exit)
    (condition-case err
        (let ((status  (process-exit-status proc))
              (cname   (process-get proc :command-name))
              (cbuf    (process-get proc :buffer-name))
              (message (process-get proc :message))
              (errorm  (process-get proc :error))
              (package (process-get proc :el-get-package))
              (final-f (process-get proc :el-get-final-func))
              (next    (process-get proc :el-get-start-process-list))
              (el-get-sources (process-get proc :el-get-sources)))
          (if (not (eq 0 status))
              (progn
                (when (process-buffer proc)
                  (set-window-buffer (selected-window) cbuf))
                (error "el-get: %s %s" cname errorm))
            (message "el-get: %s" message))

          (when cbuf (kill-buffer cbuf))
          (if next
              (el-get-start-process-list package next final-f)
            (when (functionp final-f)
              (funcall final-f package))))
      ((debug error)
       (el-get-installation-failed (process-get proc :el-get-package) err)))))

(defvar el-get-default-process-sync nil
  "Non-nil value asks `el-get-start-process-list' to run current
process synchronously. Can be overridden by :sync property in
commands argument of `el-get-start-process-list'")

(defun el-get-start-process-list (package commands final-func)
  "Run each command one after the other, in order, stopping at
first error.

Commands should be a list of plists with at least the following
properties:

:default-directory

   default-directory from where to start the command

:command-name

   Name of the command to start, gives the name of the Emacs subprocess.

:buffer-name

   Name of the buffer associated with the command.

:process-filter

   Function to use as a process filter.

:shell

   When set to a non-nil value, use start-process-shell-command
   rather than the default start-process.

:program

   The program to start

:args

   The list of arguments for the program to start

:message

   The message to send upon success

:error

   The error to send upon failure

:sync

   When set to non-nil value, run synchronously.

:stdin

   Standard input to use for the process.  A lisp value is
   expected, it will get `prin1-to-string' then either saved to a
   file for a synchronous process or sent with
   `process-send-string' for an asynchronous one.

Any other property will get put into the process object.
"
  (condition-case err
      (if commands
        (let* ((c       (car commands))
293
               (next    (cdr commands))
294 295 296 297 298 299 300
               (cdir    (plist-get c :default-directory))
               (cname   (plist-get c :command-name))
               (cbuf    (plist-get c :buffer-name))
               (killed  (when (get-buffer cbuf) (kill-buffer cbuf)))
               (filter  (plist-get c :process-filter))
               (program (plist-get c :program))
               (shell   (plist-get c :shell))
301 302 303
               (args    (if shell
			    (mapcar #'shell-quote-argument (plist-get c :args))
			  (plist-get c :args)))
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
               (sync    (if (plist-member c :sync) (plist-get c :sync)
                          el-get-default-process-sync))
	       (stdin   (plist-get c :stdin))
               (default-directory (if cdir
                                      (file-name-as-directory
                                       (expand-file-name cdir))
                                    default-directory)))
          (if sync
              (let* ((startf (if shell #'call-process-shell-command #'call-process))
		     (infile (when stdin (make-temp-file "el-get")))
		     (dummy  (when infile
			       (with-temp-file infile
				 (insert (prin1-to-string stdin)))))
                     (dummy  (message "el-get is waiting for %S to complete" cname))
		     (status (apply startf program infile cbuf t args))
                     (message (plist-get c :message))
320
                     (errorm  (plist-get c :error)))
321
                (el-get-verbose-message "Running commands synchronously: %S" commands)
322 323 324 325 326 327 328 329 330 331 332 333
		(when el-get-verbose
		  (message "%S" (with-current-buffer cbuf (buffer-string))))
                (if (eq 0 status)
                    (message "el-get: %s" message)
                  (set-window-buffer (selected-window) cbuf)
                  (error "el-get: %s %s" cname errorm))
                (when cbuf (kill-buffer cbuf))
                (if next
                    (el-get-start-process-list package next final-func)
                  (when (functionp final-func)
                    (funcall final-func package))))
            ;; async case
334
            (el-get-verbose-message "Running commands asynchronously: %S" commands)
335 336 337 338 339 340 341 342
            (let* ((startf (if shell #'start-process-shell-command #'start-process))
                   (process-connection-type nil) ; pipe, don't pretend we're a pty
                   (proc (apply startf cname cbuf program args)))
              ;; add the properties to the process, then set the sentinel
              (mapc (lambda (x) (process-put proc x (plist-get c x))) c)
              (process-put proc :el-get-sources el-get-sources)
              (process-put proc :el-get-package package)
              (process-put proc :el-get-final-func final-func)
343
              (process-put proc :el-get-start-process-list next)
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
	      (when stdin
		(process-send-string proc (prin1-to-string stdin))
		(process-send-eof proc))
              (set-process-sentinel proc 'el-get-start-process-list-sentinel)
              (when filter (set-process-filter proc filter)))))
	;; no commands, still run the final-func
	(when (functionp final-func)
	  (funcall final-func package)))
    ((debug error)
     (el-get-installation-failed package err))))

;;
;; get an executable given its command name, with friendly error message
;;
(defun el-get-executable-find (name)
  "Return the absolute path of the command to execute, and errors
out if that can not be found.

This function will first look for existing function named
\"el-get-NAME-executable\" and call that. This function, if it
exists, must handle error cases.

Then, it will look for existing variable named \"el-get-NAME\"
and error if that's not nil and not an existing file name.

Baring variable named \"el-get-NAME\", it will call
`executable-find' on NAME and use the output of that, or error
out if it's nil."
  (let ((fname (intern (format "el-get-%s-executable" name)))
	(vname (intern (format "el-get-%s" name))))
    (cond
     ((fboundp fname)
      (funcall fname))

     ;; vname is bound here, we want to check for the variable named vname
     ;; (bound-and-true-p vname) won't cut it
     ((ignore-errors (symbol-value vname))
      (let ((command (symbol-value vname)))
	(unless (and (file-exists-p command)
		     (file-executable-p command))
	  (error
	   (concat "The variable `%s' points to \"%s\", "
		   "which is not an executable file name on your system.")
	   name command))
	command))

     (t
      (let ((command (executable-find name)))
	(unless command
	  (error
	   "The command named '%s' can not be found with `executable-find'"
	   name))
	command)))))

(provide 'el-get-core)