Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

62 lines (53 sloc) 2.224 kb
;; Copyright (c) Stuart Sierra, 2012. All rights reserved. The use and
;; distribution terms for this software are covered by the Eclipse
;; Public License 1.0 (
;; which can be found in the file epl-v10.html at the root of this
;; distribution. By using this software in any fashion, you are
;; agreeing to be bound by the terms of this license. You must not
;; remove this notice, or any other, from this software.
(ns ^{:author "Stuart Sierra"
:doc "Read and track namespace information from files"}
(:require [ :as io]
[ :as parse]
[ :as track])
(:import ( PushbackReader)))
(defn read-file-ns-decl
"Attempts to read a (ns ...) declaration from file, and returns the
unevaluated form. Returns nil if read fails, or if the first form
is not a ns declaration."
(with-open [rdr (PushbackReader. (io/reader file))]
(parse/read-ns-decl rdr)))
(defn clojure-file?
"Returns true if the represents a normal Clojure source
[^ file]
(and (.isFile file)
(.endsWith (.getName file) ".clj")))
;;; Dependency tracker
(defn- files-and-deps [files]
(reduce (fn [m file]
(if-let [decl (read-file-ns-decl file)]
(let [deps (parse/deps-from-ns-decl decl)
name (second decl)]
(-> m
(assoc-in [:depmap name] deps)
(assoc-in [:filemap file] name)))
{} files))
(defn add-files
"Reads ns declarations from files; returns an updated dependency
tracker with those files added."
[tracker files]
(let [{:keys [depmap filemap]} (files-and-deps files)]
(-> tracker
(track/add depmap)
(update-in [::filemap] (fnil merge {}) filemap))))
(defn remove-files
"Returns an updated dependency tracker with files removed. The files
must have been previously added with add-files."
[tracker files]
(-> tracker
(track/remove (keep (::filemap tracker {}) files))
(update-in [::filemap] #(apply dissoc % files))))
Jump to Line
Something went wrong with that request. Please try again.