Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(_prepare.el): Improve Eask-file loader to search file upward #89

Merged
merged 6 commits into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 48 additions & 9 deletions lisp/_prepare.el
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
(require 'url-vars)

(require 'cl-lib)
(require 'files)
(require 'ls-lisp)
(require 'pp)
(require 'rect)
Expand Down Expand Up @@ -609,14 +610,52 @@ 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 from DIR.

If argument DIR is nil, we use `default-directory' instead."
(setq dir (or dir default-directory))
(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--find-files (start-path)
"Find the Eask-file from 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)))))))
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 ()
"Display environment information at the very top of the execution."
Expand Down Expand Up @@ -678,7 +717,7 @@ Eask file in the workspace."
(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")))
Expand Down
1 change: 1 addition & 0 deletions test/development/compat.el
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down