From fe0f70754ecdca47f16d3c7e1c4e1798d63382f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20R=C3=A9mot?= Date: Mon, 20 Feb 2017 14:04:39 +0100 Subject: [PATCH] Add a priority system to the visitors Add an argument to the visitors to define the priority of a visitor. Possible values are `:early`, `:normal` and `:late`. --- ede-php-autoload-composer.el | 31 +++++++++++++++------ test/ede-php-autoload-composer-test.el | 38 ++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/ede-php-autoload-composer.el b/ede-php-autoload-composer.el index 91dd054..6e3d618 100644 --- a/ede-php-autoload-composer.el +++ b/ede-php-autoload-composer.el @@ -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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -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) diff --git a/test/ede-php-autoload-composer-test.el b/test/ede-php-autoload-composer-test.el index df78a57..8a263eb 100644 --- a/test/ede-php-autoload-composer-test.el +++ b/test/ede-php-autoload-composer-test.el @@ -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