Skip to content

Instantly share code, notes, and snippets.

@ninjudd
Created November 20, 2012 00:39
Show Gist options
  • Save ninjudd/4115184 to your computer and use it in GitHub Desktop.
Save ninjudd/4115184 to your computer and use it in GitHub Desktop.
(defn extract-dependencies
"Extract all files proto depends on into dest."
[project proto-path protos dest]
(in-project (dissoc project :prep-tasks)
[proto-path (.getPath proto-path)
dest (.getPath dest)
protos protos]
(require '[clojure.java.io :as io]))
(letfn [(dependencies [proto-file]
(when (.exists proto-file)
(for [line (line-seq (io/reader proto-file))
:when (.startsWith line "import")]
(second (re-matches #".*\"(.*)\".*" line)))))]
(loop [deps (mapcat #(dependencies (io/file proto-path %)) protos)]
(when-let [[dep & deps] (seq deps)]
(let [proto-file (io/file dest dep)]
(if (or (.exists (io/file proto-path dep))
(.exists proto-file))
(recur deps)
(do (.mkdirs (.getParentFile proto-file))
(when-let [resource (io/resource (str "proto/" dep))]
(io/copy (io/reader resource) proto-file))
(recur (concat deps (dependencies proto-file)))))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment