Skip to content

Instantly share code, notes, and snippets.

@vikeri
Created December 29, 2016 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vikeri/37f178987a4d6ac251ab936ff3882467 to your computer and use it in GitHub Desktop.
Save vikeri/37f178987a4d6ac251ab936ff3882467 to your computer and use it in GitHub Desktop.
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