Skip to content

Commit

Permalink
Merge pull request #28 from montoux/ft-reader-conditonals
Browse files Browse the repository at this point in the history
Added support for reader conditional files (cljc)
  • Loading branch information
Raynes committed May 30, 2015
2 parents 752b1e5 + d8c12b6 commit 6c7d513
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 26 deletions.
13 changes: 8 additions & 5 deletions project.clj
@@ -1,10 +1,13 @@
(defproject bultitude "0.2.6"
(defproject bultitude "0.2.7-SNAPSHOT"
:min-lein-version "2.0.0"
:description "A library for find Clojure namespaces on the classpath."
:url "https://github.com/Raynes/bultitude"
:license {:name "Eclipse Public License 1.0"}
:dependencies [[org.clojure/clojure "1.4.0"]
:dependencies [[org.clojure/clojure "1.6.0"]
[org.tcrawley/dynapath "0.2.3"]]
:aliases {"test-all" ["with-profile" "dev,default:dev,1.3,default:dev,1.2,default" "test"]}
:profiles {:1.3 {:dependencies [[org.clojure/clojure "1.3.0"]]}
:1.2 {:dependencies [[org.clojure/clojure "1.2.1"]]}})
:aliases {"test-all" ["with-profile" "dev,1.7:dev,default:dev,1.5:dev,1.4:dev,1.3,dev" "test"]}
:profiles {:test {:resources ["test-resources"]}
:1.7 {:dependencies [[org.clojure/clojure "1.7.0-RC1"]]}
:1.5 {:dependencies [[org.clojure/clojure "1.5.1"]]}
:1.4 {:dependencies [[org.clojure/clojure "1.4.0"]]}
:1.3 {:dependencies [[org.clojure/clojure "1.3.0"]]}})
29 changes: 15 additions & 14 deletions src/bultitude/core.clj
Expand Up @@ -10,13 +10,12 @@
(declare namespace-forms-in-dir
file->namespace-forms)

(defn- clj? [^File f]
(and (not (.isDirectory f))
(.endsWith (.getName f) ".clj")))
(defn- clojure-source-file? [^File f]
(and (.isFile f)
(re-matches #".*\.cljc?" (.getName f))))

(defn- clj-jar-entry? [^JarEntry f]
(and (not (.isDirectory f))
(.endsWith (.getName f) ".clj")))
(defn- clojure-source-jar-entry? [^JarEntry f]
(re-matches #".*\.cljc?" (.getName f)))

(defn- jar? [^File f]
(and (.isFile f) (.endsWith (.getName f) ".jar")))
Expand Down Expand Up @@ -52,7 +51,7 @@
([dir] (namespace-forms-in-dir dir true))
([dir ignore-unreadable?]
(for [^File f (file-seq (io/file dir))
:when (and (clj? f) (.canRead f))
:when (and (clojure-source-file? f) (.canRead f))
:let [ns-form (ns-form-for-file f ignore-unreadable?)]
:when ns-form]
ns-form)))
Expand All @@ -73,7 +72,7 @@
(try
(let [jarfile (JarFile. jar)]
(for [entry (enumeration-seq (.entries jarfile))
:when (clj-jar-entry? entry)
:when (clojure-source-jar-entry? entry)
:let [ns-form (ns-form-in-jar-entry jarfile entry
ignore-unreadable?)]
:when ns-form]
Expand Down Expand Up @@ -155,12 +154,14 @@
(map second (apply namespace-forms-on-classpath args)))

(defn path-for
"Transform a namespace into a .clj file path relative to classpath root."
[namespace]
(str (-> (str namespace)
(.replace \- \_)
(.replace \. \/))
".clj"))
"Transform a namespace into a file path relative to classpath root, using the given extension (.clj default)."
([namespace]
(path-for namespace "clj"))
([namespace extension]
(str (-> (str namespace)
(.replace \- \_)
(.replace \. \/))
"." extension)))

(defn doc-from-ns-form
"Extract the docstring from a given ns form without evaluating the form. The docstring returned should be the return value of (:doc (meta namespace-symbol)) if the ns-form were to be evaluated."
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions test/bulti_tude/cond.cljc
@@ -0,0 +1 @@
(ns bulti-tude.cond)
19 changes: 12 additions & 7 deletions test/bultitude/core_test.clj
Expand Up @@ -5,19 +5,23 @@

(deftest namespaces-in-dir-test
(testing namespaces-in-dir
(is (= '(bulti-tude.test) (namespaces-in-dir "test/bulti_tude")))))
(is (= '#{bulti-tude.cond bulti-tude.test}
(set (namespaces-in-dir "test/bulti_tude"))))))

(deftest namespaces-forms-in-dir-test
(testing namespace-forms-in-dir
(is (= '((ns bulti-tude.test)) (namespace-forms-in-dir "test/bulti_tude")))))
(is (= '#{(ns bulti-tude.cond) (ns bulti-tude.test)}
(set (namespace-forms-in-dir "test/bulti_tude"))))))

(deftest file->namespaces-test
(testing "on a directory with a clj in it"
(is (= '(bulti-tude.test) (file->namespaces nil (io/file "test/bulti_tude"))))))
(is (= '#{bulti-tude.cond bulti-tude.test}
(set (file->namespaces nil (io/file "test/bulti_tude")))))))

(deftest file->namespace-forms-test
(testing "on a directory with a clj in it"
(is (= '((ns bulti-tude.test)) (file->namespace-forms nil (io/file "test/bulti_tude"))))))
(is (= '#{(ns bulti-tude.cond) (ns bulti-tude.test)}
(set (file->namespace-forms nil (io/file "test/bulti_tude")))))))

(deftest namespaces-on-classpath-test
(testing "find clojure.core"
Expand All @@ -37,7 +41,7 @@
(set (namespaces-on-classpath :prefix "bultitude")))))
(testing "dash handling in prefixes"
(is (=
#{'bulti-tude.test}
'#{bulti-tude.cond bulti-tude.test}
(set (namespaces-on-classpath :prefix "bulti-tude"))))))

(deftest namespace-forms-on-classpath-test
Expand Down Expand Up @@ -80,5 +84,6 @@
(in-ns callee-ns-name))))

(deftest test-invalid-namespace
(is (thrown? Exception
(ns-form-for-file (io/file "test/bultitude/invalid.clj" true)))))
(is (= nil (ns-form-for-file (io/file "test-resources/bultitude/invalid.clj") true)))
(is (thrown-with-msg? RuntimeException #"Map literal must contain an even number of forms"
(ns-form-for-file (io/file "test-resources/bultitude/invalid.clj") false))))

0 comments on commit 6c7d513

Please sign in to comment.