kotlinter-gradle icon indicating copy to clipboard operation
kotlinter-gradle copied to clipboard

How to run formatKotlin on a specific file/files?

Open rinatsolmano opened this issue 3 years ago • 3 comments

Hi, I'm wondering if it possible to run

./gradlew formatKotlin -Pfiles = {path to file}

or something like that.

Thanks.

rinatsolmano avatar Jun 14 '21 09:06 rinatsolmano

I can think of:

tasks.register("sampleTask", org.jmailen.gradle.kotlinter.tasks.FormatTask) {
    source project.property("files")
}

which would allow you to call ./gradlew sampleTask -Pfiles={path relative to project directory}

Can you describe your use case?

mateuszkwiecinski avatar Jun 14 '21 10:06 mateuszkwiecinski

Yeh, use very similar solution in our project. That's only for the formatting in the pre commit hook.

#!/bin/sh
set -e

echo 1>&2 "running formatKotlin"

filesToFormat="$(git --no-pager diff --name-status --no-color --cached | awk '$1 != "D" && $2 ~ /\.kts|\.kt/ { print $2}')"
echo "files to format $filesToFormat"

for sourceFilePath in $filesToFormat
do
  ./gradlew formatKotlinFile -Pfile="$(pwd)/$sourceFilePath"
  git add "$sourceFilePath"
done;

I know it wont work perfect for the partial commit, but it's fine for us now.

Hope you'll adjust your own pre commit hooks and I switch on it.

rinatsolmano avatar Jun 14 '21 11:06 rinatsolmano

We have exactly the same use case. Any updates or workarounds on this specific use case so far?

ThomasRichtsfeld avatar Feb 07 '23 06:02 ThomasRichtsfeld