Skip to content

Commit

Permalink
move namespace conversion to file path utilities into cljs.util, move…
Browse files Browse the repository at this point in the history
… parse-ns into

cljs.analyzer where it belongs
  • Loading branch information
dnolen committed Sep 19, 2014
1 parent f73e482 commit 88873ed
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 74 deletions.
46 changes: 37 additions & 9 deletions src/clj/cljs/analyzer.clj
Expand Up @@ -162,12 +162,6 @@
`(binding [*cljs-warning-handlers* ~handlers]
~@body))

(defn munge-path [ss]
(clojure.lang.Compiler/munge (str ss)))

(defn ns->relpath [s]
(str (string/replace (munge-path s) \. \/) ".cljs"))

(def ^:private constant-counter (atom 0))

(defn gen-constant-id [value]
Expand Down Expand Up @@ -315,7 +309,7 @@
(nil? (get (-> env :ns :requires) ns-sym))
;; macros may refer to namespaces never explicitly required
;; confirm that the library at least exists
(nil? (io/resource (ns->relpath ns-sym))))
(nil? (io/resource (util/ns->relpath ns-sym))))
(warning :undeclared-ns env {:ns-sym ns-sym})))

(defn core-name?
Expand Down Expand Up @@ -1005,7 +999,7 @@
(when-not (or (contains? (::namespaces @env/*compiler*) dep)
(contains? (:js-dependency-index @env/*compiler*) (name dep))
(deps/find-classpath-lib dep))
(let [relpath (ns->relpath dep)
(let [relpath (util/ns->relpath dep)
src (locate-src relpath)]
(if src
(analyze-file src)
Expand Down Expand Up @@ -1616,6 +1610,40 @@ argument, which the reader will use in any emitted errors."
(cons form (forms-seq*))))))]
(forms-seq*))))

(defn parse-ns
([src] (parse-ns src nil nil))
([src dest opts]
(env/ensure
(let [namespaces' (::namespaces @env/*compiler*)
ret
(binding [*cljs-ns* 'cljs.user
*analyze-deps* false]
(loop [forms (forms-seq src)]
(if (seq forms)
(let [env (empty-env)
ast (no-warn (analyze env (first forms) nil opts))]
(if (= (:op ast) :ns)
(let [ns-name (:name ast)
deps (merge (:uses ast) (:requires ast))]
(merge
{:ns (or ns-name 'cljs.user)
:provides [ns-name]
:requires (if (= ns-name 'cljs.core)
(set (vals deps))
(cond-> (conj (set (vals deps)) 'cljs.core)
(get-in @env/*compiler* [:opts :emit-constants])
(conj 'constants-table)))
:file dest
:source-file src}
(when (and dest (.exists ^File dest))
{:lines (with-open [reader (io/reader dest)]
(-> reader line-seq count))})))
(recur (rest forms)))))))]
;; TODO this _was_ a reset! of the old namespaces atom; should we capture and
;; then restore the entirety of env/*compiler* here instead?
(swap! env/*compiler* assoc ::namespaces namespaces')
ret))))

(defn requires-analysis? [^File f ^File cache]
(or (not (.exists cache))
(> (.lastModified f) (.lastModified cache))
Expand All @@ -1625,7 +1653,7 @@ argument, which the reader will use in any emitted errors."

(defn analyze-file
([f] (analyze-file f nil))
([f opts]
([f {:keys [output-dir] :as opts}]
(let [res (cond
(instance? File f) f
(instance? java.net.URL f) f
Expand Down
10 changes: 5 additions & 5 deletions src/clj/cljs/closure.clj
Expand Up @@ -619,7 +619,7 @@ should contain the source for the given namespace name."
(let [{:keys [provides source-url]} source]
(if (and provides source-url)
(assoc relpaths (.getPath ^URL source-url)
(ana/ns->relpath (first provides)))
(util/ns->relpath (first provides)))
relpaths))
(if-let [url (:url source)]
(let [path (.getPath ^URL url)]
Expand Down Expand Up @@ -696,14 +696,14 @@ should contain the source for the given namespace name."
(defn path-relative-to
"Generate a string which is the path to input relative to base."
[^File base input]
(let [base-path (comp/path-seq (.getCanonicalPath base))
input-path (comp/path-seq (.getCanonicalPath (io/file ^URL (deps/-url input))))
(let [base-path (util/path-seq (.getCanonicalPath base))
input-path (util/path-seq (.getCanonicalPath (io/file ^URL (deps/-url input))))
count-base (count base-path)
common (count (take-while true? (map #(= %1 %2) base-path input-path)))
prefix (repeat (- count-base common 1) "..")]
(if (= count-base common)
(last input-path) ;; same file
(comp/to-path (concat prefix (drop common input-path)) "/"))))
(util/to-path (concat prefix (drop common input-path)) "/"))))

(defn add-dep-string
"Return a goog.addDependency string for an input."
Expand Down Expand Up @@ -773,7 +773,7 @@ should contain the source for the given namespace name."
(let [out-file (if-let [ns (and (:source-map opts)
(first (:provides js)))]
(io/file (io/file (output-directory opts))
(ana/ns->relpath ns)))
(util/ns->relpath ns)))
source-url (:source-url js)]
(when (and out-file source-url
(or (not (.exists ^File out-file))
Expand Down
61 changes: 2 additions & 59 deletions src/clj/cljs/compiler.clj
Expand Up @@ -935,40 +935,6 @@
(not (.exists (io/file (str (.getPath dest) ".map"))))
(not (get-in @env/*compiler* [::compiled-cljs (.getAbsolutePath dest)]))))))))

(defn parse-ns
([src] (parse-ns src nil nil))
([src dest opts]
(env/ensure
(let [namespaces' (::ana/namespaces @env/*compiler*)
ret
(binding [ana/*cljs-ns* 'cljs.user
ana/*analyze-deps* false]
(loop [forms (ana/forms-seq src)]
(if (seq forms)
(let [env (ana/empty-env)
ast (ana/no-warn (ana/analyze env (first forms) nil opts))]
(if (= (:op ast) :ns)
(let [ns-name (:name ast)
deps (merge (:uses ast) (:requires ast))]
(merge
{:ns (or ns-name 'cljs.user)
:provides [ns-name]
:requires (if (= ns-name 'cljs.core)
(set (vals deps))
(cond-> (conj (set (vals deps)) 'cljs.core)
(get-in @env/*compiler* [:opts :emit-constants])
(conj 'constants-table)))
:file dest
:source-file src}
(when (and dest (.exists ^File dest))
{:lines (with-open [reader (io/reader dest)]
(-> reader line-seq count))})))
(recur (rest forms)))))))]
;; TODO this _was_ a reset! of the old ana/namespaces atom; should we capture and
;; then restore the entirety of env/*compiler* here instead?
(swap! env/*compiler* assoc ::ana/namespaces namespaces')
ret))))

(defn compile-file
"Compiles src to a file of the same name, but with a .js extension,
in the src file's directory.
Expand All @@ -992,7 +958,7 @@
dest-file (io/file dest)]
(if (.exists src-file)
(try
(let [{ns :ns :as ns-info} (parse-ns src-file dest-file opts)]
(let [{ns :ns :as ns-info} (ana/parse-ns src-file dest-file opts)]
(if (requires-compilation? src-file dest-file opts)
(do (mkdirs dest-file)
(when (contains? (::ana/namespaces @env/*compiler*) ns)
Expand All @@ -1007,29 +973,6 @@
(throw (ex-info (str "failed compiling file:" src) {:file src} e))))
(throw (java.io.FileNotFoundException. (str "The file " src " does not exist.")))))))

(defn path-seq
[file-str]
(->> File/separator
java.util.regex.Pattern/quote
re-pattern
(string/split file-str)))

(defn to-path
([parts]
(to-path parts File/separator))
([parts sep]
(apply str (interpose sep parts))))

(defn ^File to-target-file
[target cljs-file]
(let [relative-path (string/split
(ana/munge-path
(str (:ns (parse-ns cljs-file)))) #"\.")
parents (butlast relative-path)]
(io/file
(io/file (to-path (cons target parents)))
(str (last relative-path) ".js"))))

(defn cljs-files-in
"Return a sequence of all .cljs files in the given directory."
[dir]
Expand All @@ -1056,7 +999,7 @@
output-files []]
(if (seq cljs-files)
(let [cljs-file (first cljs-files)
output-file (to-target-file target-dir cljs-file)
output-file (util/to-target-file target-dir cljs-file (ana/parse-ns cljs-file))
ns-info (compile-file cljs-file output-file opts)]
(recur (rest cljs-files) (conj output-files (assoc ns-info :file-name (.getPath output-file)))))
output-files)))))
Expand Down
30 changes: 29 additions & 1 deletion src/clj/cljs/util.clj
Expand Up @@ -7,7 +7,8 @@
; You must not remove this notice, or any other, from this software.

(ns cljs.util
(:require [clojure.java.io :as io])
(:require [clojure.java.io :as io]
[clojure.string :as string])
(:import [java.io File]))

;; next line is auto-generated by the build-script - Do not edit!
Expand All @@ -33,3 +34,30 @@
(re-matches #".*ClojureScript (.*)$"))]
(and match (second match)))))

(defn munge-path [ss]
(clojure.lang.Compiler/munge (str ss)))

(defn ns->relpath [s]
(str (string/replace (munge-path s) \. \/) ".cljs"))

(defn path-seq
[file-str]
(->> File/separator
java.util.regex.Pattern/quote
re-pattern
(string/split file-str)))

(defn to-path
([parts]
(to-path parts File/separator))
([parts sep]
(apply str (interpose sep parts))))

(defn ^File to-target-file
[target-dir cljs-file ns-info]
(let [relative-path (string/split
(munge-path (str (:ns ns-info))) #"\.")
parents (butlast relative-path)]
(io/file
(io/file (to-path (cons target-dir parents)))
(str (last relative-path) ".js"))))

0 comments on commit 88873ed

Please sign in to comment.