Skip to content

Commit

Permalink
Merge pull request #139 from jaredsburrows/pr/jaredsburrows/standardi…
Browse files Browse the repository at this point in the history
…ze-reports

standardize reports
  • Loading branch information
jaredsburrows committed Dec 27, 2020
2 parents 1732396 + ba6c01d commit 600ba64
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ import kotlinx.html.visit
*
* @property projects list of [Project]s for thr HTML report.
*/
class HtmlReport(private val projects: List<Project>) {
class HtmlReport(private val projects: List<Project>) : 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<String?, List<Project>>()
val licenseMap = LicenseHelper.licenseMap

Expand Down Expand Up @@ -173,8 +173,7 @@ class HtmlReport(private val projects: List<Project>) {
}.toString()
}

/** Html report when there are no open source licenses. */
private fun noOpenSourceHtml(): String = StringBuilder()
override fun emptyReport(): String = StringBuilder()
.appendHTML()
.html {
head {
Expand Down Expand Up @@ -204,8 +203,7 @@ class HtmlReport(private val projects: List<Project>) {
} as String
}

@HtmlTagMarker
private fun FlowOrInteractiveOrPhrasingContent.a(
@HtmlTagMarker private fun FlowOrInteractiveOrPhrasingContent.a(
href: String? = null,
target: String? = null,
classes: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Project>) {
class JsonReport(private val projects: List<Project>) : 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 ->
Expand Down Expand Up @@ -44,6 +44,8 @@ class JsonReport(private val projects: List<Project>) {
return gson.toJson(reportList, object : TypeToken<MutableList<Map<String, Any?>>>() {}.type)
}

override fun emptyReport(): String = EMPTY_JSON

companion object {
private const val PROJECT = "project"
private const val DESCRIPTION = "description"
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
"""
<html>
<head>
<style>
body { font-family: sans-serif }
body { font-family: sans-serif }
pre { background-color: #eeeeee; padding: 1em; white-space: pre-wrap; display: inline-block }
</style>
<title>Open source licenses</title>
Expand Down Expand Up @@ -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 =
"""
<html>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
"""
[]
Expand All @@ -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 =
"""
[
Expand Down Expand Up @@ -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 =
"""
[
Expand Down

0 comments on commit 600ba64

Please sign in to comment.