Clojure China

想自己模拟一个 lein repl 然而环境不对

#1

想模拟一个 lein repl 运行我用 Cirru 语法生成的 Clojure 代码, 于是做了个 plugin.
我翻了下 lein 内部代码, 似乎用 nrepl 做的, 细节没看明白.
我自己的实现, 是用 eval 实现的, 基本功能可以跑, 然而刚才发现是 classpath 不对, 导致 require 运行出错.
我应该怎样模拟 lein repl 的环境呢?

(ns leiningen.sepal-repl
  (:require [leiningen.core.eval :as eval]
            [cirru.sepal :refer [transform-x]]
            [cirru.parser-combinator :refer [pare]]
            [clojure.term.colors :refer :all]
            [clojure.pprint :as pp]
            [fipp.edn :refer [pprint] :rename {pprint fipp}]))

(defn- eval-lines [lines]
  (if (> (count lines) 0)
    (let [quoted-code (transform-x (first lines))
          _ (println
              (magenta
                (str "    -> " (with-out-str (pp/write quoted-code)))))
          result (try
                   (eval quoted-code)
                   (catch Exception e (println e)))]
      (print (blue "    <- "))
      (fipp result)
      (recur (rest lines)))))

(defn- eval-print []
  (print (blue "sepal> "))
  (flush)
  (let [code (read-line) details (pare code)]
    (if (:failed details)
      (println details)
      (eval-lines (:value details)))
    (recur)))

(comment "reusing code from https://github.com/Cirru/sepal-repl.clj")

(defn sepal-repl [project & args]
  (println "Starting REPL for Sepal.clj ...")
  (flush)
  (eval-print))
#2

模拟 lein repl 用第三方的 add-classpath 对环境做了强制的设置, 暂时可以用了

https://github.com/technomancy/leiningen/blob/d701275464c185f4f22be58450b1af4b94a79836/leiningen-core/src/leiningen/core/eval.clj#L327-L328