From ba6c01de9bc86f7453cd2e7fae95cb12e4acf427 Mon Sep 17 00:00:00 2001 From: Jared Burrows Date: Sat, 26 Dec 2020 22:12:26 -0500 Subject: [PATCH] standardize reports --- .../jaredsburrows/license/LicenseReportTask.kt | 4 ++-- .../license/internal/pom/Project.kt | 2 +- .../license/internal/report/HtmlReport.kt | 16 +++++++--------- .../license/internal/report/JsonReport.kt | 12 +++++++----- .../license/internal/report/Report.kt | 18 ++++++++++++++++++ .../internal/report/HtmlReportSpec.groovy | 6 +++--- .../internal/report/JsonReportSpec.groovy | 6 +++--- 7 files changed, 41 insertions(+), 23 deletions(-) create mode 100644 src/main/kotlin/com/jaredsburrows/license/internal/report/Report.kt diff --git a/src/main/kotlin/com/jaredsburrows/license/LicenseReportTask.kt b/src/main/kotlin/com/jaredsburrows/license/LicenseReportTask.kt index a9b84982..f1c62d94 100644 --- a/src/main/kotlin/com/jaredsburrows/license/LicenseReportTask.kt +++ b/src/main/kotlin/com/jaredsburrows/license/LicenseReportTask.kt @@ -254,7 +254,7 @@ open class LicenseReportTask : DefaultTask() { // tasks can't be final createNewFile() // Write report for file - bufferedWriter().use { it.write(HtmlReport(projects).string()) } + bufferedWriter().use { it.write(HtmlReport(projects).toString()) } } // Log output directory for user @@ -272,7 +272,7 @@ open class LicenseReportTask : DefaultTask() { // tasks can't be final createNewFile() // Write report for file - bufferedWriter().use { it.write(JsonReport(projects).string()) } + bufferedWriter().use { it.write(JsonReport(projects).toString()) } } // Log output directory for user diff --git a/src/main/kotlin/com/jaredsburrows/license/internal/pom/Project.kt b/src/main/kotlin/com/jaredsburrows/license/internal/pom/Project.kt index feda4ab4..2e0a1986 100644 --- a/src/main/kotlin/com/jaredsburrows/license/internal/pom/Project.kt +++ b/src/main/kotlin/com/jaredsburrows/license/internal/pom/Project.kt @@ -1,7 +1,7 @@ package com.jaredsburrows.license.internal.pom /** - * Represents the information that is used to make HTML and JSON reports. + * Represents the information that is used to make reports. * * @property name name of the library in the POM. * @property description description of the library in the POM. diff --git a/src/main/kotlin/com/jaredsburrows/license/internal/report/HtmlReport.kt b/src/main/kotlin/com/jaredsburrows/license/internal/report/HtmlReport.kt index 8149632d..8193a995 100644 --- a/src/main/kotlin/com/jaredsburrows/license/internal/report/HtmlReport.kt +++ b/src/main/kotlin/com/jaredsburrows/license/internal/report/HtmlReport.kt @@ -30,13 +30,13 @@ import kotlinx.html.visit * * @property projects list of [Project]s for thr HTML report. */ -class HtmlReport(private val projects: List) { +class HtmlReport(private val projects: List) : Report { - /** Return Html as a String. */ - fun string(): String = if (projects.isEmpty()) noOpenSourceHtml() else openSourceHtml() + override fun toString(): String = report() - /** Html report when there are open source licenses. */ - private fun openSourceHtml(): String { + override fun report(): String = if (projects.isEmpty()) emptyReport() else fullReport() + + override fun fullReport(): String { val projectsMap = hashMapOf>() val licenseMap = LicenseHelper.licenseMap @@ -173,8 +173,7 @@ class HtmlReport(private val projects: List) { }.toString() } - /** Html report when there are no open source licenses. */ - private fun noOpenSourceHtml(): String = StringBuilder() + override fun emptyReport(): String = StringBuilder() .appendHTML() .html { head { @@ -204,8 +203,7 @@ class HtmlReport(private val projects: List) { } as String } - @HtmlTagMarker - private fun FlowOrInteractiveOrPhrasingContent.a( + @HtmlTagMarker private fun FlowOrInteractiveOrPhrasingContent.a( href: String? = null, target: String? = null, classes: String? = null, diff --git a/src/main/kotlin/com/jaredsburrows/license/internal/report/JsonReport.kt b/src/main/kotlin/com/jaredsburrows/license/internal/report/JsonReport.kt index 662c488b..a2082e63 100644 --- a/src/main/kotlin/com/jaredsburrows/license/internal/report/JsonReport.kt +++ b/src/main/kotlin/com/jaredsburrows/license/internal/report/JsonReport.kt @@ -9,13 +9,13 @@ import com.jaredsburrows.license.internal.pom.Project * * @property projects list of [Project]s for thr JSON report. */ -class JsonReport(private val projects: List) { +class JsonReport(private val projects: List) : Report { - /** Return Json as a [String]. */ - fun string(): String = if (projects.isEmpty()) EMPTY_JSON else json() + override fun toString(): String = report() - /** Json report when there are open source licenses. */ - private fun json(): String { + override fun report(): String = if (projects.isEmpty()) emptyReport() else fullReport() + + override fun fullReport(): String { val reportList = projects.map { project -> // Handle multiple licenses val licensesJson = project.licenses.map { license -> @@ -44,6 +44,8 @@ class JsonReport(private val projects: List) { return gson.toJson(reportList, object : TypeToken>>() {}.type) } + override fun emptyReport(): String = EMPTY_JSON + companion object { private const val PROJECT = "project" private const val DESCRIPTION = "description" diff --git a/src/main/kotlin/com/jaredsburrows/license/internal/report/Report.kt b/src/main/kotlin/com/jaredsburrows/license/internal/report/Report.kt new file mode 100644 index 00000000..04d9ec39 --- /dev/null +++ b/src/main/kotlin/com/jaredsburrows/license/internal/report/Report.kt @@ -0,0 +1,18 @@ +package com.jaredsburrows.license.internal.report + +/** + * Used to be the base configuration for each report. + */ +interface Report { + /** Return a pretty print of the report. */ + override fun toString(): String + + /** Return the report with license or empty report if there are none. */ + fun report(): String + + /** Return the full report if are open source licenses are found. */ + fun fullReport(): String + + /** Return the empty report if no open source licenses are found. */ + fun emptyReport(): String +} diff --git a/src/test/groovy/com/jaredsburrows/license/internal/report/HtmlReportSpec.groovy b/src/test/groovy/com/jaredsburrows/license/internal/report/HtmlReportSpec.groovy index af8b1beb..b773555c 100644 --- a/src/test/groovy/com/jaredsburrows/license/internal/report/HtmlReportSpec.groovy +++ b/src/test/groovy/com/jaredsburrows/license/internal/report/HtmlReportSpec.groovy @@ -14,13 +14,13 @@ final class HtmlReportSpec extends Specification { def report = new HtmlReport(projects) when: - def actual = report.string() + def actual = report.toString() def expected = """ Open source licenses @@ -59,7 +59,7 @@ final class HtmlReportSpec extends Specification { def sut = new HtmlReport(projects) when: - def actual = sut.string() + def actual = sut.toString() def expected = """ diff --git a/src/test/groovy/com/jaredsburrows/license/internal/report/JsonReportSpec.groovy b/src/test/groovy/com/jaredsburrows/license/internal/report/JsonReportSpec.groovy index f4f18f3f..96451c86 100644 --- a/src/test/groovy/com/jaredsburrows/license/internal/report/JsonReportSpec.groovy +++ b/src/test/groovy/com/jaredsburrows/license/internal/report/JsonReportSpec.groovy @@ -14,7 +14,7 @@ final class JsonReportSpec extends Specification { def sut = new JsonReport(projects) when: - def actual = sut.string() + def actual = sut.toString() def expected = """ [] @@ -35,7 +35,7 @@ final class JsonReportSpec extends Specification { def sut = new JsonReport(projects) when: - def actual = sut.string() + def actual = sut.toString() def expected = """ [ @@ -88,7 +88,7 @@ final class JsonReportSpec extends Specification { def sut = new JsonReport(projects) when: - def actual = sut.string() + def actual = sut.toString() def expected = """ [