Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
String replace in boot
(deftask string-replace
"Replace strings in sources."
[p path PATH regex "Path to replace in."
s subs SUBS {str str} "Strings to replace"
r role ROLE kw "Input role of file after substitution, :resource or :source (default: :source)"]
(let [tmp (c/tmp-dir!)
fltr #(c/by-re [path] (c/input-files %))
; prev (atom nil)
]
(c/with-pre-wrap
[fs]
(if-let [all-files (fltr fs)]
(time
(do
(c/empty-dir! tmp)
; (util/info "New files count: %s \n" (count (fltr (c/fileset-diff @prev fs))))
; (util/info "File count 2: %s \n" (count (c/by-re [path] (c/input-files fs))))
(reduce
(fn [fs f]
(let [f-path (c/tmp-path f)]
; (util/info "%s \n" f-path)
(spit
(doto (io/file tmp f-path) io/make-parents)
(reduce-kv #(.replace %1 %2 %3) (slurp (c/tmp-file f)) subs))))
all-files)
; (fltr (c/fileset-diff @prev fs)))
; (reset! prev fs)
(-> fs
(c/rm all-files)
(c/add-resource tmp)
(c/commit!))))
fs))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment