Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@remleduff
Created March 18, 2010 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save remleduff/336731 to your computer and use it in GitHub Desktop.
Save remleduff/336731 to your computer and use it in GitHub Desktop.
(ns leiningen.repl
"Starts a REPL using the project's classpath."
(:use [leiningen.compile :only [eval-in-project find-lib-jars]])
(:require [clojure.main])
(:import [java.net URL URLClassLoader]
[java.io File FilenameFilter]))
(def *project*)
(def *classpath*)
(def ext-dirs (System/getProperty "java.ext.dirs"))
(def separator (System/getProperty "path.separator"))
(defn split-classpath [classpath]
(.split classpath separator))
(defn file->url [file]
"Returns the file:// URL for the provided java.io.File or filename"
(.toURL (File. (str file))))
(defn url->file [url]
"Returns the java.io.File referenced by the provided file:// URL"
(File. (.toURI url)))
(defn files-in-dir
([dir]
(let [dirFile (File. dir)]
(.listFiles dirFile)))
([dir suffix]
(let [dirFile (File. dir)
fileFilter (proxy [FilenameFilter] []
(accept [dir name] (.endsWith name suffix)))]
(.listFiles dirFile fileFilter))))
(def boot-classpath (.split (System/getProperty "sun.boot.class.path") separator
))
(def ext-classpath
(mapcat #(files-in-dir % ".jar") (.split (System/getProperty "java.ext.dirs")
separator)))
(defn make-classpath [& directoryNames]
(into-array (map file->url (concat directoryNames))))
(defn format-path [path]
(apply str (interpose separator (map url->file path))))
(defn repl
[project & args]
(let [system-classpath (make-classpath boot-classpath ext-classpath)
project-classpath (make-classpath (remove nil? [(:source-path project)
(:test-path project)
(:compile-path project)
(:resources-path project
)])
(map str (find-lib-jars project)))
classpath (concat system-classpath project-classpath)
classLoader (URLClassLoader. classpath nil)]
(.setContextClassLoader (Thread/currentThread) classLoader)
(System/setProperty "java.class.path" (format-path project-classpath))
(binding [*project* project, *classpath* classpath]
(clojure.main/repl))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment