8000 Copy unprocessed files in plugin by rpiaggio · Pull Request #892 · gemini-hlsw/clue · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clue-plugin-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

2. **Directory Structure**:
- GraphQL queries should be placed in `<project>/src/clue/scala/[package]/`
- Other sources placed there (hand-written subqueries, the classes operations decode to) are
compiled too: the ones the generator doesn't process are copied as they are.

3. **Schema Reference Configuration**:
- Create or update `.scalafix.conf` to reference GraphQL schemas:
Expand Down
22 changes: 22 additions & 0 deletions sbt-plugin/src/main/scala/clue/sbt/CluePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,26 @@ object CluePlugin extends AutoPlugin {
}.value)
}

// What makes the generator rewrite a source: one of its annotations, or a `gql:` comment.
private val GeneratorTrigger =
"""@(clue\.annotation\.)?GraphQL(Schema|Stub)?\b|//\s*gql:""".r

// scalafix only writes the sources the generator changed, so the others would never be compiled.
// They are copied as they are, which lets `clueSourceDirectory` also hold plain definitions (such
// as hand-written subqueries, or the classes the operations decode to).
private def copyUnprocessedSources(from: File, to: File): Unit =
(from ** "*.scala").get().foreach { source =>
IO.relativizeFile(from, source).foreach { relative =>
val target: File = new File(to, relative.getPath)
val contents: String = IO.read(source)
// A source the generator rewrites is never copied over its generated counterpart, even
// when scalafix skipped it this time. Without a counterpart, the generator left it alone.
val unprocessed: Boolean = GeneratorTrigger.findFirstIn(contents).isEmpty
if (!target.exists || (unprocessed && IO.read(target) != contents))
IO.write(target, contents)
}
}

override def derivedProjects(proj: ProjectDefinition[?]): Seq[Project] = Seq(
Project(
proj.id + "-clue",
Expand All @@ -107,9 +127,11 @@ object CluePlugin extends AutoPlugin {
val outTo = root.relativize(to.toPath).normalize
Def.task {
streams.value.log.info(s"Generating Clue code from $outFrom to $outTo")
// A failed generation fails the task here, so nothing is copied then.
val _ = (Compile / scalafix)
.toTask(s" GraphQLGen --out-from=$outFrom --out-to=$outTo")
.value
copyUnprocessedSources(from, to)
(to ** "*.scala").get()
}
}.taskValue,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package test

// Not processed by the generator: it is copied as it is, so the main sources can use it.
case class CharacterId(value: String)
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package test

object App {
def main(args: Array[String]): Unit =
StarWarsQuery.Variables("r2d2") // compile-check that this was generated
// compile-check that the query was generated and the plain source copied
StarWarsQuery.Variables(CharacterId("r2d2").value)
}
Loading
0