Skip to content

Commit

Permalink
feat: add sbt caching (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
Katrix authored Sep 4, 2023
1 parent 4479195 commit 8f68be5
Showing 1 changed file with 34 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ object GqlCodeGenPlugin extends AutoPlugin {

val findResources = taskKey[Seq[CustomResourceGroup]]("Find the resources")

val codeGenInput = taskKey[Seq[(String, Seq[File])]]("The code generator input")
val codeGenInput = taskKey[Seq[CodeGenInput]]("The code generator input")

val invokeCodeGen = taskKey[Seq[File]]("Invoke the code generator")

Expand All @@ -59,6 +59,8 @@ object GqlCodeGenPlugin extends AutoPlugin {

import autoImport._

case class CodeGenInput(json: String, inFiles: Seq[File], outFiles: Seq[File])

override def projectSettings: Seq[Setting[_]] =
List(
Gql.validate := true,
Expand Down Expand Up @@ -122,33 +124,47 @@ object GqlCodeGenPlugin extends AutoPlugin {

def escapeSlash(s: String) = s.replace("\\", "\\\\")

val (queries, outFiles) = rg.files.map { in =>
val queryInputs = rg.files.map { in =>
val fn = in.name.replaceAll("\\.", "_")
val outFile = f / s"${fn}.scala"
s"""{"query": "${escapeSlash(in.absolutePath)}", "output": "${escapeSlash(outFile.absolutePath)}"}""" -> outFile
}.unzip

s"""{"schema":"${escapeSlash(rg.schemaPath.absolutePath)}","shared":"${escapeSlash(sh.absolutePath)}","queries":[${queries
.mkString(",")}]}""" -> (outFiles ++ Seq(sh))
CodeGenInput(
json = s"""{"query": "${escapeSlash(in.absolutePath)}", "output": "${escapeSlash(outFile.absolutePath)}"}""",
inFiles = Seq(in),
outFiles = Seq(outFile)
)
}
val (queries, inFiless, outFiless) = queryInputs.flatMap(CodeGenInput.unapply).unzip3

CodeGenInput(
json =
s"""{"schema":"${escapeSlash(rg.schemaPath.absolutePath)}","shared":"${escapeSlash(sh.absolutePath)}","queries":[${queries
.mkString(",")}]}""",
inFiles = inFiless.flatten,
outFiles = outFiless.flatten ++ Seq(sh)
)
}
},
Gql.invokeCodeGen := {
val streamsObj = streams.value
val cp = (Compile / externalDependencyClasspath).value

val cmd = Gql.codeGenInput.value

val log = streams.value.log

val args =
List(
"java",
"gql.client.codegen.GeneratorCli"
) ++ List("--validate").filter(_ => Gql.validate.value) ++ List("--input") ++ cmd.map(_._1)
val cachedFun =
FileFunction.cached(streamsObj.cacheDirectory / "gql-invoke-codegen", inStyle = FilesInfo.full, outStyle = FilesInfo.full) { _ =>
val args =
List(
"java",
"gql.client.codegen.GeneratorCli"
) ++ List("--validate").filter(_ => Gql.validate.value) ++ List("--input") ++ cmd.map(_.json)

scala.sys.process.Process(args, None, "CLASSPATH" -> cp.map(_.data.toString).mkString(File.pathSeparator)).! match {
case 0 => cmd.flatMap(_.outFiles).toSet
case n => sys.error(s"Process exited with code $n")
}
}

scala.sys.process.Process(args, None, "CLASSPATH" -> cp.map(_.data.toString).mkString(File.pathSeparator)).! match {
case 0 => cmd.flatMap(_._2)
case n => sys.error(s"Process exited with code $n")
}
cachedFun(cmd.flatMap(_.inFiles).toSet).toSeq
},
Compile / sourceGenerators += Gql.invokeCodeGen.taskValue
)
Expand Down

0 comments on commit 8f68be5

Please sign in to comment.