-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add html reporter module to ktlint library (#641)
* add html reporter module to ktlint library * reorder imports on HtmlReporterTest file
- Loading branch information
Showing
10 changed files
with
443 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
plugins { | ||
id 'org.jetbrains.kotlin.jvm' | ||
id 'com.vanniktech.maven.publish' | ||
} | ||
|
||
dependencies { | ||
implementation project(':ktlint-core') | ||
implementation deps.kotlin.stdlib | ||
|
||
testImplementation deps.junit | ||
testImplementation deps.assertj | ||
} |
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,4 @@ | ||
GROUP=com.pinterest.ktlint | ||
POM_NAME=ktlint-reporter-html | ||
POM_ARTIFACT_ID=ktlint-reporter-html | ||
POM_PACKAGING=jar |
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,94 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.pinterest.ktlint</groupId> | ||
<artifactId>pom</artifactId> | ||
<version>0.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>ktlint-reporter-html</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.jetbrains.kotlin</groupId> | ||
<artifactId>kotlin-stdlib</artifactId> | ||
<version>${kotlin.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.pinterest.ktlint</groupId> | ||
<artifactId>ktlint-core</artifactId> | ||
<version>0.0.0-SNAPSHOT</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<version>${assertj.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory> | ||
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory> | ||
</build> | ||
|
||
<profiles> | ||
<profile> | ||
<id>release</id> | ||
<activation> | ||
<property> | ||
<name>deploy</name> | ||
<value>maven-central</value> | ||
</property> | ||
</activation> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-source-plugin</artifactId> | ||
<version>3.0.1</version> | ||
<executions> | ||
<execution> | ||
<id>attach-sources</id> | ||
<phase>verify</phase> | ||
<goals> | ||
<goal>jar-no-fork</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>3.0.2</version> | ||
<executions> | ||
<execution> | ||
<id>attach-javadocs</id> | ||
<phase>verify</phase> | ||
<goals> | ||
<goal>jar</goal> | ||
</goals> | ||
<configuration> | ||
<classifier>javadoc</classifier> | ||
<classesDirectory>${basedir}/javadoc</classesDirectory> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</profile> | ||
</profiles> | ||
|
||
</project> |
147 changes: 147 additions & 0 deletions
147
ktlint-reporter-html/src/main/kotlin/com/pinterest/ktlint/reporter/html/HtmlReporter.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,147 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2019 Matheus Candido | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package com.pinterest.ktlint.reporter.html | ||
|
||
import com.pinterest.ktlint.core.LintError | ||
import com.pinterest.ktlint.core.Reporter | ||
import java.io.PrintStream | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
class HtmlReporter(private val out: PrintStream) : Reporter { | ||
|
||
private val acc = ConcurrentHashMap<String, MutableList<LintError>>() | ||
private var issueCount = 0 | ||
private var correctedCount = 0 | ||
|
||
override fun onLintError(file: String, err: LintError, corrected: Boolean) { | ||
if (!corrected) { | ||
issueCount += 1 | ||
acc.getOrPut(file) { mutableListOf() }.add(err) | ||
} else { | ||
correctedCount += 1 | ||
} | ||
} | ||
|
||
override fun afterAll() { | ||
html { | ||
head { | ||
cssLink("https://fonts.googleapis.com/css?family=Source+Code+Pro") | ||
text("<style>\n") | ||
text("body {\n") | ||
text(" font-family: 'Source Code Pro', monospace;\n") | ||
text("}\n") | ||
text("h3 {\n") | ||
text(" font-size: 12pt;\n") | ||
text("}") | ||
text("</style>\n") | ||
} | ||
body { | ||
if (!acc.isEmpty()) { | ||
|
||
h1 { text("Overview") } | ||
|
||
paragraph { | ||
text("Issues found: $issueCount") | ||
} | ||
|
||
paragraph { | ||
text("Issues corrected: $correctedCount") | ||
} | ||
|
||
acc.forEach { file: String, errors: MutableList<LintError> -> | ||
h3 { text(file) } | ||
ul { | ||
errors.forEach { (line, col, ruleId, detail) -> | ||
item("($line, $col): $detail ($ruleId)") | ||
} | ||
} | ||
} | ||
} else { | ||
paragraph { | ||
text("Congratulations, no issues found!") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun html(body: () -> Unit) { | ||
out.println("<html>") | ||
body() | ||
out.println("</html>") | ||
} | ||
|
||
private fun head(body: () -> Unit) { | ||
out.println("<head>") | ||
body() | ||
out.println("</head>") | ||
} | ||
|
||
private fun body(body: () -> Unit) { | ||
out.println("<body>") | ||
body() | ||
out.println("</body>") | ||
} | ||
|
||
private fun h1(body: () -> Unit) { | ||
out.print("<h1>") | ||
body() | ||
out.println("</h1>") | ||
} | ||
|
||
private fun h3(body: () -> Unit) { | ||
out.print("<h3>") | ||
body() | ||
out.println("</h3>") | ||
} | ||
|
||
private fun text(value: String) { | ||
out.print(value) | ||
} | ||
|
||
private fun ul(body: () -> Unit) { | ||
out.println("<ul>") | ||
body() | ||
out.println("</ul>") | ||
} | ||
|
||
private fun item(value: String) { | ||
out.print("<li>") | ||
text(value) | ||
out.println("</li>") | ||
} | ||
|
||
private fun cssLink(link: String) { | ||
out.print("<link href=\"") | ||
out.print(link) | ||
out.println("\" rel=\"stylesheet\" />") | ||
} | ||
|
||
private fun paragraph(body: () -> Unit) { | ||
out.print("<p>") | ||
body() | ||
out.println("</p>") | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...-reporter-html/src/main/kotlin/com/pinterest/ktlint/reporter/html/HtmlReporterProvider.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,34 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2019 Matheus Candido | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package com.pinterest.ktlint.reporter.html | ||
|
||
import com.pinterest.ktlint.core.Reporter | ||
import com.pinterest.ktlint.core.ReporterProvider | ||
import java.io.PrintStream | ||
|
||
class HtmlReporterProvider : ReporterProvider { | ||
override val id: String = "html" | ||
override fun get(out: PrintStream, opt: Map<String, String>): Reporter = HtmlReporter(out) | ||
} |
1 change: 1 addition & 0 deletions
1
...rter-html/src/main/resources/META-INF/services/com.pinterest.ktlint.core.ReporterProvider
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 @@ | ||
com.pinterest.ktlint.reporter.html.HtmlReporterProvider |
Oops, something went wrong.