All Downloads are FREE. Search and download functionalities are using the official Maven repository.

indigoplugin.generators.EmbedAseprite.scala Maven / Gradle / Ivy

The newest version!
package indigoplugin.generators

import io.circe.parser._
import indigoplugin.datatypes.Aseprite

object EmbedAseprite {

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

    val asepriteJson =
      if (!os.exists(filePath))
        throw new Exception("Aseprite json data file to embed was not found: " + filePath.toString())
      else {
        os.read(filePath)
      }

    val aseprite =
      decode[Aseprite](asepriteJson) match {
        case Right(s) =>
          s

        case Left(e) =>
          throw new Exception(
            "Failed to deserialise json into Aseprite.\nPLEASE NOTE: Aseprite data must be exported using the 'array' option, the 'hash' format is not supported.\n" + e.getMessage
          )
      }

    val wd = outDir / Generators.OutputDirName

    os.makeDir.all(wd)

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

    val contents =
      s"""package $fullyQualifiedPackage
      |
      |import indigo.shared.formats.*
      |
      |// DO NOT EDIT: Generated by Indigo.
      |object $moduleName:
      |
      |  val aseprite: Aseprite =
      |    ${aseprite.render}
      |""".stripMargin

    os.write.over(file, contents)

    Seq(file)
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy