https://kotlinlang.org logo
#detekt
Title
# detekt
s

sanogueralorenzo

09/09/2020, 12:12 PM
Hello 👋 I was trying to write a pre-commit hook that runs only on changed files (so it takes a second instead of 15 running everything) and I was facing some issues: Some notes: I'm using detekt-cli jar (so I can use the
-input
only on changed files) and also to avoid the gradle overhead/plugin
Copy code
# Get kotlin changed files
changedFiles=$(git --no-pager diff --diff-filter=d --name-only HEAD | grep '\.kt[s"]\?$')

if [ -n "$changedFiles" ]; then
  # `paste -sd "," -` replaces newlines with commas so detekt cli -i parameter can take it
  java -jar config/detekt/detekt-cli-1.11.2-all.jar --build-upon-default-config --config "config/detekt/detekt.yml" --baseline "config/detekt/baseline.xml" -p config/detekt/detekt-formatting-1.11.2.jar --auto-correct -i "$( printf "%s" "$changedFiles" | paste -sd "," - )"

  if [ $? -ne 0 ]; then
    echo "***********************************************"
    echo "                 Detekt failed                 "
    echo " Please fix the above issues before committing "
    echo "***********************************************"
    exit 1
  fi
fi
The above takes around 2 seconds and it is working properly from the tests I've been doing My main issue is that from
java -jar
to
--auto-correct
is something I already have in my top level detekt task and my top level detekt baseline task (and I wouldn't want to have all that script line repeated in several files since you will have to remember to update all of them when updating version. Is there any way to reuse that part? Thanks! 🙏
👋 1
b

Brais Gabin

09/09/2020, 12:22 PM
The cli and the Gradle plugin are two different clients with two different apis so I don’t think that you could reuse them.
s

sanogueralorenzo

09/09/2020, 12:25 PM
Sorry for the confusion, I'm not using the gradle plugin at all just the cli
I want to be able to extract
java -jar config/detekt/detekt-cli-1.11.2-all.jar --build-upon-default-config --config "config/detekt/detekt.yml" --baseline "config/detekt/baseline.xml" -p config/detekt/detekt-formatting-1.11.2.jar --auto-correct
into a unique script that can be reused by other scripts (by appending some parameters at the end depending on the script)
b

Brais Gabin

09/09/2020, 1:02 PM
Oh! I think that this issue will help you: https://github.com/detekt/detekt/issues/2318
🙏 1
43 Views