Search code examples
clojure

How to load Clojure files from directory to REPL while preserving dependency order?


I have 2 external Clojure files in my file system.(a.clj and b.clj)

/Users/e/.somedir/a.clj:

(ns a (:require [b]))

(b/printt)

/Users/e/.somedir/b.clj:

(ns b)

(defn printt [] (println "aaaa"))

The thing is I want to be able to load those files to my REPL(preserving dependency order)

If I load a.clj first I get this exception:

(clojure.main/load-script "/Users/e/.somedir/a.clj"):

CompilerException java.io.FileNotFoundException: Could not locate b__init.class or b.clj on classpath., compiling:(/Users/e/.somedir/a.clj:1:1)

If I load b.clj first there is no problem.

So how can I figure this dependency order out and load b.clj first, imagine that this is a complex dependency graph, are there any code examples?

P.S: I don't want to use leiningen or any other build tool(want to do it programmatically). There could be some kinda library/code snippet that shows me files that needed to get loaded in dependency order. How do build tools like leiningen achieve this thing? They do it in some way.


Solution

  • I think I found the solution first of all I created dependency graph then I sorted namespaces in topological order so this is the thing I've been looking for a while. Now I can load Clojure files in that order(increasing dependency order).