-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba6c01d
commit 11a6522
Showing
7 changed files
with
212 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/main/kotlin/com/jaredsburrows/license/internal/report/CsvReport.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.jaredsburrows.license.internal.report | ||
|
||
import com.jaredsburrows.license.internal.pom.Project | ||
|
||
/** | ||
* Generates CSV report of projects dependencies. | ||
* | ||
* @property projects list of [Project]s for thr CSV report. | ||
*/ | ||
class CsvReport(private val projects: List<Project>) : Report { | ||
|
||
override fun toString(): String = report() | ||
|
||
override fun report(): String = if (projects.isEmpty()) emptyReport() else fullReport() | ||
|
||
override fun fullReport(): String { | ||
val projectInfoList = arrayListOf<String>() | ||
projectInfoList.add(COLUMNS) | ||
|
||
projects.map { project -> | ||
val projectInfo = arrayListOf<String?>().apply { | ||
// Project Name | ||
addCsvString(project.name) | ||
|
||
// Project Description | ||
addCsvString(project.description) | ||
|
||
// Project Version | ||
addCsvString(project.version) | ||
|
||
// Project Developers | ||
addCsvList(project.developers) { it.name } | ||
|
||
// Project Url | ||
addCsvString(project.url) | ||
|
||
// Project Year | ||
addCsvString(project.year) | ||
|
||
// Project License Names | ||
addCsvList(project.licenses) { it.name } | ||
|
||
// Project License Url | ||
addCsvList(project.licenses) { it.url } | ||
|
||
// Project Dependency | ||
addCsvString(project.gav) | ||
} | ||
|
||
// Add each row to the list | ||
projectInfoList.add(projectInfo.toCsv()) | ||
} | ||
|
||
return projectInfoList.joinToString(separator = "\n") | ||
} | ||
|
||
override fun emptyReport(): String = EMPTY_CSV | ||
|
||
private fun ArrayList<String?>.toCsv(): String = this.joinToString(separator = ",") | ||
|
||
private fun ArrayList<String?>.addCsvString(element: String): Boolean { | ||
return this.add(element.toCsvString()) | ||
} | ||
|
||
private fun <T> ArrayList<String?>.addCsvList( | ||
elements: List<T>, | ||
transform: ((T) -> CharSequence)? = null | ||
): Boolean { | ||
// return when { | ||
// elements.isEmpty() -> this.add(null) | ||
// elements.size == 1 -> this.add(elements.joinToString(separator = ",", transform = transform)) | ||
// else -> this.add("\"${elements.joinToString(separator = ",", transform = transform)}\"") | ||
// } | ||
|
||
return when { | ||
elements.isEmpty() -> this.add(null) | ||
else -> { | ||
val blah = elements.joinToString(separator = ",", transform = transform) | ||
when (elements.size) { | ||
1 -> this.add(blah) | ||
else -> this.add("\"${blah}\"") | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun String.toCsvString(): String? = if (this.isNotEmpty()) this else null | ||
|
||
companion object { | ||
private const val COLUMNS = "project,description,version,developers,url,year,licenses,license urls,dependency" | ||
private const val EMPTY_CSV = "" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4
src/main/kotlin/com/jaredsburrows/license/internal/report/Report.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/test/groovy/com/jaredsburrows/license/internal/report/CsvReportSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.jaredsburrows.license.internal.report | ||
|
||
import com.jaredsburrows.license.internal.pom.Developer | ||
import com.jaredsburrows.license.internal.pom.License | ||
import com.jaredsburrows.license.internal.pom.Project | ||
import spock.lang.Specification | ||
|
||
final class CsvReportSpec extends Specification { | ||
def 'no open source csv'() { | ||
given: | ||
def projects = [] | ||
def sut = new CsvReport(projects) | ||
|
||
when: | ||
def actual = sut.toString() | ||
def expected = "" | ||
|
||
then: | ||
actual == expected | ||
} | ||
|
||
def 'open source csv - missing values'() { | ||
given: | ||
def developer = new Developer(name: 'name') | ||
def project1 = new Project( | ||
name: 'name', | ||
developers: [], | ||
gav: 'foo:bar:1.2.3' | ||
) | ||
def project2 = new Project( | ||
name: 'name', | ||
developers: [developer, developer], | ||
gav: 'foo:bar:1.2.3' | ||
) | ||
def projects = [project1, project2] | ||
def sut = new CsvReport(projects) | ||
|
||
when: | ||
def actual = sut.toString() | ||
def expected = | ||
"project,description,version,developers,url,year,licenses,license urls,dependency\n" + | ||
"name,null,null,null,null,null,null,null,foo:bar:1.2.3\n" + | ||
"name,null,null,\"name,name\",null,null,null,null,foo:bar:1.2.3" | ||
|
||
then: | ||
actual == expected | ||
} | ||
|
||
def 'open source csv - all values'() { | ||
given: | ||
def developer = new Developer(name: 'name') | ||
def developers = [developer, developer] | ||
def license = new License( | ||
name: 'name', | ||
url: 'url' | ||
) | ||
def project = new Project( | ||
name: 'name', | ||
description: 'description', | ||
version: '1.0.0', | ||
licenses: [license], | ||
url: 'url', | ||
developers: developers, | ||
year: 'year', | ||
gav: 'foo:bar:1.2.3' | ||
) | ||
def projects = [project, project] | ||
def sut = new CsvReport(projects) | ||
|
||
when: | ||
def actual = sut.toString() | ||
def expected = | ||
"project,description,version,developers,url,year,licenses,license urls,dependency\n" + | ||
"name,description,1.0.0,\"name,name\",url,year,name,url,foo:bar:1.2.3\n" + | ||
"name,description,1.0.0,\"name,name\",url,year,name,url,foo:bar:1.2.3" | ||
|
||
then: | ||
actual == expected | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters