From 1726a1a03e145bcd69f74477d6a502218e0d1950 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sat, 31 Dec 2022 15:19:23 +0800 Subject: [PATCH 1/6] feature: Improve Eask-file loader to search file upward --- lisp/_prepare.el | 49 +++++++++++++++++++++++++++++++------- test/development/compat.el | 1 + 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/lisp/_prepare.el b/lisp/_prepare.el index 28a5214d..98ba8f49 100644 --- a/lisp/_prepare.el +++ b/lisp/_prepare.el @@ -602,6 +602,7 @@ Eask file in the workspace." (defun eask-file-load (location &optional noerror) "Load Eask file in the LOCATION." + (jcs-print (expand-file-name location user-emacs-directory)) (when-let* ((target-eask-file (expand-file-name location user-emacs-directory)) (result (eask--alias-env (load target-eask-file 'noerror t)))) (setq eask-file target-eask-file ; assign eask file only if success @@ -609,14 +610,46 @@ Eask file in the workspace." (run-hooks 'eask-file-loaded-hook) result)) -(defun eask-file-try-load (relative-path) - "Try load eask file in RELATIVE-PATH." - (or (eask-file-load (concat relative-path (format "Easkfile.%s" emacs-version)) t) - (eask-file-load (concat relative-path (format "Eask.%s" emacs-version)) t) - (eask-file-load (concat relative-path (format "Easkfile.%s" emacs-major-version)) t) - (eask-file-load (concat relative-path (format "Eask.%s" emacs-major-version)) t) - (eask-file-load (concat relative-path "Easkfile") t) - (eask-file-load (concat relative-path "Eask") t))) +(defun eask--match-file (name) + "Check to see if NAME is our target Eask-file, then return it." + (let ((name (file-name-nondirectory (directory-file-name name))) + (easkfile-full (format "Easkfile.%s" emacs-version)) + (easkfile-major (format "Easkfile.%s" emacs-major-version)) + (easkfile "Easkfile") + (eask-full (format "Eask.%s" emacs-version)) + (eask-major (format "Eask.%s" emacs-major-version)) + (eask "Eask")) + (car (member name (list easkfile-full easkfile-major easkfile + eask-full eask-major eask))))) + +(defun eask--all-files (&optional dir) + "Return a list of Eask files. + +If argument DIR is nil, we use `default-directory' instead." + (setq dir (or dir default-directory)) + (let* ((files (append (directory-files dir t "Easkfile[.0-9]*\\'") + (directory-files dir t "Eask[.0-9]*\\'"))) + (files (cl-remove-if #'file-directory-p files))) + (cl-remove-if-not #'eask--match-file files))) + +(defun eask-file-try-load (start-path) + "Try load eask file in START-PATH. + +This uses function `locate-dominating-file' to look up directory tree." + (when-let* + (;; XXX: This is redundant, but the simplest way to find the root path! + (root (locate-dominating-file start-path #'eask--all-files)) + (files (eask--all-files root)) ; get all available Eask-files + ;; Filter it to restrict to this Emacs version! + (files (cl-remove-if-not #'eask--match-file files)) + ;; Make `Easkfile.29.1' > `Easkfile.29' > `Easkfile' (same with `Eask' file) + (files (sort files #'string-greaterp)) + ;; Make `Easkfile' > `Eask' higher precedent! + (files (sort files (lambda (item1 item2) + (and (string-prefix-p "Easkfile" item1) + (not (string-prefix-p "Easkfile" item2)))))) + (file (car files))) + (eask-file-load file))) (defun eask--print-env-info () "Display environment information at the very top of the execution." diff --git a/test/development/compat.el b/test/development/compat.el index 21ed36de..23a73856 100644 --- a/test/development/compat.el +++ b/test/development/compat.el @@ -23,6 +23,7 @@ package--activate-all package-activate-all package-generate-description-file + locate-dominating-file directory-empty-p url-file-exists-p) "List of function to check Emacs compatibility.") From 65a4bac342fa05454fc3a1aaadef3f8f6d915774 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sat, 31 Dec 2022 15:27:19 +0800 Subject: [PATCH 2/6] clean up --- lisp/_prepare.el | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lisp/_prepare.el b/lisp/_prepare.el index 98ba8f49..ec404215 100644 --- a/lisp/_prepare.el +++ b/lisp/_prepare.el @@ -602,7 +602,6 @@ Eask file in the workspace." (defun eask-file-load (location &optional noerror) "Load Eask file in the LOCATION." - (jcs-print (expand-file-name location user-emacs-directory)) (when-let* ((target-eask-file (expand-file-name location user-emacs-directory)) (result (eask--alias-env (load target-eask-file 'noerror t)))) (setq eask-file target-eask-file ; assign eask file only if success @@ -711,7 +710,7 @@ This uses function `locate-dominating-file' to look up directory tree." (custom-file (locate-user-emacs-file "custom.el")) (special (eask-special-p))) (unless special - (if (eask-file-try-load "../../") + (if (eask-file-try-load "./") (eask-msg "✓ Loading Eask file in %s... done!" eask-file) (eask-msg "✗ Loading Eask file... missing!") (eask-help "core/init"))) From 0e0265004128592e745e52937c99d16e0aaebe6a Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sat, 31 Dec 2022 15:30:48 +0800 Subject: [PATCH 3/6] changelog --- CHANGELOG.md | 1 + lisp/_prepare.el | 1 + 2 files changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93e4b370..0b35f1be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how * Remove dependency `s.el` (962dd5f8d0da1443368ac2d79b0a013c153a804e) * Acknowledge cleaning state for `clean all` command (#87) * Fix keyword lint undetected (#88) +* Improve Eask-file loader to search file upward (#89) ## 0.7.x > Released Sep 08, 2022 diff --git a/lisp/_prepare.el b/lisp/_prepare.el index ec404215..7218e607 100644 --- a/lisp/_prepare.el +++ b/lisp/_prepare.el @@ -10,6 +10,7 @@ (require 'url-vars) (require 'cl-lib) +(require 'files) (require 'ls-lisp) (require 'pp) (require 'rect) From 8c5aaa99f4e65b5c718b8d9e35ac47c373e73823 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sat, 31 Dec 2022 15:36:37 +0800 Subject: [PATCH 4/6] Allow error searching directory files --- lisp/_prepare.el | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lisp/_prepare.el b/lisp/_prepare.el index 7218e607..4e09f73e 100644 --- a/lisp/_prepare.el +++ b/lisp/_prepare.el @@ -627,9 +627,10 @@ Eask file in the workspace." If argument DIR is nil, we use `default-directory' instead." (setq dir (or dir default-directory)) - (let* ((files (append (directory-files dir t "Easkfile[.0-9]*\\'") - (directory-files dir t "Eask[.0-9]*\\'"))) - (files (cl-remove-if #'file-directory-p files))) + (when-let* ((files (append + (ignore-errors (directory-files dir t "Easkfile[.0-9]*\\'")) + (ignore-errors (directory-files dir t "Eask[.0-9]*\\'")))) + (files (cl-remove-if #'file-directory-p files))) (cl-remove-if-not #'eask--match-file files))) (defun eask-file-try-load (start-path) From 7486c95e80af0549b888973a5e376de43ce2e89c Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sat, 31 Dec 2022 16:05:48 +0800 Subject: [PATCH 5/6] Extract find-file --- lisp/_prepare.el | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lisp/_prepare.el b/lisp/_prepare.el index 4e09f73e..8ccafed6 100644 --- a/lisp/_prepare.el +++ b/lisp/_prepare.el @@ -633,8 +633,8 @@ If argument DIR is nil, we use `default-directory' instead." (files (cl-remove-if #'file-directory-p files))) (cl-remove-if-not #'eask--match-file files))) -(defun eask-file-try-load (start-path) - "Try load eask file in START-PATH. +(defun eask--find-files (start-path) + "Find the Eask-file from START-PATH. This uses function `locate-dominating-file' to look up directory tree." (when-let* @@ -648,8 +648,13 @@ This uses function `locate-dominating-file' to look up directory tree." ;; Make `Easkfile' > `Eask' higher precedent! (files (sort files (lambda (item1 item2) (and (string-prefix-p "Easkfile" item1) - (not (string-prefix-p "Easkfile" item2)))))) - (file (car files))) + (not (string-prefix-p "Easkfile" item2))))))) + files)) + +(defun eask-file-try-load (start-path) + "Try load eask file in START-PATH." + (when-let* ((files (eask--find-files start-path)) + (file (car files))) (eask-file-load file))) (defun eask--print-env-info () From 98193f475904ec8d41d52aadb970766bde93f112 Mon Sep 17 00:00:00 2001 From: Jen-Chieh Shen Date: Sat, 31 Dec 2022 16:11:56 +0800 Subject: [PATCH 6/6] improve doc --- lisp/_prepare.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lisp/_prepare.el b/lisp/_prepare.el index 8ccafed6..4bd62940 100644 --- a/lisp/_prepare.el +++ b/lisp/_prepare.el @@ -623,7 +623,7 @@ Eask file in the workspace." eask-full eask-major eask))))) (defun eask--all-files (&optional dir) - "Return a list of Eask files. + "Return a list of Eask files from DIR. If argument DIR is nil, we use `default-directory' instead." (setq dir (or dir default-directory))