Skip to content

Commit

Permalink
plugin requires now JavaBasePlugin instead of JavaPlugin for activation.
Browse files Browse the repository at this point in the history
Required for kotlin multiplatform 1.9.20 activating only base plugin with jvm().withJava() (#84)
  • Loading branch information
xvik committed Jul 5, 2023
1 parent 2fabf1a commit 412ae82
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
* Update animalsniffer 1.22 -> 1.23
* Plugin requires now JavaBasePlugin instead of JavaPlugin.
Required for kotlin multiplatform 1.9.20 activating only base plugin with jvm().withJava() (#84)

NOTE: animalsniffer 1.23 reports problems on fields without line number (but with field name),
Plugin output would contain line 1 (default for not defined line)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,17 @@ class AnimalSniffer extends SourceTask implements VerificationTask, Reporting<An
}

@TaskAction
@SuppressWarnings('CatchException')
@SuppressWarnings(['CatchException', 'VariableName'])
@CompileStatic(TypeCheckingMode.SKIP)
void run() {
String sortedPath = preparePath(getSource())
if (getDebug()) {
printTaskConfig(sortedPath)
}
Set<File> _sourceDirs = collectSourceDirs()
antBuilder.withClasspath(getAnimalsnifferClasspath()).execute {
ant.taskdef(name: 'animalsniffer', classname: 'org.codehaus.mojo.animal_sniffer.ant.CheckSignatureTask')
ReportCollector collector = new ReportCollector(getSourcesDirs())
ReportCollector collector = new ReportCollector(_sourceDirs)
replaceBuildListener(project, collector)
getAnimalsnifferSignatures().each { signature ->
try {
Expand All @@ -173,7 +174,7 @@ class AnimalSniffer extends SourceTask implements VerificationTask, Reporting<An
// enclosing class could be parsed after inlined and so ignoring annotation on enclosing class
// would be ignored (actually, this problem appears only on windows)
path(path: sortedPath)
getSourcesDirs().each {
_sourceDirs.each {
sourcepath(path: it.absoluteFile)
}
annotation(className: 'org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement')
Expand Down Expand Up @@ -257,7 +258,7 @@ class AnimalSniffer extends SourceTask implements VerificationTask, Reporting<An
.append(getAnimalsnifferSignatures().files.collect { "\t\t$it.name" }.join(NL))
.append(NL)
.append('\n\tsources:\n')
.append(getSourcesDirs().sort().collect { "\t\t${project.relativePath(it)}" }.join(NL))
.append(collectSourceDirs().sort().collect { "\t\t${project.relativePath(it)}" }.join(NL))
.append(NL)
.append('\n\tfiles:\n')
.append(path.split(File.pathSeparator).collect { "\t\t${it.replace(rootDir, '')}" }.join(NL))
Expand Down Expand Up @@ -292,4 +293,19 @@ class AnimalSniffer extends SourceTask implements VerificationTask, Reporting<An
// lambda case (Some$$Lambda$1). Ant removes every odd $ in a row
return sortedPath.join(File.pathSeparator).replace('$$', '$$$')
}

@CompileStatic(TypeCheckingMode.SKIP)
private Set<File> collectSourceDirs() {
Set<File> res = [] as Set
res.addAll(getSourcesDirs())
// HACK to support kotlin multiplatform source path for jvm case (when withJava() active)
// this MUST BE rewritten into separate support for multiplatform
if (project.plugins.findPlugin('org.jetbrains.kotlin.multiplatform')) {
project.kotlin.sourceSets.each {
println it.kotlin.sourceDirectories.files
res.addAll(it.kotlin.sourceDirectories.files)
}
}
return res
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.gradle.api.artifacts.Dependency
import org.gradle.api.file.FileCollection
import org.gradle.api.file.RegularFile
import org.gradle.api.plugins.ExtraPropertiesExtension
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.plugins.JavaBasePlugin
import org.gradle.api.plugins.ReportingBasePlugin
import org.gradle.api.reporting.ReportingExtension
import org.gradle.api.specs.NotSpec
Expand Down Expand Up @@ -57,7 +57,7 @@ class AnimalSnifferPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
// activated only when java plugin is enabled
project.plugins.withType(JavaPlugin) {
project.plugins.withType(JavaBasePlugin) {
this.project = project
project.plugins.apply(ReportingBasePlugin)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ class UpstreamKitTest extends AbstractKitTest {
then: "task successful"
result.task(':animalsnifferMain').outcome == TaskOutcome.SUCCESS
// testKit is incompatible with configuration cache, but I can check number of errors!
result.output.contains('1 problem was found storing the configuration cache.\n' +
'- Gradle runtime: support for using a Java agent with TestKit builds is not yet implemented with the configuration cache.')
result.output.contains('2 problems were found storing the configuration cache.')

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,59 @@ class KotlinSourceKitTest extends AbstractKitTest {
"""[Undefined reference] invalid.(Sample.kt:11)
>> int Boolean.compare(boolean, boolean)
[Undefined reference] invalid.(Sample.kt:16)
>> java.nio.file.Path java.nio.file.Paths.get(String, String[])
""")

then: "report correct"
File file = file('/build/reports/animalsniffer/main.text')
file.exists()
file.readLines() == [
"invalid.Sample:11 Undefined reference: int Boolean.compare(boolean, boolean)",
"invalid.Sample:16 Undefined reference: java.nio.file.Path java.nio.file.Paths.get(String, String[])"
]
}

def "Check kotlin multiplatform support"() {
setup:
build """
plugins {
id 'org.jetbrains.kotlin.multiplatform' version '1.9.0-RC'
id 'ru.vyarus.animalsniffer'
}
kotlin {
jvm().withJava()
}
animalsniffer {
ignoreFailures = true
}
repositories { mavenCentral()}
dependencies {
signature 'org.codehaus.mojo.signature:java16-sun:1.0@signature'
implementation platform('org.jetbrains.kotlin:kotlin-bom')
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
implementation 'org.slf4j:slf4j-api:1.7.25'
}
"""
fileFromClasspath('src/jvmMain/kotlin/invalid/Sample.kt', '/ru/vyarus/gradle/plugin/animalsniffer/kotlin/invalid/Sample.kt')
// debug()

when: "run task"
BuildResult result = run('check')

then: "task successful"
result.task(':check').outcome == TaskOutcome.SUCCESS

then: "found 2 violations"
result.output.contains("2 AnimalSniffer violations were found in 1 files")
result.output.replaceAll('\r', '').contains(
"""[Undefined reference] invalid.(Sample.kt:11)
>> int Boolean.compare(boolean, boolean)
[Undefined reference] invalid.(Sample.kt:16)
>> java.nio.file.Path java.nio.file.Paths.get(String, String[])
""")
Expand Down

0 comments on commit 412ae82

Please sign in to comment.