Skip to content

Commit

Permalink
Merge pull request #18 from stevenremot/add-priority
Browse files Browse the repository at this point in the history
Add a priority system to the visitors
  • Loading branch information
stevenremot authored Feb 20, 2017
2 parents c25e7dd + fe0f707 commit ed0bbff
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
31 changes: 22 additions & 9 deletions ede-php-autoload-composer.el
Original file line number Diff line number Diff line change
Expand Up @@ -223,23 +223,36 @@ PROJECT-DIR is the absolute path of the project directory."
"Return the absolute path to the project directory in the given CONTEXT."
(cdr (assoc 'project-dir context)))

(defun ede-php-autoload-composer-define-visitor (visitor)
(defun ede-php-autoload-composer-define-visitor (visitor &optional step)
"Add a new VISITOR to the list of composer visitors.
A visitor is a function that takes a context and the current list
of autoloads, and returns a new list of autoloads.
All visitors are executed when a composer project is detected, to
generate the composer autoloads."
(add-to-list 'ede-php-autoload-composer--visitors visitor))

(defun ede-php-autoload-composer--run-visitors (context autoloads)
generate the composer autoloads.
STEP is the autoload construction step at which the visitor
should execute. It can be `:early', `:normal' or `:late. It
defaults to `:normal'.'"
(let* ((real-step (or step :normal))
(pair (assoc real-step ede-php-autoload-composer--visitors)))
(if pair
(setf (cdr pair) (push visitor (cdr pair)))
(add-to-list 'ede-php-autoload-composer--visitors (cons real-step (list visitor))))))

(defun ede-php-autoload-composer--run-visitors (visitors context autoloads)
"Run all the visitors on a specified CONTEXT, with the initial AUTOLOADS.
Returns the new list of autoloads."
(let ((current-autoloads autoloads))
(dolist (visitor ede-php-autoload-composer--visitors)
(setq current-autoloads (funcall visitor context current-autoloads)))
(let ((current-autoloads autoloads)
step-visitors)

(dolist (step '(:early :normal :late))
(setq step-visitors (cdr (assoc step visitors)))
(dolist (visitor step-visitors)
(setq current-autoloads (funcall visitor context current-autoloads))))

current-autoloads))

;; Basic visitors ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -287,7 +300,7 @@ information into AUTOLOADS."
(lock-file (expand-file-name ede-php-autoload-composer-lock-file project-dir))
(composer-lock (when (file-exists-p lock-file) (json-read-file lock-file)))
(context (ede-php-autoload-composer-make-context composer-data composer-lock project-dir)))
(ede-php-autoload-composer--run-visitors context autoloads)))
(ede-php-autoload-composer--run-visitors ede-php-autoload-composer--visitors context autoloads)))


(provide 'ede-php-autoload-composer)
Expand Down
38 changes: 38 additions & 0 deletions test/ede-php-autoload-composer-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,44 @@
:psr-4 (("Test3" . "test3/")
("Test4" . "test4/"))))))

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

(ede-php-autoload-composer-define-visitor 'test-visitor-1)
(should (equal ede-php-autoload-composer--visitors '((:normal . (test-visitor-1)))))

(ede-php-autoload-composer-define-visitor 'test-visitor-2 :late)
(should (equal ede-php-autoload-composer--visitors '((:late . (test-visitor-2))
(:normal . (test-visitor-1)))))


(ede-php-autoload-composer-define-visitor 'test-visitor-3 :early)
(should (equal ede-php-autoload-composer--visitors '((:early . (test-visitor-3))
(:late . (test-visitor-2))
(:normal . (test-visitor-1)))))


(ede-php-autoload-composer-define-visitor 'test-visitor-4)
(should (equal ede-php-autoload-composer--visitors '((:early . (test-visitor-3))
(:late . (test-visitor-2))
(:normal . (test-visitor-4 test-visitor-1))))))

(ert-deftest ede-php-autoload-composer--run-visitors ()
"`ede-php-autoload-composer--run-visitors' should build configuration from visitors."
(should (equal
(ede-php-autoload-composer--run-visitors
'((:late . ((lambda (context autoloads)
(push :late autoloads))))
(:early . ((lambda (context autoloads)
(push :early autoloads))))
(:normal . ((lambda (context autoloads)
(push :normal autoloads)))))
nil
'())

'(:late :normal :early))))

(provide 'ede-php-autoload-composer-test)

;;; ede-php-autoload-composer-test.el ends here

0 comments on commit ed0bbff

Please sign in to comment.