Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
Initial Kotlin support (#210)
Browse files Browse the repository at this point in the history
* Initial Kotlin support

* Simplify sentence
  • Loading branch information
fbenz authored Mar 23, 2018
1 parent 154df15 commit f180334
Show file tree
Hide file tree
Showing 19 changed files with 1,059 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<module>spring-auto-restdocs-core</module>
<module>spring-auto-restdocs-example</module>
<module>spring-auto-restdocs-docs</module>
<module>spring-auto-restdocs-dokka-json</module>
</modules>

<organization>
Expand Down Expand Up @@ -126,6 +127,8 @@
<roots>
<root>src/main/java</root>
<root>src/test/java</root>
<root>src/main/kotlin</root>
<root>src/test/kotlin</root>
</roots>
</configuration>
<executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -51,6 +51,11 @@ public FieldDocumentationObjectVisitor(SerializerProvider provider,
this.typeFactory = typeFactory;
}

@Override
public void property(BeanProperty prop) throws JsonMappingException {
optionalProperty(prop);
}

@Override
public void optionalProperty(BeanProperty prop) throws JsonMappingException {
String jsonName = prop.getName();
Expand Down
29 changes: 29 additions & 0 deletions spring-auto-restdocs-dokka-json/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# IntelliJ
.idea/
*.iml

# Genertaed files
target/
42 changes: 42 additions & 0 deletions spring-auto-restdocs-dokka-json/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# KDoc and Javadoc to JSON converter for Spring Auto REST Docs

This library is a [Dokka](https://github.com/Kotlin/dokka) extension.
Dokka is a documentation engine for Kotlin, performing the same function as Javadoc for Java.
Mixed-language Java/Kotlin projects are fully supported.
Dokka understands standard Javadoc comments in Java files and KDoc comments in Kotlin files.
The same holds true for this Dokka extension.

## Usage with Maven

This Dokka extension can be used with the standard `dokka-maven-plugin`.
To avoid any incompatibilities, the Dokka version of the `dokka-maven-plugin` and of this extension should be the same.
If this extension is included as a dependency of the plugin, the output format `auto-restdocs-json` can be used.

Example usage:
```
<plugin>
<groupId>org.jetbrains.dokka</groupId>
<artifactId>dokka-maven-plugin</artifactId>
<version>${dokka.version}</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>dokka</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>capital.scalable</groupId>
<artifactId>spring-auto-restdocs-dokka-json</artifactId>
<version>${spring-auto-restdocs-dokka-json.version}</version>
</dependency>
</dependencies>
<configuration>
<outputFormat>auto-restdocs-json</outputFormat>
<outputDir>${jsonDirectory}</outputDir>
</configuration>
</plugin>
```

96 changes: 96 additions & 0 deletions spring-auto-restdocs-dokka-json/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<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>capital.scalable</groupId>
<artifactId>spring-auto-restdocs-parent</artifactId>
<version>1.0.12-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>

<artifactId>spring-auto-restdocs-dokka-json</artifactId>
<packaging>jar</packaging>

<name>Spring Auto REST Docs Dokka JSON</name>
<description>Dokka extension that produces Spring Auto REST Docs' JSON format</description>
<url>https://github.com/scacap/spring-auto-restdocs</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.2.30</kotlin.version>
<junit.version>4.12</junit.version>
</properties>

<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test-junit</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.dokka</groupId>
<artifactId>dokka-fatjar</artifactId>
<version>0.9.16</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>2.9.4.1</version>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>

<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>jcenter</id>
<url>https://jcenter.bintray.com/</url>
</repository>
</repositories>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*-
* #%L
* Spring Auto REST Docs Dokka JSON
* %%
* Copyright (C) 2015 - 2018 Scalable Capital GmbH
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package capital.scalable.dokka.json

import com.google.inject.Inject
import org.jetbrains.dokka.*
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.OutputStreamWriter

class JsonFileGenerator @Inject constructor(val locationService: FileLocationService) : Generator {

@set:Inject(optional = true)
lateinit var formatService: FormatService

override fun buildPages(nodes: Iterable<DocumentationNode>) {
val specificLocationService = locationService.withExtension(formatService.extension)

for ((_, items) in nodes.groupBy { specificLocationService.location(it) }) {
if (items.any { it.kind == NodeKind.Class }) {
val location = locationOverride(items.find { it.kind == NodeKind.Class }!!)
val file = location.file
file.parentFile?.mkdirsOrFail()
try {
FileOutputStream(file).use {
OutputStreamWriter(it, Charsets.UTF_8).use {
it.write(formatService.format(location, items))
}
}
} catch (e: Throwable) {
println(e)
}
}
buildPages(items.flatMap { it.members })
}
}

private fun locationOverride(node: DocumentationNode): FileLocation {
val path = node.path
// Remove class name. It is appended again below.
.dropLast(1)
.filter { it.name.isNotEmpty() && it.kind != NodeKind.Module }
.joinToString("") {
if (it.kind == NodeKind.Class) {
// Parent class if the node is a nested class
"${it.name}."
} else {
// Turn package dots into folders
"${it.name.replace(".", "/")}/"
}
}
val className = node.name
return FileLocation(File(locationService.root.path, "$path$className.json"))
}

override fun buildOutlines(nodes: Iterable<DocumentationNode>) {}

override fun buildSupportFiles() {}

override fun buildPackageList(nodes: Iterable<DocumentationNode>) {}

private fun File.mkdirsOrFail() {
if (!mkdirs() && !exists()) {
throw IOException("Failed to create directory $this")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*-
* #%L
* Spring Auto REST Docs Dokka JSON
* %%
* Copyright (C) 2015 - 2018 Scalable Capital GmbH
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package capital.scalable.dokka.json

import org.jetbrains.dokka.*
import org.jetbrains.dokka.Formats.FormatDescriptor
import org.jetbrains.dokka.Kotlin.KotlinDescriptorSignatureProvider
import org.jetbrains.dokka.Samples.DefaultSampleProcessingService
import org.jetbrains.dokka.Samples.SampleProcessingService
import kotlin.reflect.KClass

class JsonFormatDescriptor : FormatDescriptor {
override val formatServiceClass = JsonFormatService::class

override val packageDocumentationBuilderClass = KotlinPackageDocumentationBuilder::class
override val javaDocumentationBuilderClass = KotlinJavaDocumentationBuilder::class

override val generatorServiceClass = JsonFileGenerator::class
override val outlineServiceClass: KClass<out OutlineFormatService>? = null
override val sampleProcessingService: KClass<out SampleProcessingService> = DefaultSampleProcessingService::class
override val packageListServiceClass: KClass<out PackageListService>? = DefaultPackageListService::class
override val descriptorSignatureProvider = KotlinDescriptorSignatureProvider::class
}
Loading

0 comments on commit f180334

Please sign in to comment.