-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.boot
179 lines (164 loc) · 6.87 KB
/
build.boot
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
(set-env!
:source-paths #{"src" "content"}
:resource-paths #{"resources"}
:dependencies '[[boot/core "2.7.1"]
[org.clojure/tools.nrepl "0.2.12"]
[pandeiro/boot-http "0.7.6" :exclusions [org.clojure/clojure]]
[adzerk/boot-cljs "1.7.228-2"]
[adzerk/boot-reload "0.4.13"]
[hashobject/boot-s3 "0.1.2-SNAPSHOT" :exclusions [org.clojure/clojure]]
[hiccup "1.0.5" :exclusions [org.clojure/clojure]]
[perun "0.4.1-SNAPSHOT"]]
:checkouts '[[perun "0.4.1-SNAPSHOT"]])
(require '[boot.core :as boot]
'[boot.pod :as pod]
'[adzerk.boot-cljs :refer [cljs]]
'[pandeiro.boot-http :refer [serve]]
'[adzerk.boot-reload :refer [reload]]
'[hashobject.boot-s3 :refer [s3-sync]]
'[clojure.java.io :as io]
'[clojure.string :as str]
'[io.perun :as p]
'[io.perun.core :as perun]
'[io.perun.meta :as pm]
'[nicerthantriton.core :as ntt])
(defn tagify
[entries]
(->> entries
(mapcat (fn [entry]
(map #(-> [% entry]) (:tags entry))))
(reduce (fn [result [tag entry]]
(let [topic (get ntt/tag-topics tag)
path (ntt/topic-href topic)]
(-> result
(update-in [path :entries] conj entry)
(assoc-in [path :entry :title] topic))))
{})))
(defn post?
[{:keys [path]}]
(.startsWith path "public/posts/"))
(def +recent-posts-defaults+
{:num-posts 5})
(deftask recent-posts
"Adds the `n` most recent posts to global metadata as `:recent-posts`"
[n num-posts NUMPOSTS int "The number of posts to store"]
(with-pre-wrap fileset
(let [options (merge +recent-posts-defaults+ *opts*)
global-meta (pm/get-global-meta fileset)
recent (->> (pm/get-meta fileset)
(filter post?)
(sort-by :date-published #(compare %2 %1))
(take (:num-posts options)))]
(perun/report-info "recent-posts" "added %s posts to metadata" (count recent))
(pm/set-global-meta fileset (assoc global-meta :recent-posts recent)))))
(deftask topics
"Generates topics from the tags on each post, and adds them to global metadata as `:topics`"
[]
(with-pre-wrap fileset
(let [global-meta (pm/get-global-meta fileset)
topics (->> (pm/get-meta fileset)
(mapcat :tags)
(map #(get ntt/tag-topics %))
set
(sort-by #(.indexOf ntt/topic-order %)))]
(perun/report-info "topics" "added %s generated topics to metadata" (count topics))
(pm/set-global-meta fileset (assoc global-meta :topics topics)))))
(deftask set-tier
"Sets the `:tier` key in perun's global metadata, for render fn's to dispatch on"
[v val VAL kw "The value to set for :tier"]
(with-pre-wrap fileset
(let [global-meta (pm/get-global-meta fileset)]
(perun/report-info "tier" "set :tier to %s" val)
(pm/set-global-meta fileset (assoc global-meta :tier val)))))
(def minify-css-deps '[[asset-minifier "0.2.0"]])
(deftask minify-css
"Would you believe that this task finds CSS files, and minifies them?"
[]
(let [out (boot/tmp-dir!)
prev (atom nil)
pod (-> (boot/get-env)
(update-in [:dependencies] into minify-css-deps)
pod/make-pod
future)]
(boot/with-pre-wrap fileset
(let [files (->> (boot/fileset-diff @prev fileset :hash)
boot/ls
(boot/by-ext [".css"]))]
(doseq [file files
:let [new-file (io/file out (boot/tmp-path file))]]
(io/make-parents new-file)
(pod/with-call-in @pod
(asset-minifier.core/minify-css ~(.getPath (boot/tmp-file file))
~(.getPath new-file))))
(perun/report-info "minify-css" "Minified %s css file(s)" (count files))
(-> fileset (boot/add-resource out) boot/commit!)))))
(def content-types
{"html" "text/html; charset=utf-8"
"css" "text/css; charset=utf-8"
"js" "application/javascript; charset=utf-8"
"svg" "image/svg+xml; charset=utf-8"
"xml" "application/xml; charset=utf-8"
"json" "application/json; charset=utf-8"})
(deftask s3-metadata
"Adds boot-s3-compatible metadata for setting headers on files destined for s3"
[]
(boot/with-pre-wrap fileset
(let [output (boot/output-files fileset)
s3-meta (->> (keys content-types)
(map #(->
[% (map :path
(boot/by-re
[(re-pattern (str "^public/.*\\." %))]
output))]))
(mapcat (fn [[type files]]
(map #(-> [% {:hashobject/boot-s3
{:metadata
{:content-type
(get content-types type)}}}])
files)))
(into {}))]
(boot/add-meta fileset s3-meta))))
(deftask build
"Build nicerthantriton.com"
[t tier TIER kw "The tier we're building for"]
(comp (p/global-metadata)
(set-tier :val tier)
(p/markdown :options {:extensions {:smarts true}})
(p/atom-feed :filterer post?)
(p/collection :renderer 'nicerthantriton.core/index
:filterer post?)
(p/assortment :renderer 'nicerthantriton.core/topic
:filterer post?
:grouper tagify)
(recent-posts)
(topics)
(p/render :renderer 'nicerthantriton.core/post
:filterer post?)
(p/render :renderer 'nicerthantriton.core/page)
(p/sitemap :filterer #(not= (:slug %) "404"))
#_(p/print-meta :extensions [".html"])))
(deftask dev
"Build nicerthantriton.com dev environment with reloading"
[]
(comp (serve :resource-root "public/")
(watch)
(build :tier :dev)
(reload :asset-path "/public" :port 62728 :ws-port 62728)
(cljs)
#_(p/print-meta)))
(def aws-edn
(read-string (slurp "aws.edn")))
(deftask deploy
"Build nicerthantriton.com and deploy to production s3 bucket"
[]
(comp (build :tier :prod)
(minify-css)
(p/inject-scripts :scripts #{"ga-inject.js"})
(cljs :optimizations :advanced)
(sift :include [#"\.cljs\.edn$" #"js/main\.out/"] :invert true)
(s3-metadata)
(s3-sync :bucket "nicerthantriton.com"
:source "public"
:access-key (:access-key aws-edn)
:secret-key (:secret-key aws-edn)
:metadata {:cache-control "max-age=315360000, no-transform, public"})))