From c158c2d2462ed635daca8e84792e49a0e8e1dd32 Mon Sep 17 00:00:00 2001 From: Slava Schmidt Date: Thu, 31 Mar 2016 07:21:06 +0200 Subject: [PATCH] #112, feat: Add debugging command for state machine --- compiler/src/main/scala/de/zalando/apifirst/ast.scala | 7 +++---- .../scala/de/zalando/swagger/HypermediaConverterTest.scala | 2 +- .../scala/de/zalando/play/swagger/sbt/PlaySwagger.scala | 5 ++++- .../de/zalando/play/swagger/sbt/SwaggerPrettyPrinter.scala | 6 ++++++ 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/compiler/src/main/scala/de/zalando/apifirst/ast.scala b/compiler/src/main/scala/de/zalando/apifirst/ast.scala index 65191f1..6734056 100644 --- a/compiler/src/main/scala/de/zalando/apifirst/ast.scala +++ b/compiler/src/main/scala/de/zalando/apifirst/ast.scala @@ -50,7 +50,7 @@ object Hypermedia { object State { def apply(name: String): State = if (Self.name.equalsIgnoreCase(name)) Self else NamedState(name) - def toDot(table: StateTransitionsTable): String = { + def toDot(table: StateTransitionsTable): Seq[String] = { val transitions = for { (from, destinations) <- table (to, condition) <- destinations @@ -69,9 +69,8 @@ object Hypermedia { } map { name => s""" "$name" [color="green",style="bold"] """ } - s"digraph {\n${transitions.mkString("\n")}\n" + - s"${acceptingNodes.mkString("\n")}\n" + - s"${startingNodes.mkString("\n")}\n}" + val lines = transitions ++ acceptingNodes ++ startingNodes + "digraph {" +: lines.toSeq :+ "}" } } diff --git a/compiler/src/test/scala/de/zalando/swagger/HypermediaConverterTest.scala b/compiler/src/test/scala/de/zalando/swagger/HypermediaConverterTest.scala index cb4d902..8910aa8 100644 --- a/compiler/src/test/scala/de/zalando/swagger/HypermediaConverterTest.scala +++ b/compiler/src/test/scala/de/zalando/swagger/HypermediaConverterTest.scala @@ -27,7 +27,7 @@ class HypermediaConverterTest extends FunSpec with MustMatchers with ExpectedRes val ast = ModelConverter.fromModel(base, model, Some(file)) val hypermedia = ast.stateTransitions val expected = asInFile(file, "hypermedia") - val media = State.toDot(hypermedia) + val media = State.toDot(hypermedia).mkString("\n") if (expected.isEmpty && media.nonEmpty) dump(media, file, "hypermedia") clean(media) mustBe clean(expected) diff --git a/plugin/src/main/scala/de/zalando/play/swagger/sbt/PlaySwagger.scala b/plugin/src/main/scala/de/zalando/play/swagger/sbt/PlaySwagger.scala index e1c6c2a..9cf16bd 100644 --- a/plugin/src/main/scala/de/zalando/play/swagger/sbt/PlaySwagger.scala +++ b/plugin/src/main/scala/de/zalando/play/swagger/sbt/PlaySwagger.scala @@ -62,6 +62,8 @@ object PlaySwagger extends AutoPlugin { lazy val swaggerPrintFlatAstTypes = taskKey[Seq[Unit]]("Prints AST type information before after optimisation") lazy val swaggerPrintFlatAstParameters = taskKey[Seq[Unit]]("Prints AST parameter information after type optimisation") + lazy val swaggerStateDiagram = taskKey[Seq[Unit]]("Prints hypermedia state transitions") + } // Users have to explicitly enable it @@ -141,8 +143,9 @@ object PlaySwagger extends AutoPlugin { swaggerPrintRawAstParameters <<= (swaggerRawData, streams) map prettyPrint(SwaggerPrettyPrinter.parameters), swaggerPrintFlatAstTypes <<= (swaggerPreparedData, streams) map prettyPrint(SwaggerPrettyPrinter.types), - swaggerPrintFlatAstParameters <<= (swaggerPreparedData, streams) map prettyPrint(SwaggerPrettyPrinter.parameters) + swaggerPrintFlatAstParameters <<= (swaggerPreparedData, streams) map prettyPrint(SwaggerPrettyPrinter.parameters), + swaggerStateDiagram <<= (swaggerPreparedData, streams) map prettyPrint(SwaggerPrettyPrinter.states) ) def prettyPrint(printer: (SwaggerCompilationTask, StrictModel) => Seq[String]): diff --git a/plugin/src/main/scala/de/zalando/play/swagger/sbt/SwaggerPrettyPrinter.scala b/plugin/src/main/scala/de/zalando/play/swagger/sbt/SwaggerPrettyPrinter.scala index 63998bf..5722974 100644 --- a/plugin/src/main/scala/de/zalando/play/swagger/sbt/SwaggerPrettyPrinter.scala +++ b/plugin/src/main/scala/de/zalando/play/swagger/sbt/SwaggerPrettyPrinter.scala @@ -1,6 +1,7 @@ package de.zalando.play.swagger.sbt import de.zalando.apifirst.Application.StrictModel +import de.zalando.apifirst.Hypermedia import de.zalando.play.compiler.SwaggerCompilationTask /** @@ -20,6 +21,11 @@ object SwaggerPrettyPrinter { withFileName("Parameters:\t", task, lines) } + def states(task: SwaggerCompilationTask, ast: StrictModel): Seq[String] = { + val lines = Hypermedia.State.toDot(ast.stateTransitions) + withFileName("State Diagram:\t", task, lines) + } + def withFileName(prefix: String, task: SwaggerCompilationTask, lines: Seq[String]): Seq[String] = s"\n$prefix${formatText(task.definitionFile.getName)(blue,black)}\n" +: "\n" +: lines :+ "\n" :+ "\n"