aboutsummaryrefslogtreecommitdiff
path: root/src/clojure
diff options
context:
space:
mode:
authorStuart Sierra <mail@stuartsierra.com>2009-04-19 18:23:28 +0000
committerStuart Sierra <mail@stuartsierra.com>2009-04-19 18:23:28 +0000
commit98de5647ce68e29d6e3a200640dd388ca8cb3b05 (patch)
tree1be5e24974c04bff2918cabbc4872e4f6c2273c9 /src/clojure
parent721587466da5d45b68ee2c3e5aaa29a2cd7ff2d4 (diff)
Added new libs: jar.clj, classpath.clj, find_namespaces.clj
Diffstat (limited to 'src/clojure')
-rw-r--r--src/clojure/contrib/classpath.clj36
-rw-r--r--src/clojure/contrib/find_namespaces.clj120
-rw-r--r--src/clojure/contrib/jar.clj32
3 files changed, 188 insertions, 0 deletions
diff --git a/src/clojure/contrib/classpath.clj b/src/clojure/contrib/classpath.clj
new file mode 100644
index 00000000..e78686f5
--- /dev/null
+++ b/src/clojure/contrib/classpath.clj
@@ -0,0 +1,36 @@
+;;; classpath.clj: utilities for working with the Java class path
+
+;; by Stuart Sierra, http://stuartsierra.com/
+;; April 19, 2009
+
+;; Copyright (c) Stuart Sierra, 2009. All rights reserved. The use
+;; and distribution terms for this software are covered by the Eclipse
+;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+;; which can be found in the file epl-v10.html at the root of this
+;; distribution. By using this software in any fashion, you are
+;; agreeing to be bound by the terms of this license. You must not
+;; remove this notice, or any other, from this software.
+
+
+(ns clojure.contrib.classpath
+ (:require [clojure.contrib.jar :as jar])
+ (:import (java.io File)
+ (java.util.jar JarFile)))
+
+(defn classpath
+ "Returns a sequence of File objects of the elements on CLASSPATH."
+ []
+ (map #(File. %)
+ (.split (System/getProperty "java.class.path")
+ (System/getProperty "path.separator"))))
+
+(defn classpath-directories
+ "Returns a sequence of File objects for the directories on classpath."
+ []
+ (filter #(.isDirectory %) (classpath)))
+
+(defn classpath-jarfiles
+ "Returns a sequence of JarFile objects for the JAR files on classpath."
+ []
+ (map #(JarFile. %) (filter jar/jar-file? (classpath))))
+
diff --git a/src/clojure/contrib/find_namespaces.clj b/src/clojure/contrib/find_namespaces.clj
new file mode 100644
index 00000000..2964d61f
--- /dev/null
+++ b/src/clojure/contrib/find_namespaces.clj
@@ -0,0 +1,120 @@
+;;; find_namespaces.clj: search for ns declarations in dirs, JARs, or CLASSPATH
+
+;; by Stuart Sierra, http://stuartsierra.com/
+;; April 19, 2009
+
+;; Copyright (c) Stuart Sierra, 2009. All rights reserved. The use
+;; and distribution terms for this software are covered by the Eclipse
+;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+;; which can be found in the file epl-v10.html at the root of this
+;; distribution. By using this software in any fashion, you are
+;; agreeing to be bound by the terms of this license. You must not
+;; remove this notice, or any other, from this software.
+
+
+(ns clojure.contrib.find-namespaces
+ (:require [clojure.contrib.classpath :as cp]
+ [clojure.contrib.jar :as jar])
+ (import (java.io File FileReader BufferedReader PushbackReader
+ InputStreamReader)
+ (java.util.jar JarFile)))
+
+
+;;; Finding namespaces in a directory tree
+
+(defn clojure-source-file?
+ "Returns true if file is a normal file with a .clj extension."
+ [#^File file]
+ (and (.isFile file)
+ (.endsWith (.getName file) ".clj")))
+
+(defn find-clojure-sources-in-dir
+ "Searches recursively under dir for Clojure source files (.clj).
+ Returns a sequence of File objects, in breadth-first sort order."
+ [#^File dir]
+ ;; Use sort by absolute path to get breadth-first search.
+ (sort-by #(.getAbsolutePath %)
+ (filter clojure-source-file? (file-seq dir))))
+
+(defn read-ns-decl
+ "Attempts to read a (ns ...) declaration from rdr, and returns the
+ unevaluated form. Returns nil if read fails, or if the first form
+ is not a ns declaration."
+ [#^PushbackReader rdr]
+ (try (let [form (read rdr)]
+ (when (and (list? form) (= 'ns (first form)))
+ form))
+ (catch Exception e nil)))
+
+(defn read-file-ns-decl
+ "Attempts to read a (ns ...) declaration from file, and returns the
+ unevaluated form. Returns nil if read fails, or if the first form
+ is not a ns declaration."
+ [#^File file]
+ (with-open [rdr (PushbackReader. (BufferedReader. (FileReader. file)))]
+ (read-ns-decl rdr)))
+
+(defn find-ns-decls-in-dir
+ "Searches dir recursively for (ns ...) declarations in Clojure
+ source files; returns the unevaluated ns declarations."
+ [#^File dir]
+ (filter identity (map read-file-ns-decl (find-clojure-sources-in-dir dir))))
+
+(defn find-namespaces-in-dir
+ "Searches dir recursively for (ns ...) declarations in Clojure
+ source files; returns the symbol names of the declared namespaces."
+ [#^File dir]
+ (map second (find-ns-decls-in-dir dir)))
+
+
+;;; Finding namespaces in JAR files
+
+(defn clojure-sources-in-jar
+ "Returns a sequence of filenames ending in .clj found in the JAR file."
+ [#^JarFile jar-file]
+ (filter #(.endsWith % ".clj") (jar/filenames-in-jar jar-file)))
+
+(defn read-ns-decl-from-jarfile-entry
+ "Attempts to read a (ns ...) declaration from the named entry in the
+ JAR file, and returns the unevaluated form. Returns nil if the read
+ fails, or if the first form is not a ns declaration."
+ [#^JarFile jarfile #^String entry-name]
+ (with-open [rdr (PushbackReader.
+ (BufferedReader.
+ (InputStreamReader.
+ (.getInputStream jarfile (.getEntry jarfile entry-name)))))]
+ (read-ns-decl rdr)))
+
+(defn find-ns-decls-in-jarfile
+ "Searches the JAR file for Clojure source files containing (ns ...)
+ declarations; returns the unevaluated ns declarations."
+ [#^JarFile jarfile]
+ (filter identity
+ (map #(read-ns-decl-from-jarfile-entry jarfile %)
+ (clojure-sources-in-jar jarfile))))
+
+(defn find-namespaces-in-jarfile
+ "Searches the JAR file for Clojure source files containing (ns ...)
+ declarations. Returns a sequence of the symbol names of the
+ declared namespaces."
+ [#^JarFile jarfile]
+ (map second (find-ns-decls-in-jarfile jarfile)))
+
+
+;;; Finding namespaces anywhere on CLASSPATH
+
+(defn find-ns-decls-on-classpath
+ "Searches CLASSPATH (both directories and JAR files) for Clojure
+ source files containing (ns ...) declarations. Returns a sequence
+ of the unevaluated ns declaration forms."
+ []
+ (concat
+ (mapcat find-ns-decls-in-dir (cp/classpath-directories))
+ (mapcat find-ns-decls-in-jarfile (cp/classpath-jarfiles))))
+
+(defn find-namespaces-on-classpath
+ "Searches CLASSPATH (both directories and JAR files) for Clojure
+ source files containing (ns ...) declarations. Returns a sequence
+ of the symbol names of the declared namespaces."
+ []
+ (map second (find-ns-decls-on-classpath)))
diff --git a/src/clojure/contrib/jar.clj b/src/clojure/contrib/jar.clj
new file mode 100644
index 00000000..7eab2f5e
--- /dev/null
+++ b/src/clojure/contrib/jar.clj
@@ -0,0 +1,32 @@
+;;; jar.clj: utilities for working with Java JAR files
+
+;; by Stuart Sierra, http://stuartsierra.com/
+;; April 19, 2009
+
+;; Copyright (c) Stuart Sierra, 2009. All rights reserved. The use
+;; and distribution terms for this software are covered by the Eclipse
+;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+;; which can be found in the file epl-v10.html at the root of this
+;; distribution. By using this software in any fashion, you are
+;; agreeing to be bound by the terms of this license. You must not
+;; remove this notice, or any other, from this software.
+
+
+(ns clojure.contrib.jar
+ (:import (java.io File)
+ (java.util.jar JarFile)))
+
+(defn jar-file?
+ "Returns true if file is a normal file with a .jar or .JAR extension."
+ [#^File file]
+ (and (.isFile file)
+ (or (.endsWith (.getName file) ".jar")
+ (.endsWith (.getName file) ".JAR"))))
+
+(defn filenames-in-jar
+ "Returns a sequence of Strings naming the non-directory entries in
+ the JAR file."
+ [#^JarFile jar-file]
+ (map #(.getName %)
+ (filter #(not (.isDirectory %))
+ (enumeration-seq (.entries jar-file)))))