com.netflix.java.refactor.gradle.RefactorAndFixSourceTask.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-source-refactor Show documentation
Show all versions of java-source-refactor Show documentation
Pluggable and distributed refactoring tool for Java source code
package com.netflix.java.refactor.gradle
import com.netflix.java.refactor.SourceSet
import org.gradle.api.DefaultTask
import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.api.tasks.TaskAction
import org.gradle.logging.StyledTextOutputFactory
import java.nio.file.Path
import java.util.*
import javax.inject.Inject
open class RefactorAndFixSourceTask : DefaultTask() {
// see http://gradle.1045684.n5.nabble.com/injecting-dependencies-into-task-instances-td5712637.html
@Inject
open fun getTextOutputFactory(): StyledTextOutputFactory? = null
private class RuleDescriptor(val name: String, val description: String)
@TaskAction
fun refactorSource() {
val fixesByRule = hashMapOf>()
project.convention.getPlugin(JavaPluginConvention::class.java).sourceSets.forEach {
val sourceSet = SourceSet(it.allJava.map { it.toPath() }, it.compileClasspath.map { it.toPath() })
sourceSet.allAutoRefactorsOnClasspath().forEach {
val (refactor, scanner) = it
sourceSet.allJava().forEach { source ->
scanner.scan(source)
if(source.changedFile) {
fixesByRule.getOrPut(RuleDescriptor(refactor.value, refactor.description), { HashSet() }).add(source.file())
}
}
}
}
printReport(fixesByRule)
}
private fun printReport(fixesByRule: Map>) {
val textOutput = getTextOutputFactory()!!.create(RefactorAndFixSourceTask::class.java)
if(fixesByRule.isEmpty()) {
textOutput.style(Styling.Green).println("Passed refactoring check with no changes necessary")
} else {
textOutput.text("Refactoring operations were performed on this project. ")
.withStyle(Styling.Bold).println("Please review the changes and commit.\n")
fixesByRule.entries.forEachIndexed { i, entry ->
val (rule, ruleFixes) = entry
textOutput.withStyle(Styling.Bold).text("${"${i+1}.".padEnd(2)} ${rule.description}")
textOutput.text(" (${ruleFixes.size} files changed) - ")
textOutput.withStyle(Styling.Yellow).println(rule.description)
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy