mirror of
https://github.com/SqrtMinusOne/micromamba.el.git
synced 2025-12-10 13:23:02 +03:00
Remove dependency on f.el
This commit is contained in:
parent
452bd0fe1e
commit
148e36e326
1 changed files with 22 additions and 45 deletions
|
|
@ -199,39 +199,40 @@ Returns an alist with the following keys:
|
||||||
(vars-export . ,vars-export)
|
(vars-export . ,vars-export)
|
||||||
(scripts . ,scripts))))
|
(scripts . ,scripts))))
|
||||||
|
|
||||||
(defun micromamba--env-dir-is-valid (candidate)
|
|
||||||
"Confirm that CANDIDATE is a valid conda environment."
|
|
||||||
(let ((dir (file-name-as-directory candidate)))
|
|
||||||
(and (not (s-blank? candidate))
|
|
||||||
(f-directory? dir)
|
|
||||||
(or (f-directory? (concat dir micromamba-env-executables-dir))
|
|
||||||
(f-directory? (concat dir micromamba-env-meta-dir))))))
|
|
||||||
|
|
||||||
(defun micromamba--contains-env-yml? (candidate) ;; adapted from conda.el
|
|
||||||
"Does CANDIDATE contain an environment.yml?"
|
|
||||||
(f-exists? (f-expand "environment.yml" candidate)))
|
|
||||||
|
|
||||||
(defun micromamba--find-env-yml (dir) ;; adapted from conda.el
|
(defun micromamba--find-env-yml (dir) ;; adapted from conda.el
|
||||||
"Find an environment.yml in DIR or its parent directories."
|
"Find an environment.yml or .yaml in DIR or its parent directories."
|
||||||
;; TODO: implement an optimized finder with e.g. projectile? Or a series of
|
(let ((containing-path (locate-dominating-file dir
|
||||||
;; finder functions, that stop at the project root when traversing
|
(lambda (parent)
|
||||||
(let ((containing-path (f-traverse-upwards 'micromamba--contains-env-yml? dir)))
|
(directory-files parent nil "environment.[yml|yaml]")))))
|
||||||
(when containing-path
|
(when containing-path
|
||||||
(f-expand "environment.yml" containing-path))))
|
(let ((yml-candidate
|
||||||
|
(concat (file-name-as-directory containing-path) "environment.yml"))
|
||||||
|
(yaml-candidate
|
||||||
|
(concat (file-name-as-directory containing-path) "environment.yaml")))
|
||||||
|
(or (when (file-readable-p yml-candidate) yml-candidate)
|
||||||
|
(when (file-readable-p yaml-candidate) yaml-candidate))))))
|
||||||
|
|
||||||
|
(defun micromamba--read-file-into-string (filename)
|
||||||
|
"Read the contents of FILENAME into a string."
|
||||||
|
(with-temp-buffer
|
||||||
|
(let ((coding-system-for-read 'utf-8))
|
||||||
|
(insert-file-contents filename)
|
||||||
|
(buffer-string))))
|
||||||
|
|
||||||
(defun micromamba--get-name-from-env-yml (filename) ;; adapted from conda.el
|
(defun micromamba--get-name-from-env-yml (filename) ;; adapted from conda.el
|
||||||
"Pull the `name` property out of the YAML file at FILENAME."
|
"Pull the `name` property out of the YAML file at FILENAME."
|
||||||
;; TODO: find a better way than slurping it in and using a regex...
|
|
||||||
(when filename
|
(when filename
|
||||||
(let ((env-yml-contents (f-read-text filename)))
|
(let ((env-yml-contents (micromamba--read-file-into-string filename)))
|
||||||
(when (string-match "name:[ ]*\\([A-z0-9-_.]+\\)[ ]*$" env-yml-contents)
|
(when (string-match "name:[ ]*\\([A-z0-9-_.]+\\)[ ]*$" env-yml-contents)
|
||||||
(match-string 1 env-yml-contents)))))
|
(match-string 1 env-yml-contents)))))
|
||||||
|
|
||||||
(defun micromamba--infer-env-from-buffer () ;; adapted from conda.el
|
(defun micromamba--infer-env-from-buffer () ;; adapted from conda.el
|
||||||
"Search up the project tree for an `environment.yml` defining a conda env."
|
"Search up the project tree for an `environment.yml` defining a conda env.
|
||||||
|
|
||||||
|
Return `micromamba-fallback-environment' if not found."
|
||||||
(let* ((filename (buffer-file-name))
|
(let* ((filename (buffer-file-name))
|
||||||
(working-dir (if filename
|
(working-dir (if filename
|
||||||
(f-dirname filename)
|
(file-name-directory filename)
|
||||||
default-directory)))
|
default-directory)))
|
||||||
(when working-dir
|
(when working-dir
|
||||||
(or
|
(or
|
||||||
|
|
@ -276,30 +277,6 @@ The parameters value is an alist as defined by
|
||||||
(setq eshell-path-env (getenv "PATH")))
|
(setq eshell-path-env (getenv "PATH")))
|
||||||
|
|
||||||
;; "public" functions
|
;; "public" functions
|
||||||
|
|
||||||
(defun micromamba-env-default-location ()
|
|
||||||
"Default location of the conda environments -- under the Anaconda installation."
|
|
||||||
(let ((candidates (alist-get 'envs_dirs (micromamba--get-config))))
|
|
||||||
(f-full (aref candidates 0))))
|
|
||||||
|
|
||||||
|
|
||||||
(defun micromamba-env-name-to-dir (name)
|
|
||||||
"Translate NAME to the directory where the environment is located."
|
|
||||||
(if (and (string= name "base")
|
|
||||||
(micromamba--env-dir-is-valid micromamba-home))
|
|
||||||
(file-name-as-directory (expand-file-name micromamba-home))
|
|
||||||
(let* ((default-location (file-name-as-directory (micromamba-env-default-location)))
|
|
||||||
(initial-possibilities (list name (concat default-location name)))
|
|
||||||
(possibilities (if (boundp 'venv-location)
|
|
||||||
(if (stringp venv-location)
|
|
||||||
(cons venv-location initial-possibilities)
|
|
||||||
(nconc venv-location initial-possibilities))
|
|
||||||
initial-possibilities))
|
|
||||||
(matches (-filter 'micromamba--env-dir-is-valid possibilities)))
|
|
||||||
(if (> (length matches) 0)
|
|
||||||
(file-name-as-directory (expand-file-name (car matches)))
|
|
||||||
(error "No such conda environment: %s" name)))))
|
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun micromamba-activate (prefix)
|
(defun micromamba-activate (prefix)
|
||||||
"Switch to environment with PREFIX (path). Prompt if called interactively.
|
"Switch to environment with PREFIX (path). Prompt if called interactively.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue