2022-11-16 14:02:29 +08:00
|
|
|
/*
|
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
* license agreements; and to You under the Apache License, version 2.0:
|
|
|
|
|
*
|
|
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
2023-06-22 14:19:26 +01:00
|
|
|
* This file is part of the Apache Pekko project, which was derived from Akka.
|
2022-11-16 14:02:29 +08:00
|
|
|
*/
|
|
|
|
|
|
2018-03-13 23:45:55 +09:00
|
|
|
/*
|
2022-02-04 12:36:44 +01:00
|
|
|
* Copyright (C) 2018-2022 Lightbend Inc. <https://www.lightbend.com>
|
2018-03-13 23:45:55 +09:00
|
|
|
*/
|
|
|
|
|
|
2022-11-16 14:02:29 +08:00
|
|
|
import de.heikoseeberger.sbtheader.{ CommentCreator, HeaderPlugin, NewLine }
|
|
|
|
|
import org.apache.commons.lang3.StringUtils
|
2022-12-02 14:49:16 +01:00
|
|
|
import sbt._
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2024-01-21 11:27:34 +11:00
|
|
|
object CopyrightHeader extends AutoPlugin {
|
|
|
|
|
import HeaderPlugin.autoImport._
|
2023-07-30 11:55:14 +02:00
|
|
|
|
2024-01-21 11:27:34 +11:00
|
|
|
override def requires = HeaderPlugin
|
|
|
|
|
override def trigger = allRequirements
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2024-01-21 11:27:34 +11:00
|
|
|
override def projectSettings = Def.settings(
|
|
|
|
|
Seq(Compile, Test).flatMap { config =>
|
2020-01-02 07:24:59 -05:00
|
|
|
inConfig(config)(
|
|
|
|
|
Seq(
|
2022-11-16 14:02:29 +08:00
|
|
|
headerLicense := Some(HeaderLicense.Custom(apacheHeader)),
|
2020-01-02 07:24:59 -05:00
|
|
|
headerMappings := headerMappings.value ++ Map(
|
|
|
|
|
HeaderFileType.scala -> cStyleComment,
|
|
|
|
|
HeaderFileType.java -> cStyleComment,
|
2023-03-06 10:43:02 +01:00
|
|
|
HeaderFileType.conf -> hashLineComment,
|
2023-05-29 03:35:14 +01:00
|
|
|
HeaderFileType("template") -> cStyleComment,
|
|
|
|
|
HeaderFileType("sbt") -> cStyleComment)))
|
2024-01-21 11:27:34 +11:00
|
|
|
})
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2023-07-13 06:58:20 +02:00
|
|
|
val apacheFromAkkaSourceHeader: String =
|
2022-11-16 14:02:29 +08:00
|
|
|
"""Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
|license agreements; and to You under the Apache License, version 2.0:
|
|
|
|
|
|
|
|
|
|
|
| https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
2023-06-22 14:19:26 +01:00
|
|
|
|This file is part of the Apache Pekko project, which was derived from Akka.
|
2022-11-16 14:02:29 +08:00
|
|
|
|""".stripMargin
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2023-07-13 06:58:20 +02:00
|
|
|
val apacheHeader: String =
|
2023-07-07 08:41:19 +02:00
|
|
|
"""Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
|contributor license agreements. See the NOTICE file distributed with
|
|
|
|
|
|this work for additional information regarding copyright ownership.
|
|
|
|
|
|The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
|
|(the "License"); you may not use this file except in compliance with
|
|
|
|
|
|the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
|
| http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
|
|Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
|distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
|WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
|See the License for the specific language governing permissions and
|
|
|
|
|
|limitations under the License.
|
|
|
|
|
|""".stripMargin
|
|
|
|
|
|
2023-03-03 13:24:19 +01:00
|
|
|
val apacheSpdxHeader: String = "SPDX-License-Identifier: Apache-2.0"
|
|
|
|
|
|
2022-11-16 14:02:29 +08:00
|
|
|
val cStyleComment = HeaderCommentStyle.cStyleBlockComment.copy(commentCreator = new CommentCreator() {
|
2018-10-29 17:19:37 +08:00
|
|
|
|
2018-03-13 23:45:55 +09:00
|
|
|
override def apply(text: String, existingText: Option[String]): String = {
|
2018-10-29 17:19:37 +08:00
|
|
|
val formatted = existingText match {
|
2023-03-03 13:24:19 +01:00
|
|
|
case Some(currentText) if isValidCopyrightAnnotated(currentText) =>
|
|
|
|
|
currentText
|
|
|
|
|
case Some(currentText) if isOnlyLightbendOrEpflCopyrightAnnotated(currentText) =>
|
2023-07-07 08:41:19 +02:00
|
|
|
HeaderCommentStyle.cStyleBlockComment.commentCreator(apacheFromAkkaSourceHeader,
|
|
|
|
|
existingText) + NewLine * 2 + currentText
|
2023-03-03 13:24:19 +01:00
|
|
|
case Some(currentText) =>
|
2024-01-21 11:27:34 +11:00
|
|
|
throw new IllegalStateException(s"Unable to detect copyright for header: [$currentText]")
|
2018-10-29 17:19:37 +08:00
|
|
|
case None =>
|
2020-01-02 07:24:59 -05:00
|
|
|
HeaderCommentStyle.cStyleBlockComment.commentCreator(text, existingText)
|
2018-10-29 17:19:37 +08:00
|
|
|
}
|
|
|
|
|
formatted.trim
|
2018-03-13 23:45:55 +09:00
|
|
|
}
|
2023-03-03 13:24:19 +01:00
|
|
|
})
|
2022-11-16 14:02:29 +08:00
|
|
|
|
2023-03-03 13:24:19 +01:00
|
|
|
val hashLineComment = HeaderCommentStyle.hashLineComment.copy(commentCreator = new CommentCreator() {
|
|
|
|
|
|
2023-03-06 10:43:02 +01:00
|
|
|
// deliberately hardcode use of apacheSpdxHeader and ignore input text
|
2023-03-03 13:24:19 +01:00
|
|
|
override def apply(text: String, existingText: Option[String]): String = {
|
|
|
|
|
val formatted = existingText match {
|
|
|
|
|
case Some(currentText) if isApacheCopyrighted(currentText) =>
|
|
|
|
|
currentText
|
|
|
|
|
case Some(currentText) =>
|
2023-03-06 10:43:02 +01:00
|
|
|
HeaderCommentStyle.hashLineComment.commentCreator(apacheSpdxHeader, existingText) + NewLine * 2 + currentText
|
2023-03-03 13:24:19 +01:00
|
|
|
case None =>
|
2023-03-06 10:43:02 +01:00
|
|
|
HeaderCommentStyle.hashLineComment.commentCreator(apacheSpdxHeader, existingText)
|
2023-03-03 13:24:19 +01:00
|
|
|
}
|
|
|
|
|
formatted.trim
|
|
|
|
|
}
|
|
|
|
|
})
|
2022-11-16 14:02:29 +08:00
|
|
|
|
2023-03-03 13:24:19 +01:00
|
|
|
private def isApacheCopyrighted(text: String): Boolean =
|
|
|
|
|
StringUtils.containsIgnoreCase(text, "licensed to the apache software foundation (asf)") ||
|
|
|
|
|
StringUtils.containsIgnoreCase(text, "www.apache.org/licenses/license-2.0") ||
|
|
|
|
|
StringUtils.contains(text, "Apache-2.0")
|
2022-11-16 14:02:29 +08:00
|
|
|
|
2023-03-03 13:24:19 +01:00
|
|
|
private def isLAMPCopyrighted(text: String): Boolean =
|
|
|
|
|
StringUtils.containsIgnoreCase(text, "lamp/epfl")
|
2022-11-16 14:02:29 +08:00
|
|
|
|
2023-03-03 13:24:19 +01:00
|
|
|
private def isLightbendCopyrighted(text: String): Boolean =
|
|
|
|
|
StringUtils.containsIgnoreCase(text, "lightbend inc.")
|
2022-11-16 14:02:29 +08:00
|
|
|
|
2023-09-21 18:35:39 +01:00
|
|
|
private def isValidCopyrightAnnotated(text: String): Boolean =
|
2023-03-03 13:24:19 +01:00
|
|
|
isApacheCopyrighted(text)
|
2022-11-16 14:02:29 +08:00
|
|
|
|
2024-01-21 11:27:34 +11:00
|
|
|
private def isOnlyLightbendOrEpflCopyrightAnnotated(text: String): Boolean =
|
2023-03-03 13:24:19 +01:00
|
|
|
(isLightbendCopyrighted(text) || isLAMPCopyrighted(text)) && !isApacheCopyrighted(text)
|
2018-03-13 23:45:55 +09:00
|
|
|
}
|