• Stars
    star
    130
  • Rank 268,442 (Top 6 %)
  • Language
    Kotlin
  • License
    MIT License
  • Created over 3 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Gradle plugin to apply ktfmt to your builds, and reformat you Kotlin source code like a glimpse ๐Ÿงน๐Ÿ˜

ktfmt-gradle ๐Ÿงน๐Ÿ˜

A wrapper to apply ktfmt to your Gradle builds, and reformat you Kotlin source code like a glimpse.

Plugin Portal Pre Merge Checks Language License Use this template Twitter

How to use ๐Ÿ‘ฃ

ktfmt-gradle is distributed through Gradle Plugin Portal. To use it you need to add the following dependency to your gradle files.

Please note that those code needs to be added the gradle file of the module where you want to reformat the code (not the top level build.gradle[.kts] file).

If you're using the plugin{} blocks in your Gradle file:

plugins {
    id("com.ncorti.ktfmt.gradle") version "<latest_version>"
}

If you're instead using the Groovy Gradle files and the old buildscript block:

buildscript {
    repositories {
        maven { url "https://plugins.gradle.org/m2/" }
    }

    dependencies {
        classpath "com.ncorti.ktfmt.gradle:plugin:<latest_version>"
    }
}

apply plugin: "com.ncorti.ktfmt.gradle"

Requirements

Please note that ktfmt-gradle relies on ktfmt hence the minimum supported JDK version is 11.

Please also note the following requirements:

  • Kotlin 1.4+. In order to reformat Kotlin 1.4 code, you need run on Gradle to 6.8+ (This is due to Gradle 6.7 embedding Kotlin 1.3.x - See #12660).

  • Android. ktfmt-gradle relies on features from Android Gradle Plugin 4.1+. So make sure you bump AGP before applying this plugin.

Task

By default, ktfmt-gradle will add two Gradle tasks to your build:

  • ktfmtCheck that will check if the code in your module is ktfmt-compliant
  • ktfmtFormat that will reformat your code with ktfmt

Those two tasks will invoke ktfmt on the whole module. More specific tasks are avialable based on the module type.

Jvm/Js Modules

For Jvm/Js modules, the plugin will create a check/format task for every source set. For example, jvm modules will have a ktfmtCheckMain and ktfmtCheckTest tasks for the main and test source sets.

Multiplatform Modules

Kotlin Multiplatform modules will have separate tasks for every target/source set. You will have tasks like ktfmtCheckCommonMain and ktfmtCheckCommonTest and so on. If you target also Android, the tasks explained below will be added as well.

Android Modules

Kotlin Android modules will also have separate tasks for every source set. Due to how source sets are handled on Android, you can expect ktfmt tasks to follow the convention: ktfmt[Check|Format][SourceSet][Variant]JavaSource. For example, the ktfmtCheckAndroidTestDebugJavaSource.

Features ๐ŸŽจ

  • 100% Kotlin-only plugin.
  • Parallel file processing with Kotlin Coroutines.
  • Supports incremental builds (i.e. checks tasks won't rerun if source is unchanged).
  • Configurable thanks to the ktfmt{} block.
  • Integrated with Jvm/Android/KMM modules.

Configuring ๐Ÿ› 

You can configure the behavior of the ktfmt invocation with the ktfmt block in your build.gradle.[kts] file.

To enable different styles you can simply:

ktfmt {
    // Dropbox style - 4 space indentation
    dropboxStyle()
    
    // Google style - 2 space indentation
    googleStyle()
    
    // KotlinLang style - 4 space indentation - From kotlinlang.org/docs/coding-conventions.html
    kotlinLangStyle()
}

If you wish to have further control on the tool you can instead:

ktfmt {
    // Breaks lines longer than maxWidth. Default 100.
    maxWidth.set(80)
    // blockIndent is the indent size used when a new block is opened, in spaces.
    blockIndent.set(8)
    // continuationIndent is the indent size used when a line is broken because it's too
    continuationIndent.set(8)
    // Whether ktfmt should remove imports that are not used.
    removeUnusedImports.set(false)
}

Using with a pre-commit hook ๐ŸŽฃ

You can leverage the --include-only to let ktfmt-gradle run only on a specific subset of files.

To this you can register a simple task of type KtfmtCheckTask or KtfmtFormatTask in your build.gradle.kts as follows:

import com.ncorti.ktfmt.gradle.tasks.*

tasks.register<KtfmtFormatTask>("ktfmtPrecommit") {
    source = project.fileTree(rootDir)
    include("**/*.kt")
}

You can then invoke the task with --include-only and a comma separated list of relative path of files:

./gradlew ktfmtPrecommit --include-only=src/main/java/File1.kt:src/main/java/File2.kt

The task will execute only on the file you passed and will skip all the others.

Contributing ๐Ÿค

Feel free to open a issue or submit a pull request for any bugs/improvements.

License ๐Ÿ“„

This project is licensed under the MIT License - see the License file for details

More Repositories

1

kotlin-android-template

Android + Kotlin + Github Actions + ktlint + Detekt + Gradle Kotlin DSL + buildSrc = โค๏ธ
Kotlin
1,675
star
2

slidetoact

A simple 'Slide to Unlock' Material widget for Android, written in Kotlin ๐Ÿ“ฑ๐ŸŽจ๐Ÿฆ„
Kotlin
1,150
star
3

kotlin-gradle-plugin-template

๐Ÿ˜ A template to let you started with custom Gradle Plugins + Kotlin in a few seconds
Kotlin
440
star
4

kscript-template

๐Ÿ“œ Get started with kscript in a few seconds... with colored logs, github actions and more!
Kotlin
34
star
5

myonnaise

๐Ÿฏ A RxJava library to access Raw EMG data from your Myo ๐Ÿ“ˆ (plus an Android companion App ๐Ÿ“ฑ)
Kotlin
29
star
6

tech-conferences-italy

A community-curated list of conferences around Italy ๐Ÿ‡ฎ๐Ÿ‡น
HTML
26
star
7

rules4android

A collection of JUnit 4 Rules for Android Developers ๐Ÿ”ฌ
Kotlin
25
star
8

thebakery

Website for The Developers' Bakery Podcast ๐Ÿ‘จโ€๐Ÿณ
HTML
22
star
9

alfred-looks

โ•ฐ(โ—•ใƒฎโ—•)ใคยค=[]โ€”โ€”โ€” A collection of Alfred 4 snippets from looks.wtf (โŒโ– _โ– )
Kotlin
9
star
10

telebot

A Telegram Bot skeleton written in Go (a.k.a. your bot in 20 lines)
Go
7
star
11

cortinico.github.io

Just my personal website โœˆ๏ธ
JavaScript
6
star
12

mno-notes

Public repository of MNO students' notes (University of Pisa)
TeX
5
star
13

space-unlimited-space

๐Ÿ“ฆ Unleash the power of Github Container Registry to store artifacts and consume them auth-free.
5
star
14

docker-redex

Docker file for Facebook ReDex container (zip your apks inside a container)
M4
4
star
15

adventofcode-2020

๐ŸŽ… Marry XMas ๐ŸŽ„ Kotlin solutions for my Advent of Code 2020 ๐Ÿคถ
Kotlin
4
star
16

gulp-letsencrypt

Slides of letsencrypt seminar - 2016 GULP (Pisa Linux User Group)
TeX
4
star
17

adventofcode-2021

๐ŸŽ… Marry XMas ๐ŸŽ„ Kotlin solutions for my Advent of Code 2021 ๐Ÿคถ
Kotlin
4
star
18

docker-java8-32bit

Dockerfile for creating a 32bit containter and Java8 JRE
3
star
19

reproducer-grammarly-textinput

Reproducer for ANR on Samsung devices with Grammarly integration
Kotlin
3
star
20

p2p-gossipico

A simulation of Gossipico with Peersim - Final assignment of P2P course (unipi)
TeX
3
star
21

p2p-pingpong

A simulation of Gnutella Ping Pong protocol - 1st assignment of P2P course (unipi)
Java
2
star
22

cortinico

2
star
23

sol-cars

A simple system for car sharing - Final project of operting system course SOL (unipi)
C
2
star
24

android-beginners-material

A list of material for wannabe Android developers
2
star
25

telebotgae

A Telegram Bot skeleton written in Go with App Engine super powers (deploy in several seconds!)
Go
2
star
26

repro-36296

Reproducer for https://github.com/facebook/react-native/issues/36296
Java
2
star
27

RnReproducer32595

Java
1
star
28

reproducer-rnscreens-bridgeless

Kotlin
1
star
29

gulp-raspi

Slides del seminario del Linux Day 2014 Pisa su Raspberry Pi
TeX
1
star
30

reproducer-strictmodeviolation

Reproducer for https://issuetracker.google.com/issues/270704908
Kotlin
1
star
31

adventofcode-2022

๐ŸŽ… Marry XMas ๐ŸŽ„ Kotlin solutions for my Advent of Code 2022 ๐Ÿคถ
Kotlin
1
star
32

ae-drisc

A DRISC intrepreter written in OCaml - Course of System Architecture AE (unipi)
OCaml
1
star
33

adventofcode-2023

๐ŸŽ… Marry XMas ๐ŸŽ„ Kotlin solutions for my Advent of Code 2023 ๐Ÿคถ
Kotlin
1
star