skuzzle / restrict-imports-enforcer-rule

Gradle plugin & Maven Enforcer rule that restricts usage of unwanted imports in Java, Kotlin and Groovy source files.
MIT License
75 stars 12 forks source link

Allow multiple banned-import-groups within a single enforcer rule #9

Closed skuzzle closed 6 years ago

skuzzle commented 7 years ago

This would allow influences of rules to other rules. My favorite:

...
<bannedImportGroups>
    <bannedImportGroup>
        <basePackage>com.foo.**</basePackage>
        <allowedImports>...</allowedImports>
    </bannedImportGroup>
    <bannedImportGroup>
        <basePackage>com.foo.bar.**</basePackage>
        <allowedImports>...</allowedImports>
    </bannedImportGroup>
</bannedImportGroups>
...

In this sample, source files from com.foo.bar would only be subject to the rules within the second group.

Another approach could be that all groups are applied to a single source file and groups with a more specific basePackage can override rules from groups with less specific basePackage.

skuzzle commented 6 years ago

This will be implemented as follows:

  1. Multiple banned import groups can be specified per EnforcerRule definition
  2. When analyzing a source file, the group with the most specific base package pattern matching the source file's package will be chosen
  3. Specifity of package pattern can roughly be described as follows 3.1. p1 is more specific than p2 if p2 contains more ** parts 3.2. p1 is more specific than p2 if p2 contains more * parts 3.3. If both patterns contain equal amount of ** and * parts, the pattern with the most specific last part is more specific than the other
skuzzle commented 6 years ago

Released as 0.13.0