Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
boot update file task
(ns myns.core
{:boot/export-tasks true}
[boot.core :as core]
[clojure.edn :as edn]
[ :as io]))
(core/deftask update-file
"Updates files whose path matches `match-files` with the value returned from `expr`.
`expr` is a function that takes the parsed content of the file and returns the new
content of the file. Optionally, you can specify `parse-fn`, a function that takes the
`slurp`ed content of the file and returns the parsed version. By default `parse-fn`
is set to `clojure.edn/read-string`."
[m match-files MATCH #{regex} "The set of regexes that the paths must match"
e expr VAL code "The function called to transform the content of the matched files"
p parse-fn VAL code "The function called to parse the files matched."]
(when-not (and match-files expr)
(throw (Exception. "need match-files and expr to update files.")))
(let [parse-fn (or parse-fn edn/read-string)
matching-tmp-files (->> fileset core/input-files (core/by-re match-files))
update-content (comp expr parse-fn slurp core/tmp-file)]
(reduce (fn [fileset tmp-file]
(let [new-dir (core/tmp-dir!)]
(-> (io/file new-dir (:path tmp-file))
(spit (update-content tmp-file)))
(core/add-resource fileset new-dir))) fileset matching-tmp-files)))))

This comment has been minimized.

Copy link

@zlorf zlorf commented Sep 9, 2016

You should add (io/make-parents new-dir (:path tmp-file)) before line 27 to make it work with nested paths.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.