How to make macros available for file upload in Clojure?

Suppose you have the following code:

(ns foo) (defmacro defproject [project-name version & args] ... ) (defn read-project ([file] (load-file file))) 

Update : the full code can be found at https://github.com/derkork/intellij-leiningen-plugin/blob/master/src/de/janthomae/leiningenplugin/leiningen/LeiningenProjectFile.clj

Now I call read-project in the file "project.clj", which has the following contents:

 (defproject de.janthomae/leiningenplugin "1.0.0-SNAPSHOT" ... ) 

And I get the following error message

 java.lang.Exception: Unable to resolve symbol: defproject in this context (project.clj:1) at clojure.lang.Compiler.analyze(Compiler.java:4420) at clojure.lang.Compiler.analyze(Compiler.java:4366) at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:2828) at clojure.lang.Compiler.analyzeSeq(Compiler.java:4594) at clojure.lang.Compiler.analyze(Compiler.java:4405) at clojure.lang.Compiler.analyze(Compiler.java:4366) 

This tells me that he does not find my defproject macro. And I don’t know at all why, because the macro is defined just a few lines in front. Do I need to export it somehow so that it can be seen from files loaded with load-file?

+4
source share
1 answer

project.clj does not know where to look for foo / defproject. So, if you are on a replica, you can do

 user> (in-ns 'foo) foo> (read-project "project.clj") 

This will run the code from project.clj inside the foo namespace, where defproject is defined. Or you can place (in-ns' foo) inside project.clj. The result will be the same.

But you can also write something like this in project.clj:

 (foo/defproject ...) 

This will call defproject inside your current namespace at startup (foo / read-project "project.clj").


Update

from test_uberjar.clj:

 (def project (binding [*ns* (the-ns 'leiningen.core)] (read-project "test_projects/sample_no_aot/project.clj"))) 

from test_deps.clj:

 (:use [leiningen.core :only [read-project defproject]] ...) 

Thus, defproject is always available when loading a file.

+1
source

All Articles