Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to run formatKotlin on a specific file/files? #207

Open
rinatsolmano opened this issue Jun 14, 2021 · 3 comments
Open

How to run formatKotlin on a specific file/files? #207

rinatsolmano opened this issue Jun 14, 2021 · 3 comments

Comments

@rinatsolmano
Copy link

Hi,
I'm wondering if it possible to run

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

or something like that.

Thanks.

@mateuszkwiecinski
Copy link
Contributor

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?

@rinatsolmano
Copy link
Author

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.

@ThomasRichtsfeld
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants