spotless icon indicating copy to clipboard operation
spotless copied to clipboard

Add support for a file name field in license headers

Open OxygenCobalt opened this issue 2 years ago • 7 comments

It would be useful if there was a field in the license header template called $FILE, $file.fileName, or similar that would be replaced with the name of the file the header is being applied to.

For example, I could write something like:

/*
 * Copyright (c) 2021 OxygenCobalt
 * $file.fileName is part of Foo.
 */

And it would end up resolving to:

/*
 * Copyright (c) 2021 OxygenCobalt
 * Bar.kt is part of Foo.
 */

I'm mostly drawing this idea from IntelliJ's Copyright Notice format.

OxygenCobalt avatar Mar 13 '22 23:03 OxygenCobalt

Happy to take a PR for this.

nedtwigg avatar Mar 13 '22 23:03 nedtwigg

Well, I'm not too familiar with spotless's codebase, but I can try to take a crack at it.

OxygenCobalt avatar Mar 14 '22 02:03 OxygenCobalt

I stumbled upon this issue when I was attempting to setup Spotless for my Android project whose copyright header template is:

/*
 * $file.fileName
 * Module: $module.name
 * Project: $project.name
 * Copyright © $year, My Company. All rights reserved.
 */

I did upvote this. AFAIK, the Spotless license header check is language/platform/IDE agnostic and most of these variables are injected by Android Studio. So not sure whether this is a legible feature request.

Kiran-B avatar Mar 14 '22 03:03 Kiran-B

Just wondering, as a stop-gap solution, whether we could put support for Regex in licenseHeaderFile? Ex:

/*
 * "\([A-z]*.kt)\"
 * Module: "\([A-z\.]*)\"
 * Project: "\([A-z]*)\"
 * Copyright © $year, My Company. All rights reserved.
 */

Kiran-B avatar Mar 14 '22 03:03 Kiran-B

For substitutions that have constant values (e.g. Module and Project) you can do this already via replaceRegex.

To inject something dynamic like the filename or author, you'll have to create a custom step, and it's pretty straightforward to modify the LicenseStep to do this.

nedtwigg avatar Mar 14 '22 06:03 nedtwigg

Thanks, @nedtwigg for the help and for pointing me in the right direction. Will have a look into this. Module is dynamic. But based on what you suggested, I feel I should be able to validate the general structure of the license header.

Kiran-B avatar Mar 14 '22 07:03 Kiran-B

Honestly, I'd be willing to add most of the copyright notice format to the license header functionality in a PR. I feel like that would definitely help people trying to migrate from the somewhat unreliable IntelliJ plugin to spotless without issue. The only issue is that I'm quite busy right now, so I won't be able to get to it immediately.

OxygenCobalt avatar Mar 14 '22 14:03 OxygenCobalt

Hey, I was looking for the ability to add the file path in the license header and fell into this issue. Any updates @OxygenCobalt ? Do you have started something?

julb avatar Jan 02 '23 10:01 julb

Published in plugin-maven 2.35.0 and plugin-gradle 6.17.0 thanks to @STAR-ZERO.

nedtwigg avatar Mar 13 '23 17:03 nedtwigg