An open API service indexing awesome lists of open source software.

https://github.com/kotools/samples

Gradle plugin that inlines read-only Kotlin and Java code samples into Dokka documentation, ensuring they are always correct and visible not only online but also in IDEs.
https://github.com/kotools/samples

dokka gradle java kotlin

Last synced: 3 months ago
JSON representation

Gradle plugin that inlines read-only Kotlin and Java code samples into Dokka documentation, ensuring they are always correct and visible not only online but also in IDEs.

Awesome Lists containing this project

README

          

# Kotools Samples

[![Latest][kotools-samples-badge]][kotools-samples-releases]
[![Gradle][gradle-badge]][gradle]
[![Kotlin][kotlin-badge]][kotlin]
[![Java][java-badge]][java]
[![Dokka][dokka-badge]][dokka]

**Kotools Samples** is a [Gradle] plugin designed to help [Kotlin] library
authors integrate **read-only samples** into their documentation. It addresses a
limitation in [Dokka], which does not allow making code samples **non-editable**
or **non-executable**.

## โญ๏ธ Key Features

- **Readonly inlined code samples:** Displays your examples inlined in the
documentation, ensuring they are non-editable and non-executable, preventing
unwanted modification or execution.
- **Sample visibility in IDE:** Unlike many libraries (such as Jetpack Compose),
Kotools Samples makes your examples visible within the IDE (e.g.,
IntelliJ IDEA, Android Studio), enhancing accessibility and usability during
development.
- **Always correct samples:** Ensures your examples are always up-to-date by
compiling them alongside your main and test sources. Any breaking changes in
your codebase will trigger compilation errors, prompting you to update the
examples.
- **Kotlin/JVM support:** Fully supports the Kotlin/JVM platform, with [Kotlin]
Multiplatform support in future releases.
- **Seamless integration:** Works effortlessly with [Kotlin] and [Dokka],
smoothly integrating into your Gradle build process.

## ๐Ÿ› ๏ธ Installation

For adding Kotools Samples to your Kotlin/JVM project, it is recommended to use
the [Gradle] plugins DSL in [Kotlin]. Just replace the `$version` variable by
the [latest version](#kotools-samples) or by another one available in the
[changelog](CHANGELOG.md).

```kotlin
plugins {
id("org.kotools.samples") version "$version"
}
```

See [this plugin on the Gradle Plugin Portal][kotools-samples-plugin] for more
installation options.

> The `org.kotools.samples.jvm` plugin is deprecated (see
> [#42](https://github.com/kotools/samples/issues/42)).

## ๐Ÿง‘โ€๐Ÿ’ป Usage example

Kotools Samples ensures that your code samples are integrated into your
documentation without affecting your main sources. For doing so, it creates a
source set named `sample` dedicated for code samples.

Here's a [Kotlin] sample:

```kotlin
// File location: src/sample/kotlin/IntSample.kt

import kotlin.test.Test

class IntSample {
@Test
fun addition() {
val x = 1
val y = 2
check(x + y == 3)
}
}
```

Reference this sample in your [Dokka] documentation:

```kotlin
// File location: src/main/kotlin/Int.kt

/**
* Performs an addition with [x] and [y] integers (`x + y`).
*
* SAMPLE: [IntSample.addition]
*/
public fun addition(x: Int, y: Int): Int = x + y
```

Here's the documentation generated by [Dokka] with Kotools Samples:

![Screenshot](projects/demonstration/screenshot.png)

## โœ… Supported constructs

Kotools Samples offers powerful features for writing expressive samples and
integrating them into the documentation.

### ๐Ÿงช Sample sources

This Gradle plugin is able to extract the body of member functions, which can be
an expression body, even from multiple classes located in the same file. In case
of empty body, it extracts a placeholder indicating that the sample is not yet
implemented.

Top-level functions being incompatible with the `@Test` annotation from Kotlin,
they are not supported. This is for ensuring that all samples are testable.

```kotlin
package samples

import kotlin.test.Test

class FirstSample {
@Test
fun body() {
val x = 1
val y = 2
check(x + y == 3)
}

@Test
fun expressionBody(): Unit = check(1 + 2 == 3)
}

class SecondSample { // Multiple classes in single file is supported.
@Test
fun empty() {} // Placeholder: TODO("Sample is not yet implemented.")
}
```

### ๐ŸŽฏ Main sources

For integrating samples into the documentation, this plugin supports KDoc
comments with one or multiple lines, containing one or more sample references.

```kotlin
/** SAMPLE: [samples.FirstSample.body] */
fun singleLineKDoc() = Unit

/**
* SAMPLE: [samples.FirstSample.body]
*/
fun multiLineKDoc() = Unit

/**
* SAMPLE: [samples.FirstSample.body]
* SAMPLE: [samples.FirstSample.expressionBody]
*/
fun multipleSamples() = Unit
```

## ๐Ÿ“ Documentation

Here's additional documentation for learning more about this project:

- [Dependency compatibility](documentation/dependencies.md)
- [Security Policy](SECURITY.md)

## ๐Ÿค Community

Join our thriving community! Connect, share insights, and collaborate with
fellow developers to make Kotools Samples even more powerful.

- [#kotools on Kotlin Slack](https://kotlinlang.slack.com/archives/C05H0L1LD25)

## ๐Ÿ“ฃ Show Your Support

If you find this project valuable, show your support by giving us a โญ๏ธ on
GitHub. Your feedback and engagement mean the world to us!

## ๐Ÿšง Contributing

Contributions are welcome! Feel free to submit bug reports, feature requests, or
pull requests to improve the plugin.

## ๐Ÿ™ Acknowledgements

Thanks to [Loรฏc Lamarque][@LVMVRQUXL] for creating and sharing this project with
the open source community.

Thanks to all the [people that ever contributed][kotools-samples-contributors]
through code or other means such as bug reports, feature suggestions and so on.

## ๐Ÿ“„ License

This project is licensed under the [MIT License](LICENSE.txt).

[@LVMVRQUXL]: https://github.com/LVMVRQUXL
[dokka]: https://kotl.in/dokka
[dokka-badge]: https://img.shields.io/badge/Dokka-v2.0.0-blue
[gradle]: https://gradle.org
[gradle-badge]: https://img.shields.io/badge/Gradle-v8.12.1-blue?logo=gradle
[java]: https://www.java.com
[java-badge]: https://img.shields.io/badge/Java-v17-blue
[kotlin]: https://kotlinlang.org
[kotlin-badge]: https://img.shields.io/badge/Kotlin-v2.0.21-blue?logo=kotlin
[kotools-samples-badge]: https://img.shields.io/github/v/release/kotools/samples?label=Latest
[kotools-samples-contributors]: https://github.com/kotools/samples/graphs/contributors
[kotools-samples-plugin]: https://plugins.gradle.org/plugin/org.kotools.samples
[kotools-samples-releases]: https://github.com/kotools/samples/releases