Skip to content

Commit

Permalink
[Fix #12] support .cljs files (or at least move...
Browse files Browse the repository at this point in the history
them out of the way)

cljs munging might just work but not really tested.
  • Loading branch information
benedekfazekas committed Feb 27, 2016
1 parent 5c7bba2 commit 3b52cd9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
4 changes: 2 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:eval-in :leiningen
:plugins [[thomasa/mranderson "0.4.6"]]
:plugins [[thomasa/mranderson "0.4.7-SNAPSHOT"]]
:java-source-paths ["java-src"]
:javac-options ["-target" "1.6" "-source" "1.6"]
:filespecs [{:type :bytes :path "mranderson/project.clj" :bytes ~(slurp "project.clj")}]
:dependencies [^:source-dep [com.cemerick/pomegranate "0.3.0"]
^:source-dep [org.clojure/tools.namespace "0.2.11"]
^:source-dep [org.clojure/tools.namespace "0.3.0-alpha3"]
^:source-dep [me.raynes/fs "1.4.6"]
[com.googlecode.jarjar/jarjar "1.3"]]
:profiles {:dev {:dependencies [[org.clojure/clojure "1.7.0"]]}})
9 changes: 5 additions & 4 deletions src/leiningen/source_deps.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

(defn- zip-target-file
[target-dir entry-path]
;; remove leading slash in case some bonehead created a zip with absolute
;; file paths in it.
(let [entry-path (str/replace-first (str entry-path) #"^/" "")]
(fs/file target-dir entry-path)))

Expand All @@ -33,7 +31,9 @@
(->> entries
(filter #(not (.isDirectory ^java.util.zip.ZipEntry %)))
(map #(.getName %))
(filter #(or (.endsWith % ".clj") (.endsWith % ".cljc")))))))
(filter #(or (.endsWith % ".clj")
(.endsWith % ".cljc")
(.endsWith % ".cljs")))))))

(defn- cljfile->prefix [clj-file]
(->> (str/split clj-file #"/")
Expand All @@ -44,6 +44,7 @@
(->> clj-files
(map cljfile->prefix)
(remove #(str/blank? %))
(remove #(= "clojure.core" %))
(reduce #(if (%1 %2) (assoc %1 %2 (inc (%1 %2))) (assoc %1 %2 1) ) {})
(filter #(< 1 (val %)))
(map first)
Expand Down Expand Up @@ -256,7 +257,7 @@
(doseq [file (clojure-source-files [srcdeps])]
(update-deftypes file old-ns new-deftype)))
;; move actual ns-s
(move/move-ns old-ns new-ns srcdeps [srcdeps]))
(move/move-ns old-ns new-ns srcdeps (file->extension (str clj-file)) [srcdeps]))
;; a clj file without ns
(when-not (= "project.clj" clj-file)
(let [old-path (str "target/srcdeps/" clj-file)
Expand Down
40 changes: 19 additions & 21 deletions src/mranderson/move.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"}
mranderson.move
(:require [clojure.string :as str]
[clojure.java.io :as io])
[clojure.java.io :as io]
[mranderson.util :as util])
(:import (java.io File FileNotFoundException)))

(defn- update-file
Expand All @@ -40,26 +41,27 @@
(str/replace "-" "_")
(str/replace "." File/separator)))

(defn- find-file-for-sym [path sym]
(->> [".clj" ".cljc"]
(map #(io/file path (str (sym->file-name sym) %)))
(filter #(.exists %))
first))

(defn- sym->file
[path sym extension]
(io/file path (str (sym->file-name sym) extension)))

(defn- clojure-source-files [dirs]
(defn- update? [file extension-of-moved]
(let [file-ext (util/file->extension file)
all-extensions #{".cljc" ".cljs" ".clj"}]
(or
(and (= ".cljc" extension-of-moved)
(all-extensions file-ext))
(= file-ext extension-of-moved)
(= file-ext ".cljc"))))

(defn- clojure-source-files [dirs extension]
(->> dirs
(map io/file)
(filter #(.exists ^File %))
(mapcat file-seq)
(filter (fn [^File file]
(and (.isFile file)
(or
(.endsWith (.getName file) ".clj")
(.endsWith (.getName file) ".cljc")))))
(update? (str file) extension))))
(map #(.getCanonicalFile ^File %))))

(def ^:private symbol-regex
Expand All @@ -82,20 +84,16 @@
new-name
match)))))

(defn- file->extension
[file]
(re-find #"\.cljc?$" (str file)))

(defn move-ns-file
"ALPHA: subject to change. Moves the .clj or .cljc source file (found relative
to source-path) for the namespace named old-sym to a file for a
namespace named new-sym.
WARNING: This function moves and deletes your source files! Make
sure you have a backup or version control."
[old-sym new-sym source-path]
(if-let [old-file (find-file-for-sym source-path old-sym)]
(let [new-file (sym->file source-path new-sym (file->extension old-file))]
[old-sym new-sym extension source-path]
(if-let [old-file (sym->file source-path old-sym extension)]
(let [new-file (sym->file source-path new-sym extension)]
(.mkdirs (.getParentFile new-file))
(io/copy old-file new-file)
(.delete old-file)
Expand All @@ -116,7 +114,7 @@
WARNING: This function modifies and deletes your source files! Make
sure you have a backup or version control."
[old-sym new-sym source-path dirs]
(move-ns-file old-sym new-sym source-path)
(doseq [file (clojure-source-files dirs)]
[old-sym new-sym source-path extension dirs]
(move-ns-file old-sym new-sym extension source-path)
(doseq [file (clojure-source-files dirs extension)]
(update-file file replace-ns-symbol old-sym new-sym)))
5 changes: 5 additions & 0 deletions src/mranderson/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
(and (.isFile file)
(or
(.endsWith file-name ".cljc")
(.endsWith file-name ".cljs")
(.endsWith file-name ".clj")))))))))
([dirs]
(clojure-source-files-relative dirs nil)))
Expand Down Expand Up @@ -113,3 +114,7 @@
(assert (string? v)
(str "Something went wrong, version is not a string: " v))
v))

(defn file->extension
[file]
(re-find #"\.clj[cs]?$" file))

0 comments on commit 3b52cd9

Please sign in to comment.