Skip to content

Instantly share code, notes, and snippets.

@olivergeorge
Created June 8, 2022 06:47
Show Gist options
  • Save olivergeorge/51fdd5008ac05cc5dda80a244f14c58e to your computer and use it in GitHub Desktop.
Save olivergeorge/51fdd5008ac05cc5dda80a244f14c58e to your computer and use it in GitHub Desktop.
(ns rewrite-field-spoec
(:require [clojure.java.io :as io]
[rewrite-clj.zip :as z]))
(defn file? [f] (.isFile f))
(defn get-path [f] (.getPath f))
(defn all-source-files
[dir-path]
(->> (io/file dir-path)
(file-seq)
(filter file?)
(map get-path)))
(defn massage-field-spec
[{:keys [type form/widget-type] :as field-spec}]
(cond-> field-spec
(and type (not widget-type))
(assoc :form/widget-type type)))
(defn process-file
[filename]
(prn filename)
(let [zloc (-> (z/of-file filename)
(z/up)
(z/postwalk
(fn select [zloc]
(let [x (z/sexpr zloc)]
(and (map? x)
(:type x)
(= (z/sexpr (z/left (z/up zloc))) :field-spec))))
(fn visit [zloc]
(z/edit zloc massage-field-spec))))]
(spit filename (z/root-string zloc))))
(comment (doall (map process-file (all-source-files "src/common/constants"))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment