Skip to content

Instantly share code, notes, and snippets.

@henryw374
Created February 15, 2019 10:56
Show Gist options
  • Save henryw374/1ec99ae256a0604eda98c606b21ed2c3 to your computer and use it in GitHub Desktop.
Save henryw374/1ec99ae256a0604eda98c606b21ed2c3 to your computer and use it in GitHub Desktop.
use ns-graph to create a function that returns all transitive dependencies of a namespace
(ns deps
"use https://github.com/alexander-yakushev/ns-graph
to create a function that returns all transitive dependencies of a namespace.
handy if you're trying to pull a ns out of a project
"
(:require [ns-graph.core :as nsg]
[clojure.java.io :as io]))
(alter-var-root #'nsg/all-clojure-files (constantly
(fn all-clojure-files [root]
(->> (file-seq (io/file root))
(filter #(re-matches #".*\.clj.?" (str %)))))))
(defn transitive-dependencies-of [src-dirs ns-sym]
(let [graph-data (-> (nsg/parsed-files->graph-data
(nsg/parse-directories src-dirs (constantly true)) false)
:links)]
(loop [banked #{ns-sym}
next [ns-sym]]
(if-not next
banked
(recur
(clojure.set/union banked (set next))
(->> next
(keep (fn [sym]
(->> graph-data
(keep (fn [[[from-sym] [to-sym]]]
(when (= sym from-sym)
to-sym))))))
(apply concat)
(filter (complement banked))
seq))))))
(comment
(transitive-dependencies-of ["src"] 'clojure.edn)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment