-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(build): CopyrightHeader AutoPlugin (#413)
We move the way we manage our header license to an auto plugin. We still make use of the `sbt-header` plugin but we override some methods and settings in order to have the following behavior - if a file already contains a copyright header we are not touching it and sbt headerCheck will pass - if no header is found we add a copyright header at compilation with current year This PR is only about this auto plugin, after we merge this another PR will come to align all copyright headers and years We take advantage of this PR to re-activate `sbt headerCheckAll` in CI
- Loading branch information
Showing
3 changed files
with
73 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package raw.build | ||
|
||
import de.heikoseeberger.sbtheader.HeaderPlugin.autoImport._ | ||
import de.heikoseeberger.sbtheader.{ CommentCreator, HeaderPlugin } | ||
import sbt.Keys._ | ||
import sbt.{ Def, _ } | ||
|
||
import java.time.LocalDate | ||
|
||
|
||
object CopyrightHeader extends AutoPlugin { | ||
|
||
override def requires: Plugins = HeaderPlugin | ||
|
||
override def trigger: PluginTrigger = allRequirements | ||
|
||
protected def headerMappingSettings: Seq[Def.Setting[_]] = | ||
Seq(Compile, Test).flatMap { config => | ||
inConfig(config)( | ||
Seq( | ||
headerLicense := Some(HeaderLicense.Custom(header)), | ||
headerMappings := headerMappings.value ++ Map( | ||
HeaderFileType.scala -> cStyleComment, | ||
HeaderFileType.java -> cStyleComment | ||
) | ||
) | ||
) | ||
} | ||
|
||
override def projectSettings: Seq[Def.Setting[_]] = Def.settings(headerMappingSettings, additional) | ||
|
||
def additional: Seq[Def.Setting[_]] = | ||
Def.settings(Compile / compile := { | ||
(Compile / headerCreate).value | ||
(Compile / compile).value | ||
}, Test / compile := { | ||
(Test / headerCreate).value | ||
(Test / compile).value | ||
}) | ||
|
||
def header: String = { | ||
val currentYear = "2024" | ||
s"""|/* | ||
| * Copyright $currentYear RAW Labs S.A. | ||
| * | ||
| * Use of this software is governed by the Business Source License | ||
| * included in the file licenses/BSL.txt. | ||
| * | ||
| * As of the Change Date specified in that file, in accordance with | ||
| * the Business Source License, use of this software will be governed | ||
| * by the Apache License, Version 2.0, included in the file | ||
| * licenses/APL.txt. | ||
| */""".stripMargin | ||
} | ||
|
||
val cStyleComment = HeaderCommentStyle.cStyleBlockComment.copy(commentCreator = new CommentCreator() { | ||
val CopyrightPattern = "Copyright (\\d{4}) RAW Labs S.A.".r | ||
|
||
override def apply(text: String, existingText: Option[String]): String = { | ||
existingText match { | ||
case Some(existingHeader) if CopyrightPattern.findFirstIn(existingHeader).isDefined => | ||
// matches the pattern with any year, return it unchanged | ||
existingHeader.trim | ||
case _ => | ||
header | ||
} | ||
} | ||
}) | ||
|
||
} |