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

Checks if it's an obelisk project #93

Closed
wants to merge 2 commits into from
Closed
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
24 changes: 23 additions & 1 deletion dante.el
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,43 @@ will be in different GHCi sessions."

(put 'dante-target 'safe-local-variable #'stringp)

;; A set for saving obelisk projects' root directories.
(setq obelisk-projects-dirs (make-hash-table :test 'equal))

(defun dante-project-root ()
"Get the root directory for the project.
If `dante-project-root' is set as a variable, return that,
otherwise look for a .cabal file, or use the current dir."
(file-name-as-directory
(or dante-project-root
(set (make-local-variable 'dante-project-root)
(file-name-directory (or (dante-cabal-find-file) (dante-buffer-file-name)))))))
(file-name-directory (or (obelisk-project-root)
(dante-cabal-find-file)
(dante-buffer-file-name)))))))

(defun obelisk-project-root ()
"Checks if a source file is a 'ob init' project's file stucture. In case it is, returns project's root
directory, also saves that directory in `obelisk-projects-dirs' set data structure so that `dante-repl-by-file'
can check faster is the project it is an obelisk project "
(let* ((file-dir-as-list (--filter (not (string= "" it)) (split-string (file-name-directory default-directory) "/")))
(obelisk-folder-stucture? (--any? (-contains? file-dir-as-list it) '("backend" "common" "frontend"))))
(when obelisk-folder-stucture?
(let* ((obelisk-root-folder (--take-while (-none? (lambda (a) (string= it a)) '("backend" "common" "frontend"))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should instead check for .obelisk/impl as well?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Let's say you've opened a file in /home/elgusy/stuff/obelisk/learning-reflex/frontend/src/my-lib/a.hs. Then we check if one of ob's default source files containing folders (common, backend, frontend) is in this path. In case it is, we take a probable project's root directory and then finally we check/confirm if that root directory contains .obelisk/impl folder

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I don't understand. What would happen if you check for existence of just .obelisk/impl (instead of those source folders)?

Copy link
Author

@elgusy elgusy Oct 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To check the existence of the folder .obelisk/impl I need to provide its parent directory, the obelisk root folder, in my above example, - learning-reflex. To track that root folder I check if either of the strings (common, backend, frontend) is contained in the opened source file's path.

file-dir-as-list))
(project-root (string-join (-concat '("") obelisk-root-folder `("")) "/")))
(if (gethash project-root obelisk-projects-dirs nil)
project-root
(when (file-directory-p (concat project-root ".obelisk/impl"))
(puthash project-root t obelisk-projects-dirs)
project-root))))))

(defun dante-repl-by-file (root files cmdline)
"Return if ROOT / file exists for any file in FILES, return CMDLINE."
(when (-any? (lambda (file) (file-exists-p (concat root file))) files) cmdline))

(defcustom dante-repl-command-line-methods-alist
`((styx . ,(lambda (root) (dante-repl-by-file root '("styx.yaml") '("styx" "repl" dante-target))))
(obelisk . ,(lambda (root) (when (gethash root obelisk-projects-dirs nil) '("ob" "repl"))))
(nix . ,(lambda (root) (dante-repl-by-file root '("shell.nix" "default.nix")
'("nix-shell" "--pure" "--run" (concat "cabal repl " (or dante-target "") " --builddir=dist/dante")))))
(impure-nix
Expand Down