Refactor autoloads generation

- Correctly replace references to load-file-name and $# in autoloads.
- Don't load resulting autoloads file twice
- Read package autoloads literally (a little faster)
This commit is contained in:
Henrik Lissner 2019-07-27 16:59:10 +02:00
parent 94d5b73b45
commit 21a27b52d8
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -1,5 +1,8 @@
;;; core/cli/autoloads.el -*- lexical-binding: t; -*- ;;; core/cli/autoloads.el -*- lexical-binding: t; -*-
(require 'autoload)
(defvar doom-autoload-excluded-packages '("gh") (defvar doom-autoload-excluded-packages '("gh")
"Packages that have silly or destructive autoload files that try to load "Packages that have silly or destructive autoload files that try to load
everyone in the universe and their dog, causing errors that make babies cry. No everyone in the universe and their dog, causing errors that make babies cry. No
@ -48,23 +51,16 @@ It also caches `load-path', `Info-directory-list', `doom-disabled-packages',
(message " M-x doom/restart") (message " M-x doom/restart")
(message " M-x doom/reload")) (message " M-x doom/reload"))
(defun doom--reload-files (&rest files)
(if noninteractive
(add-hook 'doom-cli-post-success-execute-hook #'doom--warn-refresh-session-h)
(dolist (file files)
(load-file (byte-compile-dest-file file)))))
(defun doom--byte-compile-file (file) (defun doom--byte-compile-file (file)
(let ((byte-compile-warnings (if doom-debug-mode byte-compile-warnings)) (let ((byte-compile-warnings (if doom-debug-mode byte-compile-warnings))
(byte-compile-dynamic t) (byte-compile-dynamic t)
(byte-compile-dynamic-docstrings t)) (byte-compile-dynamic-docstrings t))
(condition-case e (condition-case-unless-debug e
(when (byte-compile-file file) (when (byte-compile-file file)
;; Give autoloads file a chance to report error (if noninteractive
(load (if doom-debug-mode (add-hook 'doom-cli-post-success-execute-hook #'doom--warn-refresh-session-h)
file ;; Give autoloads file a chance to report error
(byte-compile-dest-file file)) (load file 'noerror 'nomessage)))
nil t))
((debug error) ((debug error)
(let ((backup-file (concat file ".bk"))) (let ((backup-file (concat file ".bk")))
(print! (warn "Copied backup to %s") (relpath backup-file)) (print! (warn "Copied backup to %s") (relpath backup-file))
@ -108,7 +104,6 @@ even if it doesn't need reloading!"
(print! (debug "Ignoring %s") (relpath file))) (print! (debug "Ignoring %s") (relpath file)))
((let ((generated-autoload-load-name (file-name-sans-extension file))) ((let ((generated-autoload-load-name (file-name-sans-extension file)))
(require 'autoload)
(autoload-generate-file-autoloads file (current-buffer))) (autoload-generate-file-autoloads file (current-buffer)))
(print! (debug "Nothing in %s") (relpath file))) (print! (debug "Nothing in %s") (relpath file)))
@ -240,7 +235,7 @@ Run this whenever your `doom!' block, or a module autoload file, is modified."
(noninteractive t) (noninteractive t)
(backup-inhibited t) (backup-inhibited t)
(version-control 'never) (version-control 'never)
(case-fold-search nil) ; reduce magit (case-fold-search nil) ; reduce magic
(autoload-timestamps nil) (autoload-timestamps nil)
;; Where we'll store the files we'll scan for autoloads. This should ;; Where we'll store the files we'll scan for autoloads. This should
@ -303,8 +298,7 @@ Run this whenever your `doom!' block, or a module autoload file, is modified."
;; few marginal performance boosts) ;; few marginal performance boosts)
(print! "> Byte-compiling %s..." (relpath doom-autoload-file)) (print! "> Byte-compiling %s..." (relpath doom-autoload-file))
(when (doom--byte-compile-file doom-autoload-file) (when (doom--byte-compile-file doom-autoload-file)
(print! (success "Finished compiling %s") (relpath doom-autoload-file))) (print! (success "Finished compiling %s") (relpath doom-autoload-file))))
(doom--reload-files doom-autoload-file))
t))) t)))
@ -318,14 +312,14 @@ them,and remove unnecessary `provide' statements or blank links."
(unless (member pkg doom-autoload-excluded-packages) (unless (member pkg doom-autoload-excluded-packages)
(let ((file (straight--autoloads-file pkg))) (let ((file (straight--autoloads-file pkg)))
(when (file-exists-p file) (when (file-exists-p file)
(insert-file-contents file) (insert-file-contents-literally file)
(when (save-excursion (save-excursion
(and (re-search-forward "\\_<load-file-name\\_>" nil t) (while (re-search-forward "\\(?:\\_<load-file-name\\|#\\$\\)\\_>" nil t)
(not (nth 8 (syntax-ppss))))) ;; Set `load-file-name' so that the contents of autoloads
;; Set `load-file-name' so that the contents of autoloads ;; files can pretend they're in the file they're expected to
;; files can pretend they're in the file they're expected to ;; be in, rather than `doom-package-autoload-file'.
;; be in, rather than `doom-package-autoload-file'. (replace-match (prin1-to-string (abbreviate-file-name file))
(insert (format "(setq load-file-name %S)\n" (abbreviate-file-name file)))) t t)))
(while (re-search-forward "^\\(?:;;\\(.*\n\\)\\|\n\\|(provide '[^\n]+\\)" nil t) (while (re-search-forward "^\\(?:;;\\(.*\n\\)\\|\n\\|(provide '[^\n]+\\)" nil t)
(unless (nth 8 (syntax-ppss)) (unless (nth 8 (syntax-ppss))
(replace-match "" t t))) (replace-match "" t t)))
@ -417,6 +411,5 @@ This should be run whenever your `doom!' block or update your packages."
;; few marginal performance boosts) ;; few marginal performance boosts)
(print! (start "Byte-compiling %s...") (relpath doom-package-autoload-file)) (print! (start "Byte-compiling %s...") (relpath doom-package-autoload-file))
(when (doom--byte-compile-file doom-package-autoload-file) (when (doom--byte-compile-file doom-package-autoload-file)
(print! (success "Finished compiling %s") (relpath doom-package-autoload-file))) (print! (success "Finished compiling %s") (relpath doom-package-autoload-file)))))
(doom--reload-files doom-package-autoload-file)))
t)) t))