Skip to content

Commit

Permalink
add html reporter module to ktlint library (#641)
Browse files Browse the repository at this point in the history
* add html reporter module to ktlint library

* reorder imports on HtmlReporterTest file
  • Loading branch information
arrmixer authored and shashachu committed Nov 20, 2019
1 parent f034735 commit bc08930
Show file tree
Hide file tree
Showing 10 changed files with 443 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ktlint-reporter-html/build.gradle
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
}
4 changes: 4 additions & 0 deletions ktlint-reporter-html/gradle.properties
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
94 changes: 94 additions & 0 deletions ktlint-reporter-html/pom.xml
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>
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>")
}
}
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)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.pinterest.ktlint.reporter.html.HtmlReporterProvider
Loading

0 comments on commit bc08930

Please sign in to comment.