dotenv.el icon indicating copy to clipboard operation
dotenv.el copied to clipboard

Dotenv - Emacs plugin for loading project's .env file.

#+TITLE: dotenv.el --- Plugin for loading .env project files.

Dotenv parser by following rules:

  • skip empty lines
  • trim every string (all spaces before first non-whitespace char and after last non-whitespace char will be removed)
  • skip commentary lines (=#= at the line start)
  • skip lines which doesn't look like an proper assignment
  • variable name and its value will be trimmed
  • Installation ** use-package #+begin_src elisp (use-package dotenv :ensure nil :quelpa (dotenv :repo "pkulev/dotenv.el" :fetcher github :upgrade t)) #+end_src

  • Examples ** Integration with projectile Hook for loading project's /.env/ (if exists) after switching project via projectile. #+begin_src elisp (use-package dotenv :ensure nil :after projectile :quelpa (dotenv :repo "pkulev/dotenv.el" :fetcher github :upgrade t) :config (defun dotenv-projectile-hook () "Projectile hook." (dotenv-update-project-env (projectile-project-root)))

     (add-to-list 'projectile-after-switch-project-hook #'dotenv-projectile-hook))
    

    #+end_src

** Transform variable name or contents using =dotenv-transform-alist= In this example /.env/ file contains =PYTHONPATH= among other vaiables. Assuming that =PYTHONPATH= contains list of pathes (delimeted by '=:=') relative to the project's root we can transform it into absolute. To do that add to =dotenv-transform-alist= a pair of a predicate function and a transform function. Predicate function takes =k= and =v= and returns =t= or =nil=, transform function takes =k= and =v= and returns two-element list with new values for =k= and =v=. #+begin_src elisp (use-package dotenv :ensure nil :after projectile :quelpa (dotenv :repo "pkulev/dotenv.el" :fetcher github :upgrade t) :config (defun dotenv-absolutify-pythonpath (k v) (list k (dotenv-absolutify-path-var-in-project v)))

   (add-to-list
    'dotenv-transform-alist
    '((lambda (k v) (string= k "PYTHONPATH")) . dotenv-absolutify-pythonpath))

   (defun dotenv-projectile-hook ()
    "Projectile hook."
    (dotenv-update-project-env (projectile-project-root)))

   (add-to-list 'projectile-after-switch-project-hook #'dotenv-projectile-hook))

#+end_src

  • API reference ** Customizable variables |--------------------------+----------+---------------------------------------| | name | type | description | |--------------------------+----------+---------------------------------------| | =dotenv-file-name= | string | Dotenv file name (default: .env). | |--------------------------+----------+---------------------------------------| | =dotenv-transform-alist= | alist | List of predicate-transform pairs for | | | | custom key or/and value processing. | |--------------------------+----------+---------------------------------------|

** Public functions |---------------------------+-----------------------+-----------------------+-----------------------------| | name | arguments | returns | description | |---------------------------+-----------------------+-----------------------+-----------------------------| | dotenv-load | abs-path (str) | list of (k . v) pairs | Load .env from file. | | dotenv-get | key (str), path (str) | string | Returns string value | | | | | by provided key. | |---------------------------+-----------------------+-----------------------+-----------------------------| | dotenv-set | TODO | TODO | TODO | | dotenv-update-project-env | project-root (str) | nil | Updates project environment | | | | | with .env. | | dotenv-update-env | env-pairs, ?override | TODO | TODO | | dotenv-dump-env | | | | |---------------------------+-----------------------+-----------------------+-----------------------------|