Skip to content

Instantly share code, notes, and snippets.

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)
(if-let [all-files (fltr fs)]
(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))))
(fn [fs f]
(let [f-path (c/tmp-path f)]
; (util/info "%s \n" f-path)
(doto (io/file tmp f-path) io/make-parents)
(reduce-kv #(.replace %1 %2 %3) (slurp (c/tmp-file f)) subs))))
; (fltr (c/fileset-diff @prev fs)))
; (reset! prev fs)
(-> fs
(c/rm all-files)
(c/add-resource tmp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment