Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
toshi
committed
Apr 8, 2015
0 parents
commit 79cc3a1
Showing
6 changed files
with
228 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/RUNNING_PID | ||
/logs/ | ||
/project/*-shim.sbt | ||
/project/project/ | ||
/project/target/ | ||
/target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# sbt-slick-codegen | ||
|
||
slick-codegen hook for sbt | ||
|
||
## Install | ||
|
||
```scala | ||
// plugins.sbt | ||
|
||
addSbtPlugin("com.github.tototoshi" % "sbt-slick-codegen" % "0.1.0-SNAPSHOT | ||
``` | ||
## Configuration | ||
```scala | ||
import com.github.tototoshi.sbt.slick.CodegenPlugin._ | ||
// required | ||
// Register codegen hook | ||
sourceGenerators in Compile <+= slickCodegen | ||
// required | ||
slickCodegenDatabaseUrl in Compile := "jdbc:postgresql://localhost/example" | ||
|
||
// required | ||
slickCodegenDatabaseUser in Compile := "dbuser" | ||
|
||
// required | ||
slickCodegenDatabasePassword in Compile := "dbpassword" | ||
|
||
// required (If not set, postgresql driver is choosen) | ||
slickCodegenDriver := scala.slick.driver.PostgresDriver | ||
|
||
// required (If not set, postgresql driver is choosen) | ||
slickCodegenJdbcDriver := "org.postgresql.Driver" | ||
|
||
// optional but maybe you want | ||
slickCodegenOutputPackage in Compile := "com.example.models" | ||
|
||
// optional, pass your own custom source code generator | ||
slickCodegenCodeGenerator in Compile := { (model: m.Model) => new SourceCodeGenerator(model) } | ||
|
||
// optional | ||
slickCodegenExcludedTables := Seq("play_evolutions") | ||
``` | ||
|
||
## License | ||
|
||
Apache 2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
scalariformSettings | ||
|
||
sbtPlugin := true | ||
|
||
name := """sbt-slick-codegen""" | ||
|
||
organization := "com.github.tototoshi" | ||
|
||
version := "0.1.0-SNAPSHOT" | ||
|
||
scalaVersion := "2.10.5" | ||
|
||
libraryDependencies ++= Seq( | ||
"com.typesafe.slick" %% "slick" % "2.1.0", | ||
"com.typesafe.slick" %% "slick-codegen" % "2.1.0" | ||
) | ||
|
||
publishMavenStyle := true | ||
|
||
publishTo <<= version { (v: String) => _publishTo(v) } | ||
|
||
def _publishTo(v: String) = { | ||
val nexus = "https://oss.sonatype.org/" | ||
if (v.trim.endsWith("SNAPSHOT")) Some("snapshots" at nexus + "content/repositories/snapshots") | ||
else Some("releases" at nexus + "service/local/staging/deploy/maven2") | ||
} | ||
|
||
publishArtifact in Test := false | ||
|
||
pomExtra := | ||
<url>http://github.com/tototoshi/sbt-slick-codegen</url> | ||
<licenses> | ||
<license> | ||
<name>Apache License, Version 2.0</name> | ||
<url>http://www.apache.org/licenses/LICENSE-2.0.html</url> | ||
<distribution>repo</distribution> | ||
</license> | ||
</licenses> | ||
<scm> | ||
<url>git@github.com:tototoshi/sbt-slick-codegen</url> | ||
<connection>scm:git:git@github.com:tototoshi/sbt-slick-codegen.git</connection> | ||
</scm> | ||
<developers> | ||
<developer> | ||
<id>tototoshi</id> | ||
<name>Toshiyuki Takahashi</name> | ||
<url>http://tototoshi.github.io</url> | ||
</developer> | ||
</developers> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sbt.version=0.13.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.3.0") | ||
|
||
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "0.2.2") | ||
|
||
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.0") |
118 changes: 118 additions & 0 deletions
118
src/main/scala/com/github/tototoshi/sbt/slick/SlickCodegenPlugin.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package com.github.tototoshi.sbt.slick | ||
|
||
import sbt._ | ||
import Keys._ | ||
import scala.slick.codegen.SourceCodeGenerator | ||
import scala.slick.driver.JdbcProfile | ||
import scala.slick.{ model => m } | ||
|
||
object CodegenPlugin extends sbt.Plugin { | ||
|
||
case class SlickCodegenDatabase(url: String, user: String, password: String) | ||
|
||
lazy val slickCodegen: TaskKey[Seq[File]] = TaskKey[Seq[File]]("Command to run codegen") | ||
|
||
lazy val slickCodegenDatabase: SettingKey[SlickCodegenDatabase] = | ||
SettingKey[SlickCodegenDatabase]("Database used by codegen") | ||
|
||
lazy val slickCodegenDatabaseUrl: SettingKey[String] = | ||
SettingKey[String]("URL of database used by codegen") | ||
|
||
lazy val slickCodegenDatabaseUser: SettingKey[String] = | ||
SettingKey[String]("User of database used by codegen") | ||
|
||
lazy val slickCodegenDatabasePassword: SettingKey[String] = | ||
SettingKey[String]("Password of database used by codegen") | ||
|
||
lazy val slickCodegenDriver: SettingKey[JdbcProfile] = | ||
SettingKey[JdbcProfile]("Slick driver used by codegen") | ||
|
||
lazy val slickCodegenJdbcDriver: SettingKey[String] = | ||
SettingKey[String]("Jdbc driver used by codegen") | ||
|
||
lazy val slickCodegenOutputPackage: SettingKey[String] = | ||
SettingKey[String]("Package of generated code") | ||
|
||
lazy val slickCodegenOutputFile: SettingKey[String] = | ||
SettingKey[String]("Generated file") | ||
|
||
lazy val slickCodegenOutputContainer: SettingKey[String] = | ||
SettingKey[String]("Container of generated source code") | ||
|
||
lazy val slickCodegenCodeGenerator: SettingKey[m.Model => SourceCodeGenerator] = | ||
SettingKey[m.Model => SourceCodeGenerator]("Function to create CodeGenerator to be used") | ||
|
||
lazy val slickCodegenExcludedTables: SettingKey[Seq[String]] = | ||
SettingKey[Seq[String]]("Tables that should be excluded") | ||
|
||
lazy val defaultSourceCodeGenerator: m.Model => SourceCodeGenerator = (model: m.Model) => | ||
new SourceCodeGenerator(model) | ||
|
||
private def gen( | ||
generator: m.Model => SourceCodeGenerator, | ||
driver: JdbcProfile, | ||
jdbcDriver: String, | ||
url: String, | ||
user: String, | ||
password: String, | ||
outputDir: String, | ||
pkg: String, | ||
fileName: String, | ||
container: String, | ||
excluded: Seq[String], | ||
s: TaskStreams): Unit = { | ||
|
||
s.log.info(s"Generate source code with slick-codegen: url=${url}, user=${user}") | ||
|
||
val database = driver.simple.Database.forURL(url = url, driver = jdbcDriver, user = user, password = password) | ||
val model = database.withSession { | ||
implicit session => | ||
val tables = driver.defaultTables.filterNot(t => excluded contains t.name.name) | ||
driver.createModel(Some(tables)) | ||
} | ||
|
||
generator(model).writeToFile( | ||
profile = "scala.slick.driver." + driver.toString, | ||
folder = outputDir, | ||
pkg = pkg, | ||
container = container, | ||
fileName = fileName | ||
) | ||
|
||
s.log.info(s"Source code has generated in ${outputDir}/${fileName}") | ||
} | ||
|
||
override def settings: Seq[Setting[_]] = Seq( | ||
slickCodegenDriver := scala.slick.driver.PostgresDriver, | ||
slickCodegenJdbcDriver := "org.postgresql.Driver", | ||
slickCodegenDatabaseUrl := "Database url is not set", | ||
slickCodegenDatabaseUser := "Database user is not set", | ||
slickCodegenDatabasePassword := "Database password is not set", | ||
slickCodegenOutputPackage := "com.example", | ||
slickCodegenOutputFile := "Tables.scala", | ||
slickCodegenOutputContainer := "Tables", | ||
slickCodegenExcludedTables := Seq(), | ||
slickCodegenCodeGenerator := defaultSourceCodeGenerator, | ||
slickCodegen := { | ||
val outDir = (sourceManaged in Compile).value.getPath | ||
val outPkg = (slickCodegenOutputPackage in Compile).value | ||
val outFile = (slickCodegenOutputFile in Compile).value | ||
gen( | ||
(slickCodegenCodeGenerator in Compile).value, | ||
(slickCodegenDriver in Compile).value, | ||
(slickCodegenJdbcDriver in Compile).value, | ||
(slickCodegenDatabaseUrl in Compile).value, | ||
(slickCodegenDatabaseUser in Compile).value, | ||
(slickCodegenDatabasePassword in Compile).value, | ||
outDir, | ||
outPkg, | ||
outFile, | ||
(slickCodegenOutputContainer in Compile).value, | ||
(slickCodegenExcludedTables in Compile).value, | ||
streams.value | ||
) | ||
Seq(file(outDir + "/" + outPkg.replaceAllLiterally(".", "/") + "/" + outFile)) | ||
} | ||
) | ||
|
||
} |