Skip to content

Commit

Permalink
Merge 9e6ca72 into d1f89d9
Browse files Browse the repository at this point in the history
  • Loading branch information
OdysseusLives committed Feb 18, 2020
2 parents d1f89d9 + 9e6ca72 commit c818d8f
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import org.gradle.api.plugins.JavaPluginConvention
@CompileStatic
class UndeclaredDependencyRule extends GradleLintRule implements GradleModelAware {
private static final String DEPENDENCIES_BLOCK = 'rootDependenciesBlock'

String description = 'Ensure that directly used transitives are declared as first order dependencies, for Gradle versions 4.5 - 5'
String description = 'Ensure that directly used transitives are declared as first order dependencies'
DependencyService dependencyService

@Override
Expand Down Expand Up @@ -42,13 +41,13 @@ class UndeclaredDependencyRule extends GradleLintRule implements GradleModelAwar
def sortedSourceSets = convention.sourceSets.sort(false, dependencyService.sourceSetComparator())

sortedSourceSets.each { sourceSet ->
def confName = sourceSet.compileConfigurationName
def confName = sourceSet.compileClasspathConfigurationName
violations.put(confName, new HashMap())

def undeclaredDependencies = dependencyService.undeclaredDependencies(confName)
def filteredUndeclaredDependencies = filterUndeclaredDependencies(undeclaredDependencies, confName)

if (! filteredUndeclaredDependencies.isEmpty()) {
if (!filteredUndeclaredDependencies.isEmpty()) {
def dependencyBlock = bookmark(DEPENDENCIES_BLOCK)
if (dependencyBlock != null) {
filteredUndeclaredDependencies.each { undeclared ->
Expand All @@ -65,9 +64,9 @@ class UndeclaredDependencyRule extends GradleLintRule implements GradleModelAwar
addBuildLintViolation("one or more classes are required by your code directly, " +
"and you require a dependencies block in your subproject $project")
.insertAfter(project.buildFile, 0, """\
dependencies {
}
""".stripIndent())
dependencies {
}
""".stripIndent())
}
}
}
Expand Down Expand Up @@ -95,8 +94,14 @@ class UndeclaredDependencyRule extends GradleLintRule implements GradleModelAwar
ASTNode dependencyBlock = dependencyAndBlock.getValue()

addBuildLintViolation("one or more classes in $undeclaredDependency are required by your code directly")
.insertIntoClosureAtTheEnd(dependencyBlock, "$configurationName '$undeclaredDependency'")
.insertIntoClosureAtTheEnd(dependencyBlock, "${declarationConfigurationName(configurationName)} '$undeclaredDependency'")
}
}
}

private static String declarationConfigurationName(String configName) {
return configName
.replace('compileClasspath', 'implementation')
.replace('CompileClasspath', 'Implementation')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ abstract class TestKitSpecification extends Specification {
return subprojectDir
}

def dependencies(File _buildFile, String... confs = ['compile', 'testCompile']) {
def dependencies(File _buildFile, String... confs = ['compile', 'testCompile', 'implementation', 'testImplementation', 'api']) {
_buildFile.text.readLines()
.collect { it.trim() }
.findAll { line -> confs.any { c -> line.startsWith(c) } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ class SupportJavaLibrarySpec extends TestKitSpecification {
sub1BuildFileText.contains(expectedDependencies)

where:
// configuration << ['testImplementation', 'testCompile', 'implementation'] // coming up!
configuration << ['testCompile']
configuration << ['testImplementation', 'testCompile', 'implementation']
}

@Unroll
Expand All @@ -80,9 +79,9 @@ class SupportJavaLibrarySpec extends TestKitSpecification {
def sub1BuildFileText = new File("$projectDir/sub1", 'build.gradle').text
def expectedDependencies = """
dependencies {
testCompile '$junit'
testImplementation '$junit'
}
""".stripIndent() // TODO: fix to make this testImplementation!
""".stripIndent()
sub1BuildFileText.contains(expectedDependencies)

where:
Expand Down

0 comments on commit c818d8f

Please sign in to comment.