aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/CodegenPlugin.scala
blob: b3a97ca91dca19a2da50ba38b3c3f314b4a1bf94 (plain) (blame)
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
import sbt._
import sbt.Keys._
import complete.DefaultParsers._

object CodegenPlugin extends AutoPlugin {
  override def requires = sbt.plugins.JvmPlugin
  type TableColumn = (String, String)

  object autoImport {
    lazy val codegenURI =
      SettingKey[String]("codegen-uri", "uri for the database configuration")
    lazy val codegenPackage = SettingKey[String](
      "codegen-package",
      "package in which to place generated code")
    lazy val codegenOutputPath = SettingKey[String](
      "codegen-output-path",
      "directory to with the generated code will be written")
    lazy val codegenSchemaWhitelist = SettingKey[List[String]](
      "codegen-schema-whitelist",
      "schemas and tables to process")
    lazy val codegenForeignKeys = SettingKey[Map[TableColumn, TableColumn]](
      "codegen-foreign-keys",
      "foreign key references to data models add manually")
    lazy val codegenSchemaBaseClassParts = SettingKey[List[String]](
      "codegen-schema-base-class-parts",
      "parts inherited by each generated schema object")
    lazy val codegenIdType = SettingKey[Option[String]](
      "codegen-id-type",
      "The in-scope type `T` of kind `T[TableRow]` to apply in place T for id columns"
    )
    lazy val codegenSchemaImports = SettingKey[List[String]](
      "codegen-schema-imports",
      "A list of things to import into each schema definition"
    )
    lazy val codegenTypeReplacements = SettingKey[Map[String, String]](
      "codegen-type-replacements",
      "A map of types to find and replace"
    )

    lazy val slickCodeGenTask =
      TaskKey[Unit]("gen-tables", "generate the table definitions")

  }

  import autoImport._

  override lazy val projectSettings = Seq(
    codegenSchemaWhitelist := List.empty,
    codegenForeignKeys := Map.empty,
    codegenSchemaBaseClassParts := List.empty,
    codegenIdType := Option.empty,
    codegenSchemaImports := List.empty,
    codegenTypeReplacements := Map.empty,
    slickCodeGenTask := Def.taskDyn {
      Def.task {
        Generator.run(
          new java.net.URI(codegenURI.value),
          codegenPackage.value,
          Some(codegenSchemaWhitelist.value).filter(_.nonEmpty),
          codegenOutputPath.value,
          codegenForeignKeys.value,
          (if (codegenIdType.value.isEmpty)
             codegenSchemaBaseClassParts.value :+ "DefaultIdTypeMapper"
           else
             codegenSchemaBaseClassParts.value) match {
            case Nil => "AnyRef"
            case parts => parts.mkString(" with ")
          },
          codegenIdType.value,
          codegenSchemaImports.value,
          codegenTypeReplacements.value
        )
      }
    }.value
  )

}