Comments (30)
Initial implementation was merged (should be in next 7.3.0
release). Missing:
buildSrc
support- composite builds support
from ktlint-gradle.
That are good questions 👍
Regarding them I am thinking of adding two possible git hooks:
- runs Gradle task
ktlintCheck
before commit - runs Gradle task
ktlintFormat
before commit
This way git hook will be not detached from plugin and plugin configuration.
from ktlint-gradle.
@ubuntudroid It is one of possible issues that I may work for next release.
from ktlint-gradle.
Plugin can provide tasks for both check and format hooks, as implementation should be pretty similar. Then it will be up to user of plugin what to select.
from ktlint-gradle.
@Rubyj I have a sample project where I install the githook every time you run clean
: https://github.com/AdamMc331/PokeDex/blob/master/build.gradle.kts#L54-L57
This is an easy way for new developers who clone your project to get the git hooks. I'm not sure if something like that should be added to the ktlint-gradle plugin (we might not want to make it that opinionated, plus give people a chance to write their own git hook scripts), but it should be quick to implement yourself. :)
The idea came from here: https://proandroiddev.com/ooga-chaka-git-hooks-to-enforce-code-quality-11ce8d0d23cb check the "Gradle as a trojan horse" section.
from ktlint-gradle.
@AdamMc331 @Tapchicoma Thank you!! This should work for my use case
from ktlint-gradle.
Sounds like a sold plan. How are inputs handled? Eg. custom rulesets?
Also, once installed, this would be somewhat out-of-band from our plugin. Are we okay with that?
There are other solutions that exist for installing commit hooks with Gradle.
https://plugins.gradle.org/search?term=git-hook
from ktlint-gradle.
@Tapchicoma any news here? Would be great to have that to make set up as easy as it is for ktlint.
from ktlint-gradle.
@Tapchicoma This would make sense to add as a standalone plugin, similar to ktlint-idea
.
from ktlint-gradle.
@JLLeitschuh not sure if this will be possible if this git hook will call ktlintCheck
or ktlintFormat
tasks from main plugin.
from ktlint-gradle.
If you call ktlint --install-git-pre-commit-hook
what does it do?
I thought that was all this proposed addition would do, (although I'm not 100% certain that it would work in a Gradle environment).
If you're planning on having this Gradle task get executed, I think that might be outside the scope of this plugin.
I think that this goal better achieved with a plugin specific for installing pre-commit hooks.
https://plugins.gradle.org/search?term=git-hook
If you want to add a sample that demonstrates using one of these plugins, that would be an acceptable addition to this repo.
from ktlint-gradle.
ktlint --install-git-pre-coomit-hook
simply creates a file called pre-commit in .git/hooks which contains the code to run the checks.
This hook uses ktlint directly so we would need our own variant of the command as one of the benefits from this gradle plugin is that users don't have to have raw ktlint installed on their machine.
Having a task named installGitPreCommitHook
which installs a hook executing the ktlintCheck
for the user would certainly be super useful.
from ktlint-gradle.
@ubuntudroid I'm just concerned about feature creep. This seems like it's outside the scope of this project when there are other plugins that fill this niche in the ecosystem.
from ktlint-gradle.
Hm, as this project provides a Gradle wrapper around ktlint and as there is such a functionality in ktlint which currently isn't supported/broken when using the Gradle plugin I personally would consider it in scope.
But surely it's up to you to decide in the end as the project lead. :)
from ktlint-gradle.
Did someone manage to get a hook working that displays in Android Studio? On 3.5-canary7 it just shows Commit failed with error
but I can't see the error at all and have to run it from the command line.
from ktlint-gradle.
@PaulWoitaschek no, unfortunately not. That being said it is possible to see the error in the error event popping up after a failed commit, but the error is somewhere in that very long event message and thus super hard to find.
So right now I am doing the same and just run the ktlint gradle task manually on command line after I see that a commit has failed.
from ktlint-gradle.
By passing the quiet flag to gradle the output is short enough that the tooltip shows information on what happened:
./gradlew ktLintCheck -q
It's not pretty and you can't just click on the lines but at least you can see something.
What I actually want is to run the formatter as well:
if ! ./gradlew ktLintCheck -q; then;
./gradlew ktLintFormat -q
./gradlew ktLintCheck -q
fi
This kind of works and formats the file correctly if possible, but the fixed formatting is not in the commit you just made.
I'm not sure if this can be solved with the plugin hooks at all.
from ktlint-gradle.
from ktlint-gradle.
@Tapchicoma any news here?
from ktlint-gradle.
@ljfantin sorry, was busy with other tasks. I have an idea how to do it, will try to implement it this weekend.
from ktlint-gradle.
Just tried this out and the tasks do not show up in my project. Is that because the .git
folder is not on the same level as the gradle project? If yes, is it possible to make that work?
My stucture looks like this:
- .git
- backend
--- build.gradle
--- src
- frontend
--- package.json
from ktlint-gradle.
@rubengees yes, you are right - git hook tasks are added only if plugin was applied to the root project and there is .git
folder.
I don't think that it is worth the effort to support such case - then, imho, too much corner cases plugin should handle.
To solve your particular problem - you can add "fake" .git
folder - backend/.git
, run install git hook task, copy the hook from backend/.git/hooks/pre-commit
to .git/hooks/pre-commit
and update paths in pre-commit
to use backend
folder.
from ktlint-gradle.
I see thanks! Will try that later. An important thing for me though is to share the setup with team members. It should be as simple as possible to get more adoption and less CI failures caused by code style issues 😁. A similar tool we use for our JavaScript frontend achieves this, by simply running one command.
from ktlint-gradle.
@Tapchicoma I tried this by just running .git init
inside the sub folder, resyncing gradle with the idea plugin applied, but still don't see the git tasks. I do see the ktlintApplyToIdea
ones though. Any other thoughts?
@rubengees Did you get a chance to try? Did it work out for you?
from ktlint-gradle.
@AdamMc331 there are two conditions:
- Should be Gradle root project
- Should exits
.git
folder
see
I would say possible logic adjustment could be done:
- always add git hook tasks to Gradle root project
- if there is not
.git/
folder available - fail the task - optionally allow to pass
.git/
folder location via-PktlintGitRoot=
flag
from ktlint-gradle.
Opened more specific git hook enhancement issues and closing this one as initial implementation was added back ago.
If you want to propose some additional git-hook enhancement, please open a separate issue for it.
from ktlint-gradle.
@Tapchicoma Is there any way to have this pre commit hook installed by default? I am trying to eliminated the need for developers to actually run this command to activate the pre-commit hook. I want it to be activated for them.
Or, maybe a way to add this to be in the build step?
from ktlint-gradle.
Or, maybe a way to add this to be in the build step?
IMHO, doesn't make sense to install this githook on every build (though task itself is cacheable).
I am not aware of any way how to install it automatically. I would say you should run ktlint on your CI setup and maybe create a "proxy" task "projectSetup" that will trigger commit hook installation task (probably some more related to setup tasks as well).
from ktlint-gradle.
@AdamMc331 interesting usage of "clean" task. Have you noticed that some developers are not using it?
from ktlint-gradle.
I think it's rare that a developer doesn't run a clean build at some point. On our small team of five people though it was never an issue, but I guess there's potential that someone might not get the git hooks installed right away.
It's very similar to your suggestion of a projectSetup
task except now it's one that's potentially built in to a normal day's workflow - and certainly for someone cloning a project for the first time.
from ktlint-gradle.
Related Issues (20)
- Ktlint Fails to Detect Formatting Issues in Nested Modules HOT 7
- Failing Builds Automatically HOT 1
- Same Issue HOT 6
- Cannot apply to buildSrc HOT 4
- Using this plugin with the official IntelliJ IDE plugin HOT 3
- K2 issue: A failure occurred while executing worker.KtLintWorkAction FILE HOT 2
- Ktlint checks generated source since 12.1.0 HOT 6
- Task KtlintMainSourceSetCheck failed HOT 2
- Task :runKtlintCheckOverMainSourceSet FAILED HOT 3
- import checks HOT 1
- Caches work incorrectly: additionalEditorconfig should be @Input instead of @Internal HOT 3
- Filter to exclude a generated dir is not working in version 12.1.0 HOT 3
- ktlint pre-commit hook HOT 1
- Using ktlint-gradle with ktlint-intellij-plugin and baseline file
- Is there way to know which (maximum) ktlint versions are supported? HOT 2
- Version number in documentation
- deprecate old android flag
- unknown ktlint version HOT 2
- hxgc
- Deprecation warning with `ActionUpdateThread` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ktlint-gradle.