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 do we utilize spotless:off/on with a check that throws? #966

Closed
ghost opened this issue Oct 13, 2021 · 4 comments
Closed

How do we utilize spotless:off/on with a check that throws? #966

ghost opened this issue Oct 13, 2021 · 4 comments
Labels

Comments

@ghost
Copy link

ghost commented Oct 13, 2021

I have a custom rule to enforce all dependency versions are coming from a java-platform or Spring constraint, and I want it to hard-error instead of trimming off the version string.

    custom 'Drop version portion of dependency', {
      def match
      if ((match = it =~ /(\s*\S*(?:api|[cC]ompileOnly|[rR]untimeOnly|[iI]mplementation|[tT]est))\('([\w.-]+):([\w.-]+):([\w.-]+)'\)/)) {
        throw new AssertionError("Error in: ${target} ${match.group(0)}.\nDo not declare raw dependency versions in project Gradle files. 'spotlessApply' cannot resolve this issue.\n")
      }   

How can I allow a developer to temporarily play with a custom version override using spotless:off/spotless:on with a rule like this that does an AssertionError?

@nedtwigg
Copy link
Member

I think that will be very tricky to do using spotless:off, see #691 for how it works under the hood.

Much easier I think would be for your rule to look for // #allowRawDeps, in which case it disables itself. Your error message can say "Disable this temporarily with // #allowRawDeps".

@ghost
Copy link
Author

ghost commented Oct 13, 2021

I'll give that a try and report back.

@ghost
Copy link
Author

ghost commented Oct 14, 2021

I'm trying this custom rule:

custom 'Do not use raw dependency versions. Prefer constraints.', { String fileAsStr ->
      fileAsStr.eachLine {thisLine , count ->
        def match
        if ((match = thisLine =~ /(\s*\S*(?:api|[cC]ompileOnly|[rR]untimeOnly|[iI]mplementation|[tT]est))\('([\w.-]+):([\w.-]+):([\w.-]+)'\).*$/)) {
          if (!match.group(0).contains("spotless:ignore")) {
            throw new AssertionError("Error in ${target} line ${count+1}:\n${match.group(0)}\nDo not declare raw dependency versions in project Gradle files. 'spotlessApply' cannot resolve this issue.\n")
          }
        }
      }
      fileAsStr
    }

Testing against a strings like

distributedTestRuntimeOnly('org.springframework:spring-oxm:5.3.9') // spotless:ignore   <-- OK
distributedTestRuntimeOnly('junit:junit:4.3.2') <-- errors

@nedtwigg
Copy link
Member

Looks like this worked. Feel free to comment back if there's more to share :)

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

No branches or pull requests

1 participant