-
-
Save joinr/ea5f770a1bab353bf057d5abf94dd741 to your computer and use it in GitHub Desktop.
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
;;updated example from the old clojure 1.0 | |
;;https://gist.github.com/na-ka-na/415269 | |
(ns documentation | |
(:require [clojure.java.io :as io] | |
[clojure.java.shell :as shell] | |
[clojure [string :as s]])) | |
(def +sep+ (System/getProperty "file.separator")) | |
(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" | |
" * " (s/join "\n * " (s/split doc #"\n")) "\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" | |
" * " (s/join "\n * " (s/split class-doc #"\n")) "\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] | |
(let [options-map (into {} (map vec (partition 2 options))) | |
[package c-name javadoc] (generate-javadoc options-map) | |
path-of-java-file (str (s/join +sep+ [(System/getProperty "user.dir") "src" | |
(.replaceAll package "\\." (str +sep+ +sep+)) c-name]) | |
".java")] | |
(when (or *compile-files* (:javadoc? options-map)) | |
(do (io/make-parents (io/file path-of-java-file)) | |
(spit path-of-java-file javadoc) | |
(let [cmd (str "javadoc -sourcepath src -d doc " package " > doc/javadoc.out 2> doc/javadoc.err") | |
javadoc-ret (shell/sh "cmd.exe" "/c" cmd :return-map true)] | |
(io/delete-file path-of-java-file)))) | |
`(gen-class ~@options))) | |
(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 | |
) | |
;;this should emit documentation in ./doc/documentation | |
;;ensure javadoc is on your path. | |
;;Note: use this for testing or limited drop-in replacement | |
;;to compile javadocs conveniently. It'll run javadoc | |
;;each time, which could get slow if repetitive. | |
(gen-class+javadoc | |
:class-doc "Class Docs | |
<ul> <li> One </li> <li> Two </li> </ul> | |
" | |
:name "documentation.MyCoolClass" | |
:methods [ | |
#^{:static true} [method1 [clojure.lang.PersistentArrayMap] int] | |
#^{:static true} [method2 [int int float] clojure.lang.LazySeq] | |
#^{:static true} [method3 [] Object] | |
] | |
:javadoc? true) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment