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

com.apollographql.apollo.gradle.internal.ApolloDownloadSchemaTask.kt Maven / Gradle / Ivy

There is a newer version: 4.1.0
Show newest version
package com.apollographql.apollo.gradle.internal

import org.gradle.api.DefaultTask
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.options.Option
import java.io.File

abstract class ApolloDownloadSchemaTask : DefaultTask() {
  @get:Input
  abstract val endpoint: Property

  @get:Optional
  @get:Input
  var header = emptyList() // cannot be lazy for @Option to work

  @get:Input
  abstract val schemaRelativeToProject: Property

  init {
    /**
     * We cannot know in advance if the backend schema changed so don't cache or mark this task up-to-date
     * This code actually redundant because the task has no output but adding it make it explicit.
     */
    outputs.upToDateWhen { false }
    outputs.cacheIf { false }
  }

  @TaskAction
  fun taskAction() {
    val schema = project.file(schemaRelativeToProject.get())

    SchemaDownloader.download(
        endpoint = endpoint.get(),
        schema = schema,
        headers = header.toMap(),
        connectTimeoutSeconds = System.getProperty("okHttp.connectTimeout", "600").toLong(),
        readTimeoutSeconds = System.getProperty("okHttp.readTimeout", "600").toLong()
    )
  }

  private fun List.toMap(): Map {
    return map {
      val index = it.indexOf(':')
      check(index > 0 && index < it.length - 1) {
        "header should be in the form 'Name: Value'"
      }

      it.substring(0, index).trim() to it.substring(index + 1, it.length).trim()
    }.toMap()
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy