11package junitbuild.compatibility.roseau
22
3+ import com.fasterxml.jackson.databind.ObjectMapper
4+ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
5+ import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature.WRITE_DOC_START_MARKER
36import org.gradle.api.DefaultTask
7+ import org.gradle.api.GradleException
48import org.gradle.api.file.ConfigurableFileCollection
9+ import org.gradle.api.file.DirectoryProperty
510import org.gradle.api.file.RegularFileProperty
611import org.gradle.api.tasks.CacheableTask
712import org.gradle.api.tasks.Classpath
813import org.gradle.api.tasks.CompileClasspath
9- import org.gradle.api.tasks.OutputFile
14+ import org.gradle.api.tasks.InputFile
15+ import org.gradle.api.tasks.Optional
16+ import org.gradle.api.tasks.OutputDirectory
17+ import org.gradle.api.tasks.PathSensitive
18+ import org.gradle.api.tasks.PathSensitivity
1019import org.gradle.api.tasks.TaskAction
1120import org.gradle.kotlin.dsl.assign
1221import org.gradle.process.ExecOperations
1322import java.io.ByteArrayOutputStream
23+ import java.io.File
24+ import java.io.FileOutputStream
1425import javax.inject.Inject
1526
1627@CacheableTask
@@ -31,12 +42,29 @@ abstract class RoseauDiff : DefaultTask() {
3142 @get:CompileClasspath
3243 abstract val v2: RegularFileProperty
3344
34- @get:OutputFile
35- abstract val csvReport: RegularFileProperty
45+ @get:InputFile
46+ @get:PathSensitive(PathSensitivity .NONE )
47+ abstract val configFile: RegularFileProperty
48+
49+ @get:InputFile
50+ @get:PathSensitive(PathSensitivity .NONE )
51+ @get:Optional
52+ abstract val acceptedChangesCsvFile: RegularFileProperty
53+
54+ @get:OutputDirectory
55+ abstract val reportDir: DirectoryProperty
3656
3757 @TaskAction
3858 fun run () {
39- csvReport.get().asFile.parentFile.mkdirs()
59+ val reportDir = reportDir.get().asFile.absoluteFile
60+ val reports = listOf (
61+ Report (reportDir.resolve(" breaking-changes.html" ), Report .Format .HTML ),
62+ Report (reportDir.resolve(" breaking-changes.csv" ), Report .Format .CSV )
63+ )
64+ reports.forEach { report -> report.file.delete() }
65+
66+ val effectiveConfigFile = writeEffectiveConfigFile(reports)
67+
4068 val output = ByteArrayOutputStream ()
4169 val result = execOperations.javaexec {
4270 mainClass = " io.github.alien.roseau.cli.RoseauCLI"
@@ -46,16 +74,53 @@ abstract class RoseauDiff : DefaultTask() {
4674 " --v1" , v1.get().asFile.absolutePath,
4775 " --v2" , v2.get().asFile.absolutePath,
4876 " --diff" ,
49- " --report " , csvReport.get().asFile.absolutePath ,
50- " --fail " ,
77+ " --fail-on-bc " ,
78+ " --config " , effectiveConfigFile.absolutePath ,
5179 )
80+ if (acceptedChangesCsvFile.isPresent) {
81+ args(" --ignored" , acceptedChangesCsvFile.get().asFile.absolutePath)
82+ }
5283 standardOutput = output
5384 errorOutput = output
5485 isIgnoreExitValue = true
5586 }
5687 if (result.exitValue != 0 ) {
5788 System .out .write(output.toByteArray())
5889 System .out .flush()
90+ reports.filter { it.file.exists() }.let { writtenReports ->
91+ if (writtenReports.isNotEmpty()) {
92+ println (" Reports:" )
93+ writtenReports.forEach {
94+ println (" - ${it.format.name} : ${it.file.toURI()} " )
95+ }
96+ }
97+ }
98+ if (result.exitValue == 1 ) {
99+ throw GradleException (" Breaking API changes detected" )
100+ }
101+ result.assertNormalExitValue()
102+ }
103+ }
104+
105+ private fun writeEffectiveConfigFile (reports : List <Report >): File {
106+ val effectiveConfigFile = temporaryDir.resolve(" roseau.yaml" )
107+ configFile.get().asFile.copyTo(effectiveConfigFile, overwrite = true )
108+ FileOutputStream (effectiveConfigFile, true ).bufferedWriter().use { writer ->
109+ val yamlFactory = YAMLFactory .builder().disable(WRITE_DOC_START_MARKER ).build()
110+ val mapper = ObjectMapper (yamlFactory)
111+ mapper.writeValue(
112+ writer, mapOf (
113+ " reports" to reports
114+ )
115+ )
116+ }
117+ return effectiveConfigFile
118+ }
119+
120+ private data class Report (val file : File , val format : Format ) {
121+ enum class Format {
122+ HTML , CSV
59123 }
60124 }
125+
61126}
0 commit comments