From 7238eb03e8dae65bac0a3664637c0700eb93642a Mon Sep 17 00:00:00 2001 From: JAremko Date: Thu, 11 Feb 2021 15:52:32 +0200 Subject: [PATCH] Better .elc cleanup --- core/core-compilation.el | 29 ++++++++++++----------------- core/core-spacemacs.el | 2 +- init.el | 14 +++++++------- 3 files changed, 20 insertions(+), 25 deletions(-) diff --git a/core/core-compilation.el b/core/core-compilation.el index 74ef4e503..381275860 100644 --- a/core/core-compilation.el +++ b/core/core-compilation.el @@ -27,6 +27,7 @@ "core/libs/package-build-badges.el" "core/libs/package-build.el" "core/libs/package-recipe-mode.el" + "core/libs/package-recipe.el" "core/libs/page-break-lines.el" "core/libs/quelpa.el" "core/libs/spinner.el") @@ -40,24 +41,18 @@ File paths are relative to the `spacemacs-start-directory'.") (unless (file-exists-p (concat fbp ".elc")) (byte-compile-file (concat fbp ".el")))))) -(defun spacemacs//remove-byte-compiled-files (files) - "Remove .elc files corresponding to the source FILES." - (dolist (file files) - (let ((file-elc (thread-last file - (file-truename) - (file-name-sans-extension) - (format "%s.elc")))) - (when (file-exists-p file-elc) - (delete-file file-elc))))) +(defun spacemacs//remove-byte-compiled-files-in-dir (dir) + "Remove all .elc files in DIR directory." + (dolist (elc (directory-files-recursively dir "\\.elc$")) + (when (file-exists-p elc) + (delete-file elc)))) -(defun spacemacs//contains-newer-than-byte-compiled-p (files) - "Return true if any file in FILES is newer than its byte-compiled version." - (cl-dolist (file files) - (let* ((file-el (file-truename file)) - (file-elc (thread-last file-el - (file-name-sans-extension) - (format "%s.elc")))) - (when (file-newer-than-file-p file-el file-elc) +(defun spacemacs//dir-contains-stale-byte-compiled-files-p (dir) + "Returns true if any .elc file in DIR directory is stale or orphaned." + (cl-dolist (elc (directory-files-recursively dir "\\.elc$")) + (let ((el (substring elc 0 -1))) + (unless (and (file-exists-p el) + (file-newer-than-file-p elc el)) (cl-return t))))) (defun spacemacs//update-last-emacs-version () diff --git a/core/core-spacemacs.el b/core/core-spacemacs.el index bfe64020b..23821b6a3 100644 --- a/core/core-spacemacs.el +++ b/core/core-spacemacs.el @@ -256,7 +256,7 @@ Note: the hooked function is not executed when in dumped mode." (let ((default-directory spacemacs-start-directory)) (if dotspacemacs-byte-compile (spacemacs//ensure-byte-compilation spacemacs--compiled-files) - (spacemacs//remove-byte-compiled-files spacemacs--compiled-files))) + (spacemacs//remove-byte-compiled-files-in-dir spacemacs-core-directory))) ;; Check if revision has changed. (spacemacs//revision-check)) diff --git a/init.el b/init.el index 6110a471f..686126955 100644 --- a/init.el +++ b/init.el @@ -25,16 +25,16 @@ (load (concat spacemacs-core-directory "core-dumper.el") nil (not init-file-debug)) -;; Clean compiled files if they become stale or Emacs version has changed. +;; Remove compiled core files if they become stale or Emacs version has changed. (load (concat spacemacs-core-directory "core-compilation.el") nil (not init-file-debug)) (load spacemacs--last-emacs-version-file t (not init-file-debug)) -(let ((default-directory spacemacs-start-directory)) - (when (or (not (string= spacemacs--last-emacs-version emacs-version)) - (spacemacs//contains-newer-than-byte-compiled-p - spacemacs--compiled-files)) - (spacemacs//remove-byte-compiled-files spacemacs--compiled-files))) -(when (not (string= spacemacs--last-emacs-version emacs-version)) +(when (or (not (string= spacemacs--last-emacs-version emacs-version)) + (spacemacs//dir-contains-stale-byte-compiled-files-p + spacemacs-core-directory)) + (spacemacs//remove-byte-compiled-files-in-dir spacemacs-core-directory)) +;; Update saved Emacs version. +(unless (string= spacemacs--last-emacs-version emacs-version) (spacemacs//update-last-emacs-version)) (if (not (version<= spacemacs-emacs-min-version emacs-version))