Permalink
Browse files

generates ns with require/refer, and no use statement

  • Loading branch information...
1 parent b84e6b3 commit e2f7d95ddc5a3fa75b357286cf0e2debe4c3fe0b Alex Baranosky committed Dec 23, 2012
View
@@ -5,6 +5,6 @@
:dev-dependencies [[org.clojure/tools.trace "0.7.3"]
[org.clojars.runa/clj-schema "0.7.0"]]
:test-selectors {:default (constantly true)
- :integration :intgration
+ :integration :integration
:unit :unit})
@@ -1,7 +1,7 @@
(ns slam.hound.asplode
(:import java.io.PushbackReader))
-(defn ns-to-map [ns-form]
+(defn- ns-to-map [ns-form]
(let [[_ ns-name maybe-doc & clauses] ns-form
ns-meta (meta ns-name)
[ns-meta clauses] (if (string? maybe-doc)
View
@@ -25,10 +25,10 @@
(defn- failure-details [msg]
(when-let [sym (missing-sym-name msg)]
{:missing sym
- :possible-types (cond (class-name? sym) [:import :use]
- (re-find #"Unable to resolve var: \w+/" msg) [:require :use]
- (re-find #"No such (var|namespace)" msg) [:require]
- :else [:use :import])}))
+ :possible-types (cond (class-name? sym) [:import :require-refer]
+ (re-find #"Unable to resolve var: \w+/" msg) [:require-as :require-refer]
+ (re-find #"No such (var|namespace)" msg) [:require-as]
+ :else [:require-refer :import])}))
(defn- check-for-failure [ns-map body]
(let [sandbox-ns `slamhound.sandbox#
@@ -44,18 +44,17 @@
(remove-ns (.name *ns*)))))))
(defn candidates [type missing]
- (println "ALL NS::" (all-ns))
(case type
:import (for [class-name search/available-classes
:when (= missing (last (.split class-name "\\.")))]
(symbol class-name))
- :require (for [n (all-ns)
- :when (= missing (last (.split (name (ns-name n)) "\\.")))]
- [(ns-name n) :as (symbol missing)])
- :use (for [n (all-ns)
- [sym var] (ns-publics n)
- :when (= missing (name sym))]
- [(ns-name n) :only [sym]])))
+ :require-as (for [n (all-ns)
+ :when (= missing (last (.split (name (ns-name n)) "\\.")))]
+ [(ns-name n) :as (symbol missing)])
+ :require-refer (for [n (all-ns)
+ [sym var] (ns-publics n)
+ :when (= missing (name sym))]
+ [(ns-name n) :refer [sym]])))
(defn- butlast-regex [candidate]
(if (symbol? candidate)
@@ -71,11 +70,17 @@
@v
#"swank|lancet"))
+(defn- new-type-to-old-type [new-type]
+ (case new-type
+ :require-as :require
+ :require-refer :use
+ new-type))
+
(defn- disambiguate [candidates missing ns-map type]
;; TODO: prefer things in src/classes to jars
(debug :disambiguating missing :in candidates)
(->> candidates
- (sort-by (juxt (complement (in-original-pred (type (:old ns-map))))
+ (sort-by (juxt (complement (in-original-pred ((new-type-to-old-type type) (:old ns-map))))
(comp count str)))
(remove #(re-find disambiguator-blacklist (str %)))
first))
@@ -102,7 +107,6 @@
(let [type-idx (if (= last-missing missing)
(inc type-to-try)
0)]
- (println "ARGS::" [missing possible-types type-idx])
(if-let [type (get possible-types type-idx)]
(recur (grow-step missing type ns-map) missing type-idx)
(throw (Exception. (str "Couldn't resolve " missing
View
@@ -1,8 +1,7 @@
(ns slam.hound.stitch
- (:use [slam.hound.prettify :only [prettify]]
- clojure.tools.trace))
+ (:use [slam.hound.prettify :only [prettify]]))
-(def ^:private ns-clauses [:use :require :import])
+(def ^:private ns-map-clauses [:require-as :require-refer :import])
(def ^:private ns-clauses-to-preserve [:refer-clojure :gen-class :load])
@@ -17,37 +16,38 @@
(defn- group-by-namespace [uses]
(for [[namespace subclause] (group-by first uses)]
- [namespace :only (vec (sort (for [[_ _ [var]] subclause]
- var)))]))
+ [namespace :refer (vec (sort (for [[_ _ [var]] subclause]
+ var)))]))
(defn collapse-clause [ns-map clause]
(case clause
- :use (update-in ns-map [:use] group-by-namespace)
+ :require-refer (update-in ns-map [:require-refer] group-by-namespace)
:import (update-in ns-map [:import] group-by-package)
- ns-map))
+ :require-as ns-map))
(defn- collapse-clauses [ns-map]
- (reduce collapse-clause ns-map ns-clauses))
+ (reduce collapse-clause ns-map ns-map-clauses))
(defn sort-subclauses [ns-map]
;; lists aren't comparable? huh?
(reduce #(update-in %1 [%2] (partial sort-by str))
ns-map
- ns-clauses))
+ ns-map-clauses))
(defn ns-from-map [ns-map]
- `(~'ns ~(:name ns-map)
- ~@(if-let [doco (:doc (:meta ns-map))] ; avoid inserting nil
- [doco])
- ~@(for [clause-type (concat ns-clauses ns-clauses-to-preserve)
- :when (seq (clause-type ns-map))]
- (cons clause-type (clause-type ns-map)))))
+ (let [ns-map (-> ns-map ;; combining :require-as and :require-refer into :require
+ (assoc :require (concat (:require-as ns-map) (:require-refer ns-map)))
+ (dissoc :require-as :require-refer))]
+ `(~'ns ~(:name ns-map)
+ ~@(if-let [doco (:doc (:meta ns-map))] ; avoid inserting nil
+ [doco])
+ ~@(for [clause-type (concat [:require :import] ns-clauses-to-preserve)
+ :when (seq (clause-type ns-map))]
+ (cons clause-type (clause-type ns-map))))))
(defn stitch-up [ns-map]
(-> ns-map
collapse-clauses
sort-subclauses
ns-from-map
prettify))
-
-;; (trace-ns 'slam.hound.stitch)
@@ -1,31 +1,33 @@
(ns slam.hound.asplode-test
(:require [clojure.test :refer [deftest is]]
- [slam.hound.asplode :refer [ns-to-map asplode]]))
+ [slam.hound.asplode :refer [asplode]])
+ (:import java.io.StringReader))
-;; just inline this stuff?
-(def sample-ns-map
- {:name 'slamhound.sample
- :meta {:doc "Testing some things going on here."}
- :use '[[slam.hound.stitch :only [ns-from-map]]
- [clojure.test :only [is]]
- [clojure.test :only [deftest]]]
- :require '([clojure.java.io :as io] [clojure.set :as set])
- :import '(java.io.File java.io.ByteArrayInputStream
- clojure.lang.Compiler$BodyExpr java.util.UUID)
- :refer-clojure '(:exclude [compile test])})
+(deftest ^:unit test-asplode
+ (is (= [{:name 'slamhound.sample
+ :meta {:doc "Testing some things going on here."}
+ :refer-clojure '(:exclude [compile test])
+ :old {:name 'slamhound.sample
+ :meta {:doc "Testing some things going on here."}
+ :use '[[slam.hound.stitch :only [ns-from-map]]
+ [clojure.test :only [is]]
+ [clojure.test :only [deftest]]]
+ :require '([clojure.java.io :as io] [clojure.set :as set])
+ :import '(java.io.File java.io.ByteArrayInputStream
+ clojure.lang.Compiler$BodyExpr java.util.UUID)
+ :refer-clojure '(:exclude [compile test])}}
+ '((do something))]
-(def sample-ns-form '(ns slamhound.sample
- "Testing some things going on here."
- (:use [slam.hound.stitch :only [ns-from-map]]
- [clojure.test :only [is]]
- [clojure.test :only [deftest]])
- (:require [clojure.java.io :as io]
- [clojure.set :as set])
- (:import java.io.File java.io.ByteArrayInputStream
- clojure.lang.Compiler$BodyExpr
- java.util.UUID)
- (:refer-clojure :exclude [compile test])))
-
-(deftest ^:unit test-ns-to-map
- (is (= sample-ns-map (dissoc (ns-to-map sample-ns-form) :old))))
+ (asplode (StringReader. (str '(ns slamhound.sample
+ "Testing some things going on here."
+ (:use [slam.hound.stitch :only [ns-from-map]]
+ [clojure.test :only [is]]
+ [clojure.test :only [deftest]])
+ (:require [clojure.java.io :as io]
+ [clojure.set :as set])
+ (:import java.io.File java.io.ByteArrayInputStream
+ clojure.lang.Compiler$BodyExpr
+ java.util.UUID)
+ (:refer-clojure :exclude [compile test]))
+ '(do something)))))))
@@ -1,19 +1,8 @@
(ns slam.hound.regrow-test
- (:require [clojure.test :refer [deftest is]]
+ (:require [clojure.test :refer [deftest is testing]]
[slam.hound.regrow :refer [regrow in-original-pred]]))
-(def sample-ns-map
- {:name 'slamhound.sample
- :meta {:doc "Testing some things going on here."}
- :use '[[slam.hound.stitch :only [ns-from-map]]
- [clojure.test :only [is]]
- [clojure.test :only [deftest]]]
- :require '([clojure.java.io :as io] [clojure.set :as set])
- :import '(java.io.File java.io.ByteArrayInputStream
- clojure.lang.Compiler$BodyExpr java.util.UUID)
- :refer-clojure '(:exclude [compile test])})
-
(def sample-body
'((set/union #{:a} #{:b})
(UUID/randomUUID)
@@ -23,17 +12,36 @@
(deftest ^:unit test-ns-to-map
(is (= (ns-from-map {:ns 'slam.hound}))))))
-(deftest ^:unit test-grow-import
- (is (= sample-ns-map (regrow [(dissoc sample-ns-map :import)
- sample-body]))))
+(deftest ^:unit test-regrow
+ (testing "regrows the :require-as, :require-refer, and :import k/v pairs onto the ns-map"
+ (is (= {:name 'slamhound.sample
+ :meta {:doc "Testing some things going on here."}
+ :refer-clojure '(:exclude [compile test])
+ :require-as '([clojure.java.io :as io] [clojure.set :as set])
+ :require-refer '([slam.hound.stitch :refer [ns-from-map]] [clojure.test :refer [is]] [clojure.test :refer [deftest]])
+ :import '(java.io.File java.io.ByteArrayInputStream clojure.lang.Compiler$BodyExpr java.util.UUID)
+ :old {:name 'slamhound.sample
+ :meta {:doc "Testing some things going on here."}
+ :use '[[slam.hound.stitch :only [ns-from-map]] [clojure.test :only [is]] [clojure.test :only [deftest]]]
+ :require '([clojure.java.io :as io] [clojure.set :as set])
+ :import '(java.io.File java.io.ByteArrayInputStream clojure.lang.Compiler$BodyExpr java.util.UUID)
+ :refer-clojure '(:exclude [compile test])}}
+
+ (regrow [{:name 'slamhound.sample
+ :meta {:doc "Testing some things going on here."}
+ :refer-clojure '(:exclude [compile test])
+ :old {:name 'slamhound.sample
+ :meta {:doc "Testing some things going on here."}
+ :use '[[slam.hound.stitch :only [ns-from-map]]
+ [clojure.test :only [is]]
+ [clojure.test :only [deftest]]]
+ :require '([clojure.java.io :as io] [clojure.set :as set])
+ :import '(java.io.File java.io.ByteArrayInputStream
+ clojure.lang.Compiler$BodyExpr java.util.UUID)
+ :refer-clojure '(:exclude [compile test])}}
+ sample-body])))))
-(deftest ^:unit test-grow-require
- (is (= sample-ns-map (regrow [(dissoc sample-ns-map :require)
- sample-body]))))
-(deftest ^:unit test-grow-use
- (is (= sample-ns-map (regrow [(dissoc sample-ns-map :use)
- sample-body]))))
(deftest ^:unit test-grow-preserve
@@ -42,5 +50,4 @@
(is (not (in-orig? 'java.sql.Date))))
(is (= '(java.io.File java.util.Date)
(:import (regrow [{:old {:import '((java.util Date))}}
- '(vector (Date.) (File. "/tmp"))])))))
-
+ '(vector (Date.) (File. "/tmp"))])))))
@@ -4,11 +4,11 @@
(def sample-ns-form '(ns slamhound.sample
"Testing some things going on here."
- (:use [slam.hound.stitch :only [ns-from-map]]
- [clojure.test :only [is]]
- [clojure.test :only [deftest]])
(:require [clojure.java.io :as io]
- [clojure.set :as set])
+ [clojure.set :as set]
+ [slam.hound.stitch :refer [ns-from-map]]
+ [clojure.test :refer [is]]
+ [clojure.test :refer [deftest]])
(:import java.io.File java.io.ByteArrayInputStream
clojure.lang.Compiler$BodyExpr
java.util.UUID)
@@ -17,10 +17,10 @@
(def sample-ns-map
{:name 'slamhound.sample
:meta {:doc "Testing some things going on here."}
- :use '[[slam.hound.stitch :only [ns-from-map]]
- [clojure.test :only [is]]
- [clojure.test :only [deftest]]]
- :require '([clojure.java.io :as io] [clojure.set :as set])
+ :require-refer '[[slam.hound.stitch :refer [ns-from-map]]
+ [clojure.test :refer [is]]
+ [clojure.test :refer [deftest]]]
+ :require-as '([clojure.java.io :as io] [clojure.set :as set])
:import '(java.io.File java.io.ByteArrayInputStream
clojure.lang.Compiler$BodyExpr java.util.UUID)
:refer-clojure '(:exclude [compile test])})
@@ -32,10 +32,10 @@
(deftest ^:unit test-sort
(is (= {:name 'slamhound.sample
:meta {:doc "Testing some things going on here."}
- :use '[[clojure.test :only [deftest]]
- [clojure.test :only [is]]
- [slam.hound.stitch :only [ns-from-map]]]
- :require '([clojure.java.io :as io] [clojure.set :as set])
+ :require-refer '[[clojure.test :refer [deftest]]
+ [clojure.test :refer [is]]
+ [slam.hound.stitch :refer [ns-from-map]]]
+ :require-as '([clojure.java.io :as io] [clojure.set :as set])
:import '(clojure.lang.Compiler$BodyExpr
java.io.ByteArrayInputStream java.io.File java.util.UUID)
:refer-clojure '(:exclude [compile test])}
@@ -46,24 +46,25 @@
(java.io ByteArrayInputStream File)
(java.util UUID)]}
(collapse-clause {:import '(clojure.lang.Compiler$BodyExpr
- java.io.ByteArrayInputStream
- java.io.File java.util.UUID)}
- :import))))
+ java.io.ByteArrayInputStream
+ java.io.File java.util.UUID)}
+ :import))))
(deftest ^:unit test-collapse-use
- (is (= {:use '[[clojure.test :only [deftest is]]
- [slam.hound.stitch :only [ns-from-map]]]}
- (collapse-clause {:use '[[clojure.test :only [deftest]]
- [slam.hound.stitch :only [ns-from-map]]
- [clojure.test :only [is]]]}
- :use))))
+ (is (= {:require-refer '[[clojure.test :refer [deftest is]]
+ [slam.hound.stitch :refer [ns-from-map]]]}
+ (collapse-clause {:require-refer '[[clojure.test :refer [deftest]]
+ [slam.hound.stitch :refer [ns-from-map]]
+ [clojure.test :refer [is]]]}
+ :require-refer))))
(deftest ^:unit test-stitch-up
(is (= "(ns slamhound.sample
\"Testing some things going on here.\"
- (:use [clojure.test :only [deftest is]]
- [slam.hound.stitch :only [ns-from-map]])
- (:require [clojure.java.io :as io] [clojure.set :as set])
+ (:require [clojure.java.io :as io]
+ [clojure.set :as set]
+ [clojure.test :refer [deftest is]]
+ [slam.hound.stitch :refer [ns-from-map]])
(:import (clojure.lang Compiler$BodyExpr)
(java.io ByteArrayInputStream File)
(java.util UUID))
Oops, something went wrong.

0 comments on commit e2f7d95

Please sign in to comment.