Last active
September 9, 2016 09:48
-
-
Save kennyjwilli/97f83cc49934bc08af4d3905487a9b49 to your computer and use it in GitHub Desktop.
boot update file task
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
(ns myns.core | |
{:boot/export-tasks true} | |
(:require | |
[boot.core :as core] | |
[clojure.edn :as edn] | |
[clojure.java.io :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."))) | |
(core/with-pre-wrap | |
fileset | |
(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)] | |
(core/commit! | |
(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))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should add
(io/make-parents new-dir (:path tmp-file))
before line 27 to make it work with nested paths.