-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.clj
executable file
·174 lines (136 loc) · 4.59 KB
/
build.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env bb
;; Requires
;; ##################################
(require '[clojure.string :as str])
(require '[clojure.java.io :as io])
(require '[babashka.pods :as pods])
(require '[clojure.pprint :as pprint])
;; Pods
;; ##################################
(pods/load-pod 'retrogradeorbit/bootleg "0.1.9")
;; Pod requires
;; ##################################
(require '[pod.retrogradeorbit.bootleg.utils :as bootleg-utils])
;; Icons
;; ##################################
(defn file->file-name-parts
[a-file]
(str/split (.getName a-file) #"\."))
(defn file->file-ext
[a-file]
(last (file->file-name-parts a-file)))
(defn svg?
[a-file]
(and (.isFile a-file)
(= "svg" (file->file-ext a-file))))
(def outline-icons (->> (io/file "heroicons/optimized/outline")
file-seq
(filter svg?)))
(def solid-icons (->> (io/file "heroicons/optimized/solid")
file-seq
(filter svg?)))
;; Helpers
;; ##################################
(defn is-blank?
[entry]
(and (string? entry)
(str/blank? entry)))
(defn html->hiccup
[html]
(->> (-> html
(str/replace #"(\n|\s)+" " ")
(str/trim))
bootleg-utils/html->hiccup
(remove is-blank?)
vec))
(defn file->file-name [a-file]
(first (file->file-name-parts a-file)))
;; Core
;; ##################################
(defn svg-hiccup->def-str
[icon-name svg-hiccup]
(str "(def " icon-name " " svg-hiccup ")"))
(defn snakeify
[a-str]
(str/replace a-str "-" "_"))
(defn transform-keyword
[k]
(symbol (str "com.fulcrologic.fulcro.dom/" (name k))))
(def keyword-conversions
{:stroke :stroke
:xmlns :xmlns
:fill :fill
:d :d
:viewbox :viewBox
:fill-rule :fillRule
:clip-rule :clipRule
:stroke-linecap :strokeLinecap
:stroke-linejoin :strokeLinejoin
:stroke-width :strokeWidth})
(defn transform-args
[args]
(reduce-kv
(fn [m k v]
(let [new-k (or (get keyword-conversions k)
(throw (Exception. (str "No conversion rule for " k))))]
(assoc m new-k v)))
{}
args))
;; Reagent
;; ##################################
(defn reactify
[svg-hiccup]
(condp #(%1 %2) svg-hiccup
keyword? svg-hiccup
map? (transform-args svg-hiccup)
vector? (mapv reactify svg-hiccup)
(throw (Exception. (str "Unknown token: " svg-hiccup)))))
(defn make-reagent-icon
[ns-name a-file]
(let [file-name (file->file-name a-file)
icon-def-str (svg-hiccup->def-str file-name (-> a-file slurp html->hiccup reactify))
file-content (str "(ns to.fluent.heroicons-clojure.reagent." ns-name "." file-name "\n"
" (:refer-clojure :exclude [" file-name "]))"
"\n\n"
icon-def-str
"\n\n"
"(def " file-name "-icon " file-name ")")
file-name* (snakeify file-name)
file-path (str "src/to/fluent/heroicons_clojure/reagent/" (snakeify ns-name))]
(.mkdirs (io/file file-path))
(spit (str file-path "/" file-name* ".cljc") file-content)))
;; Fulcro
;; ##################################
(defn fulcroify
[svg-hiccup]
(condp #(%1 %2) svg-hiccup
keyword? (transform-keyword svg-hiccup)
map? (transform-args svg-hiccup)
vector? (apply list (map fulcroify svg-hiccup))
(throw (Exception. (str "Unknown token: " svg-hiccup)))))
(defn make-fulcro-icon
[ns-name a-file]
(let [file-name (file->file-name a-file)
icon-def-str (svg-hiccup->def-str file-name (-> a-file slurp html->hiccup fulcroify))
file-content (str "(ns to.fluent.heroicons-clojure.fulcro." ns-name "." file-name "\n"
" (:refer-clojure :exclude [" file-name "]))"
"\n\n"
icon-def-str
"\n\n"
"(def " file-name "-icon " file-name ")")
file-name* (snakeify file-name)
file-path (str "src/to/fluent/heroicons_clojure/fulcro/" (snakeify ns-name))]
(.mkdirs (io/file file-path))
(spit (str file-path "/" file-name* ".cljc") file-content)))
;; Generate solid icons
;; ##################################
(doseq [solid-icon solid-icons]
(println "Processing solid" (file->file-name solid-icon))
(make-reagent-icon "solid" solid-icon)
(make-fulcro-icon "solid" solid-icon))
;; Generate outline icons
;; ##################################
(doseq [outline-icon outline-icons]
(println "Processing outline" (file->file-name outline-icon))
(make-reagent-icon "outline" outline-icon)
(make-fulcro-icon "outline" outline-icon))