首页 > 解决方案 > 如何确定定义 Clojure 命名空间的完整文件路径?

问题描述

例如:

(defn show-full-path-of-ns [namespace-symbol]
    ;; how to do it?
    )

(show-full-path-of-ns 'ring.middleware.session)
;; => ~/.m2/repository/ring/ring-core/1.7.1/ring-core-1.7.1.jar!ring/middleware/session.clj

(是的,我知道可以将命名空间拆分为多个文件,但我们可能会忽略这一点。)

标签: clojure

解决方案


这是该问题的部分解决方案。它适用于类路径中的 jar 文件,但它可能会给你一些想法。

(ns so.find-namespaces
    (:require
        [clojure.tools.namespace.find :as f]
        [clojure.java.classpath :as cp])
    (:import
        [java.util.jar JarFile]))

(apply hash-map (->> (cp/classpath)
                     (filter (memfn isFile))
                     (mapcat #(interleave (f/find-namespaces-in-jarfile (JarFile. %)) (repeat (.getCanonicalPath %))))))

无论如何,如果有解决方案,它可能就在这里


推荐阅读