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
(use-package dotenv
:ensure nil
:quelpa
(dotenv :repo "pkulev/dotenv.el"
:fetcher github :upgrade t))
Hook for loading project’s .env (if exists) after switching project via projectile.
(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))
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
.
(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))