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

com.avito.android.network_contracts.NetworkContractsRootPlugin.kt Maven / Gradle / Ivy

The newest version!
package com.avito.android.network_contracts

import com.avito.android.network_contracts.codegen.SetupTmpMtlsFilesTask
import com.avito.android.network_contracts.configuration.codegenConfiguration
import com.avito.android.network_contracts.extension.NetworkContractsRootExtension
import com.avito.android.network_contracts.internal.http.HttpClientService
import com.avito.android.network_contracts.scheme.fixation.upsert.UpdateRemoteApiSchemesTask
import com.avito.android.network_contracts.shared.reportFile
import com.avito.android.network_contracts.validation.ValidateNetworkContractsRootTask
import com.avito.android.tls.TlsConfigurationPlugin
import com.avito.git.gitStateProvider
import com.avito.kotlin.dsl.getMandatoryStringProperty
import com.avito.kotlin.dsl.isRoot
import com.avito.kotlin.dsl.typedNamed
import com.avito.logger.GradleLoggerPlugin
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.register

public class NetworkContractsRootPlugin : Plugin {

    override fun apply(target: Project) {
        check(target.isRoot()) {
            "NetworkContractsRootPlugin must be applied to root project"
        }

        target.extensions.create(NetworkContractsRootExtension.NAME)

        target.codegenConfiguration.setArtifactsExecutable()

        configureSetupMtlsVariablesTask(target)
        configureVerificationRootTask(target)
        configureContractFixationTask(target)
    }

    private fun configureSetupMtlsVariablesTask(
        target: Project
    ) {
        target.tasks.register(SetupTmpMtlsFilesTask.NAME) {
            val buildDirectory = project.layout.buildDirectory
            this.tmpCrt.set(buildDirectory.dir(SetupTmpMtlsFilesTask.NAME).map { it.file("tmp_mtls_crt.crt") })
            this.tmpKey.set(buildDirectory.dir(SetupTmpMtlsFilesTask.NAME).map { it.file("tmp_mtls_key.key") })
            this.loggerFactory.set(GradleLoggerPlugin.provideLoggerFactory(this))
            this.tlsCredentialsService.set(TlsConfigurationPlugin.provideCredentialsService(project))
        }
    }

    private fun configureVerificationRootTask(
        project: Project
    ) {
        project.tasks.register(ValidateNetworkContractsRootTask.NAME) {
            this.rootDir.set(project.rootDir)
            this.projectPath.set(project.path)
            this.verdictFile.set(project.reportFile("networkContracts", "validation.txt"))
        }
    }

    private fun configureContractFixationTask(project: Project) {
        val validationTask = project.rootProject.tasks
            .typedNamed(ValidateNetworkContractsRootTask.NAME)

        project.tasks.register(UpdateRemoteApiSchemesTask.NAME, UpdateRemoteApiSchemesTask::class.java) {
            it.httpClientService.set(HttpClientService.provideHttpClientService(project))
            it.author.set(project.getMandatoryStringProperty("avito.networkContracts.fixation.author"))
            it.branchName.set(project.gitStateProvider().map { it.currentBranch.name })
            it.loggerFactory.set(GradleLoggerPlugin.getLoggerFactory(project))
            it.validationReport.set(validationTask.flatMap { it.verdictFile })
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy