|
| 1 | +/* |
| 2 | + * Copyright (C) 2021 Rick Busarow |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package modulecheck.core |
| 17 | + |
| 18 | +import modulecheck.api.ConfigurationName |
| 19 | +import modulecheck.api.Project2 |
| 20 | +import modulecheck.api.context.ProjectContext |
| 21 | +import modulecheck.core.context.OverShotDependencies |
| 22 | +import modulecheck.parsing.* |
| 23 | +import modulecheck.parsing.ModuleRef.StringRef |
| 24 | +import modulecheck.parsing.ModuleRef.TypeSafeRef |
| 25 | +import java.io.File |
| 26 | + |
| 27 | +data class OverShotDependencyFinding( |
| 28 | + override val dependentPath: String, |
| 29 | + override val buildFile: File, |
| 30 | + override val dependencyProject: Project2, |
| 31 | + override val dependencyIdentifier: String, |
| 32 | + override val configurationName: ConfigurationName |
| 33 | +) : DependencyFinding("demoted") { |
| 34 | + |
| 35 | + override fun fix(): Boolean { |
| 36 | + |
| 37 | + val blocks = DependencyBlockParser |
| 38 | + .parse(buildFile) |
| 39 | + |
| 40 | + val blockMatchPairOrNull = blocks.firstNotNullOfOrNull { block -> |
| 41 | + |
| 42 | + val match = matchingDeclaration(block) ?: return@firstNotNullOfOrNull null |
| 43 | + |
| 44 | + block to match |
| 45 | + } |
| 46 | + |
| 47 | + if (blockMatchPairOrNull != null) { |
| 48 | + |
| 49 | + val (block, match) = blockMatchPairOrNull |
| 50 | + |
| 51 | + val moduleDeclaration = newModuleDeclaration(match) |
| 52 | + |
| 53 | + val newDeclaration = newDeclarationText(match, moduleDeclaration) |
| 54 | + |
| 55 | + val oldDeclarationLine = match.statementWithSurroundingText |
| 56 | + .lines() |
| 57 | + .first { it.contains(match.declarationText.lines().first()) } |
| 58 | + |
| 59 | + val indent = "(\\s*)".toRegex() |
| 60 | + .find(oldDeclarationLine) |
| 61 | + ?.destructured |
| 62 | + ?.component1() |
| 63 | + ?: " " |
| 64 | + |
| 65 | + val newBlock = block.contentString.replace( |
| 66 | + match.declarationText, |
| 67 | + match.declarationText + "\n$indent" + newDeclaration |
| 68 | + ) |
| 69 | + |
| 70 | + val fileText = buildFile.readText() |
| 71 | + .replace(block.contentString, newBlock) |
| 72 | + |
| 73 | + buildFile.writeText(fileText) |
| 74 | + return true |
| 75 | + } |
| 76 | + |
| 77 | + return false |
| 78 | + } |
| 79 | + |
| 80 | + private fun matchingDeclaration(block: DependenciesBlock) = |
| 81 | + ( |
| 82 | + block.allDeclarations |
| 83 | + .filterIsInstance<ModuleDependencyDeclaration>() |
| 84 | + .maxByOrNull { declaration -> declaration.configName == configurationName.value } |
| 85 | + ?: block.allDeclarations |
| 86 | + .filterNot { it is ModuleDependencyDeclaration } |
| 87 | + .maxByOrNull { declaration -> declaration.configName == configurationName.value } |
| 88 | + ?: block.allDeclarations |
| 89 | + .lastOrNull() |
| 90 | + ) |
| 91 | + |
| 92 | + private fun newModuleDeclaration(match: DependencyDeclaration) = when (match) { |
| 93 | + is ExternalDependencyDeclaration -> dependencyProject.path |
| 94 | + is ModuleDependencyDeclaration -> when (match.moduleRef) { |
| 95 | + is StringRef -> dependencyProject.path |
| 96 | + is TypeSafeRef -> StringRef(dependencyProject.path).toTypeSafe().value |
| 97 | + } |
| 98 | + is UnknownDependencyDeclaration -> dependencyProject.path |
| 99 | + } |
| 100 | + |
| 101 | + private fun newDeclarationText(match: DependencyDeclaration, moduleDeclaration: String): String { |
| 102 | + return match.declarationText |
| 103 | + .replace(match.configName, configurationName.value) |
| 104 | + .let { |
| 105 | + when (match) { |
| 106 | + is ExternalDependencyDeclaration -> it.replace("""(["']).*(["'])""".toRegex()) { mr -> |
| 107 | + val quotes = mr.destructured.component1() |
| 108 | + |
| 109 | + "project($quotes$moduleDeclaration$quotes)" |
| 110 | + } |
| 111 | + is ModuleDependencyDeclaration -> it.replace(match.moduleRef.value, moduleDeclaration) |
| 112 | + is UnknownDependencyDeclaration -> it.replace( |
| 113 | + match.argument, |
| 114 | + "project(\"$moduleDeclaration\")" |
| 115 | + ) |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + override fun toString(): String { |
| 121 | + return "OverShotDependency(\n" + |
| 122 | + "\tdependentPath='$dependentPath', \n" + |
| 123 | + "\tbuildFile=$buildFile, \n" + |
| 124 | + "\tdependencyProject=$dependencyProject, \n" + |
| 125 | + "\tdependencyIdentifier='$dependencyIdentifier', \n" + |
| 126 | + "\tconfigurationName=$configurationName\n" + |
| 127 | + ")" |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +val ProjectContext.overshotDependencies: OverShotDependencies get() = get(OverShotDependencies) |
0 commit comments