-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
lib_table.clj
157 lines (131 loc) · 6.93 KB
/
lib_table.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
(ns polylith.clj.core.lib.text-table.lib-table
(:require [polylith.clj.core.util.interface.str :as str-util]
[polylith.clj.core.text-table.interface :as text-table]))
(def type->color {"component" :green
"base" :blue})
(defn lib [[name {:keys [version size type]}]]
[(cond-> {:name name
:version (or version "-")}
size (assoc :size size)
type (assoc :type type))])
(defn brick-libs [{:keys [name lib-deps]}]
[name (set (mapcat lib
(concat (:src lib-deps)
(:test lib-deps))))])
(defn lib-cell [column row library]
(text-table/cell column row library :none :left :horizontal))
(defn lib-column [libraries]
(concat [(text-table/cell 1 1 "library" :none :left :horizontal)]
(map-indexed #(lib-cell 1 (+ 3 %1) %2)
(map :name libraries))))
(defn version-column [libraries]
(concat [(text-table/cell 3 1 "version" :none :left :horizontal)]
(map-indexed #(lib-cell 3 (+ 3 %1) %2)
(map :version libraries))))
(defn type-column [libraries]
(concat [(text-table/cell 5 1 "type" :none :left :horizontal)]
(map-indexed #(lib-cell 5 (+ 3 %1) %2)
(map :type libraries))))
(defn size-kb [{:keys [size]} thousand-separator]
(if size
(str-util/sep-1000 (quot size 1024) thousand-separator)
"-"))
(defn kb-cell [row library thousand-separator]
(let [size (size-kb library thousand-separator)]
(text-table/cell 7 row size :none :right :horizontal)))
(defn size-column [libraries thousand-separator]
(concat [(text-table/cell 7 1 "KB" :none :right :horizontal)]
(map-indexed #(kb-cell (+ 3 %1) %2 thousand-separator)
libraries)))
(defn flag-cell [column row lib-dep src-deps test-deps]
(let [src (if (contains? src-deps lib-dep) 1 0)
test (if (contains? test-deps lib-dep) 2 0)
flag (get ["-" "x" "t" "x"] (+ src test))]
(text-table/cell column row flag :purple :center :horizontal)))
(defn project-column [column {:keys [alias lib-deps unmerged]} libraries]
(let [deps (:lib-deps unmerged lib-deps)
src-deps (set (mapcat lib (:src deps)))
test-deps (set (mapcat lib (:test deps)))]
(concat [(text-table/cell column 1 alias :purple :center :horizontal)]
(map-indexed #(flag-cell column (+ 3 %1) %2 src-deps test-deps)
libraries))))
(defn project-columns [libraries projects]
(apply concat (map-indexed #(project-column (+ 9 (* 2 %1)) %2 libraries)
projects)))
(defn profile-flag-cell [column row lib-dep lib-deps]
(let [flag (if (contains? lib-deps lib-dep) "x" "-")]
(text-table/cell column row flag :purple :center :horizontal)))
(defn profile-column [column libraries [profile {:keys [lib-deps]}]]
(let [deps (set (mapcat lib lib-deps))]
(concat [(text-table/cell column 1 profile :purple :center :horizontal)]
(map-indexed #(profile-flag-cell column (+ 3 %1) %2 deps)
libraries))))
(defn profile-columns [column libraries profile-to-settings]
(apply concat (map-indexed #(profile-column (+ column (* 2 %1)) libraries %2)
profile-to-settings)))
(defn contains-lib? [library libraries]
(or (contains? libraries library)
(contains? libraries {:name (:name library)})))
(defn brick-cell-sign [library src-libs]
(if (contains? src-libs library) "x" "t"))
(defn brick-cell [column row library src-libs brick-libs empty-character]
(let [flag (if (contains-lib? library brick-libs)
(brick-cell-sign library src-libs)
empty-character)]
(text-table/cell column row flag :none :left :vertical)))
(defn brick-column [column {:keys [name type]} libraries src-libs brick->libs empty-character]
(concat [(text-table/cell column 1 name (type->color type) :right :vertical)]
(map-indexed #(brick-cell column (+ 3 %1) %2 src-libs (brick->libs name) empty-character)
libraries)))
(defn brick-columns [column bricks libraries src-libs brick->libs empty-character]
(apply concat (map-indexed #(brick-column (+ column (* 2 %1)) %2 libraries src-libs brick->libs empty-character)
bricks)))
(defn profile-lib [[_ {:keys [lib-deps]}]]
(mapcat lib lib-deps))
(defn table [{:keys [settings components bases projects]} is-all]
(let [{:keys [profile-to-settings empty-character thousand-separator color-mode compact-views]} settings
entities (concat components bases projects)
src-libs (set (concat (mapcat lib (mapcat #(-> % :lib-deps :src) entities))
(mapcat profile-lib profile-to-settings)))
test-libs (set (mapcat lib (mapcat #(-> % :lib-deps :test) entities)))
libraries (sort-by (juxt :name :version)
(set (concat src-libs test-libs)))
all-bricks (concat components bases)
brick->libs (into {} (map brick-libs all-bricks))
bricks (if is-all
all-bricks
(filter #(-> % :name brick->libs empty? not) all-bricks))
lib-col (lib-column libraries)
version-col (version-column libraries)
type-col (type-column libraries)
size-col (size-column libraries thousand-separator)
project-cols (project-columns libraries projects)
profile-col (+ 9 (* 2 (count projects)))
n#dev (count (filter :is-dev projects))
profile-cols (if (zero? n#dev) [] (profile-columns profile-col libraries profile-to-settings))
brick-col (+ profile-col (* 2 (count profile-to-settings)))
n#projects (count projects)
n#profiles (if (zero? n#dev) 0 (count profile-to-settings))
n#bricks (count bricks)
brick-cols (brick-columns brick-col bricks libraries src-libs brick->libs empty-character)
end-space-column (* 2 (+ 4 n#projects n#profiles))
space-columns (range 2 end-space-column 2)
space-brick-columns (range (+ 2 end-space-column) (+ end-space-column (* 2 n#bricks)) 2)
space (if (contains? compact-views "libs") " " " ")
spaces (concat (text-table/spaces 1 space-columns (repeat " "))
(text-table/spaces 1 space-brick-columns (repeat space)))
cells (text-table/merge-cells lib-col version-col type-col size-col project-cols profile-cols brick-cols spaces)
line (text-table/line 2 cells)
section2 (+ 6 (* 2 n#projects))
section3 (+ 8 (* 2 (+ n#projects n#profiles)))
sections (if (zero? n#dev) [8 section3] [8 section2 section3])
spaces (text-table/spaces 2 sections (repeat " "))]
(text-table/table " " color-mode cells line spaces)))
(defn print-table [workspace is-all]
(text-table/print-table (table workspace is-all)))
(comment
(require '[dev.development :as dev])
(print-table dev/workspace false)
(require '[dev.jocke :as jocke])
(print-table jocke/workspace false)
#__)