forked from http4s/http4s
/
MimeGenerator.scala
278 lines (267 loc) · 10.4 KB
/
MimeGenerator.scala
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package mimegenerator
import cats.effect.{ContextShift, ExitCode, IO, IOApp}
import io.circe._
import io.circe.generic.semiauto._
import java.io.File
import java.io.PrintWriter
import fs2.Stream
import org.http4s.circe._
import org.http4s.client.blaze._
import org.http4s.Uri
import scala.concurrent.ExecutionContext.global
import treehugger.forest._
import definitions._
import treehuggerDSL._
/**
* MimeLoader is able to generate a scala file with a database of MediaTypes.
* The list of MediaTypes is produced from the list published by `mime-db` in json format.
* This json file is parsed, converted to a list of MediaTypes grouped by main type and
* converted to a Treehugger syntax tree which is later printed as a scala source file
*/
object MimeLoader {
implicit val MimeDescrDecoder: Decoder[MimeDescr] = deriveDecoder[MimeDescr]
val url = Uri.uri("https://cdn.rawgit.com/jshttp/mime-db/master/db.json")
// Due to the limits on the jvm class size (64k) we cannot put all instances in one object
// This particularly affects `application` which needs to be divided in 2
val maxSizePerSection = 500
def readMimeDB(implicit cs: ContextShift[IO]): Stream[IO, List[Mime]] =
for {
client <- BlazeClientBuilder[IO](global).stream
_ <- Stream.eval(IO(println(s"Downloading mimedb from $url")))
value <- Stream.eval(client.expect[Json](url))
obj <- Stream.eval(IO(value.arrayOrObject(JsonObject.empty, _ => JsonObject.empty, identity)))
} yield {
obj.toMap
.map(x => (x._1.split("/").toList, x._2))
.collect {
case (m :: s :: Nil, d) =>
d.as[MimeDescr] match {
case Right(md) => Some(Mime(m, s, md))
case Left(_) => None
}
}
.collect {
case Some(x) => x
}
.toList
.sortBy(m => (m.mainType, m.secondaryType))
}
/**
* This method will generate trees to produce code for a mime mime type set
*/
def toTree(mainType: String, objectName: String, mediaTypeClassName: String)(
mimes: List[Mime]): (List[Tree], String) = {
def subObject(
partial: Boolean,
listVal: String,
objectName: String,
mimes: List[Mime]): Tree = {
val all
: Tree = LAZYVAL(listVal, ListClass.TYPE_OF(TYPE_REF(REF(mediaTypeClassName)))) := LIST(
mimes.map(m => REF(m.valName)))
val mediaTypeClass = RootClass.newClass(mediaTypeClassName)
val vals: List[Tree] = mimes.map(_.toTree(mediaTypeClass))
val allVals = vals :+ all
(if (partial) TRAITDEF(objectName) else OBJECTDEF(objectName)) := BLOCK(allVals)
}
if (mimes.length <= maxSizePerSection) {
(List(subObject(false, "all", objectName, mimes)), objectName)
} else {
val subObjects: List[(Tree, String)] = mimes
.sliding(maxSizePerSection, maxSizePerSection)
.zipWithIndex
.map {
case (mimes, i) =>
val mimeObjectName = s"${objectName}_$i"
(subObject(true, s"part_$i", mimeObjectName, mimes), mimeObjectName)
}
.toList
val reducedAll =
subObjects.zipWithIndex.map { case (_, i) => REF(s"part_$i") }.foldLeft(NIL) { (a, b) =>
a.SEQ_++(b)
}
val all
: Tree = LAZYVAL("all", ListClass.TYPE_OF(TYPE_REF(REF(mediaTypeClassName)))) := reducedAll
val objectPartsDefinition = OBJECTDEF(s"${objectName}_parts")
.withFlags(PRIVATEWITHIN("http4s")) := BLOCK(subObjects.map(_._1))
val objectDefinition = OBJECTDEF(objectName).withParents(subObjects.map(x =>
s"${objectName}_parts.${x._2}")) := BLOCK(all)
(List(objectPartsDefinition, objectDefinition), mainType)
}
}
/**
* Takes all the main type generated trees and put them together on the final tree
*/
def coalesce(
l: List[(List[Tree], String)],
topLevelPackge: String,
objectName: String,
mediaTypeClassName: String): Tree = {
val privateWithin = topLevelPackge.split("\\.").toList.lastOption.getOrElse("this")
val reducedAll =
l.sortBy(_._2).reverse.map(m => REF(s"${m._2.replaceAll("-", "_")}.all")).foldLeft(NIL) {
(a, b) =>
a.SEQ_++(b)
}
val all
: Tree = LAZYVAL("allMediaTypes", ListClass.TYPE_OF(TYPE_REF(REF(mediaTypeClassName)))) := reducedAll
val compressible: Tree = VAL("Compressible", BooleanClass) := TRUE
val uncompressible: Tree = VAL("Uncompressible", BooleanClass) := FALSE
val binary: Tree = VAL("Binary", BooleanClass) := TRUE
val notBinary: Tree = VAL("NotBinary", BooleanClass) := FALSE
(TRAITDEF(objectName).withFlags(PRIVATEWITHIN(privateWithin)) := BLOCK(
List(all, compressible, uncompressible, binary, notBinary) ::: l.flatMap(_._1)))
.inPackage(topLevelPackge)
.withComment("Autogenerated file, don't edit")
}
// All actual file writing happens here
private def treeToFile(f: File, t: Tree): Unit = {
// Create the dir if needed
Option(f.getParentFile).foreach(_.mkdirs())
val writer = new PrintWriter(f)
writer.write(treeToString(t))
writer.close()
}
/**
* This method will dowload the MimeDB and produce a file with generated code for http4s
*/
def toFile(f: File, topLevelPackge: String, objectName: String, mediaTypeClassName: String)(
implicit cs: ContextShift[IO]): IO[Unit] =
(for {
m <- readMimeDB
t <- Stream.emit(m.groupBy(_.mainType).toList.sortBy(_._1).map {
case (t, l) => toTree(t, s"${t.replaceAll("-", "_")}", mediaTypeClassName)(l)
})
o <- Stream.emit(coalesce(t.toList, topLevelPackge, objectName, mediaTypeClassName))
_ <- Stream.emit(treeToFile(f, o))
} yield ()).compile.drain
}
final case class MimeDescr(extensions: Option[List[String]], compressible: Option[Boolean])
final case class Mime(mainType: String, secondaryType: String, descr: MimeDescr) {
// Binary is not on MimeDB. we'll use same mnemonics as in http4s before #1770
def isBinary: Boolean = mainType match {
case "audio" => true
case "image" => true
case "message" => false
case "text" => false
case "video" => true
case "multipart" => false
case "application" =>
secondaryType match {
case "atom+xml" => false
case "base64" => true
case "excel" => true
case "font-woff" => true
case "gnutar" => true
case "gzip" => true
case "hal+json" => true
case "java-archive" => true
case "javascript" => false
case "json" => true
case "lha" => true
case "lzx" => true
case "mspowerpoint" => true
case "msword" => true
case "octet-stream" => true
case "pdf" => true
case "problem+json" => true
case "postscript" => true
case "rss+xml" => false
case "soap+xml" => false
case "vnd.api+json" => true
case "vnd.google-earth.kml+xml" => false
case "vnd.google-earth.kmz" => true
case "vnd.ms-fontobject" => true
case "vnd.oasis.opendocument.chart" => true
case "vnd.oasis.opendocument.database" => true
case "vnd.oasis.opendocument.formula" => true
case "vnd.oasis.opendocument.graphics" => true
case "vnd.oasis.opendocument.image" => true
case "vnd.oasis.opendocument.presentation" => true
case "vnd.oasis.opendocument.spreadsheet" => true
case "vnd.oasis.opendocument.text" => true
case "vnd.oasis.opendocument.text-master" => true
case "vnd.oasis.opendocument.text-web" => true
case "vnd.openxmlformats-officedocument.presentationml.presentation" => true
case "vnd.openxmlformats-officedocument.presentationml.slide" => true
case "vnd.openxmlformats-officedocument.presentationml.slideshow" => true
case "vnd.openxmlformats-officedocument.presentationml.template" => true
case "vnd.openxmlformats-officedocument.spreadsheetml.sheet" => true
case "vnd.openxmlformats-officedocument.spreadsheetml.template" => true
case "vnd.openxmlformats-officedocument.wordprocessingml.document" => true
case "vnd.openxmlformats-officedocument.wordprocessingml.template" => true
case "x-7z-compressed" => true
case "x-ace-compressed" => true
case "x-apple-diskimage" => true
case "x-arc-compressed" => true
case "x-bzip" => true
case "x-bzip2" => true
case "x-chrome-extension" => true
case "x-compress" => true
case "x-debian-package" => true
case "x-dvi" => true
case "x-font-truetype" => true
case "x-font-opentype" => true
case "x-gtar" => true
case "x-gzip" => true
case "x-latex" => true
case "x-rar-compressed" => true
case "x-redhat-package-manager" => true
case "x-shockwave-flash" => true
case "x-tar" => true
case "x-tex" => true
case "x-texinfo" => true
case "x-vrml" => false
case "x-www-form-urlencoded" => false
case "x-x509-ca-cert" => true
case "x-xpinstall" => true
case "xhtml+xml" => false
case "xml-dtd" => false
case "xml" => false
case "zip" => true
case _ => false
}
case _ => false
}
val compressibleRef =
if (descr.compressible.forall(_ == true)) Mime.CompressibleRef else Mime.UncompressibleRef
val binaryRef = if (isBinary) Mime.BinaryRef else Mime.NotBinaryRef
val extensions: Tree =
descr.extensions.filter(_.nonEmpty).map(x => LIST(x.map(LIT))).getOrElse(NIL)
val valName: String = s"`$secondaryType`"
def toTree(mediaTypeClass: ClassSymbol): Tree =
if (descr.extensions.isEmpty) {
LAZYVAL(valName, mediaTypeClass) := NEW(
mediaTypeClass,
LIT(mainType),
LIT(secondaryType),
compressibleRef,
binaryRef)
} else {
LAZYVAL(valName, mediaTypeClass) := NEW(
mediaTypeClass,
LIT(mainType),
LIT(secondaryType),
compressibleRef,
binaryRef,
extensions)
}
}
object Mime {
// References to constants
val CompressibleRef = REF("Compressible")
val UncompressibleRef = REF("Uncompressible")
val BinaryRef = REF("Binary")
val NotBinaryRef = REF("NotBinary")
}
/**
* Utility launcher
*/
object MimeLoaderApp extends IOApp {
def run(args: List[String]): IO[ExitCode] = {
val dir = args.headOption.getOrElse(".")
MimeLoader
.toFile(new File(dir, "MimeDB.scala"), "org.http4s", s"MimeDB", "MediaType")
.map(_ => ExitCode.Success)
}
}