Bob Kuhar
Bob Kuhar

Reputation: 11110

Can I lookup things within a Lein Project in the REPL?

Say I have a vanilla project.clj like

(defproject myservice "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :mailing-list {:name "[email protected]" :post "[email protected]"}
  :license {:name "Eclipse Public License"
            :url  "http://www.eclipse.org/legal/epl-v10.html"}

  :dependencies [[org.clojure/clojure "1.6.0"]
                 [ring/ring-core "1.4.0"]
                 [ring/ring-jetty-adapter "1.4.0"]
                 [compojure "1.4.0"]
                 [ring/ring-defaults "0.1.5"]
                 [org.clojure/tools.logging "0.3.1"]
                 [clj-http "2.0.0"]]

  :plugins [[lein-ring "0.9.7"]]

  :ring {:handler myservice.core/standalone-app
         :port 3000}
  :profiles {
             :uberjar {:ring {:handler myservice.core/app}}}
  )

In a lein repl, can I lookup values from the project.clj? How? Of course my blind hack didn't work?

user=> (:mailing-list project)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: project in this context, compiling:(/private/var/folders/1g/fnytl2x93sx6hp2f1rsf4h1r5xtqv_/T/form-init6671981825845237047.clj:1:1)

The follow on question is can I use stuff from the project map further on in the project.clj? Like if I wanted to pull that mailing list :name out and substitute it in as a :deb :maintainer?

:deb 
  {:toDir "target"
   :package "mysevice"
   :maintainer {:name "Meeples", :email "[email protected]"}
   ...
  }

I'm sure you can tell, I'm kind-of new to this, but the project.clj is just executable Clojure, no? If I knew the name of the project's map, I should be able to query it, right?

Upvotes: 3

Views: 60

Answers (2)

Antony Woods
Antony Woods

Reputation: 4462

You can def data as you usually would and include them using ~

(def mailing-list {:name "[email protected]" :post "[email protected]"})

(defproject myservice "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :mailing-list ~mailing-list
  :license {:name "Eclipse Public License"
            :url  "http://www.eclipse.org/legal/epl-v10.html"}

  :dependencies [[org.clojure/clojure "1.6.0"]
                 [ring/ring-core "1.4.0"]
                 [ring/ring-jetty-adapter "1.4.0"]
                 [compojure "1.4.0"]
                 [ring/ring-defaults "0.1.5"]
                 [org.clojure/tools.logging "0.3.1"]
                 [clj-http "2.0.0"]]

  :plugins [[lein-ring "0.9.7"]]

  :ring {:handler myservice.core/standalone-app
         :port 3000}
  :profiles {
             :uberjar {:ring {:handler myservice.core/app}}}
  :deb {
        :toDir "target"
        :package "mysevice"
        :maintainer {:name "Meeples", :email (:name ~mailing-list)}})

Upvotes: 1

nha
nha

Reputation: 18005

This is the relevant line in leiningen : https://github.com/technomancy/leiningen/blob/b29b2ea41b6d177a8a57493b979164eab0931e4d/leiningen-core/src/leiningen/core/project.clj#L405

Given the namespace is leiningen.core.project, the map should be under it.

Upvotes: 0

Related Questions