spotless icon indicating copy to clipboard operation
spotless copied to clipboard

Support gradle-node-plugin for prettier

Open breucode opened this issue 5 years ago • 6 comments

It would be great, if spotless supported https://github.com/node-gradle/gradle-node-plugin to download npm for prettier. This would make it optional to install npm.

breucode avatar Nov 03 '20 13:11 breucode

I second this wholeheartedly. It really helps to keep things consistent because you can reuse the same npm for multiple needs.

A workaround would be to allow a lazy setup (at execution time) of npm binary. Then the wiring to gradle-node-plugin can be done manually. I've tried a custom npm-setup task and made spotlessInternalRegisterDependencies depend on it but I have no idea how configuration of spotless steps could be altered at execution time (I don't think I can set the npm path then, can I?).

dweiss avatar Dec 16 '20 13:12 dweiss

I don't think I can set the npm path then, can I?

You can set the npm path (docs, code). I think it has to be set at configuration-time, not execution time.

Happy to take a PR to improve this situation.

nedtwigg avatar Dec 16 '20 23:12 nedtwigg

Sorry, I wasn't clear - I know npm path can be set at configuration time. I don't think it can be set afterward, at execution time. This is the difference that would make it possible to run with gradle-node-plugin because then you wire up a dependency between spotless and gradle-node-plugin's npminstall, add a doFirst clause to spotless task and set up the npm path pointing at the right location.

My experiment shows this is currently impossible because npm path resolution takes place eagerly at configuration time.

dweiss avatar Dec 17 '20 08:12 dweiss

doFirst runs after @TaskAction, which is surprising. It's only "first" compared to doLast actions, unfortunately. But agreed, there would be more flexibility if you could set the exe at execution time, and I'm happy to merge a PR that does that.

FYI, I use this tiny little buildsrc plugin which lazily installs node at configuration time, to workaround this and other similar issues.

nedtwigg avatar Dec 17 '20 19:12 nedtwigg

Really? Well, even if then you could just add a task dependency in between to set the exe path - you get the gist. I didn't think about using buildsrc to set up node beforehand, good tip. May look into this, eventually. I'd love to provide a patch but I'm swamped in work already, eh.

dweiss avatar Dec 17 '20 19:12 dweiss

Thank you for the great plugin - it's really useful!

Just to share, we're facing this same challenge and have a working setup - but some minor caveats.

This works for us:

plugins {
    ...
    id 'com.diffplug.spotless' version '5.14.2'
    id "com.github.node-gradle.node" version "2.2.4"
}

spotless {
    java {
        prettier(...)
                .npmExecutable("$node.variant.npmExec")
                .config(...)
    }
}

spotlessInternalRegisterDependencies.dependsOn(nodeSetup)

The caveats are:

  • spotlessInternalRegisterDependencies.dependsOn(nodeSetup) seems to be the only reliable way to make all potential spotless invocations work. The Internal in the name makes this uncomfortable, so I'd be keen to know if there's a better way to specify this task dependency.
  • (Not relevant to spotless, but maybe of interest) node-gradle recently removed the property which exposes the path to npm, so we're locked to a specific version of that plugin at present.

rnorth avatar Aug 10 '21 14:08 rnorth

Thanks to a series of great PRs from @simschla, this is now possible in plugin-gradle 6.14.0. See this or this example.

nedtwigg avatar Jan 26 '23 22:01 nedtwigg