Permalink
Browse files

TNS-35: Platform-selection args for dir,file,find

* clojure.tools.namespace.file:

    * Add definitions for standard file extensions for Clojure and
      ClojureScript.

    * Add 'clojurescript-file?'

* clojure.tools.namespace.find:

    * Add "platform" definitions 'clj' and 'cljs' to encapsulate reader
      options and file extensions.

    * Add optional "platform" argument to most functions.

    * Rename functions with "clojure" in their name to be more generic,
      deprecate old names.

* clojure.tools.namespace.dir:

    * Deprecate 'scan' and 'scan-all', replace with new functions
      'scan-dirs' and 'scan-files' taking "platform" argument.

* clojure.tools.namespace.repl

    * Use new 'scan-dirs' function in c.t.n.dir
  • Loading branch information...
stuartsierra committed Jul 31, 2015
1 parent f0c9442 commit 64e807c1888b494d04bb4dc96432d4d76aa0db54
@@ -11,57 +11,104 @@
file-modification timestamps"}
clojure.tools.namespace.dir
(:require [clojure.tools.namespace.file :as file]
[clojure.tools.namespace.find :as find]
[clojure.tools.namespace.track :as track]
[clojure.java.classpath :refer [classpath-directories]]
[clojure.java.io :as io]
[clojure.set :as set]
[clojure.string :as string])
(:import (java.io File) (java.util.regex Pattern)))
(defn- find-files [dirs]
(defn- find-files [dirs platform]
(->> dirs
(map io/file)
(filter #(.exists ^File %))
(mapcat file-seq)
(filter file/clojure-file?)
(map #(.getCanonicalFile ^File %))))
(mapcat #(find/find-sources-in-dir % platform))))
(defn- modified-files [tracker files]
(filter #(< (::time tracker 0) (.lastModified ^File %)) files))
(defn- deleted-files [tracker files]
(set/difference (::files tracker #{}) (set files)))
(defn- update-files [tracker deleted modified]
(defn- update-files [tracker deleted modified {:keys [read-opts]}]
(let [now (System/currentTimeMillis)]
(-> tracker
(update-in [::files] #(if % (apply disj % deleted) #{}))
(file/remove-files deleted)
(update-in [::files] into modified)
(file/add-files modified)
(file/add-files modified read-opts)
(assoc ::time now))))
(defn scan
"Scans directories for files which have changed since the last time
'scan' was run; update the dependency tracker with
(defn scan-files
"Scans files to find those which have changed since the last time
'scan-files' was run; updates the dependency tracker with
new/changed/deleted files.
files is the collection of files to scan.
Optional third argument is map of options:
:platform Either clj (default) or cljs, both defined in
clojure.tools.namespace.find, controls reader options for
parsing files.
:add-all? If true, assumes all extant files are modified regardless
of filesystem timestamps."
{:added "0.3.0"}
([tracker files] (scan-files tracker files nil))
([tracker files {:keys [platform add-all?]}]
(let [deleted (seq (deleted-files tracker files))
modified (if add-all?
files
(seq (modified-files tracker files)))]
(if (or deleted modified)
(update-files tracker deleted modified platform)
tracker))))
(defn scan-dirs
"Scans directories for files which have changed since the last time
'scan-dirs' or 'scan-files' was run; updates the dependency tracker
with new/changed/deleted files.
dirs is the collection of directories to scan, defaults to all
directories on Clojure's classpath.
Optional third argument is map of options:
:platform Either clj (default) or cljs, both defined in
clojure.tools.namespace.find, controls file extensions
and reader options.
:add-all? If true, assumes all extant files are modified regardless
of filesystem timestamps."
{:added "0.3.0"}
([tracker] (scan-dirs tracker nil nil))
([tracker dirs] (scan-dirs tracker dirs nil))
([tracker dirs {:keys [platform add-all?] :as options}]
(let [ds (or (seq dirs) (classpath-directories))]
(scan-files tracker (find-files ds platform) options))))
(defn scan
"DEPRECATED: replaced by scan-dirs.
Scans directories for Clojure (.clj, .cljc) source files which have
changed since the last time 'scan' was run; update the dependency
tracker with new/changed/deleted files.
If no dirs given, defaults to all directories on the classpath."
{:added "0.2.0"
:deprecated "0.3.0"}
[tracker & dirs]
(let [ds (or (seq dirs) (classpath-directories))
files (find-files ds)
deleted (seq (deleted-files tracker files))
modified (seq (modified-files tracker files))]
(if (or deleted modified)
(update-files tracker deleted modified)
tracker)))
(scan-dirs tracker dirs {:platform find/clj}))
(defn scan-all
"Scans directories for all Clojure source files and updates the
"DEPRECATED: replaced by scan-dirs.
Scans directories for all Clojure source files and updates the
dependency tracker to reload files. If no dirs given, defaults to
all directories on the classpath."
{:added "0.2.0"
:deprecated "0.3.0"}
[tracker & dirs]
(let [ds (or (seq dirs) (classpath-directories))
files (find-files ds)
deleted (seq (deleted-files tracker files))]
(update-files tracker deleted files)))
(scan-dirs tracker dirs {:platform find/clj :add-all? true}))
@@ -16,26 +16,52 @@
(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."
[file]
(with-open [rdr (PushbackReader. (io/reader file))]
(parse/read-ns-decl rdr)))
unevaluated form. Returns nil if read fails due to invalid syntax or
if a ns declaration cannot be found. read-opts is passed through to
tools.reader/read."
([file]
(read-file-ns-decl file nil))
([file read-opts]
(with-open [rdr (PushbackReader. (io/reader file))]
(parse/read-ns-decl rdr read-opts))))
(defn file-with-extension?
"Returns true if the java.io.File represents a file whose name ends
with one of the Strings in extensions."
{:added "0.3.0"}
[^java.io.File file extensions]
(and (.isFile file)
(let [name (.getName file)]
(some #(.endsWith name %) extensions))))
(def ^{:added "0.3.0"}
clojure-extensions
"File extensions for Clojure (JVM) files."
(list ".clj" ".cljc"))
(def ^{:added "0.3.0"}
clojurescript-extensions
"File extensions for ClojureScript files."
(list ".cljs" ".cljc"))
(defn clojure-file?
"Returns true if the java.io.File represents a normal Clojure source
file."
"Returns true if the java.io.File represents a file which will be
read by the Clojure (JVM) compiler."
[^java.io.File file]
(and (.isFile file)
(or
(.endsWith (.getName file) ".clj")
(.endsWith (.getName file) ".cljc"))))
(file-with-extension? file clojure-extensions))
(defn clojurescript-file?
"Returns true if the java.io.File represents a file which will be
read by the ClojureScript compiler."
{:added "0.3.0"}
[^java.io.File file]
(file-with-extension? file clojurescript-extensions))
;;; Dependency tracker
(defn- files-and-deps [files]
(defn- files-and-deps [files read-opts]
(reduce (fn [m file]
(if-let [decl (read-file-ns-decl file)]
(if-let [decl (read-file-ns-decl file read-opts)]
(let [deps (parse/deps-from-ns-decl decl)
name (second decl)]
(-> m
@@ -48,12 +74,15 @@
(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] merge-map filemap))))
tracker with those files added. read-opts is passed through to
tools.reader."
([tracker files]
(add-files tracker files nil))
([tracker files read-opts]
(let [{:keys [depmap filemap]} (files-and-deps files read-opts)]
(-> tracker
(track/add depmap)
(update-in [::filemap] merge-map filemap)))))
(defn remove-files
"Returns an updated dependency tracker with files removed. The files
Oops, something went wrong.

0 comments on commit 64e807c

Please sign in to comment.