Created
September 21, 2010 10:45
-
-
Save miyamuko/589523 to your computer and use it in GitHub Desktop.
コマンドを実行することで変更される環境変数を取得する #xyzzy
This file contains 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
;; detect-environment-changes | |
;; 指定したコマンド実行後に変更された環境変数を取得する | |
;; | |
;; environment | |
;; 現在のプロセスの環境変数を取得する | |
;; | |
;; system | |
;; 指定したコマンドを実行し、実行結果と実行後の環境変数を取得する | |
(defconstant +boundary+ "--- pik --- pik --- pik --- pik ---") | |
(defun system (command args &key dump-env) | |
(flet ((q (s) | |
(if (string-matchp "[&<>()@^| ]" s) | |
(format nil "\"~A\"" s) | |
s))) | |
(let ((cmd (format nil "\"~A ~{~A~^ ~}\"~@[ && echo ~*~A && set~]" (q command) (mapcar #'q args) dump-env +boundary+)) | |
(buf (get-buffer-create "*Pik Output*"))) | |
(erase-buffer buf) | |
(unwind-protect | |
(let ((status (save-window-excursion | |
(execute-shell-command cmd nil buf)))) | |
(with-set-buffer | |
(set-buffer buf) | |
(let ((output (buffer-substring (point-min) | |
(if dump-env | |
(and (scan-buffer +boundary+ :tail nil) | |
(prog1 | |
(point) | |
(forward-line))) | |
(point-max)))) | |
(env (when dump-env | |
(buffer-substring (point) (point-max))))) | |
(values status output env)))) | |
(delete-buffer buf))))) | |
(defun parse-env (output) | |
(mapcar #'(lambda (line) | |
(let ((kv (split-string line #\= 2))) | |
(cons (car kv) (cadr kv)))) | |
(split-string output #\LFD))) | |
(defun environment () | |
(multiple-value-bind (status output env) | |
(system "set" nil) | |
(parse-env output))) | |
(defun detect-environment-changes (cmd opts) | |
(set-difference (multiple-value-bind (_ _ e) | |
(system cmd opts :dump-env t) | |
(parse-env e)) | |
(environment) | |
:test 'equal)) |
This file contains 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
;; Usage | |
(detect-environment-changes | |
(merge-pathnames "vsvars32.bat" (si:getenv "VS100COMNTOOLS")) | |
nil) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment