Skip to content
This repository has been archived by the owner on Oct 2, 2020. It is now read-only.

Commit

Permalink
io/copy from the pushbackreader; added integration test of `reconstru…
Browse files Browse the repository at this point in the history
…ct-in-place`
  • Loading branch information
Alex Baranosky committed Dec 26, 2012
1 parent bedb943 commit 92892f6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
5 changes: 5 additions & 0 deletions resources/reconstructed_namespace.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(ns foo.bar
(:require [clojure.string :as str]))

(defn replace-commas [replacement]
(str/replace "asdf" #"," replacement))
7 changes: 7 additions & 0 deletions resources/test_namespace.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns foo.bar
(:require [clojure.string :as str]
[clojure.java.io :as io]
[clojure.set :as set]))

(defn replace-commas [replacement]
(str/replace "asdf" #"," replacement))
36 changes: 15 additions & 21 deletions src/slam/hound.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[slam.hound.asplode :refer [asplode]]
[slam.hound.regrow :refer [regrow]]
[slam.hound.stitch :refer [stitch-up]])
(:import (java.io File FileReader PushbackReader))))
(:import (java.io File FileReader PushbackReader)))


(defn reconstruct [filename]
Expand All @@ -18,30 +18,24 @@
(cons (.getMessage e)
(map #(str % "\n") (.getStackTrace e))))

(defn- non-whitespace-char? [ch]
(re-matches #"\S" (str ch)))

(defn- body-from-file [file-name old-ns-form]
(let [file-contents (slurp file-name)
num-non-white-chars-in-old-ns-form (count (filter non-whitespace-char? (str old-ns-form)))
non-white-so-far (atom 0)]
(apply str (drop-while (fn [ch]
(when (non-whitespace-char? ch)
(swap! non-white-so-far inc))
(< @non-white-so-far num-non-white-chars-in-old-ns-form))
file-contents))))

(defn- swap-in-reconstructed-ns-form [file]
(let [new-ns (.trim (reconstruct file))
old-ns-form (read (PushbackReader. (FileReader. file)))
body (body-from-file file old-ns-form)]
(spit file (str new-ns body))))
(defn- swap-in-reconstructed-ns-form [filename]
(let [new-ns (.trim (reconstruct filename))
rdr (PushbackReader. (FileReader. filename))]
;; scan past the namespace form
(read rdr)
;; copy in the reconstructed ns form
(io/copy new-ns (File. filename))
;; append the body
(with-open [writer (io/writer filename :append true)]
(io/copy rdr writer))))

(defn reconstruct-in-place
"Takes a file or directory and rewrites the files
with reconstructed ns forms."
[file-or-dir]
(doseq [^File f (file-seq (File. file-or-dir))
(doseq [^File f (file-seq (if (string? file-or-dir)
(File. file-or-dir)
file-or-dir))
:let [^String filename (.getName f)
^String file-path (.getAbsolutePath f)]
:when (and (.endsWith filename ".clj")
Expand All @@ -54,4 +48,4 @@
"\nException: " (stacktrace-to-str ex)))))))

(defn -main [file-or-dir]
(reconstruct-in-place file-or-dir))
(reconstruct-in-place file-or-dir))
15 changes: 12 additions & 3 deletions test/slam/hound_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns slam.hound-test
(:require [clojure.test :refer [deftest is testing]]
[slam.hound :refer [reconstruct]])
(:import java.io.StringReader))
(:require [clojure.java.io :as io]
[clojure.test :refer [deftest is testing]]
[slam.hound :refer [reconstruct reconstruct-in-place]])
(:import (java.io File StringReader)))


(def basic-ns (str '(ns slamhound.sample
Expand Down Expand Up @@ -77,3 +78,11 @@
'(defn do-it! []
(join "," ["a" "b" "c"])))))))))

(deftest ^:integration test-reconstruct-in-place
(let [tmp (doto (File/createTempFile "test_namespace_copy" ".clj")
.deleteOnExit)]
(io/copy (io/reader (io/resource "test_namespace.clj")) tmp)
(reconstruct-in-place tmp)

(is (= (slurp (io/resource "reconstructed_namespace.clj"))
(slurp tmp)))))

0 comments on commit 92892f6

Please sign in to comment.