-
Notifications
You must be signed in to change notification settings - Fork 10
/
component.clj
94 lines (87 loc) · 5.88 KB
/
component.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
(ns leiningen.polylith.cmd.create.component
(:require [clojure.string :as str]
[leiningen.polylith.cmd.create.interface :as create-ifc]
[leiningen.polylith.cmd.shared :as shared]
[leiningen.polylith.file :as file]))
(defn create-dev-links? [ws-path top-dir dev-dir interface]
(let [dir (str ws-path "/environments/" dev-dir "/src/" top-dir)
entities (set (file/directory-names dir))]
(not (contains? entities interface))))
(defn used-by-component [ws-path top-dir dev-dir interface]
(let [dir (str ws-path "/environments/" dev-dir "/src/" top-dir)
entities (set (file/directory-names dir))
component (first (filter #(= interface (shared/interface-of ws-path top-dir %)) entities))]
component))
(defn create-dev-links! [ws-path dev-dir component interface-dir component-dir]
(let [root (str ws-path "/environments/" dev-dir)
relative-parent-path (shared/relative-parent-path component-dir)
path (str "../../../components/" component)
relative-component-path (str relative-parent-path "components/" component)]
(file/create-symlink (str root "/docs/" component "-readme.md")
(str path "/readme.md"))
(file/create-symlink (str root "/project-files/components/" component "-project.clj")
(str "../" path "/project.clj"))
(file/create-symlink (str root "/src/" component-dir)
(str relative-component-path "/src/" component-dir))
(file/create-symlink (str root "/test/" component-dir)
(str relative-component-path "/test/" component-dir))
(file/create-symlink (str root "/src/" interface-dir)
(str relative-component-path "/src/" interface-dir))
(file/create-symlink (str root "/resources/" component)
(str path "/resources/" component))))
(defn create [ws-path top-dir top-ns clojure-version component interface-name]
(let [interface (if (str/blank? interface-name) component interface-name)
interface-dir (shared/full-dir-name top-dir interface)
component-dir (shared/full-dir-name top-dir component)
comp-root-dir (str ws-path "/components/" component)
interface-ns-name (shared/full-name top-ns "." interface)
component-ns-name (shared/full-name top-ns "." component)
project-ns (shared/full-name top-ns "/" component)
interfaces-dependencies (shared/full-name top-ns "/" "interfaces")
delegate-content [(str "(ns " interface-ns-name ".interface")
(str " (:require [" component-ns-name ".core :as core]))")
""
";; delegate to the implementations..."
"(defn add-two [x]"
" (core/add-two x))"]
core-content [(str "(ns " component-ns-name ".core)")
""
";; add your functions here..."
"(defn add-two [x]"
" (+ 2 x))"]
doc-content [(str "# " component " component")
""
"add documentation here..."]
test-content [(str "(ns " component-ns-name ".core-test")
(str " (:require [clojure.test :refer :all]")
(str " [" interface-ns-name ".interface :as " component "]))")
""
";; add your tests here..."
"(deftest test-add-two"
(str " (is (= 42 (" component "/add-two 40))))")]
project-content [(str "(defproject " project-ns " \"0.1\"")
(str " :description \"A " component " component.\"")
(str " :dependencies [[" interfaces-dependencies " \"1.0\"]")
(str " " (shared/->dependency "org.clojure/clojure" clojure-version) "]")
(str " :aot :all)")]
dev-dirs (file/directory-names (str ws-path "/environments"))]
(file/create-dir comp-root-dir)
(file/create-dir (str comp-root-dir "/resources"))
(file/create-file (str comp-root-dir "/resources/.keep") [""])
(file/create-dir (str comp-root-dir "/resources/" component))
(file/create-file (str comp-root-dir "/resources/" component "/.keep") [""])
(shared/create-src-dirs! ws-path (str "components/" component "/src") [interface-dir component-dir])
(shared/create-src-dirs! ws-path (str "components/" component "/test") [component-dir])
(file/create-file (str comp-root-dir "/project.clj") project-content)
(file/create-file (str comp-root-dir "/readme.md") doc-content)
(file/create-file (str comp-root-dir "/src/" interface-dir "/interface.clj") delegate-content)
(file/create-file (str comp-root-dir "/src/" component-dir "/core.clj") core-content)
(file/create-file (str comp-root-dir "/test/" component-dir "/core_test.clj") test-content)
(when-not (file/file-exists (str ws-path "/interfaces/src/" interface-dir))
(create-ifc/create-interface ws-path top-dir top-ns interface))
(doseq [dev-dir dev-dirs]
(if (create-dev-links? ws-path top-dir dev-dir interface)
(create-dev-links! ws-path dev-dir component interface-dir component-dir)
(println (str "FYI: the component " component " was created but not added to " dev-dir
" because it's interface " interface " was already "
"used by " (used-by-component ws-path top-dir dev-dir interface) "."))))))