-
Notifications
You must be signed in to change notification settings - Fork 461
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
Support multiple license headers #872
Comments
Related: #650 (disable license rule for specific file) It's already possible (under the hood) to compose a step with a filename-specific filter, e.g. this little snippet which keeps the licenseHeader step from clobbering spotless/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JavaExtension.java Line 159 in 0842bfa
It would be pretty easy to use this mechanism to expose something like this in the Gradle DSL: licenseHeader('foo').skipIfPathEndsWith('src/main/thirdparty/**/*.java')
licenseHeader('bar').onlyIfPathEndsWith('src/main/thirdparty/**/*.java') The problem is that spotless/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java Lines 282 to 285 in 0842bfa
Two different solutions come to mind. Happy to take a PR for either. Named license headerOne solution is something like this: licenseHeader('foo').named('licenseFoo').skipIfPathEndsWith('src/main/thirdparty/**/*.java')
licenseHeader('bar').named('licenseBar').onlyIfPathEndsWith('src/main/thirdparty/**/*.java') This would be pretty easy to implement. If you left off the "named()", then you'd get the "Multiple steps with name blah" error. SubformatsA broader solution would be to create the concept of a subformat, which allows you to create a step which is actually a bunch of substeps, e.g. googleJavaFormat()
licenseHeader('foo')
subformat 'thirdparty', {
onlyIfPathEndsWith 'src/main/java/thirdparty/**/*.java'
licenseHeader('lgpl')
} or googleJavaFormat()
licenseHeader('lgpl')
subformat 'thirdparty', {
skipIfPathEndsWith 'src/main/java/thirdparty/**/*.java'
licenseHeader('foo')
} or googleJavaFormat()
subformat 'thirdparty', {
onlyIfPathEndsWith 'src/main/java/thirdparty/**/*.java'
licenseHeader('lgpl')
}
subformat 'ourCode', {
onlyIfPathEndsWith 'src/main/java/com/acme/**/*.java'
licenseHeader('foo')
} The nice thing about subformats is that they could apply to any step, not just licenses. Subformat should be backed by a new spotless/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java Line 206 in 0842bfa
|
We don't have things split by folders though, I want spotless to detect which license it's working with, and update that one (or let either pass licenseHeader{
default("/* Copyright ...")
accept("/* Copyright 2 ...")
accept("/* Copyright 3 ...")
} Also we're using the maven plugin but that should be mostly transferable. |
Interesting! I guess both ideas could be extended to filter based on content rather than path: licenseHeader('foo').named('licenseFoo').onlyIfContentHas('/* Foo')
licenseHeader('bar').named('licenseBar').onlyIfContentHas('/* Bar') subformat 'ourCode', {
onlyIfContentHas '/* Copyright'
licenseHeader('foo')
} Probably should also have |
The key thing is single-responsibility principle. I think We've had previous requests for license-per-path, and your usecase of license-per-content is also valid. I'm open to your DSL idea, but it needs to handle the per-path and per-content cases, and they shouldn't require |
I like the subformat idea with path AND content-based filters! Such a feature could also be used to avoid overwriting a license header from a third-party file that is integrated into the codebase, untouched. |
Published in |
We have a situation where we need to support multiple license headers (not in the same file, but some files will have one, some the other), because some commiters have different requirements from their companies. Spotless doesn't currently have any support for this that I could find, but it would be nice if it was able to accept multiple licenses as OK, and probably designate one as the default, to add to new files without a license header.
The text was updated successfully, but these errors were encountered: