aboutsummaryrefslogtreecommitdiff
path: root/modules/find-namespaces
diff options
context:
space:
mode:
authorStuart Sierra <mail@stuartsierra.com>2010-08-07 16:41:53 -0400
committerStuart Sierra <mail@stuartsierra.com>2010-08-07 16:41:53 -0400
commita6a92b9b3d2bfd9a56e1e5e9cfba706d1aeeaae5 (patch)
treef1f3da9887dc2dc557df3282b0bcbd4d701ec593 /modules/find-namespaces
parente7930c85290f77815cdb00a60604feedfa2d0194 (diff)
Split all namespaces into sub-modules.
* Examples and tests have not been copied over. * Clojure test/compile phases are commented out in parent POM. * May require installing parent POM before full build.
Diffstat (limited to 'modules/find-namespaces')
-rw-r--r--modules/find-namespaces/pom.xml26
-rw-r--r--modules/find-namespaces/src/main/clojure/clojure/contrib/find_namespaces.clj136
2 files changed, 162 insertions, 0 deletions
diff --git a/modules/find-namespaces/pom.xml b/modules/find-namespaces/pom.xml
new file mode 100644
index 00000000..bca2b58b
--- /dev/null
+++ b/modules/find-namespaces/pom.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http//www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
+ http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.clojure.contrib</groupId>
+ <artifactId>parent</artifactId>
+ <version>1.3.0-SNAPSHOT</version>
+ <relativePath>../parent</relativePath>
+ </parent>
+ <artifactId>find-namespaces</artifactId>
+ <dependencies>
+ <dependency>
+ <groupId>org.clojure.contrib</groupId>
+ <artifactId>classpath</artifactId>
+ <version>1.3.0-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>org.clojure.contrib</groupId>
+ <artifactId>jar</artifactId>
+ <version>1.3.0-SNAPSHOT</version>
+ </dependency>
+ </dependencies>
+</project> \ No newline at end of file
diff --git a/modules/find-namespaces/src/main/clojure/clojure/contrib/find_namespaces.clj b/modules/find-namespaces/src/main/clojure/clojure/contrib/find_namespaces.clj
new file mode 100644
index 00000000..928499c7
--- /dev/null
+++ b/modules/find-namespaces/src/main/clojure/clojure/contrib/find_namespaces.clj
@@ -0,0 +1,136 @@
+;;; 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
+ ^{:author "Stuart Sierra",
+ :doc "Search for ns declarations in dirs, JARs, or CLASSPATH"}
+ 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 comment?
+ "Returns true if form is a (comment ...)"
+ [form]
+ (and (list? form) (= 'comment (first form))))
+
+(defn ns-decl?
+ "Returns true if form is a (ns ...) declaration."
+ [form]
+ (and (list? form) (= 'ns (first form))))
+
+(defn read-ns-decl
+ "Attempts to read a (ns ...) declaration from rdr, and returns the
+ unevaluated form. Returns nil if read fails or if a ns declaration
+ cannot be found. The ns declaration must be the first Clojure form
+ in the file, except for (comment ...) forms."
+ [^PushbackReader rdr]
+ (try (let [form (read rdr)]
+ (cond
+ (ns-decl? form) form
+ (comment? form) (recur rdr)
+ :else nil))
+ (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)))