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