Skip to content

Commit dcaaf15

Browse files
author
Steven Rémot
committed
Properly merge autoload entries
fixes #19
1 parent ed0bbff commit dcaaf15

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

ede-php-autoload-composer.el

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,31 @@ BASE-DIR is the prefix dir to add to each autoload path."
138138

139139
autoloads))
140140

141+
(defun ede-php-autoload-composer--merge-autoload-paths (base-paths new-paths)
142+
"Merge two paths in a autoload file in one.
143+
144+
BASE-PATHS and NEW-PATHS are either string opr list of strings.
145+
146+
It will output a list of strings with each of base and new paths in it."
147+
(let ((list-base-path (if (stringp base-paths) (list base-paths) base-paths))
148+
(list-new-paths (if (stringp new-paths) (list new-paths) new-paths)))
149+
(append list-base-path list-new-paths)))
150+
151+
(defun ede-php-autoload-composer--merge-autoload-entries (base-entries new-entries)
152+
"Merge two autoload entries (right after the autoload entry).
153+
154+
BASE-ENTRIES and NEW-ENTRIES are the entries to merge."
155+
(let ((current-entries base-entries)
156+
pair
157+
pair-path)
158+
(cl-loop for (ns . paths) in new-entries do
159+
(setq pair (assoc ns current-entries)
160+
pair-path (cdr pair))
161+
(if pair
162+
(setf (cdr pair) (ede-php-autoload-composer--merge-autoload-paths pair-path paths))
163+
(setq current-entries (push (cons ns paths) current-entries))))
164+
current-entries))
165+
141166
(defun ede-php-autoload-composer-merge-autoloads (base-autoloads new-autoloads)
142167
"Merge two internal autoload definitions in one.
143168
@@ -156,7 +181,7 @@ NEW-AUTOLOADS will be merged into BASE-AUTOLOADS. BASE-AUTOLOADS will be mutate
156181

157182
autoloads (plist-put autoloads
158183
key
159-
(append
184+
(ede-php-autoload-composer--merge-autoload-entries
160185
(plist-get autoloads key)
161186
value))
162187

test/ede-php-autoload-composer-test.el

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,22 @@
3535
'(:psr-0 (("Test" . "test/")) :psr-4 (("Test3" . "test3/")))
3636
'(:psr-0 (("Test2" . "test2/")) :psr-4 (("Test4" . "test4/"))))
3737

38-
'(:psr-0 (("Test" . "test/")
39-
("Test2" . "test2/"))
40-
:psr-4 (("Test3" . "test3/")
41-
("Test4" . "test4/"))))))
38+
'(:psr-0 (("Test2" . "test2/")
39+
("Test" . "test/"))
40+
:psr-4 (("Test4" . "test4/")
41+
("Test3" . "test3/"))))))
42+
43+
(ert-deftest ede-php-autoload-composer-merge-autoloads-concats-files-merged-part ()
44+
"`ede-php-autoload-composer-merge-autoloads' should merge paths with the same key."
45+
(should (equal
46+
47+
(ede-php-autoload-composer-merge-autoloads
48+
'(:psr-0 (("Test" . "test/")) :psr-4 (("Test3" . "test3/")))
49+
'(:psr-0 (("Test" . "test2/")) :psr-4 (("Test4" . "test4/"))))
50+
51+
'(:psr-0 (("Test" . ("test/" "test2/")))
52+
:psr-4 (("Test4" . "test4/")
53+
("Test3" . "test3/"))))))
4254

4355
(ert-deftest ede-php-autoload-composer-define-visitor ()
4456
"`:define-visitors' should define a visitor according to its step."

0 commit comments

Comments
 (0)