Search code examples
clojure

Is there a way to get Clojure file's source when a namespace provided?


There are some functions that read source code of a function like: source and source-fn.

Is there any way or function that returns source code of the Clojure file when a namespace is provided?

Such as: (all-source 'my-ns)

Returns such as:

(ns my-ns
  (:require [kezban.core :refer :all]
            [leiningen.c.util :as util]))
(defn my-fn
  []
  )
...

Solution

  • I think I found a way(it works if the ns has at least one var):

    (defn source-clj
      [ns]
      (require ns)
      (some->> ns
               ns-publics
               vals
               first
               meta
               :file
               (.getResourceAsStream (RT/baseLoader))
               IOUtils/toString))