Skip to content

Instantly share code, notes, and snippets.

@rauhs
Created November 8, 2017 16:28
Show Gist options
  • Save rauhs/a57186d3d37198a963d07df12109e999 to your computer and use it in GitHub Desktop.
Save rauhs/a57186d3d37198a963d07df12109e999 to your computer and use it in GitHub Desktop.
(defmacro with-temp-files
"Create a block where `varname` is a temporary `File` containing `content`."
[bindings & body]
(let [bindings (partition 2 bindings)]
`(let ~(into []
(mapcat
(fn [[bind-name content]]
`[~bind-name (File/createTempFile ~(str bind-name) ".json")]))
bindings)
~@(mapv
(fn [[bind-name content]]
`(io/copy ~content ~bind-name))
bindings)
(let [rv# (do ~@body)]
~@(mapv
(fn [[bind-name _]]
`(.delete ^File ~bind-name))
bindings)
rv#))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment