Last active
November 22, 2017 11:29
Revisions
-
chrismurrph revised this gist
Nov 19, 2017 . 1 changed file with 11 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,16 @@ (ns general.replace (:require [clojure.string :as s] [clojure.java.io :as io] [clojure.pprint :as pp])) (defn indexes-of [in-str find-str] (loop [idx 0 indexes []] (let [found-idx (s/index-of in-str find-str idx)] (if found-idx (recur (inc found-idx) (conj indexes found-idx)) indexes)))) (defn pp ([n x] (binding [pp/*print-right-margin* n] @@ -21,11 +28,13 @@ (defn show-in-file-hof [search-text] (fn [java-file] [search-text (.getName java-file) (indexes-of (slurp java-file) search-text)])) ;; ;; https://github.com/fulcrologic/fulcro/blob/2.0/README-fulcro-2.0.adoc ;; ;; For some reason .cljc files were not caught ;; (defn clojure-files [] (->> (directory->files "src" #".*\.clj|.*\.cljs|.*\.cljc") (map (juxt #(.getPath %) identity)))) -
chrismurrph revised this gist
Nov 19, 2017 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -26,8 +26,6 @@ ;; ;; https://github.com/fulcrologic/fulcro/blob/2.0/README-fulcro-2.0.adoc ;; (defn clojure-files [] (->> (directory->files "src" #".*\.clj|.*\.cljs|.*\.cljc") (map (juxt #(.getPath %) identity)))) -
chrismurrph created this gist
Nov 19, 2017 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,149 @@ (ns general.replace (:require [clojure.string :as s] [clojure.java.io :as io] [cljc.general.utils :as us] [clojure.pprint :as pp])) (defn pp ([n x] (binding [pp/*print-right-margin* n] (-> x pp/pprint))) ([x] (pp 120 x))) (defn directory->files [root-dir-path file-name-pattern] (filter #(re-matches file-name-pattern (.getName %)) (file-seq (io/file root-dir-path)))) (defn replace-in-file-hof [search-text replace-text] (fn [java-file] (spit java-file (s/replace (slurp java-file) search-text replace-text)))) (defn show-in-file-hof [search-text] (fn [java-file] [search-text (.getName java-file) (us/indexes-of (slurp java-file) search-text)])) ;; ;; https://github.com/fulcrologic/fulcro/blob/2.0/README-fulcro-2.0.adoc ;; ;; For some reason .cljc files were not caught ;; (defn clojure-files [] (->> (directory->files "src" #".*\.clj|.*\.cljs|.*\.cljc") (map (juxt #(.getPath %) identity)))) (defn edn-files [] (->> (directory->files "src" #".*\.edn") (map (juxt #(.getPath %) identity)))) (defn ->replacer-fns [xs] (mapv (fn [[from to]] (replace-in-file-hof from to)) xs)) (defn ->show-fns [xs] (mapv (fn [txt] (show-in-file-hof txt)) xs)) ;; ;; If these pairs ever become the same then you haven't excluded this file, ;; and subsequent replacements won't be effective! ;; (def from-tos [["om.next.impl.parser" "fulcro.client.impl.parser"] ["om.next.protocols" "fulcro.client.impl.protocols"] ;; Not reversible ["om.next.server" "fulcro.server"] ["om.dom" "fulcro.client.dom"] ["om.tempid" "fulcro.tempid"] ["om.util" "fulcro.util"] ["om.next" "fulcro.client.primitives"]]) ;; ;; If these pairs ever become the same then you haven't excluded this file, ;; and subsequent replacements won't be effective! ;; (def requires-from-tos [["om.next.impl.parser :as" "fulcro.client.impl.parser :as"] ["om.next.protocols :as" "fulcro.client.impl.protocols :as"] ;; Not reversible ["om.next.server :as" "fulcro.server :as"] ["om.dom :as" "fulcro.client.dom :as"] ["om.tempid :as" "fulcro.tempid :as"] ["om.util :as" "fulcro.util :as"] ["om.next :as" "fulcro.client.primitives :as"]]) (def exclude-f #(or (s/starts-with? % "src/main/fulcro") ;; Overwriting this present file will mean nothing happens! (s/ends-with? % "replace.clj"))) ;; File replace, to use as a manual 'play' test to verify ;; replacing works as you understand it. (defn play-test [] (let [replacements [["cljc.general.om-helpers :refer" "cljc.general.om-bad-helpers :refer"]] replacers (->replacer-fns replacements) files (->> (clojure-files) (remove #(-> % first exclude-f)) (filter #(s/starts-with? (first %) "src/main/accounting/test_data")))] (assert (= 1 (count files))) (assert (= 1 (count replacers))) (doseq [[_ java-file] files] (doseq [replacer! replacers] (replacer! java-file))))) (defn fulcro1->2 "require replacements for going from Fulcro 1 to Fulcro 2" [] (let [replacers (->replacer-fns requires-from-tos) files (->> (clojure-files) (remove #(-> % first exclude-f)))] (doseq [[_ java-file] files] (doseq [replace-all-in-file! replacers] (replace-all-in-file! java-file))))) ;; ;; If you don't crash your JVM (by asking to compile for instance) then using this to ;; reverse out all changes is possible. If crash it just use discard from version control. ;; (defn fulcro2->1 "require replacements for going from Fulcro 2 to Fulcro 1" [] (let [to-froms (mapv (comp vec reverse) requires-from-tos) replacer-fns (->replacer-fns to-froms) files (->> (clojure-files) (remove #(-> % first exclude-f)))] (doseq [[_ java-file] files] (doseq [replace-all-in-file! replacer-fns] (replace-all-in-file! java-file))))) (defn show-files [] (->> (clojure-files) (remove #(-> % first exclude-f)) (map first) (filter #(s/ends-with? % ".cljc")) pp)) (defn show-in-clojure-files [] (let [show-fns (->> from-tos (map first) ->show-fns) files (clojure-files)] (->> files (mapcat (fn [[_ java-file]] (map (fn [show] (show java-file)) show-fns))) (filter #(-> % last seq)) pp)))