aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/CodegenPlugin.scala
blob: 55d40890b99a4cf593625e6278c331e6b7093a32 (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
78
79
80
81
82
83
84
85
86
87
import sbt._
import sbt.Keys._
import complete.DefaultParsers._

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

  object autoImport {

    /**
      * Parameters for a run of codegen on a single database
      * configuration. This is useful when you want to run the same code
      * generator on multiple databases:
      *
      * @param databaseUri uri for the database configuration
      * @param outputPackage package in which to place generated code
      * @param outputPath directory to which the code will be written
      * @param codegenSchemaWhitelist schemas and tables to process
      * @param foreignKeys foreign key references to data models add manually
      */
    case class CodegenDatabase(
        databaseURI: String,
        outputPackage: String,
        outputPath: String,
        schemaWhitelist: List[String] = List.empty,
        foreignKeys: Map[TableColumn, TableColumn] = Map.empty
    )

    lazy val codegenDatabaseConfigs = SettingKey[List[CodegenDatabase]](
      "codegen-database-configs",
      "configurations for each database and its generated code")

    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(
    codegenSchemaBaseClassParts := List.empty,
    codegenIdType := Option.empty,
    codegenSchemaImports := List.empty,
    codegenTypeReplacements := Map.empty,
    slickCodeGenTask := Def.taskDyn {
      Def.task {
        codegenDatabaseConfigs.value.foreach {
          config =>
            Generator.run(
              new java.net.URI(config.databaseURI),
              config.outputPackage,
              Some(config.schemaWhitelist).filter(_.nonEmpty),
              config.outputPath,
              config.foreignKeys,
              (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
  )

}