Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

standardize reports #139

Merged
merged 1 commit into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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