]> code.delx.au - gnu-emacs-elpa/blob - async-bytecomp.el
Merge branch 'master' of github.com:jwiegley/emacs-async
[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 (defvar async-byte-compile-log-file "~/.emacs.d/async-bytecomp.log")
46
47 (defun async-byte-recompile-directory (directory &optional quiet)
48 "Compile all *.el files in DIRECTORY asynchronously.
49 All *.elc files are systematically deleted before proceeding."
50 (cl-loop with dir = (directory-files directory t "\\.elc\\'")
51 unless dir return nil
52 for f in dir
53 when (file-exists-p f) do (delete-file f))
54 ;; Ensure async is reloaded when async.elc is deleted.
55 ;; This happen when recompiling its own directory.
56 (load "async")
57 (let ((call-back
58 `(lambda (&optional ignore)
59 (if (file-exists-p async-byte-compile-log-file)
60 (let ((buf (get-buffer-create byte-compile-log-buffer))
61 (n 0))
62 (with-current-buffer buf
63 (goto-char (point-max))
64 (let ((inhibit-read-only t))
65 (insert-file-contents async-byte-compile-log-file)
66 (compilation-mode))
67 (display-buffer buf)
68 (delete-file async-byte-compile-log-file)
69 (unless ,quiet
70 (save-excursion
71 (goto-char (point-min))
72 (while (re-search-forward "^.*:Error:" nil t)
73 (cl-incf n)))
74 (if (> n 0)
75 (message "Failed to compile %d files in directory `%s'" n ,directory)
76 (message "Directory `%s' compiled asynchronously with warnings" ,directory)))))
77 (unless ,quiet
78 (message "Directory `%s' compiled asynchronously with success" ,directory))))))
79 (async-start
80 `(lambda ()
81 (require 'bytecomp)
82 ,(async-inject-variables "\\`\\(load-path\\)\\|byte\\'")
83 (let ((default-directory (file-name-as-directory ,directory))
84 error-data)
85 (add-to-list 'load-path default-directory)
86 (byte-recompile-directory ,directory 0 t)
87 (when (get-buffer byte-compile-log-buffer)
88 (setq error-data (with-current-buffer byte-compile-log-buffer
89 (buffer-substring-no-properties (point-min) (point-max))))
90 (unless (string= error-data "")
91 (with-temp-file ,async-byte-compile-log-file
92 (erase-buffer)
93 (insert error-data))))))
94 call-back)
95 (message "Started compiling asynchronously directory %s" directory)))
96
97 (defadvice package--compile (around byte-compile-async activate)
98 ;; FIXME this seems redundant and unneeded, the only thing it
99 ;; does is loading the autoload file to update load-path but
100 ;; async-byte-recompile-directory is already doing this.
101 ;; for the rest (i.e installing info) it is done anyway after
102 ;; compilation in package-activate (force arg).
103 (package-activate-1 pkg-desc)
104 (load "async-bytecomp")
105 (async-byte-recompile-directory (package-desc-dir pkg-desc) t))
106
107 (provide 'async-bytecomp)
108
109 ;;; async-bytecomp.el ends here