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

commonMain.co.touchlab.skie.plugin.switflink.ProcessSwiftSourcesTask.kt Maven / Gradle / Ivy

There is a newer version: 0.9.0-RC.5
Show newest version
package co.touchlab.skie.plugin.switflink

import org.gradle.api.DefaultTask
import org.gradle.api.file.DuplicatesStrategy
import org.gradle.api.file.FileSystemOperations
import org.gradle.api.provider.Property
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import java.io.File
import javax.inject.Inject

abstract class ProcessSwiftSourcesTask : DefaultTask() {

    @get:OutputDirectory
    abstract val output: Property

    @get:Inject
    abstract val fileSystemOperations: FileSystemOperations

    @TaskAction
    fun execute() {
        syncFiles()

        verifyFileNames()
    }

    private fun syncFiles() {
        fileSystemOperations.sync {
            duplicatesStrategy = DuplicatesStrategy.FAIL

            from(inputs.files) {
                include("**/*.swift")
            }

            into(output)
        }
    }

    private fun verifyFileNames() {
        output.get().walkTopDown()
            .filter { it.extension == "swift" }
            .groupBy { it.name }
            .forEach { (name, files) ->
                if (files.size > 1) {
                    throw IllegalStateException(
                        "Files $files have the same name '$name'. " +
                            "This is not allowed in Swift where each file in a given module must have a unique name regardless its path.",
                    )
                }
            }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy