-
Notifications
You must be signed in to change notification settings - Fork 1
/
assets.clj
181 lines (151 loc) · 4.8 KB
/
assets.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
175
176
177
178
179
180
181
(ns dar.assets
(:require [dar.container :refer :all]
[dar.assets.util :as util]
[dar.assets.cljs :as cljs]
[clojure.java.io :as io]
[clojure.string :as string]
[hiccup.core :as hiccup]
[hiccup.page :refer [html5]]))
(set! *warn-on-reflection* true)
(application development)
(define :assets/main
:doc "Name of the main package")
(define :assets/packages
:doc "List of packages to build (in dependency order)"
:args [:assets/main]
:fn (fn [main]
(util/topo-visit
:dependencies
util/read
conj
[]
[main])))
(define :assets/main-pkg
:args [:assets/main]
:fn (fn [main]
(util/read main)))
(define :assets/build-dir
:doc "A place to store build products")
(define :assets/public-dir
:doc
"A dir to store public files to be served by HTTP server"
:args [:assets/build-dir]
:fn identity)
(define :assets/public-url "/"
:doc "URL under which public dir will be served")
(defn files [type packages]
(for [pkg packages
file (get pkg type)
:let [path (util/resource-path pkg file)
src (io/resource path)]]
(do
(when-not src
(throw
(Exception. (str type " file " file " not found in package " name))))
{:path path
:src src
:pkg pkg})))
(defn copy-file [{:keys [path src]} dir]
(let [target (io/file dir path)]
(when (util/outdate? target src)
(util/cp src target))))
(defn- trim-quotes [[q :as ^String s]]
(if (or (= \' q) (= \" q))
(.substring s 1 (dec (.length s)))
s))
(defn css-url-rewrite [file url-prefix]
(let [css (slurp (:src file))]
(string/replace css #"\burl *\(([^)]+)\)"
(fn [[_ url]]
(let [^String url (trim-quotes url)]
(str
"url('"
(cond
(.contains url "data:") url
(= "//" (.substring url 2)) (util/join url-prefix (.substring url 2))
(util/absolute? url) url
:else (util/join url-prefix (:path file) ".." url))
"')"))))))
(define :css/links
:doc "Builds Css and returns a list of link tags to include in HTML page"
:args [:assets/packages :assets/public-dir :assets/public-url]
:fn (fn [packages dir prefix]
(mapv (fn [file]
(let [target (io/file dir (:path file))]
(when (util/outdate? target (:src file))
(util/write (css-url-rewrite file prefix) target)))
[:link {:href (util/join prefix (:path file))
:rel "stylesheet"
:type "text/css"}])
(files :css packages))))
(define :files
:doc "Copies :files to public-dir"
:args [:assets/packages :assets/public-dir]
:fn (fn [packages dir]
(doseq [file (files :files packages)]
(copy-file file dir))))
(include cljs/development)
(define :cljs/main
:args [:assets/main-pkg]
:fn #(-> % :page :main-ns))
(define :page
:pre [:files]
:args [:css/links :cljs/scripts :cljs/main-script :page/body :page/title :page/head]
:fn (fn [css scripts main body title head]
(hiccup/html
(html5
[:html
[:head
[:title title]
head
(seq css)
(seq scripts)]
[:body body main]]))))
(define :page/title
:args [:assets/main-pkg]
:fn (fn [pkg]
(or (-> pkg :page :title) (-> pkg :name))))
(defn- to-html [html pkg]
(cond
(nil? html) nil
(string? html) (if-let [url (io/resource (util/resource-path pkg html))]
(slurp url)
(throw
(Exception.
(str "Html file " html " not found in package " (:name pkg)))))
:else (hiccup/html html)))
(define :page/body
:args [:assets/main-pkg]
:fn (fn [pkg]
(to-html (-> pkg :page :body) pkg)))
(define :page/head
:args [:assets/main-pkg]
:fn (fn [pkg]
(to-html (-> pkg :page :head) pkg)))
(application production)
(include development)
(include cljs/production-patch)
(define :assets/public-url nil)
(define :assets/public-dir
:args [:assets/build-dir]
:fn (fn [dir]
(util/fs-join dir "out")))
(define :cljs/build-dir
:args [:assets/build-dir]
:fn (fn [dir]
(util/fs-join dir "cljs")))
(define :css/links
:args [:assets/packages :assets/public-url]
:fn (fn [packages url]
(mapv (fn [file]
[:style (css-url-rewrite file url)])
(files :css packages))))
(define :index.html
:args [:page :assets/public-dir]
:fn (fn [html dir]
(util/write html (io/file dir "index.html"))))
(defn build
([main build-dir]
(<?!evaluate (start production {:assets/main main
:assets/build-dir build-dir})
:index.html)))