]> code.delx.au - gnu-emacs-elpa/blob - async-bytecomp.el
Declare package-alist in async-bytecomp..
[gnu-emacs-elpa] / async-bytecomp.el
1 ;;; async-bytecomp.el --- Async functions to compile elisp files async
2
3 ;; Copyright (C) 2014 John Wiegley
4 ;; Copyright (C) 2014 Thierry Volpiatto
5
6 ;; Authors: John Wiegley <jwiegley@gmail.com>
7 ;; Thierry Volpiatto <thierry.volpiatto@gmail.com>
8
9 ;; Keywords: dired async byte-compile
10 ;; X-URL: https://github.com/jwiegley/dired-async
11
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
16
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28 ;;
29 ;; This package provide the `async-byte-recompile-directory' function
30 ;; which allows, as the name says to recompile a directory outside of
31 ;; your running emacs.
32 ;; The benefit is your files will be compiled in a clean environment without
33 ;; the old *.el files loaded.
34 ;; Among other things, this fix a bug in package.el which recompile
35 ;; the new files in the current environment with the old files loaded, creating
36 ;; errors in most packages after upgrades.
37 ;;
38 ;; NB: This package is advicing the function `package--compile'.
39
40 ;;; Code:
41
42 (require 'cl-lib)
43 (require 'async)
44
45 (defcustom async-bytecomp-allowed-packages
46 '(async helm helm-core helm-ls-git helm-ls-hg magit)
47 "Packages in this list will be compiled asynchronously by `package--compile'.
48 All the dependencies of these packages will be compiled async too,
49 so no need to add dependencies to this list.
50 The value of this variable can also be a list with a single element,
51 the symbol `all', in this case packages are always compiled asynchronously."
52 :group 'async
53 :type '(repeat (choice symbol)))
54
55 (defvar async-byte-compile-log-file "~/.emacs.d/async-bytecomp.log")
56
57 ;;;###autoload
58 (defun async-byte-recompile-directory (directory &optional quiet)
59 "Compile all *.el files in DIRECTORY asynchronously.
60 All *.elc files are systematically deleted before proceeding."
61 (cl-loop with dir = (directory-files directory t "\\.elc\\'")
62 unless dir return nil
63 for f in dir
64 when (file-exists-p f) do (delete-file f))
65 ;; Ensure async is reloaded when async.elc is deleted.
66 ;; This happen when recompiling its own directory.
67 (load "async")
68 (let ((call-back
69 `(lambda (&optional ignore)
70 (if (file-exists-p async-byte-compile-log-file)
71 (let ((buf (get-buffer-create byte-compile-log-buffer))
72 (n 0))
73 (with-current-buffer buf
74 (goto-char (point-max))
75 (let ((inhibit-read-only t))
76 (insert-file-contents async-byte-compile-log-file)
77 (compilation-mode))
78 (display-buffer buf)
79 (delete-file async-byte-compile-log-file)
80 (unless ,quiet
81 (save-excursion
82 (goto-char (point-min))
83 (while (re-search-forward "^.*:Error:" nil t)
84 (cl-incf n)))
85 (if (> n 0)
86 (message "Failed to compile %d files in directory `%s'" n ,directory)
87 (message "Directory `%s' compiled asynchronously with warnings" ,directory)))))
88 (unless ,quiet
89 (message "Directory `%s' compiled asynchronously with success" ,directory))))))
90 (async-start
91 `(lambda ()
92 (require 'bytecomp)
93 ,(async-inject-variables "\\`\\(load-path\\)\\|byte\\'")
94 (let ((default-directory (file-name-as-directory ,directory))
95 error-data)
96 (add-to-list 'load-path default-directory)
97 (byte-recompile-directory ,directory 0 t)
98 (when (get-buffer byte-compile-log-buffer)
99 (setq error-data (with-current-buffer byte-compile-log-buffer
100 (buffer-substring-no-properties (point-min) (point-max))))
101 (unless (string= error-data "")
102 (with-temp-file ,async-byte-compile-log-file
103 (erase-buffer)
104 (insert error-data))))))
105 call-back)
106 (unless quiet (message "Started compiling asynchronously directory %s" directory))))
107
108 (defvar package-archive-contents)
109 (defvar package-alist)
110 (declare-function package-desc-reqs "package.el" (cl-x))
111
112 (defun async-bytecomp--get-package-deps (pkg &optional only)
113 ;; Same as `package--get-deps' but parse instead `package-archive-contents'
114 ;; because PKG is not already installed and not present in `package-alist'.
115 ;; However fallback to `package-alist' in case PKG no more present
116 ;; in `package-archive-contents' due to modification to `package-archives'.
117 ;; See issue #58.
118 (let* ((pkg-desc (cadr (or (assq pkg package-archive-contents)
119 (assq pkg package-alist))))
120 (direct-deps (cl-loop for p in (package-desc-reqs pkg-desc)
121 for name = (car p)
122 when (or (assq name package-archive-contents)
123 (assq name package-alist))
124 collect name))
125 (indirect-deps (unless (eq only 'direct)
126 (delete-dups
127 (cl-loop for p in direct-deps append
128 (async-bytecomp--get-package-deps p))))))
129 (cl-case only
130 (direct direct-deps)
131 (separate (list direct-deps indirect-deps))
132 (indirect indirect-deps)
133 (t (delete-dups (append direct-deps indirect-deps))))))
134
135 (defun async-bytecomp-get-allowed-pkgs ()
136 (when (and async-bytecomp-allowed-packages
137 (listp async-bytecomp-allowed-packages))
138 (if package-archive-contents
139 (cl-loop for p in async-bytecomp-allowed-packages
140 when (assq p package-archive-contents)
141 append (async-bytecomp--get-package-deps p) into reqs
142 finally return
143 (delete-dups
144 (append async-bytecomp-allowed-packages reqs)))
145 async-bytecomp-allowed-packages)))
146
147 (defadvice package--compile (around byte-compile-async)
148 (let ((cur-package (package-desc-name pkg-desc))
149 (pkg-dir (package-desc-dir pkg-desc)))
150 (if (or (equal async-bytecomp-allowed-packages '(all))
151 (memq cur-package (async-bytecomp-get-allowed-pkgs)))
152 (progn
153 (when (eq cur-package 'async)
154 (fmakunbound 'async-byte-recompile-directory))
155 ;; Add to `load-path' the latest version of async and
156 ;; reload it when reinstalling async.
157 (when (string= cur-package "async")
158 (cl-pushnew pkg-dir load-path)
159 (load "async-bytecomp"))
160 ;; `async-byte-recompile-directory' will add directory
161 ;; as needed to `load-path'.
162 (async-byte-recompile-directory (package-desc-dir pkg-desc) t))
163 ad-do-it)))
164
165 ;;;###autoload
166 (define-minor-mode async-bytecomp-package-mode
167 "Byte compile asynchronously packages installed with package.el.
168 Async compilation of packages can be controlled by
169 `async-bytecomp-allowed-packages'."
170 :group 'async
171 :global t
172 (if async-bytecomp-package-mode
173 (ad-activate 'package--compile)
174 (ad-deactivate 'package--compile)))
175
176 (provide 'async-bytecomp)
177
178 ;;; async-bytecomp.el ends here