Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/darkleaf/di/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -826,3 +826,68 @@
(p/demolish factory obj)
(after-demolish! {:key key :object obj})
nil))))))

(defn inspect []
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should move it into a separate PR.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#27

(fn [registry]
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add space

(fn [key]
(let [factory (registry key)]
(reify p/Factory
(dependencies [_]
(p/dependencies factory))
(build [_ deps]
(into [{:key key :deps (keys deps)}]
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{:key key 
 :deps (keys deps)
 :dep-types (p/dependencies this)}

(comp
(mapcat val)
(distinct))
deps))
(demolish [_ obj]))))))


(defn- graphviz-id [x]
(str "\"" x "\""))

(defn render-graphviz [root middlewares]
(let [system (start root middlewares (inspect))
components @system]

(with-out-str
(println "digraph {")
(println "rankdir=LR;")
#_(println "splines=ortho;")
#_(println "concentrate=true;")

(println "node [shape=box color=gray style=rounded];")
(println "edge [color=gray arrowhead=empty];")

#_
(println (graphviz-id (-> components first :key))
(str "[color=magenta];"))

(doseq [{:keys [key deps]} components]
(print "{")
(doseq [token (interpose "," (map graphviz-id deps))]
(print token))
(println "}" "->" (graphviz-id key)))

#_
(doseq [[cluster keys] (->> components
(map :key)
(group-by try-namespace))
:when (and (some? cluster)
(not (str/starts-with? cluster "env"))
(not (str/starts-with? cluster "darkleaf.di.core")))]
(println "subgraph" (graphviz-id cluster) "{")
(println " " (str "label=" (graphviz-id cluster) ";"))
(println " " "cluster=true;")
(println " " "labeljust=l;")
(println " " "color=gray;")
(println " " "style=dashed;")

(doseq [key keys]
(println " " (graphviz-id key)
(str "["
"label=" (graphviz-id (name key)) ;; keywords must start with :
"];")))
(println "}"))

(println "}"))))
26 changes: 26 additions & 0 deletions test/darkleaf/di/tutorial/x_inspect_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(ns darkleaf.di.tutorial.x-inspect-test
(:require
[clojure.test :as t]
[darkleaf.di.core :as di]))

(defn a []
:ok)

(defn b [{a `a}]
:ok)

(defn c [{a `a
b `b}]
:ok)

(t/deftest ok
(with-open [info (di/start `c (di/inspect))]
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should not be a middleware

(di/inspect `c registry-middlewares)

(t/is (= [{:key `c :deps [`a `b]}
{:key `a :deps nil}
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we remove a key with nil value?

{:key `b :deps [`a]}]
@info))))


(comment
(di/render-graphviz `c nil)
,,,)