Skip to content

Instantly share code, notes, and snippets.

@na-ka-na
Created May 27, 2010 00:36
Show Gist options
  • Save na-ka-na/415269 to your computer and use it in GitHub Desktop.
Save na-ka-na/415269 to your computer and use it in GitHub Desktop.
(ns a)
(use '[clojure.contrib.io :only (make-parents spit delete-file)])
(use '[clojure.contrib.shell :only (sh)])
(use '[clojure.contrib.str-utils :only (re-split str-join)])
(defn- convert-fn-meta-into-javadoc
[impl-ns prefix mname pclasses rclass is-static?]
(let [m (meta (resolve (symbol (str (str impl-ns) "/" prefix (str mname)))))
{:keys [file line doc]} m
arglist (first (filter #(= (count pclasses) (count %)) (:arglists m)))
method-sign (str "public "
(if is-static? "static " "")
(str rclass) " "
(str mname) " ("
(apply str (drop-last (interleave pclasses (repeat " ") arglist (repeat ", "))))
") {}")]
(str
" /**\n"
" * " (str-join "\n * " (re-split #"\n" doc)) "\n"
" * Definition present at " {:file file :line line} "\n"
" */\n "
method-sign
"\n\n")))
(defn- generate-javadoc
[options-map]
(let [default-options {:prefix "-" :impl-ns (ns-name *ns*)}
{:keys [name methods prefix impl-ns class-doc]} (merge default-options options-map)
[_ package c-name] (re-matches #"(.+)\.([^\.]+)" name)
javadoc (str
"package " package ";\n"
(str
" /**\n"
" * " (str-join "\n * " (re-split #"\n" class-doc)) "\n"
" */\n "
"\n\n")
"public class " c-name "{\n\n")
javadoc (loop [methods methods
javadoc javadoc]
(if-let [[mname pclasses rclass :as msig] (first methods)]
(recur (rest methods)
(str javadoc
(convert-fn-meta-into-javadoc impl-ns prefix mname pclasses rclass (:static (meta msig)))))
javadoc))
javadoc (str javadoc "}\n")]
[package c-name javadoc]))
(defmacro gen-class+javadoc
[& options]
(do
(macroexpand `(gen-class ~@options))
(when *compile-files*
(let [options-map (into {} (map vec (partition 2 options)))
[package c-name javadoc] (generate-javadoc options-map)
path-of-java-file (str (System/getProperty "user.dir")
java.io.File/separator "src" java.io.File/separator
(.replaceAll package "\\." (str java.io.File/separator java.io.File/separator))
java.io.File/separator c-name ".java")]
(do
(clojure.contrib.io/make-parents (java.io.File. path-of-java-file))
(clojure.contrib.io/spit path-of-java-file javadoc)
(let [cmd (str "javadoc -sourcepath src -d doc " package " > doc/javadoc.out 2> doc/javadoc.err")
javadoc-ret (clojure.contrib.shell/sh "cmd.exe" "/c" cmd :return-map true)])
(clojure.contrib.io/delete-file path-of-java-file))))))
(defn -method1
"This does really cool stuff
<p>
@param opts A map containing all the necessary options
@return A cool value
@version 0.0.1
@author Chuck Norris
"
[opts]
(+ 1 2)
)
(defn -method2
"This does such cool stuff, you'll be blown off!
<p>
@param x Degree of coolness wanted
@param y Degree of your patience
@param z How much you'll pay me
@return An infinite LazySeq of my coolness.
@version 0.0.1
@author Chuck Norris"
[x y z]
(* 0 (+ x y z))
)
(defn -method3
"Meaning of life
<p>
@return An Object which will help you attain nirvana.
@version 0.0.1
@author Chuck Norris"
[]
42
)
(println "OK gen-classing a.b.c.MyCoolClass")
(gen-class+javadoc
:class-doc "Class Docs
<ul> <li> One </li> <li> Two </li> </ul>
"
:name "a.b.c.MyCoolClass"
:methods [
#^{:static true} [method1 [clojure.lang.PersistentArrayMap] int]
#^{:static true} [method2 [int int float] clojure.lang.LazySeq]
#^{:static true} [method3 [] Object]
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment