]> code.delx.au - gnu-emacs-elpa/blob - async-bytecomp.el
* async-bytecomp.el: New file.
[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 ;; Version: 1.0
10 ;; Keywords: dired async byte-compile
11 ;; X-URL: https://github.com/jwiegley/dired-async
12
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
17
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Commentary:
29 ;;
30 ;; This package provide the `async-byte-recompile-directory' function
31 ;; which allows, as the name says to recompile a directory outside of
32 ;; your running emacs.
33 ;; The benefit is your files will be compiled in a clean environment without
34 ;; the old *.el files loaded.
35 ;; Among other things, this fix a bug in package.el which recompile
36 ;; the new files in the current environment with the old files loaded, creating
37 ;; errors in most packages after upgrades.
38 ;;
39 ;; NB: This package is advicing the function `package--compile'.
40
41 ;;; Code:
42
43 (require 'async)
44
45 (defvar async-byte-compile-log-file "/tmp/async-bytecomp.log")
46
47 (defun async-byte-recompile-directory (directory &optional arg force)
48 (cl-loop with dir = (directory-files directory t "\\.elc\\'")
49 unless dir return nil
50 for f in dir
51 when (file-exists-p f) do (delete-file f))
52 (let ((call-back
53 `(lambda (&optional ignore)
54 (if (file-exists-p async-byte-compile-log-file)
55 (progn
56 (pop-to-buffer (generate-new-buffer-name
57 byte-compile-log-buffer))
58 (erase-buffer)
59 (insert-file-contents async-byte-compile-log-file)
60 (compilation-mode)
61 (delete-file async-byte-compile-log-file)
62 (let ((n 0))
63 (save-excursion
64 (goto-char (point-min))
65 (while (re-search-forward "^.*:Error:" nil t)
66 (incf n)))
67 (when (> n 0)
68 (message "Failed to compile %d files in directory `%s'" n ,directory))))
69 (message "Directory `%s' compiled asynchronously with success" ,directory)))))
70 (async-start
71 `(lambda ()
72 (require 'bytecomp)
73 ,(async-inject-variables "\\`load-path\\'")
74 (let ((default-directory (file-name-as-directory ,directory))
75 error-data)
76 (add-to-list 'load-path default-directory)
77 (byte-recompile-directory ,directory ,arg ,force)
78 (when (get-buffer byte-compile-log-buffer)
79 (setq error-data (with-current-buffer byte-compile-log-buffer
80 (buffer-substring-no-properties (point-min) (point-max))))
81 (unless (string= error-data "")
82 (with-temp-file ,async-byte-compile-log-file
83 (erase-buffer)
84 (insert error-data))))))
85 call-back)
86 (message "Started compiling asynchronously directory %s..." directory)))
87
88 (defadvice package--compile (around byte-compile-async activate)
89 (package-activate-1 pkg-desc)
90 (async-byte-recompile-directory (package-desc-dir pkg-desc) 0 t))
91
92 (provide 'async-bytecomp)
93
94 ;;; async-bytecomp.el ends here