Skip to content

Commit

Permalink
[core] Support packing elisp to *.elc and *.el.gz files
Browse files Browse the repository at this point in the history
Support packing elisp to *.elc and *.el.gz like vallina Emacs.
  • Loading branch information
Lin Sun authored and smile13241324 committed Sep 29, 2021
1 parent 151ca61 commit 497c767
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.develop
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ Other:
- Add toggles for persistent which-key keymaps, (full)-major/minor mode
keymaps and top-level keymap, under prefix ~SPC t k~ (thanks to Daniel Nicolai)
- Add =describe-ex-command= on ~SPC h d x~ (thanks to Daniel Nicolai)
- Support packing elisp to *.elc and *.el.gz (thanks to Lin Sun)
- Fixes:
- Avoid non-idempotent use of push in init code (thanks to Miciah Masters)
- Moved Spacemacs startup progress bar to =core-progress-bar.el=, removed
Expand Down
38 changes: 18 additions & 20 deletions core/core-configuration-layer.el
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ To prevent package from being installed or uninstalled set the variable
(configuration-layer//declare-used-packages configuration-layer--used-layers)
;; then load the functions and finally configure the layers
(configuration-layer//load-layers-files configuration-layer--used-layers
'("funcs.el"))
'("funcs"))
(configuration-layer//configure-layers configuration-layer--used-layers)
;; load layers lazy settings
(configuration-layer/load-auto-layer-file)
Expand Down Expand Up @@ -673,7 +673,7 @@ To prevent package from being installed or uninstalled set the variable
;; packages configuration above
(configuration-layer//set-layers-variables configuration-layer--used-layers)
(configuration-layer//load-layers-files configuration-layer--used-layers
'("keybindings.el"))
'("keybindings"))
(when (spacemacs-is-dumping-p)
;; dump stuff in layers
(dolist (layer-name configuration-layer--used-layers)
Expand Down Expand Up @@ -794,10 +794,10 @@ If USEDP or `configuration-layer--load-packages-files' is non-nil then the
(memq :can-shadow layer-specs))
(spacemacs/mplist-get-values layer-specs :can-shadow)
'unspecified))
(packages-file (concat dir "packages.el"))
(packages-file (locate-file "packages" (list dir) load-suffixes))
(packages (when (and (null packages)
(or usedp configuration-layer--load-packages-files)
(file-exists-p packages-file))
packages-file)
(configuration-layer/load-file packages-file)
(symbol-value (intern (format "%S-packages"
layer-name)))))
Expand Down Expand Up @@ -1372,14 +1372,13 @@ Possible return values:
(directory-file-name
(concat configuration-layer-directory path))))
'category
(let ((files (directory-files path)))
;; most frequent files encoutered in a layer are tested first
(when (or (member "packages.el" files)
(member "layers.el" files)
(member "config.el" files)
(member "keybindings.el" files)
(member "funcs.el" files))
'layer)))))
;; most frequent files encoutered in a layer are tested first
(when (or (locate-file "packages" (list path) load-suffixes)
(locate-file "layers" (list path) load-suffixes)
(locate-file "config" (list path) load-suffixes)
(locate-file "keybindings" (list path) load-suffixes)
(locate-file "funcs" (list path) load-suffixes))
'layer))))

(defun configuration-layer//get-category-from-path (dirpath)
"Return a category symbol from the given DIRPATH.
Expand Down Expand Up @@ -1508,14 +1507,14 @@ If `SKIP-LAYER-DEPS' is non nil then skip loading of layer dependenciesl"
(not (oref layer :deps-loaded))
(or usedp configuration-layer--load-packages-files))
(oset layer :deps-loaded t)
(configuration-layer//load-layer-files layer-name '("layers.el"))))
(configuration-layer//load-layer-files layer-name '("layers"))))
(configuration-layer//warning "Unknown declared layer %s." layer-name))))

(defun configuration-layer/declare-layer-dependencies (layer-names)
"Function to be used in `layers.el' files to declare dependencies."
(dolist (x layer-names)
(add-to-list 'configuration-layer--layers-dependencies x)
(configuration-layer//load-layer-files x '("layers.el"))))
(configuration-layer//load-layer-files x '("layers"))))

(defun configuration-layer//declare-used-layers (layers-specs)
"Declare used layers from LAYERS-SPECS list."
Expand Down Expand Up @@ -1665,7 +1664,7 @@ RNAME is the name symbol of another existing layer."
(spacemacs-customization//create-layer-group
layer-name
(configuration-layer//get-layer-parent-category layer-name))))
(configuration-layer//load-layer-files layer-name '("config.el"))))))
(configuration-layer//load-layer-files layer-name '("config"))))))

(defun configuration-layer//declare-used-packages (layers)
"Declare used packages contained in LAYERS."
Expand All @@ -1692,8 +1691,7 @@ RNAME is the name symbol of another existing layer."
(when obj
(dolist (file files)
(let ((file (concat (oref obj :dir) file)))
(if (file-exists-p file)
(configuration-layer/load-file file)))))))
(configuration-layer/load-file file t))))))

(defun configuration-layer/configured-packages-stats (packages)
"Return a statistics alist regarding the number of configured PACKAGES."
Expand Down Expand Up @@ -1784,7 +1782,7 @@ RNAME is the name symbol of another existing layer."
(goto-char (point-max))
(configuration-layer//install-packages sorted-pkg)
(configuration-layer//configure-packages sorted-pkg)
(configuration-layer//load-layer-files layer '("keybindings.el"))
(configuration-layer//load-layer-files layer '("keybindings"))
(oset layer :lazy-install nil)
(switch-to-buffer last-buffer)))))

Expand Down Expand Up @@ -2861,9 +2859,9 @@ files."
ARGS: format string arguments."
(message "(Spacemacs) %s" (apply 'format msg args)))

(defun configuration-layer/load-file (file)
(defun configuration-layer/load-file (file &optional noerror)
"Load file silently except if in debug mode."
(load file nil (not init-file-debug)))
(load file noerror (not init-file-debug)))

(provide 'core-configuration-layer)

Expand Down
56 changes: 32 additions & 24 deletions tests/core/core-configuration-layer-utest.el
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,9 @@
(layer-packages '(pkg1 pkg2 pkg3))
(mocker-mock-default-record-cls 'mocker-stub-record))
(mocker-let
((file-exists-p (f) ((:output t :occur 2)))
((locate-file
(filename path &optional suffixes predicate)
((:record-cls 'mocker-stub-record :output "packages.el" :occur 1)))
(load (f &optional noerr nomsg) ((:output nil :occur 1))))
(should (equal (cfgl-layer "layer"
:name 'layer
Expand All @@ -1063,7 +1065,8 @@
(configuration-layer--load-packages-files t)
(mocker-mock-default-record-cls 'mocker-stub-record))
(mocker-let
((file-exists-p (f) ((:output t :occur 2)))
((locate-file (filename path &optional suffixes predicate)
((:record-cls 'mocker-stub-record :output t)))
(load (f &optional noerr nomsg) ((:output nil :occur 1))))
(should (equal (cfgl-layer "layer"
:name 'layer
Expand Down Expand Up @@ -1096,7 +1099,8 @@
(layer-packages '(pkg1 pkg2 pkg3))
(mocker-mock-default-record-cls 'mocker-stub-record))
(mocker-let
((file-exists-p (f) ((:output t :occur 2)))
((locate-file (filename path &optional suffixes predicate)
((:record-cls 'mocker-stub-record :output t :occur 1)))
(load (f &optional noerr nomsg) ((:output nil :occur 1))))
(should (equal (cfgl-layer "layer"
:name 'layer
Expand Down Expand Up @@ -1139,7 +1143,9 @@
(layer-packages '(pkg1 pkg2 pkg3))
(mocker-mock-default-record-cls 'mocker-stub-record))
(mocker-let
((file-exists-p (f) ((:output t :occur 2)))
((locate-file
(filename path &optional suffixes predicate)
((:record-cls 'mocker-stub-record :output t :occur 1)))
(load (f &optional noerr nomsg) ((:output nil :occur 1))))
(should (equal (cfgl-layer "layer"
:name 'layer
Expand Down Expand Up @@ -2874,52 +2880,52 @@
(mocker-let
((file-directory-p (f)
((:record-cls 'mocker-stub-record :output t :occur 1)))
(directory-files
(directory &optional full match nosort)
((:record-cls 'mocker-stub-record :output nil :occur 1))))
(locate-file (filename path &optional suffixes predicate)
((:record-cls 'mocker-stub-record :output nil :max-occur 5))))
(should (null (configuration-layer//directory-type input))))))

(ert-deftest test-directory-type--input-is-directory-and-not-a-layer ()
(let ((input "/a/path/to/a/layer/"))
(mocker-let
((file-directory-p (f)
((:record-cls 'mocker-stub-record :output t :occur 1)))
(directory-files
(directory &optional full match nosort)
((:record-cls 'mocker-stub-record
:output '("toto.el" "tata.el")
:occur 1))))
(locate-file (filename path &optional suffixes predicate)
((:record-cls 'mocker-stub-record :output nil :max-occur 5))))
(should (null (configuration-layer//directory-type input))))))

(ert-deftest test-directory-type--layer-with-packages.el ()
(let ((input "/a/path/to/a/layer/"))
(mocker-let
((file-directory-p (f)
((:record-cls 'mocker-stub-record :output t :occur 1)))
(directory-files
(directory &optional full match nosort)
((:record-cls 'mocker-stub-record :output '("packages.el") :occur 1))))
(locate-file
(filename path &optional suffixes predicate)
((:record-cls 'mocker-stub-record
:output (expand-file-name "packages.el" input)
:occur 1))))
(should (eq 'layer (configuration-layer//directory-type input))))))

(ert-deftest test-directory-type--layer-with-config.el ()
(let ((input "/a/path/to/a/layer/"))
(mocker-let
((file-directory-p (f)
((:record-cls 'mocker-stub-record :output t :occur 1)))
(directory-files
(directory &optional full match nosort)
((:record-cls 'mocker-stub-record :output '("config.el") :occur 1))))
(locate-file
(filename path &optional suffixes predicate)
((:record-cls 'mocker-stub-record
:output (expand-file-name "config.el" input)
:occur 1))))
(should (eq 'layer (configuration-layer//directory-type input))))))

(ert-deftest test-directory-type--layer-with-keybindings.el ()
(let ((input "/a/path/to/a/layer/"))
(mocker-let
((file-directory-p (f)
((:record-cls 'mocker-stub-record :output t :occur 1)))
(directory-files
(directory &optional full match nosort)
(locate-file
(filename path &optional suffixes predicate)
((:record-cls 'mocker-stub-record
:output '("keybindings.el")
:output (expand-file-name "keybindings.el" input)
:occur 1))))
(should (eq 'layer (configuration-layer//directory-type input))))))

Expand All @@ -2928,9 +2934,11 @@
(mocker-let
((file-directory-p (f)
((:record-cls 'mocker-stub-record :output t :occur 1)))
(directory-files
(directory &optional full match nosort)
((:record-cls 'mocker-stub-record :output '("funcs.el") :occur 1))))
(locate-file
(filename path &optional suffixes predicate)
((:record-cls 'mocker-stub-record
:output (expand-file-name "funcs.el" input)
:occur 1))))
(should (eq 'layer (configuration-layer//directory-type input))))))

;; ---------------------------------------------------------------------------
Expand Down

0 comments on commit 497c767

Please sign in to comment.