Coder Social home page Coder Social logo

Comments (30)

Tapchicoma avatar Tapchicoma commented on June 17, 2024 5

Initial implementation was merged (should be in next 7.3.0 release). Missing:

  • buildSrc support
  • composite builds support

from ktlint-gradle.

Tapchicoma avatar Tapchicoma commented on June 17, 2024 4

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.

Tapchicoma avatar Tapchicoma commented on June 17, 2024 1

@ubuntudroid It is one of possible issues that I may work for next release.

from ktlint-gradle.

Tapchicoma avatar Tapchicoma commented on June 17, 2024 1

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.

AdamMc331 avatar AdamMc331 commented on June 17, 2024 1

@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.

Rubyj avatar Rubyj commented on June 17, 2024 1

@AdamMc331 @Tapchicoma Thank you!! This should work for my use case

from ktlint-gradle.

JLLeitschuh avatar JLLeitschuh commented on June 17, 2024

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.

ubuntudroid avatar ubuntudroid commented on June 17, 2024

@Tapchicoma any news here? Would be great to have that to make set up as easy as it is for ktlint.

from ktlint-gradle.

JLLeitschuh avatar JLLeitschuh commented on June 17, 2024

@Tapchicoma This would make sense to add as a standalone plugin, similar to ktlint-idea.

from ktlint-gradle.

Tapchicoma avatar Tapchicoma commented on June 17, 2024

@JLLeitschuh not sure if this will be possible if this git hook will call ktlintCheck or ktlintFormat tasks from main plugin.

from ktlint-gradle.

JLLeitschuh avatar JLLeitschuh commented on June 17, 2024

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.

ubuntudroid avatar ubuntudroid commented on June 17, 2024

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.

JLLeitschuh avatar JLLeitschuh commented on June 17, 2024

@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.

ubuntudroid avatar ubuntudroid commented on June 17, 2024

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.

PaulWoitaschek avatar PaulWoitaschek commented on June 17, 2024

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.

ubuntudroid avatar ubuntudroid commented on June 17, 2024

@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.

PaulWoitaschek avatar PaulWoitaschek commented on June 17, 2024

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.

ubuntudroid avatar ubuntudroid commented on June 17, 2024

from ktlint-gradle.

ljfantin avatar ljfantin commented on June 17, 2024

@Tapchicoma any news here?

from ktlint-gradle.

Tapchicoma avatar Tapchicoma commented on June 17, 2024

@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.

rubengees avatar rubengees commented on June 17, 2024

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.

Tapchicoma avatar Tapchicoma commented on June 17, 2024

@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.

rubengees avatar rubengees commented on June 17, 2024

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.

AdamMc331 avatar AdamMc331 commented on June 17, 2024

@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.

Tapchicoma avatar Tapchicoma commented on June 17, 2024

@AdamMc331 there are two conditions:

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.

Tapchicoma avatar Tapchicoma commented on June 17, 2024

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.

Rubyj avatar Rubyj commented on June 17, 2024

@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.

Tapchicoma avatar Tapchicoma commented on June 17, 2024

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.

Tapchicoma avatar Tapchicoma commented on June 17, 2024

@AdamMc331 interesting usage of "clean" task. Have you noticed that some developers are not using it?

from ktlint-gradle.

AdamMc331 avatar AdamMc331 commented on June 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.