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

Commit

Permalink
adding ability to reconstruct-dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Baranosky committed Dec 25, 2012
1 parent 016920f commit f6f7c46
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
3 changes: 2 additions & 1 deletion project.clj
Expand Up @@ -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"]]}}
Expand Down
50 changes: 49 additions & 1 deletion src/slam/hound.clj
Expand Up @@ -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:
Expand All @@ -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))

0 comments on commit f6f7c46

Please sign in to comment.