<commit_before>/*
 * Copyright 2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.kotlin.dsl.plugins.precompiled

import org.gradle.api.DefaultTask
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Classpath
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction

import org.gradle.internal.classloader.ClasspathHasher
import org.gradle.internal.classpath.ClassPath
import org.gradle.internal.classpath.DefaultClassPath
import org.gradle.internal.hash.HashCode

import org.gradle.kotlin.dsl.accessors.writeSourceCodeForPluginSpecBuildersFor
import org.gradle.kotlin.dsl.support.serviceOf


@CacheableTask
open class GeneratePluginSpecBuilders : DefaultTask() {

    @get:OutputDirectory
    var outputDirectory = project.objects.directoryProperty()

    @get:Classpath
    lateinit var classPath: FileCollection

    @TaskAction
    @Suppress("unused")
    internal
    fun generate() =
        outputDirectory.asFile.get().let { outputDir ->
            val classPath = DefaultClassPath.of(classPath.files)
            val packageDir = outputDir.resolve(packageName.split('.').joinToString("/")).apply {
                mkdirs()
            }
            val outputFile = packageDir.resolve("PluginAccessors.kt")
            writeSourceCodeForPluginSpecBuildersFor(
                classPath,
                outputFile,
                packageName = kotlinPackageName
            )
        }

    @get:Internal
    internal
    val kotlinPackageName by lazy {
        kotlinPackageNameFor(packageName)
    }

    private
    val packageName by lazy {
        "gradle-kotlin-dsl.plugin-spec-builders.${'$'}${hashOf(pluginDescriptorClassPath)}"
    }

    private
    val pluginDescriptorClassPath by lazy {
        DefaultClassPath.of(classPath.files)
    }

    private
    fun hashOf(classPath: ClassPath): HashCode =
        project.serviceOf<ClasspathHasher>().hash(classPath)

    private
    fun kotlinPackageNameFor(packageName: String) =
        packageName.split('.').joinToString(".") { "`$it`" }
}<commit_msg>Emit plugin spec builders to module with unique name<commit_after>/*
 * Copyright 2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.kotlin.dsl.plugins.precompiled

import org.gradle.api.DefaultTask
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Classpath
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction

import org.gradle.internal.classloader.ClasspathHasher
import org.gradle.internal.classpath.ClassPath
import org.gradle.internal.classpath.DefaultClassPath
import org.gradle.internal.hash.HashCode

import org.gradle.kotlin.dsl.accessors.writeSourceCodeForPluginSpecBuildersFor
import org.gradle.kotlin.dsl.support.serviceOf


@CacheableTask
open class GeneratePluginSpecBuilders : DefaultTask() {

    @get:OutputDirectory
    var outputDirectory = project.objects.directoryProperty()

    @get:Classpath
    lateinit var classPath: FileCollection

    @TaskAction
    @Suppress("unused")
    internal
    fun generate() =
        outputDirectory.asFile.get().let { outputDir ->
            val packageDir = outputDir.resolve(packageName.split('.').joinToString("/")).apply {
                mkdirs()
            }
            val outputFile = packageDir.resolve("PluginSpecBuildersFor$$classPathHash.kt")
            writeSourceCodeForPluginSpecBuildersFor(
                pluginDescriptorClassPath,
                outputFile,
                packageName = kotlinPackageName
            )
        }

    @get:Internal
    internal
    val kotlinPackageName by lazy {
        kotlinPackageNameFor(packageName)
    }

    // TODO: consider a package name derived from the classpath hash
    // "gradle-kotlin-dsl.plugin-spec-builders.$$classPathHash"
    private
    val packageName
        get() = "org.gradle.kotlin.dsl"

    private
    val classPathHash
        get() = hashOf(pluginDescriptorClassPath)

    private
    val pluginDescriptorClassPath by lazy {
        DefaultClassPath.of(classPath.files)
    }

    private
    fun hashOf(classPath: ClassPath): HashCode =
        project.serviceOf<ClasspathHasher>().hash(classPath)

    private
    fun kotlinPackageNameFor(packageName: String) =
        packageName.split('.').joinToString(".") { "`$it`" }
}
{'fraction_non_alphanumeric': 0.08793565683646112, 'fraction_numerical': 0.0028596961572832885, 'mean_word_length': 3.2848392036753444, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2283121', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1502, 'n_words': 382}