Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding ability to `reconstruct-dir`

  • Loading branch information...
commit f6f7c46dace3342f25014140fcd9a4c2765016fd 1 parent 016920f
Alex Baranosky authored
Showing with 51 additions and 2 deletions.
  1. +2 −1  project.clj
  2. +49 −1 src/slam/hound.clj
3  project.clj
View
@@ -3,7 +3,8 @@
:url "https://github.com/technomancy/slamhound"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
- :dependencies [[org.clojure/clojure "1.4.0"]]
+ :dependencies [[org.clojure/clojure "1.4.0"]
+ [fs "1.3.2"]]
:profiles {:dev {:dependencies [[org.clojure/tools.trace "0.7.3"]
[org.clojars.runa/clj-schema "0.7.0"]
[korma "0.3.0-beta11"]]}}
50 src/slam/hound.clj
View
@@ -2,7 +2,10 @@
(:use [slam.hound.asplode :only [asplode]]
[slam.hound.regrow :only [regrow]]
[slam.hound.stitch :only [stitch-up]])
- (:require [clojure.java.io :as io]))
+ (:require [clojure.java.io :as io]
+ [fs.core :as fs])
+ (:import (java.io FileReader
+ PushbackReader)))
(defn reconstruct [filename]
;; Reconstructing consists of three distinct phases:
@@ -11,3 +14,48 @@
asplode
regrow
stitch-up))
+
+(defn- stacktrace-to-str [^Exception e]
+ (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)))]
+ (apply str (loop [non-white-so-far 0
+ file-contents-remaining file-contents]
+ (cond (>= non-white-so-far num-non-white-chars-in-old-ns-form)
+ file-contents-remaining
+
+ (non-whitespace-char? (first file-contents-remaining))
+ (recur (inc non-white-so-far) (rest file-contents-remaining))
+
+ :else
+ (recur non-white-so-far (rest file-contents-remaining)))))))
+
+(defn- swap-in-reconstructed-ns-form [file-name]
+ (let [new-ns (.trim (reconstruct file-name))
+ old-ns-form (read (PushbackReader. (FileReader. file-name)))
+ body (body-from-file file-name old-ns-form)]
+ (spit file-name (str new-ns body))))
+
+(defn- reconstruct-dir* [root _dirs_ files]
+ (doseq [f files
+ :when (and (.endsWith f ".clj")
+ (not (.startsWith f "."))
+ (not= f "project.clj"))
+ :let [file-path (fs/file root f)]]
+ (try
+ (swap-in-reconstructed-ns-form file-path)
+ (catch Exception ex
+ (print (str "Failed to reconstruct: " (.getName file-path)
+ "\nException: " (stacktrace-to-str ex)))))))
+
+(defn reconstruct-dir
+ "Reconstructs every file ending in .clj, except project.clj, and writes
+ them back into the original files."
+ [dir]
+ (fs/walk reconstruct-dir* dir))
Please sign in to comment.
Something went wrong with that request. Please try again.