Created
July 3, 2020 19:00
-
-
Save siversss/5b42d1141c2167ff960c63a0e1d7d010 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; -*- lexical-binding:t coding:utf-8-unix -*- | |
(defun 3s/env-update-path-var (name value &optional append separator) | |
(let ((sep (or separator path-separator))) | |
(when (not (s-blank? value)) | |
(let* ((name-val (getenv name)) | |
(components (when (not (s-blank? name-val)) | |
(s-split sep name-val)))) | |
(if (null components) | |
(setenv name value) | |
(setenv name | |
(s-join sep (if append | |
(-concat components (list value)) | |
(cons value components))))))))) | |
(defun 3s-env-update (data) | |
(-each data #'(lambda (v) | |
(let ((name (car v)) | |
(val (cdr v))) | |
(if (stringp val) | |
(setenv name val) | |
(let* ((old-val (getenv name)) | |
(old-val-l (if (s-present? old-val) | |
(s-split path-separator old-val t) | |
'()))) | |
(if (equal (car val) 'append) | |
(setenv name (s-join path-separator (-concat old-val-l (cdr val)))) | |
(setenv name (s-join path-separator (-concat val old-val-l)))))))))) | |
(defun 3s-env-update-and-run (f env-sets &optional use-current-env) | |
(let ((process-environment (copy-alist (if use-current-env process-environment initial-environment)))) | |
(--each env-sets (3s-env-update (if (symbolp it) (eval it) it))) | |
(funcall f))) | |
(defun 3s-env-wrap-func (f l &optional use-current-env) | |
(lambda (&rest args) | |
(if l | |
(3s-env-update-and-run | |
(lambda () (apply f args)) | |
l | |
use-current-env) | |
(apply f args)))) | |
(defmacro 3s:env! (init env-sets action &rest body) | |
(declare (indent defun)) | |
(let ((src-set (if (null init) 'initial-environment 'process-environment)) | |
(exp (cond | |
((eq action 'call) `((,@body))) | |
((eq action 'get) `((getenv ,@body))) | |
((eq action 'do) `(,@body)) | |
(t (error "unknown action"))))) | |
`(let ((process-environment (copy-alist ,src-set))) | |
(--each ,env-sets (3s-env-update (if (symbolp it) (eval it) it))) | |
(progn | |
,@exp)))) | |
(provide '3s-env) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment