|
| 1 | +/* |
| 2 | + * Minecraft Dev for IntelliJ |
| 3 | + * |
| 4 | + * https://minecraftdev.org |
| 5 | + * |
| 6 | + * Copyright (c) 2021 minecraft-dev |
| 7 | + * |
| 8 | + * MIT License |
| 9 | + */ |
| 10 | + |
| 11 | +import com.google.gson.Gson |
| 12 | +import com.google.gson.reflect.TypeToken |
| 13 | +import java.nio.file.Files |
| 14 | +import java.nio.file.Path |
| 15 | +import javax.inject.Inject |
| 16 | +import org.gradle.api.artifacts.transform.CacheableTransform |
| 17 | +import org.gradle.api.artifacts.transform.InputArtifact |
| 18 | +import org.gradle.api.artifacts.transform.TransformAction |
| 19 | +import org.gradle.api.artifacts.transform.TransformOutputs |
| 20 | +import org.gradle.api.artifacts.transform.TransformParameters |
| 21 | +import org.gradle.api.file.FileSystemLocation |
| 22 | +import org.gradle.api.file.ProjectLayout |
| 23 | +import org.gradle.api.file.RegularFileProperty |
| 24 | +import org.gradle.api.provider.Property |
| 25 | +import org.gradle.api.provider.Provider |
| 26 | +import org.gradle.api.tasks.Input |
| 27 | +import org.gradle.api.tasks.InputFile |
| 28 | +import org.gradle.api.tasks.Internal |
| 29 | +import org.gradle.api.tasks.PathSensitive |
| 30 | +import org.gradle.api.tasks.PathSensitivity |
| 31 | + |
| 32 | +abstract class Filter : TransformAction<Filter.Params> { |
| 33 | + interface Params : TransformParameters { |
| 34 | + @get:Input |
| 35 | + val ideaVersion: Property<String> |
| 36 | + @get:Input |
| 37 | + val ideaVersionName: Property<String> |
| 38 | + @get:PathSensitive(PathSensitivity.NONE) |
| 39 | + @get:InputFile |
| 40 | + val depsFile: RegularFileProperty |
| 41 | + } |
| 42 | + |
| 43 | + @get:PathSensitive(PathSensitivity.NONE) |
| 44 | + @get:InputArtifact |
| 45 | + abstract val inputArtifact: Provider<FileSystemLocation> |
| 46 | + |
| 47 | + @get:Inject |
| 48 | + abstract val layout: ProjectLayout |
| 49 | + |
| 50 | + private val deps: List<Dep>? |
| 51 | + |
| 52 | + init { |
| 53 | + deps = run { |
| 54 | + val depsFile = parameters.depsFile.orNull?.asFile ?: return@run null |
| 55 | + if (!depsFile.exists()) { |
| 56 | + return@run null |
| 57 | + } |
| 58 | + |
| 59 | + val depList: DepList = depsFile.bufferedReader().use { reader -> |
| 60 | + Gson().fromJson(reader, DepList::class.java) |
| 61 | + } |
| 62 | + |
| 63 | + if ( |
| 64 | + parameters.ideaVersion.orNull == depList.intellijVersion && |
| 65 | + parameters.ideaVersionName.orNull == depList.intellijVersionName |
| 66 | + ) { |
| 67 | + depList.deps |
| 68 | + } else { |
| 69 | + null |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + override fun transform(outputs: TransformOutputs) { |
| 75 | + val input = inputArtifact.get().asFile.toPath() |
| 76 | + |
| 77 | + // exclude the coroutines jar |
| 78 | + // We include our own - but also IntelliJ's jar breaks sources |
| 79 | + val inputParts = input.map { it.toString() } |
| 80 | + if (!inputParts.containsAll(pathParts)) { |
| 81 | + outputs.file(inputArtifact) |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + val fileName = inputParts.last() |
| 86 | + if (fileName.startsWith("kotlinx-coroutines")) { |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + deps?.forEach { d -> |
| 91 | + if (fileName == "${d.artifactId}-${d.version}.jar") { |
| 92 | + return |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + outputs.file(inputArtifact) |
| 97 | + } |
| 98 | + |
| 99 | + companion object { |
| 100 | + private val pathParts = listOf("com.jetbrains.intellij.idea", "ideaIC", "lib") |
| 101 | + } |
| 102 | +} |
0 commit comments