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

com.apollographql.apollo3.gradle.internal.ApolloCheckDuplicatesTask.kt Maven / Gradle / Ivy

There is a newer version: 4.0.0-beta.7
Show newest version
package com.apollographql.apollo3.gradle.internal

import com.apollographql.apollo3.compiler.ApolloMetadata
import org.gradle.api.DefaultTask
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.TaskAction

@CacheableTask
abstract class ApolloCheckDuplicatesTask : DefaultTask() {
  @get:InputFiles
  @get:PathSensitive(PathSensitivity.RELATIVE)
  abstract val metadataFiles: ConfigurableFileCollection

  @get:OutputFile
  abstract val outputFile: RegularFileProperty

  @TaskAction
  fun taskAction() {
    val metadataList = metadataFiles.files.mapNotNull { ApolloMetadata.readFrom(it) }

    metadataList.flatMap { metadata ->
      metadata.compilerMetadata.resolverInfo.entries.map { it.key to metadata.moduleName }
    }
        .groupBy { it.first }
        .values
        .find { it.size > 1 }
        ?.run {
          throw IllegalStateException("duplicate Type '${get(0).first}' generated in modules: ${map { it.second }.joinToString(",")}" +
              "\nUse 'alwaysGenerateTypesMatching' in a parent module to generate the type only once")
        }

    outputFile.asFile.get().run {
      parentFile.mkdirs()
      writeText("No duplicate found.")
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy