forked from technomancy/leiningen
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Run compile and test tasks in isolated classloader
This means that we can: - Safely compile against alternative versions of Clojure. - Move classpath logic from the shell script to lein itself. - Run lein with: java -jar leiningen-standalone.jar This patch also makes the source, test and library paths configurable which is useful for people with special requirements like mixed-language projects.
- Loading branch information
Showing
9 changed files
with
118 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,69 @@ | ||
(ns leiningen.compile | ||
"Compile the namespaces listed in project.clj or all namespaces in src." | ||
(:require lancet) | ||
(:use [clojure.contrib.java-utils :only [file]] | ||
[clojure.contrib.find-namespaces :only [find-namespaces-in-dir]]) | ||
(:refer-clojure :exclude [compile])) | ||
[clojure.contrib.find-namespaces :only [find-namespaces-in-dir]] | ||
[leiningen.deps :only [deps]]) | ||
(:refer-clojure :exclude [compile]) | ||
(:import org.apache.tools.ant.taskdefs.Java | ||
(org.apache.tools.ant.types Environment$Variable Path))) | ||
|
||
(defn namespaces-to-compile | ||
"Returns a seq of the namespaces which need compiling." | ||
[project] | ||
(for [n (or (:namespaces project) | ||
(find-namespaces-in-dir (file (:source-path project)))) | ||
:let [ns-file (str (-> (name n) | ||
(.replaceAll "\\." "/") | ||
(.replaceAll "-" "_")))] | ||
:when (> (.lastModified (file (:source-path project) | ||
(str ns-file ".clj"))) | ||
(.lastModified (file (:compile-path project) | ||
(str ns-file "__init.class"))))] | ||
n)) | ||
|
||
(defn find-lib-jars | ||
"Returns a seq of Files for all the jars in the project's library directory." | ||
[project] | ||
(filter #(.endsWith (.getName %) ".jar") | ||
(file-seq (file (:library-path project))))) | ||
|
||
(defn make-path | ||
"Constructs an ant Path object from Files and strings." | ||
[& paths] | ||
(let [ant-path (Path. nil)] | ||
(doseq [path paths] | ||
(.addExisting ant-path (Path. nil (str path)))) | ||
ant-path)) | ||
|
||
(defn eval-in-project | ||
"Executes form in an isolated classloader with the classpath and compile path | ||
set correctly for the project." | ||
[project form] | ||
(let [java (Java.)] | ||
(.setProject java lancet/ant-project) | ||
(.addSysproperty java (doto (Environment$Variable.) | ||
(.setKey "clojure.compile.path") | ||
(.setValue (:compile-path project)))) | ||
(.setClasspath java (apply make-path | ||
(:source-path project) | ||
(:test-path project) | ||
(:compile-path project) | ||
(find-lib-jars project))) | ||
(.setClassname java "clojure.main") | ||
(.setValue (.createArg java) "-e") | ||
(.setValue (.createArg java) (prn-str form)) | ||
(.execute java))) | ||
|
||
(defn compile | ||
"Ahead-of-time compile the project. Looks for all namespaces under src/ | ||
unless a list of :namespaces is provided in project.clj." | ||
unless a list of :namespaces is provided in project.clj." | ||
[project] | ||
;; TODO: use a java subprocess in case a different clojure version is needed | ||
(doseq [n (or (:namespaces project) | ||
(find-namespaces-in-dir (file (:root project) "src")))] | ||
(let [ns-file (str (-> (name n) | ||
(.replaceAll "\\." "/") | ||
(.replaceAll "-" "_")))] | ||
(when (> (.lastModified (file (:root project) "src" (str ns-file ".clj"))) | ||
(.lastModified (file (:root project) "classes" | ||
(str ns-file "__init.class")))) | ||
(println "Compiling" n) | ||
(clojure.core/compile n))))) | ||
(deps project) | ||
(.mkdir (file (:compile-path project))) | ||
(let [namespaces (namespaces-to-compile project)] | ||
(when (seq namespaces) | ||
(eval-in-project project | ||
`(doseq [namespace# '~namespaces] | ||
(println "Compiling" namespace#) | ||
(clojure.core/compile namespace#)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters