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

Commit

Permalink
Only print stack trace if DEBUG is on; use c.j.io more.
Browse files Browse the repository at this point in the history
  • Loading branch information
technomancy committed Dec 26, 2012
1 parent a2a9c56 commit 89b46ba
Showing 1 changed file with 14 additions and 23 deletions.
37 changes: 14 additions & 23 deletions src/slam/hound.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
[slam.hound.stitch :refer [stitch-up]]) [slam.hound.stitch :refer [stitch-up]])
(:import (java.io File FileReader PushbackReader))) (:import (java.io File FileReader PushbackReader)))



(defn reconstruct [filename] (defn reconstruct [filename]
;; Reconstructing consists of three distinct phases: ;; Reconstructing consists of three distinct phases:
;; asploding, regrowing, and stitching. ;; asploding, regrowing, and stitching.
Expand All @@ -14,38 +13,30 @@
regrow regrow
stitch-up)) stitch-up))


(defn- stacktrace-to-str [^Exception e] (defn- swap-in-reconstructed-ns-form [file]
(cons (.getMessage e) (let [new-ns (.trim (reconstruct file))
(map #(str % "\n") (.getStackTrace e)))) rdr (PushbackReader. (io/reader file))]

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


(defn reconstruct-in-place (defn reconstruct-in-place
"Takes a file or directory and rewrites the files "Takes a file or directory and rewrites the files
with reconstructed ns forms." with reconstructed ns forms."
[file-or-dir] [file-or-dir]
(doseq [^File f (file-seq (if (string? file-or-dir) (doseq [file (file-seq (io/file file-or-dir))
(File. file-or-dir) :when (re-find #"/[^\./]+\.clj" (str file))]
file-or-dir))
:let [^String filename (.getName f)
^String file-path (.getAbsolutePath f)]
:when (and (.endsWith filename ".clj")
(not (.startsWith filename "."))
(not= filename "project.clj"))]
(try (try
(swap-in-reconstructed-ns-form file-path) (swap-in-reconstructed-ns-form file)
(catch Exception ex (catch Exception e
(println (str "Failed to reconstruct: " file-path (println "Failed to reconstruct:" file)
"\nException: " (stacktrace-to-str ex))))))) (if (System/getenv "DEBUG")
(.printStackTrace e)
(println (.getMessage e)))))))


(defn -main [file-or-dir] (defn -main [file-or-dir]
(reconstruct-in-place file-or-dir)) (reconstruct-in-place file-or-dir))

0 comments on commit 89b46ba

Please sign in to comment.