Next: , Previous: , Up: Build Tools   [Index]


M.7 Preprocess Env Vars

The environment variable DEV can be in different locations and will be spelled differently based on how the local machine is set up. For instance, on one system, it will be at $HOME/Dev while in another system it will be at /usr/local/dev. However, the ‘:tangle’ keyword does not expand variables in the form ${DEV}, but rather requires absolute paths, like /usr/local/dev. Therefore, this program works like a preprocessor for environment variables set up as part of ‘:tangle’ lines, changing them to their system environment variable values prior to tangling. It lives in the resources/tools directory.

(with-current-buffer (car (find-file-noselect "./*.org" nil nil t))
  (save-excursion
  (goto-char (point-min))
  (let ((re-search-str "\\(?::tangle\\|load-file \\(?:[\\]*\\)?[\"]\\)\s*\\(.*?/[dD]ev\\)/")
        (dev (getenv "DEV")))
    (while
            (re-search-forward re-search-str nil t)
            (replace-match dev t nil nil 1)))
  (save-buffer)
  (require 'org)
  (org-babel-tangle)))