Last active
July 28, 2025 18:27
-
-
Save pmonks/982ce90dac11a4cefe45378c0db3d555 to your computer and use it in GitHub Desktop.
Get the namespaces for a Clojure dep
This file contains hidden or 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
; | |
; Copyright © 2021 Peter Monks | |
; | |
; This Source Code Form is subject to the terms of the Mozilla Public | |
; License, v. 2.0. If a copy of the MPL was not distributed with this | |
; file, You can obtain one at https://mozilla.org/MPL/2.0/. | |
; | |
; SPDX-License-Identifier: MPL-2.0 | |
; | |
; To run this code in a REPL: | |
; | |
; clj -Sdeps '{:deps {org.clojure/tools.deps {:mvn/version "RELEASE"} org.clojure/tools.namespace {:mvn/version "RELEASE"} http-kit/http-kit {:mvn/version "RELEASE"} find-deps/find-deps {:git/url "https://github.com/hagmonk/find-deps" :sha "9bf23a52cb0a8190c9c2c7ad1d796da802f8ce7a"}}}' -M -r | |
; | |
; Note: we have to force the latest http-kit version due to https://github.com/hagmonk/find-deps/issues/7 | |
; | |
(require '[clojure.java.io :as io]) | |
(require '[clojure.tools.deps :as tdep]) | |
(require '[clojure.tools.namespace.find :as tnsf]) | |
(defn dep-nses | |
"Returns all namespaces (as a sequence of symbols) in the given dep, | |
identified by `lib-sym` and `coords`, as used in lib-map format - see | |
https://clojure.github.io/tools.deps/#:clojure.tools.deps.specs/lib-map | |
Notes: | |
* If not provided, `coords` defaults to `{:mvn/version \"RELEASE\"}` | |
* Has the side effect of downloading the dep and all of its transitive | |
dependencies, but does NOT add them to the REPL's classpath - use | |
`clojure.repl.deps/add-lib` for that" | |
([lib-sym] (dep-nses lib-sym nil)) | |
([lib-sym coords] | |
(when lib-sym | |
(let [coords (or coords {:mvn/version "RELEASE"}) | |
deps-map {:mvn/local-repo (str (System/getenv "HOME") "/.m2/repository") | |
:mvn/repos {"central" {:url "https://repo1.maven.org/maven2/"} | |
"clojars" {:url "https://repo.clojars.org/"}} | |
:deps {lib-sym coords}} | |
lib-map (tdep/resolve-deps deps-map nil) | |
relevant-lib-map (get lib-map lib-sym) | |
dep-artifacts (->> (conj (:paths relevant-lib-map) (:deps/root relevant-lib-map)) | |
(filter identity) | |
(map io/file) | |
seq)] | |
(when dep-artifacts | |
(->> (tnsf/find-namespaces dep-artifacts) | |
(map str) | |
sort | |
(map symbol))))))) | |
; Example usages | |
(dep-nses 'http-kit/http-kit) | |
(dep-nses 'http-kit/http-kit {:mvn/version "2.3.0"}) ; Fewer namespaces in v2.3.0 | |
; Use it in conjunction with the handy 'find-deps' library (https://github.com/hagmonk/find-deps) | |
(require '[find-deps.core :as fd]) | |
(let [http-kit-deps (fd/deps "http-kit") ; Note: this can be quite slow (over a minute) - it also seems to timeout from time to time | |
[lib-sym coord] (first (:deps http-kit-deps))] | |
(dep-nses lib-sym coord)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment