简体   繁体   English

我如何依赖每个clojure contrib库?

[英]How do I depend on every clojure contrib library?

I used to like to include all of clojure.contrib, and require all the libraries. 我曾经喜欢包含所有的clojure.contrib,并且需要所有的库。 This makes find-doc useful as a discovery tool. 这使得find-doc可用作发现工具。

Nowadays (clojure 1.4) clojure.contrib is split into many sub-libraries. 如今(clojure 1.4)clojure.contrib被分成许多子库。 And that rather spoils my scheme, and it also means that I am constantly having to restart the JVM every time I need a new library. 而这相当破坏了我的方案,这也意味着每次我需要一个新的库时我都不得不重新启动JVM。

So I'm busy constructing a project.clj file with many lines: 所以我正忙着构建一个包含多行的project.clj文件:

[org.clojure/algo.generic "0.0.6"]
....
[org.clojure/data.xml "0.0.4"]
....

So that I can get leiningen to put every clojure contrib library on the classpath, whether I need them or not. 因此,我可以让leiningen将每个clojure contrib库放在类路径上,无论我是否需要它们。

And I reckon that this is going to be a spectacular pain in the neck, what with the version numbers, and all. 而且我认为这将是一个壮观的颈部疼痛,版本号和所有。

And I wonder if anyone has a better way to do the same thing? 我想知道是否有人有更好的方法来做同样的事情?

EDIT: Thinking about it, if there's a web page somewhere that has a list of library names and current versions, I can turn that into a project file fairly easily. 编辑:考虑一下,如果有一个网页某处有一个库名和当前版本列表,我可以很容易地把它变成一个项目文件。

You could use pomegranate if you just want to run it in the REPL (which seems like it would be the only appropriate use case, right?). 你可以使用石榴,如果你只想在REPL中运行它(这似乎是唯一合适的用例,对吧?)。 You can have it look up the latest versions using the Maven Central API. 您可以使用Maven Central API查找最新版本。 I think this is better than maintaining some sort of dependencies project, generated or otherwise. 我认为这比维护某种依赖项目,生成或其他更好。

(require '[cemerick.pomegranate :refer [add-dependencies]])

(add-dependencies
  :coordinates '[[clj-http "0.5.8"]]
  :repositories {"clojars" "http://clojars.org/repo"})

(require '[clj-http.client :as client])

;; contrib project names from https://github.com/clojure
(def contrib ["tools.nrepl" "tools.trace" "tools.namespace" "tools.macro"
              "test.generative" "math.numeric-tower" "core.match" "core.logic"
              "data.priority-map" "core.contracts" "tools.cli" "java.jmx"
              "java.jdbc" "java.classpath" "data.xml" "data.json" "core.unify"
              "core.incubator" "core.cache" "algo.monads" "data.generators"
              "core.memoize" "math.combinatorics" "java.data" "tools.logging"
              "data.zip" "data.csv" "algo.generic" "data.codec"
              "data.finger-tree"])

(defn add-contrib-dependencies
  "look up the latest version of every contrib project in maven central,
   and add them as dependencies using pomegranate."
  [project-names]
  (add-dependencies
   :coordinates
   (map (juxt
         (comp symbol (partial format "org.clojure/%s"))
         (fn [proj]
             (Thread/sleep 100)
             (-> "http://search.maven.org/solrsearch/select?q=%s&rows=1&wt=json"
                 (format proj)
                 (client/get {:as :json})
                 :body :response :docs first :latestVersion)))
        project-names)))

Now you can just invoke this function on the list of project names: 现在,您只需在项目名称列表中调用此函数:

user=> (add-contrib-dependencies contrib)
{[org.clojure/data.zip "0.1.1"] nil,
 [org.clojure/java.classpath "0.2.0"] nil,
 [org.clojure/core.cache "0.6.2"] nil, ...}

UPDATE: as suggested earlier, I had made this answer into a library. 更新:如前所述,我已将此答案写入库中。 It can be used either as nREPL middleware or invoked manually from a running REPL session. 它既可以用作nREPL中间件,也可以从正在运行的REPL会话中手动调用。 The code can be found at https://github.com/rplevy/contrib-repl , where usage instructions can also be found. 该代码可以在https://github.com/rplevy/contrib-repl找到,其中也可以找到使用说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM