From 78d7c6c4ec9284de27e0b5ce1b6af1f213d2c51d Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Sat, 14 Oct 2023 20:05:51 +0100 Subject: [PATCH] Fixed #606 & #604: Mill codegen unique to module --- .../src/indigoplugin/IndigoGenerators.scala | 104 ++++++------------ .../generators/AssetListing.scala | 3 +- .../indigoplugin/generators/ConfigGen.scala | 3 +- .../generators/EmbedAseprite.scala | 3 +- .../indigoplugin/generators/EmbedData.scala | 3 +- .../generators/EmbedGLSLShaderPair.scala | 3 +- .../indigoplugin/generators/EmbedText.scala | 3 +- .../generators/GeneratorAcceptanceTests.scala | 29 ++--- .../src/millindigo/MillIndigo.scala | 2 +- 9 files changed, 53 insertions(+), 100 deletions(-) diff --git a/indigo-plugin/indigo-plugin/src/indigoplugin/IndigoGenerators.scala b/indigo-plugin/indigo-plugin/src/indigoplugin/IndigoGenerators.scala index 5eee28e3b..6080a61a0 100644 --- a/indigo-plugin/indigo-plugin/src/indigoplugin/IndigoGenerators.scala +++ b/indigo-plugin/indigo-plugin/src/indigoplugin/IndigoGenerators.scala @@ -17,17 +17,12 @@ import indigoplugin.generators.EmbedAseprite * @param sources * Accumulated source paths */ -final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageName: String, sources: Seq[os.Path]) { +final case class IndigoGenerators(fullyQualifiedPackageName: String, sources: Seq[os.Path => Seq[os.Path]]) { - def toSources: Seq[os.Path] = sources - def toSourceFiles: Seq[File] = sources.map(_.toIO) - - def withOutputDirectory(value: os.Path): IndigoGenerators = - this.copy(outDirectory = value) - def withOutputDirectory(value: File): IndigoGenerators = - this.copy(outDirectory = os.Path(value)) - def withOutputDirectory(value: String): IndigoGenerators = - this.copy(outDirectory = os.Path(value)) + def toSourcePaths(destination: os.Path): Seq[os.Path] = sources.flatMap(_(destination)) + def toSourcePaths(destination: File): Seq[os.Path] = sources.flatMap(_(os.Path(destination))) + def toSourceFiles(destination: os.Path): Seq[File] = sources.flatMap(_(destination)).map(_.toIO) + def toSourceFiles(destination: File): Seq[File] = sources.flatMap(_(os.Path(destination))).map(_.toIO) /** Set a fully qualified package names for your output sources, e.g. com.mycompany.generated.code */ def withPackage(packageName: String): IndigoGenerators = @@ -42,8 +37,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa */ def embedText(moduleName: String, file: os.Path): IndigoGenerators = this.copy( - sources = sources ++ - EmbedText.generate(outDirectory, moduleName, fullyQualifiedPackageName, file) + sources = sources :+ + EmbedText.generate(moduleName, fullyQualifiedPackageName, file) ) /** Embed raw text into a static variable. @@ -55,8 +50,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa */ def embedText(moduleName: String, file: File): IndigoGenerators = this.copy( - sources = sources ++ - EmbedText.generate(outDirectory, moduleName, fullyQualifiedPackageName, os.Path(file)) + sources = sources :+ + EmbedText.generate(moduleName, fullyQualifiedPackageName, os.Path(file)) ) /** Embed raw text into a static variable. @@ -68,8 +63,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa */ def embedText(moduleName: String, file: String): IndigoGenerators = this.copy( - sources = sources ++ - EmbedText.generate(outDirectory, moduleName, fullyQualifiedPackageName, os.RelPath(file).resolveFrom(os.pwd)) + sources = sources :+ + EmbedText.generate(moduleName, fullyQualifiedPackageName, os.RelPath(file).resolveFrom(os.pwd)) ) /** Embed a GLSL shader pair into a Scala module. @@ -100,9 +95,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa validate: Boolean ): IndigoGenerators = this.copy( - sources = sources ++ + sources = sources :+ EmbedGLSLShaderPair.generate( - outDirectory, moduleName, fullyQualifiedPackageName, vertexShaderPath, @@ -139,9 +133,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa validate: Boolean ): IndigoGenerators = this.copy( - sources = sources ++ + sources = sources :+ EmbedGLSLShaderPair.generate( - outDirectory, moduleName, fullyQualifiedPackageName, os.Path(vertexShaderPath), @@ -178,9 +171,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa validate: Boolean ): IndigoGenerators = this.copy( - sources = sources ++ + sources = sources :+ EmbedGLSLShaderPair.generate( - outDirectory, moduleName, fullyQualifiedPackageName, os.RelPath(vertexShaderPath).resolveFrom(os.pwd), @@ -202,9 +194,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa indigoAssets: IndigoAssets ): IndigoGenerators = this.copy( - sources = sources ++ + sources = sources :+ AssetListing.generate( - outDirectory, moduleName, fullyQualifiedPackageName, indigoAssets @@ -223,9 +214,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa indigoOptions: IndigoOptions ): IndigoGenerators = this.copy( - sources = sources ++ + sources = sources :+ ConfigGen.generate( - outDirectory, moduleName, fullyQualifiedPackageName, indigoOptions @@ -293,9 +283,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa /** Embed the data as a Scala 3 Enum. */ def asEnum(moduleName: String, file: os.Path): IndigoGenerators = gens.copy( - sources = sources ++ + sources = sources :+ EmbedData.generate( - outDirectory, moduleName, fullyQualifiedPackageName, file, @@ -308,9 +297,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa /** Embed the data as a Scala 3 Enum that extends some fully qualified module name. E.g. `com.example.MyData`. */ def asEnum(moduleName: String, file: os.Path, extendsFrom: String): IndigoGenerators = gens.copy( - sources = sources ++ + sources = sources :+ EmbedData.generate( - outDirectory, moduleName, fullyQualifiedPackageName, file, @@ -323,9 +311,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa /** Embed the data as a Scala 3 Enum. */ def asEnum(moduleName: String, file: File): IndigoGenerators = gens.copy( - sources = sources ++ + sources = sources :+ EmbedData.generate( - outDirectory, moduleName, fullyQualifiedPackageName, os.Path(file), @@ -338,9 +325,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa /** Embed the data as a Scala 3 Enum that extends some fully qualified module name. E.g. `com.example.MyData`. */ def asEnum(moduleName: String, file: File, extendsFrom: String): IndigoGenerators = gens.copy( - sources = sources ++ + sources = sources :+ EmbedData.generate( - outDirectory, moduleName, fullyQualifiedPackageName, os.Path(file), @@ -353,9 +339,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa /** Embed the data as a Scala 3 Enum. */ def asEnum(moduleName: String, file: String): IndigoGenerators = gens.copy( - sources = sources ++ + sources = sources :+ EmbedData.generate( - outDirectory, moduleName, fullyQualifiedPackageName, os.RelPath(file).resolveFrom(os.pwd), @@ -368,9 +353,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa /** Embed the data as a Scala 3 Enum that extends some fully qualified module name. E.g. `com.example.MyData`. */ def asEnum(moduleName: String, file: String, extendsFrom: String): IndigoGenerators = gens.copy( - sources = sources ++ + sources = sources :+ EmbedData.generate( - outDirectory, moduleName, fullyQualifiedPackageName, os.RelPath(file).resolveFrom(os.pwd), @@ -383,9 +367,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa /** Embed the data as a Map. */ def asMap(moduleName: String, file: os.Path): IndigoGenerators = gens.copy( - sources = sources ++ + sources = sources :+ EmbedData.generate( - outDirectory, moduleName, fullyQualifiedPackageName, file, @@ -398,9 +381,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa /** Embed the data as a Map. */ def asMap(moduleName: String, file: File): IndigoGenerators = gens.copy( - sources = sources ++ + sources = sources :+ EmbedData.generate( - outDirectory, moduleName, fullyQualifiedPackageName, os.Path(file), @@ -413,9 +395,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa /** Embed the data as a Map. */ def asMap(moduleName: String, file: String): IndigoGenerators = gens.copy( - sources = sources ++ + sources = sources :+ EmbedData.generate( - outDirectory, moduleName, fullyQualifiedPackageName, os.RelPath(file).resolveFrom(os.pwd), @@ -445,9 +426,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa */ def asCustom(moduleName: String, file: os.Path)(present: List[List[DataType]] => String): IndigoGenerators = gens.copy( - sources = sources ++ + sources = sources :+ EmbedData.generate( - outDirectory, moduleName, fullyQualifiedPackageName, file, @@ -477,9 +457,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa */ def asCustom(moduleName: String, file: File)(present: List[List[DataType]] => String): IndigoGenerators = gens.copy( - sources = sources ++ + sources = sources :+ EmbedData.generate( - outDirectory, moduleName, fullyQualifiedPackageName, os.Path(file), @@ -509,9 +488,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa */ def asCustom(moduleName: String, file: String)(present: List[List[DataType]] => String): IndigoGenerators = gens.copy( - sources = sources ++ + sources = sources :+ EmbedData.generate( - outDirectory, moduleName, fullyQualifiedPackageName, os.RelPath(file).resolveFrom(os.pwd), @@ -533,8 +511,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa */ def embedAseprite(moduleName: String, file: os.Path): IndigoGenerators = this.copy( - sources = sources ++ - EmbedAseprite.generate(outDirectory, moduleName, fullyQualifiedPackageName, file) + sources = sources :+ + EmbedAseprite.generate(moduleName, fullyQualifiedPackageName, file) ) /** Embed Aseprite data in a module. @@ -548,8 +526,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa */ def embedAseprite(moduleName: String, file: File): IndigoGenerators = this.copy( - sources = sources ++ - EmbedAseprite.generate(outDirectory, moduleName, fullyQualifiedPackageName, os.Path(file)) + sources = sources :+ + EmbedAseprite.generate(moduleName, fullyQualifiedPackageName, os.Path(file)) ) /** Embed Aseprite data in a module. @@ -563,9 +541,8 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa */ def embedAseprite(moduleName: String, file: String): IndigoGenerators = this.copy( - sources = sources ++ + sources = sources :+ EmbedAseprite.generate( - outDirectory, moduleName, fullyQualifiedPackageName, os.RelPath(file).resolveFrom(os.pwd) @@ -577,18 +554,9 @@ final case class IndigoGenerators(outDirectory: os.Path, fullyQualifiedPackageNa object IndigoGenerators { val None: IndigoGenerators = - IndigoGenerators(os.Path("/tmp/indigo-build-null"), "", Seq()) - - def default(outputDirectory: os.Path, fullyQualifiedPackageName: String): IndigoGenerators = - IndigoGenerators(outputDirectory, fullyQualifiedPackageName, Seq()) - - def mill(fullyQualifiedPackageName: String): IndigoGenerators = - default(os.pwd / "out", fullyQualifiedPackageName) - - def mill(outDirectory: os.Path, fullyQualifiedPackageName: String): IndigoGenerators = - default(outDirectory, fullyQualifiedPackageName) + IndigoGenerators("", Seq()) - def sbt(sourceManagedDirectory: File, fullyQualifiedPackageName: String): IndigoGenerators = - default(os.Path(sourceManagedDirectory), fullyQualifiedPackageName) + def apply(fullyQualifiedPackageName: String): IndigoGenerators = + IndigoGenerators(fullyQualifiedPackageName, Seq()) } diff --git a/indigo-plugin/indigo-plugin/src/indigoplugin/generators/AssetListing.scala b/indigo-plugin/indigo-plugin/src/indigoplugin/generators/AssetListing.scala index 11cc7531e..0fec7adbc 100644 --- a/indigo-plugin/indigo-plugin/src/indigoplugin/generators/AssetListing.scala +++ b/indigo-plugin/indigo-plugin/src/indigoplugin/generators/AssetListing.scala @@ -7,11 +7,10 @@ import scala.io.AnsiColor._ object AssetListing { def generate( - outDir: os.Path, moduleName: String, fullyQualifiedPackage: String, indigoAssets: IndigoAssets - ): Seq[os.Path] = { + ): os.Path => Seq[os.Path] = outDir => { val toSafeName: (String, String) => String = indigoAssets.rename.getOrElse(toDefaultSafeName) diff --git a/indigo-plugin/indigo-plugin/src/indigoplugin/generators/ConfigGen.scala b/indigo-plugin/indigo-plugin/src/indigoplugin/generators/ConfigGen.scala index 8b45947f9..28a2da2c7 100644 --- a/indigo-plugin/indigo-plugin/src/indigoplugin/generators/ConfigGen.scala +++ b/indigo-plugin/indigo-plugin/src/indigoplugin/generators/ConfigGen.scala @@ -6,11 +6,10 @@ import scala.util.matching.Regex object ConfigGen { def generate( - outDir: os.Path, moduleName: String, fullyQualifiedPackage: String, indigoOptions: IndigoOptions - ): Seq[os.Path] = { + ): os.Path => Seq[os.Path] = outDir => { val wd = outDir / Generators.OutputDirName diff --git a/indigo-plugin/indigo-plugin/src/indigoplugin/generators/EmbedAseprite.scala b/indigo-plugin/indigo-plugin/src/indigoplugin/generators/EmbedAseprite.scala index 2b73f11b0..a3e738591 100644 --- a/indigo-plugin/indigo-plugin/src/indigoplugin/generators/EmbedAseprite.scala +++ b/indigo-plugin/indigo-plugin/src/indigoplugin/generators/EmbedAseprite.scala @@ -6,11 +6,10 @@ import indigoplugin.datatypes.Aseprite object EmbedAseprite { def generate( - outDir: os.Path, moduleName: String, fullyQualifiedPackage: String, filePath: os.Path - ): Seq[os.Path] = { + ): os.Path => Seq[os.Path] = outDir => { val asepriteJson = if (!os.exists(filePath)) diff --git a/indigo-plugin/indigo-plugin/src/indigoplugin/generators/EmbedData.scala b/indigo-plugin/indigo-plugin/src/indigoplugin/generators/EmbedData.scala index 7a6a1b97c..452ddc6c3 100644 --- a/indigo-plugin/indigo-plugin/src/indigoplugin/generators/EmbedData.scala +++ b/indigo-plugin/indigo-plugin/src/indigoplugin/generators/EmbedData.scala @@ -19,14 +19,13 @@ object EmbedData { // string is kept. // Cells cannot be empty. def generate( - outDir: os.Path, moduleName: String, fullyQualifiedPackage: String, filePath: os.Path, delimiter: String, rowFilter: String => Boolean, embedMode: Mode - ): Seq[os.Path] = { + ): os.Path => Seq[os.Path] = outDir => { val lines = if (!os.exists(filePath)) throw new Exception("Path to data file not found: " + filePath.toString()) diff --git a/indigo-plugin/indigo-plugin/src/indigoplugin/generators/EmbedGLSLShaderPair.scala b/indigo-plugin/indigo-plugin/src/indigoplugin/generators/EmbedGLSLShaderPair.scala index 42424e3b1..f551dba7a 100644 --- a/indigo-plugin/indigo-plugin/src/indigoplugin/generators/EmbedGLSLShaderPair.scala +++ b/indigo-plugin/indigo-plugin/src/indigoplugin/generators/EmbedGLSLShaderPair.scala @@ -3,13 +3,12 @@ package indigoplugin.generators object EmbedGLSLShaderPair { def generate( - outDir: os.Path, moduleName: String, fullyQualifiedPath: String, vertex: os.Path, fragment: os.Path, runValidator: Boolean - ): Seq[os.Path] = { + ): os.Path => Seq[os.Path] = outDir => { val shaderFiles: Seq[os.Path] = Seq(vertex, fragment) diff --git a/indigo-plugin/indigo-plugin/src/indigoplugin/generators/EmbedText.scala b/indigo-plugin/indigo-plugin/src/indigoplugin/generators/EmbedText.scala index 5a1c6c39e..bd70c798d 100644 --- a/indigo-plugin/indigo-plugin/src/indigoplugin/generators/EmbedText.scala +++ b/indigo-plugin/indigo-plugin/src/indigoplugin/generators/EmbedText.scala @@ -3,11 +3,10 @@ package indigoplugin.generators object EmbedText { def generate( - outDir: os.Path, moduleName: String, fullyQualifiedPackage: String, filePath: os.Path - ): Seq[os.Path] = { + ): os.Path => Seq[os.Path] = outDir => { val text = if (!os.exists(filePath)) throw new Exception("Text file to embed not found: " + filePath.toString()) diff --git a/indigo-plugin/indigo-plugin/test/src/indigoplugin/generators/GeneratorAcceptanceTests.scala b/indigo-plugin/indigo-plugin/test/src/indigoplugin/generators/GeneratorAcceptanceTests.scala index 7153d264c..b68fdfbd4 100644 --- a/indigo-plugin/indigo-plugin/test/src/indigoplugin/generators/GeneratorAcceptanceTests.scala +++ b/indigo-plugin/indigo-plugin/test/src/indigoplugin/generators/GeneratorAcceptanceTests.scala @@ -20,11 +20,9 @@ class GeneratorAcceptanceTests extends munit.FunSuite { test("Can generate an enum from a CSV file") { val files = - IndigoGenerators - .default(targetDir, "com.example.test") - .embedCSV + IndigoGenerators("com.example.test").embedCSV .asEnum("StatsEnum", sourceCSV) - .toSources + .toSourcePaths(targetDir) files.headOption match { case None => @@ -54,11 +52,9 @@ class GeneratorAcceptanceTests extends munit.FunSuite { test("Can generate a map from a markdown table file") { val files = - IndigoGenerators - .default(targetDir, "com.example.test") - .embedMarkdownTable + IndigoGenerators("com.example.test").embedMarkdownTable .asMap("StatsMap", sourceMD) - .toSources + .toSourcePaths(targetDir) files.headOption match { case None => @@ -92,11 +88,9 @@ class GeneratorAcceptanceTests extends munit.FunSuite { test("Can generate a map from a markdown table file (armour, unformatted)") { val files = - IndigoGenerators - .default(targetDir, "com.example.test") - .embedMarkdownTable + IndigoGenerators("com.example.test").embedMarkdownTable .asEnum("Armour", os.pwd / "test-assets" / "data" / "armour.md") - .toSources + .toSourcePaths(targetDir) files.headOption match { case None => @@ -125,15 +119,13 @@ class GeneratorAcceptanceTests extends munit.FunSuite { test("Can generate a custom output from a markdown table file") { val files = - IndigoGenerators - .default(targetDir, "com.example.test") - .embedMarkdownTable + IndigoGenerators("com.example.test").embedMarkdownTable .asCustom("StatsMap", sourceMD) { data => s"""/* |${data.map(_.map(cell => s"${cell.asString}: ${cell.giveTypeName}").mkString(",")).mkString("\n")} |*/""".stripMargin.trim } - .toSources + .toSourcePaths(targetDir) files.headOption match { case None => @@ -166,10 +158,9 @@ class GeneratorAcceptanceTests extends munit.FunSuite { val jsonFile = os.pwd / "test-assets" / "captain" / "Captain Clown Nose Data.json" val files = - IndigoGenerators - .default(targetDir, "com.example.test") + IndigoGenerators("com.example.test") .embedAseprite("MyAnimation", jsonFile) - .toSources + .toSourcePaths(targetDir) files.headOption match { case None => diff --git a/mill-indigo/mill-indigo/src/millindigo/MillIndigo.scala b/mill-indigo/mill-indigo/src/millindigo/MillIndigo.scala index c95639c55..ec3ffcf50 100644 --- a/mill-indigo/mill-indigo/src/millindigo/MillIndigo.scala +++ b/mill-indigo/mill-indigo/src/millindigo/MillIndigo.scala @@ -20,7 +20,7 @@ trait MillIndigo extends ScalaJSModule { def indigoGenerators: IndigoGenerators override def generatedSources: T[Seq[PathRef]] = T { - indigoGenerators.toSources.map(mill.PathRef(_)) ++ super.generatedSources() + indigoGenerators.toSourcePaths(T.dest).map(mill.PathRef(_)) ++ super.generatedSources() } /** Build a static site for your game using Scala.js's fast linking. */