Skip to content

Instantly share code, notes, and snippets.

@imrekoszo
Last active March 13, 2020 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imrekoszo/b4ee78d69e71c520b0392886a9d511b7 to your computer and use it in GitHub Desktop.
Save imrekoszo/b4ee78d69e71c520b0392886a9d511b7 to your computer and use it in GitHub Desktop.
Playing around with compiling clojure namespaces
(require '[clojure.set :as set]
'[clojure.string :as str]
'[clojure.tools.namespace.dir :as dir]
'[clojure.tools.namespace.find :as find]
'[clojure.tools.namespace.track :as track])
(defn create-tracker []
(->> {:platform find/clj
:add-all? true}
(dir/scan-dirs {} repl/refresh-dirs)))
(defn all-namespaces-in [tracker]
(->> tracker
(::track/deps)
((juxt #(->> % :dependents keys)
#(->> % :dependents vals (apply concat))
#(->> % :dependencies keys)
#(->> % :dependencies vals (apply concat))))
(into #{} cat)))
(defn compilable-namespaces [problematic-ns?]
(let [t (create-tracker)
all (all-namespaces-in t)]
(loop [remaining all
problematics (into #{} (filter problematic-ns?) all)
dependents (get-in t [::track/deps :dependents])]
(let [remaining-candidate (set/difference remaining problematics)]
(if (= remaining remaining-candidate)
remaining
(recur remaining-candidate
(->> problematics
(select-keys dependents)
(vals)
(into problematics cat))
(->> problematics
(vec)
(apply dissoc dependents))))))))
(comment
(->> #{'user 'other.problematic-ns}
(compilable-namespaces)
(run! compile))
)
@imrekoszo
Copy link
Author

There are still problems with this approach when attempting a tools.namespace refresh-all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment