Permalink
Browse files

Merge commit '92892f6'

Conflicts:
	test/slam/hound_test.clj
  • Loading branch information...
2 parents 56b5b6b + 92892f6 commit a2a9c56ac396d5a999bd70d422962664da7b39e5 @technomancy committed Dec 26, 2012
Showing with 67 additions and 7 deletions.
  1. +5 −0 dev-resources/reconstructed_namespace.clj
  2. +7 −0 dev-resources/test_namespace.clj
  3. +1 −0 project.clj
  4. +42 −4 src/slam/hound.clj
  5. +12 −3 test/slam/hound_test.clj
@@ -0,0 +1,5 @@
+(ns foo.bar
+ (:require [clojure.string :as str]))
+
+(defn replace-commas [replacement]
+ (str/replace "asdf" #"," replacement))
@@ -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))
View
@@ -3,6 +3,7 @@
:url "https://github.com/technomancy/slamhound"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
+ :main slam.hound
:dependencies [[org.clojure/clojure "1.4.0"]]
:profiles {:dev {:dependencies [[org.clojure/tools.trace "0.7.3"]
[org.clojars.runa/clj-schema "0.7.0"]
View
@@ -1,8 +1,10 @@
(ns slam.hound
- (: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]
+ [slam.hound.asplode :refer [asplode]]
+ [slam.hound.regrow :refer [regrow]]
+ [slam.hound.stitch :refer [stitch-up]])
+ (:import (java.io File FileReader PushbackReader)))
+
(defn reconstruct [filename]
;; Reconstructing consists of three distinct phases:
@@ -11,3 +13,39 @@
asplode
regrow
stitch-up))
+
+(defn- stacktrace-to-str [^Exception e]
+ (cons (.getMessage e)
+ (map #(str % "\n") (.getStackTrace e))))
+
+(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 (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")
+ (not (.startsWith filename "."))
+ (not= filename "project.clj"))]
+ (try
+ (swap-in-reconstructed-ns-form file-path)
+ (catch Exception ex
+ (println (str "Failed to reconstruct: " file-path
+ "\nException: " (stacktrace-to-str ex)))))))
+
+(defn -main [file-or-dir]
+ (reconstruct-in-place file-or-dir))
View
@@ -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
"Testing some things going on here."
@@ -84,3 +85,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 a2a9c56

Please sign in to comment.