Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Adding a "compilation phase" to Heroku's publishing process (a.k.a. slug compilation)
(use 'robert.hooke)
(require 'leiningen.deps)
(require 'lancet.core)
(import (java.io File)
(java.net URL URLClassLoader)
(java.lang.reflect Method))
;; based on code from leiningen.deps (version 1.6.1)
(defn- find-lib-jars [project]
(.listFiles (File. (:library-path project))))
(defn ^{:internal true} find-jars
[project]
(filter #(.endsWith (.getName %) ".jar")
(concat (find-lib-jars project)
(.listFiles (File. (:root project) "lib/dev")))))
(defn call [^String nm & args]
(when-let [fun (ns-resolve *ns* (symbol nm))]
(apply fun args)))
(defn add-to-cp [#^File jar]
(let [#^URL url (.. jar toURI toURL)
url-ldr-cls (. (URLClassLoader. (into-array URL [])) getClass)
arr-cls (into-array Class [(. url getClass)])
arr-obj (into-array Object [url])
#^Method mthd (. url-ldr-cls getDeclaredMethod "addURL" arr-cls)]
(doto mthd
(.setAccessible true)
(.invoke (ClassLoader/getSystemClassLoader) arr-obj))
(println (format "Added %s to classpath" jar))))
(defn wrapper [method & args]
(let [result (apply method args)
jars (find-jars (first args))]
(doseq [jar jars] (add-to-cp jar))
(require 'leiningen.mvn)
(call "leiningen.mvn/mvn" (first args) "package" "-DskipTests=true")
result))
(add-hook #'leiningen.deps/deps wrapper)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment