Selaa lähdekoodia

Minor refactoring.

Thomas Flucke 5 vuotta sitten
vanhempi
commit
425d6d11f3
2 muutettua tiedostoa jossa 1 lisäystä ja 73 poistoa
  1. 1 1
      build.sbt
  2. 0 72
      src/name/tflucke/webroutes/WebRoutesPlugin.scala

+ 1 - 1
build.sbt

@@ -1,5 +1,5 @@
 lazy val root = (project in file("."))
   .enablePlugins(SbtPlugin)
   .settings(
-    name := "WebRoutes"
+    name := "web-routes"
   )

+ 0 - 72
src/name/tflucke/webroutes/WebRoutesPlugin.scala

@@ -1,72 +0,0 @@
-package name.tflucke.webroutes
-
-import sbt._
-import Keys._
-
-object WebRoutesPlugin extends AutoPlugin {
-  val routePattern = ("\\s*#\\s*Shared\\s+Route\\s*\n" +
-    "((\\s*#\\s*\\w+\\s*:\\s*[^\\s]+\\s*\n)*)" +
-    "\\s*(GET|PUT|POST|DELETE)\\s+(/[^\\s]*)+\\s+@?([\\.\\w]+.controllers).([\\w]+).([\\w]+)\\s*(\\((\\w+\\s*:\\s*\\w+\\s*)?(,\\s*\\w+\\s*:\\s*\\w+)?\\))?").r
-
-  object autoImport {
-    val generateJsRoutes = taskKey[Seq[File]]("Generate scala client routes objects")
-    val scalaJsManagedDir = settingKey[File]("Output directory to put generated files")
-    lazy val baseWebRouteSettings: Seq[Def.Setting[_]] = Seq(
-      scalaJsManagedDir := Compile / resourceManaged,
-      generateJsRoutes := {
-        println("Generating Scala Web route objects...")
-        generateJsRoutes.inputFileChanges.modified.flatMap((routesFile: java.nio.file.Path) => {
-          import scala.io.Source
-
-          val routeGroups = (for (route <- routePattern.findAllMatchIn(Source.fromFile(routesFile.toFile).mkString)) yield {
-            val propPattern = "\\s*#\\s*(\\w+)\\s*:\\s*([^\\s]+)\\s*\n".r
-            val props = propPattern.findAllMatchIn(route.group(1)).map(m => (m.group(1), m.group(2)))
-            def getProp(name: String): Option[String] = props.find(_._1.equalsIgnoreCase(name)).map(_._2)
-            val method = route.group(3)
-            val path = route.group(4)
-            val pack = route.group(5)
-            val obj = route.group(6)
-            val function = route.group(7)
-            val args = (Option(route.group(9)).getOrElse("") + Option(route.group(10)).getOrElse("")).split(",")
-            RouteDef(method, path, pack, obj, function, args,
-              getProp("type") getOrElse "Any",
-              "text")//getProp("mime") getOrElse "json"
-          }).toList.groupBy[(String, String)]((route) => (route.pack, route.controller))
-          val outFiles = routeGroups.map(tuple => {
-            val ((pack, controller), routes) = tuple
-            val content = s"""package ${pack}
-object ${controller} {
-${routes.mkString("\n")}
-}
-"""
-            val outFile = makeControllerFile(pack, controller, scalaJsManagedDir.value)
-            IO.write(outFile, content)
-            outFile
-          }).toSeq
-          outFiles
-        })
-      }
-    )
-  }
-  import autoImport._
-
-  override val projectSettings =
-    inConfig(Compile)(baseWebRouteSettings) ++
-    inConfig(Test)(baseWebRouteSettings)
-
-  def makeControllerFile(path: String, controller: String, baseDir: File): File = {
-    val pattern = "\\.?([^.]+)(.*)".r
-    path match {
-      case pattern(head, rest) => {
-        val nextDir = new File(baseDir, head)
-        if (!nextDir.exists) {
-          nextDir.mkdir()
-        }
-        makeControllerFile(rest, controller, nextDir)
-      }
-      case _ => {
-        new File(baseDir, s"${controller}.scala")
-      }
-    }
-  }
-}