Skip to content

Commit

Permalink
fix gradle 8.11 compatibility (#100);
Browse files Browse the repository at this point in the history
fix report file disable;
add AnimalSniffer task shortcut (to simplify report configuration)

(cherry picked from commit b45a7fd)
  • Loading branch information
xvik committed Nov 18, 2024
1 parent 22baa6b commit e2621ee
Show file tree
Hide file tree
Showing 12 changed files with 671 additions and 17 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
* Update animalsniffer 1.23 -> 1.24
* Fix gradle 8.11 support (#100)
* Fix file report disabling
* Add AnimalSniffer task shortcut for simpler reports configuration

### 1.7.1 (2023-07-05)
* Update animalsniffer 1.22 -> 1.23
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.gradle.api.file.FileCollection
import org.gradle.api.file.FileTree
import org.gradle.api.internal.CollectionCallbackActionDecorator
import org.gradle.api.internal.project.IsolatedAntBuilder
import org.gradle.api.model.ObjectFactory
import org.gradle.api.reporting.Report
import org.gradle.api.reporting.Reporting
import org.gradle.api.tasks.*
Expand All @@ -17,6 +18,7 @@ import org.gradle.util.ClosureBackedAction
import org.gradle.util.GradleVersion
import ru.vyarus.gradle.plugin.animalsniffer.report.AnimalSnifferReports
import ru.vyarus.gradle.plugin.animalsniffer.report.AnimalSnifferReportsImpl
import ru.vyarus.gradle.plugin.animalsniffer.report.LegacyAnimalsnifferReports
import ru.vyarus.gradle.plugin.animalsniffer.report.ReportCollector

import javax.inject.Inject
Expand Down Expand Up @@ -99,7 +101,7 @@ class AnimalSniffer extends SourceTask implements VerificationTask, Reporting<An
@Console
boolean debug

private final AnimalSnifferReportsImpl reports
private final AnimalSnifferReports reports

/**
* Due to many classloaders used by AntBuilder, have to avoid ant classes in custom listener.
Expand Down Expand Up @@ -135,14 +137,21 @@ class AnimalSniffer extends SourceTask implements VerificationTask, Reporting<An

@SuppressWarnings('ThisReferenceEscapesConstructor')
AnimalSniffer() {
reports = instantiator.newInstance(AnimalSnifferReportsImpl, this, getCallbackActionDecorator())
reports = GradleVersion.current() < GradleVersion.version('7.0')
? instantiator.newInstance(LegacyAnimalsnifferReports, this, getCallbackActionDecorator())
: instantiator.newInstance(AnimalSnifferReportsImpl, this, getObjectFactory())
}

@Inject
Instantiator getInstantiator() {
throw new UnsupportedOperationException()
}

@Inject
ObjectFactory getObjectFactory() {
throw new UnsupportedOperationException()
}

@Inject
CollectionCallbackActionDecorator getCallbackActionDecorator() {
throw new UnsupportedOperationException()
Expand Down Expand Up @@ -230,10 +239,10 @@ class AnimalSniffer extends SourceTask implements VerificationTask, Reporting<An
if (collector.errorsCnt() > 0) {
String message = "${collector.errorsCnt()} AnimalSniffer violations were found " +
"in ${collector.filesCnt()} files."
Report report = reports.firstEnabled
if (report) {
File target = GradleVersion.current() < GradleVersion.version('7.0')
? report.destination : report.outputLocation.get().asFile
Report report = reports.text
boolean legacy = GradleVersion.current() < GradleVersion.version('7.0')
if (report && (legacy ? report.enabled : report.required.get())) {
File target = legacy ? report.destination : report.outputLocation.get().asFile
collector.writeToFile(target)

String reportUrl = "file:///${target.canonicalPath.replaceAll('\\\\', '/')}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class AnimalSnifferPlugin implements Plugin<Project> {
private void registerShortcuts() {
ExtraPropertiesExtension props = project.extensions.extraProperties
// to allow custom tasks declaration without package
props.set(AnimalSniffer.simpleName, AnimalSniffer)
props.set(BuildSignatureTask.simpleName, BuildSignatureTask)
props.set(SignatureInfoTask.simpleName, SignatureInfoTask)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.vyarus.gradle.plugin.animalsniffer.report

import groovy.transform.CompileStatic
import org.gradle.api.Action
import org.gradle.api.reporting.ReportContainer
import org.gradle.api.reporting.SingleFileReport
import org.gradle.api.tasks.Internal
Expand All @@ -19,4 +20,11 @@ interface AnimalSnifferReports extends ReportContainer<SingleFileReport> {
*/
@Internal
SingleFileReport getText()

/**
* Configures the text report.
*
* @param action The Configuration closure/action.
*/
void text(Action<SingleFileReport> action);
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
package ru.vyarus.gradle.plugin.animalsniffer.report

import groovy.transform.CompileStatic
import org.gradle.api.Action
import org.gradle.api.Task
import org.gradle.api.internal.CollectionCallbackActionDecorator
import org.gradle.api.model.ObjectFactory
import org.gradle.api.reporting.SingleFileReport
import org.gradle.api.reporting.internal.TaskGeneratedSingleFileReport
import org.gradle.api.reporting.internal.TaskReportContainer
import ru.vyarus.gradle.plugin.animalsniffer.report.internal.Reports
import ru.vyarus.gradle.plugin.animalsniffer.report.internal.DefaultSingleFileReport

/**
* AnimalSniffer reports implementation.
* AnimalSniffer reports implementation. Compatible with gradle >= 7
*
* @author Vyacheslav Rusakov
* @since 14.12.2015
*/
@CompileStatic
class AnimalSnifferReportsImpl extends TaskReportContainer<SingleFileReport> implements AnimalSnifferReports {
class AnimalSnifferReportsImpl extends Reports<SingleFileReport> implements AnimalSnifferReports {

private static final String TEXT = 'text'
private static final String TYPE_TEXT = 'text'

AnimalSnifferReportsImpl(Task task, CollectionCallbackActionDecorator callbackActionDecorator) {
super(SingleFileReport, task, callbackActionDecorator)
AnimalSnifferReportsImpl(Task task, ObjectFactory objects) {
super(task.project, SingleFileReport)

add(TaskGeneratedSingleFileReport, TEXT, task)
addReport(objects.newInstance(DefaultSingleFileReport, TYPE_TEXT, task))
}

@Override
SingleFileReport getText() {
return getByName(TEXT)
return getByName(TYPE_TEXT)
}

@Override
void text(Action<SingleFileReport> action) {
action.execute(text)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ru.vyarus.gradle.plugin.animalsniffer.report

import groovy.transform.CompileStatic
import org.gradle.api.Action
import org.gradle.api.Task
import org.gradle.api.internal.CollectionCallbackActionDecorator
import org.gradle.api.reporting.SingleFileReport
import org.gradle.api.reporting.internal.TaskGeneratedSingleFileReport
import org.gradle.api.reporting.internal.TaskReportContainer

/**
* AnimalSniffer reports implementation. Compatible with gradle <= 8.10
*
* @author Vyacheslav Rusakov
* @since 14.12.2015
*/
@CompileStatic
class LegacyAnimalsnifferReports extends TaskReportContainer<SingleFileReport> implements AnimalSnifferReports {

private static final String TEXT = 'text'

LegacyAnimalsnifferReports(Task task, CollectionCallbackActionDecorator callbackActionDecorator) {
super(SingleFileReport, task, callbackActionDecorator)

add(TaskGeneratedSingleFileReport, TEXT, task)
}

@Override
SingleFileReport getText() {
return getByName(TEXT)
}

@Override
void text(Action<SingleFileReport> action) {
action.execute(text)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package ru.vyarus.gradle.plugin.animalsniffer.report.internal

import groovy.transform.CompileStatic
import org.gradle.api.Task
import org.gradle.api.file.ProjectLayout
import org.gradle.api.internal.IConventionAware
import org.gradle.api.internal.provider.DefaultProvider
import org.gradle.api.provider.Provider
import org.gradle.api.reporting.Report
import org.gradle.api.reporting.SingleFileReport
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputFile

import javax.inject.Inject

/**
* {@link SingleFileReport} implementation (gradle implementation is in internal package and could change any time).
* Supports both old and new apis (pre gradle 7 and above it).
*
* @author Vyacheslav Rusakov
* @since 17.11.2024
*/
@SuppressWarnings('AbstractClassWithoutAbstractMethod')
@CompileStatic
abstract class DefaultSingleFileReport implements SingleFileReport {

private final String name
private final Task task

@SuppressWarnings('AbstractClassWithPublicConstructor')
@Inject
DefaultSingleFileReport(String name, Task task) {
this.name = name
this.task = task
required.convention(false)
outputLocation.convention(projectLayout.file(new DefaultProvider<File>({
return ((IConventionAware) this).conventionMapping.getConventionValue(null, 'destination', false)
})))
}

@Override
Report configure(Closure cl) {
final Closure closure = (Closure) cl.clone()
closure.resolveStrategy = Closure.DELEGATE_FIRST
closure.delegate = this
closure.call(this)
return this
}

@OutputFile
File getDestination() {
return outputLocation.asFile.orNull
}

@Internal
boolean isEnabled() {
return required.get()
}

@Override
String getDisplayName() {
return "Report generated by task '$task.path' ($name)"
}

@Override
String getName() {
return name
}

void setDestination(Provider<File> provider) {
outputLocation.fileProvider(provider)
}

@Override
void setDestination(File file) {
outputLocation.fileValue(file)
}

void setEnabled(Provider<Boolean> enabled) {
required.set(enabled)
}

void setEnabled(boolean enabled) {
required.set(enabled)
}

@Override
OutputType getOutputType() {
return OutputType.FILE
}

@Inject
protected ProjectLayout getProjectLayout() {
throw new UnsupportedOperationException()
}
}
Loading

0 comments on commit e2621ee

Please sign in to comment.