Skip to content

Commit

Permalink
Fixed #634: Embed text with a present function
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Nov 18, 2023
1 parent b7b810a commit 64481fe
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,57 @@ final case class IndigoGenerators(fullyQualifiedPackageName: String, sources: Se
def withPackage(packageName: String): IndigoGenerators =
this.copy(fullyQualifiedPackageName = packageName)

/** Takes the contents of a text file, and leaves it to you to decide how to turn it into Scala code. The template
* provides nothing except the package declaration.
*
* @param moduleName
* The name for the Scala module, in this case, acts as the file name only.
* @param file
* The path to the text file to embed.
* @param present
* A function that takes and String and expects you to create a String of Scala code. You could parse JSON or read
* a list of files or... anything!
*/
def embed(moduleName: String, file: os.Path)(present: String => String): IndigoGenerators =
this.copy(
sources = sources :+
EmbedText.generate(moduleName, fullyQualifiedPackageName, file, present)
)

/** Takes the contents of a text file, and leaves it to you to decide how to turn it into Scala code. The template
* provides nothing except the package declaration.
*
* @param moduleName
* The name for the Scala module, in this case, acts as the file name only.
* @param file
* The relative path to the text file to embed.
* @param present
* A function that takes and String and expects you to create a String of Scala code. You could parse JSON or read
* a list of files or... anything!
*/
def embed(moduleName: String, file: File)(present: String => String): IndigoGenerators =
this.copy(
sources = sources :+
EmbedText.generate(moduleName, fullyQualifiedPackageName, os.RelPath(file).resolveFrom(os.pwd), present)
)

/** Takes the contents of a text file, and leaves it to you to decide how to turn it into Scala code. The template
* provides nothing except the package declaration.
*
* @param moduleName
* The name for the Scala module, in this case, acts as the file name only.
* @param file
* The relative path to the text file to embed.
* @param present
* A function that takes and String and expects you to create a String of Scala code. You could parse JSON or read
* a list of files or... anything!
*/
def embed(moduleName: String, file: String)(present: String => String): IndigoGenerators =
this.copy(
sources = sources :+
EmbedText.generate(moduleName, fullyQualifiedPackageName, os.RelPath(file).resolveFrom(os.pwd), present)
)

/** Embed raw text into a static variable.
*
* @param moduleName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,36 @@ object EmbedText {
Seq(file)
}

def generate(
moduleName: String,
fullyQualifiedPackage: String,
filePath: os.Path,
present: String => String
): os.Path => Seq[os.Path] = outDir => {

val text =
if (!os.exists(filePath)) throw new Exception("Text file to embed not found: " + filePath.toString())
else {
os.read(filePath)
}

val wd = outDir / Generators.OutputDirName

os.makeDir.all(wd)

val file = wd / s"$moduleName.scala"

val contents =
s"""package $fullyQualifiedPackage
|
|// DO NOT EDIT: Generated by Indigo.
|
|${present(text)}
|""".stripMargin

os.write.over(file, contents)

Seq(file)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class AcceptanceTests extends munit.FunSuite {
case p if p.startsWith(os.RelPath("captain")) => true
case p if p.endsWith(os.RelPath("stats.md")) => true
case p if p.endsWith(os.RelPath("armour.md")) => true
case p if p.endsWith(os.RelPath("colours.txt")) => true
case _ => false
},
None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import indigoplugin.IndigoGenerators

class GeneratorAcceptanceTests extends munit.FunSuite {

val sourceCSV = os.pwd / "test-assets" / "data" / "stats.csv"
val sourceMD = os.pwd / "test-assets" / "data" / "stats.md"
val sourceCSV = os.pwd / "test-assets" / "data" / "stats.csv"
val sourceMD = os.pwd / "test-assets" / "data" / "stats.md"
val sourceColours = os.pwd / "test-assets" / "data" / "colours.txt"

val targetDir = os.pwd / "out" / "indigo-plugin-generator-acceptance-test-output"

Expand Down Expand Up @@ -189,4 +190,70 @@ class GeneratorAcceptanceTests extends munit.FunSuite {

}

test("Can embed a txt tile") {

val files =
IndigoGenerators("com.example.test")
.embedText("ColoursText", sourceColours)
.toSourcePaths(targetDir)

files.headOption match {
case None =>
fail("No file was generated")

case Some(src) =>
assert(clue(src) == clue(targetDir / "indigo-compile-codegen-output" / "ColoursText.scala"))

val actual = os.read(src)

val expected =
s"""
|package com.example.test
|
|// DO NOT EDIT: Generated by Indigo.
|object ColoursText:
|
| val text: String =
| ${Generators.TripleQuotes}red
|green
|blue
|${Generators.TripleQuotes}
|""".stripMargin

assertEquals(actual.trim, expected.trim)
}
}

test("Can embed a txt tile and transform it") {

val files =
IndigoGenerators("com.example.test")
.embed("ColoursList", sourceColours) { text =>
"val colours: List[String] = " + text.split("\n").map(t => s"""\"$t\"""").mkString("List(", ", ", ")")
}
.toSourcePaths(targetDir)

files.headOption match {
case None =>
fail("No file was generated")

case Some(src) =>
assert(clue(src) == clue(targetDir / "indigo-compile-codegen-output" / "ColoursList.scala"))

val actual = os.read(src)

val expected =
"""
|package com.example.test
|
|// DO NOT EDIT: Generated by Indigo.
|
|val colours: List[String] = List("red", "green", "blue")
|
|""".stripMargin

assertEquals(actual.trim, expected.trim)
}
}

}
3 changes: 3 additions & 0 deletions indigo-plugin/test-assets/data/colours.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
red
green
blue

0 comments on commit 64481fe

Please sign in to comment.