Skip to content
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

Add a simple headerEndYear key for configuring year ranges in license headers #282

Merged
merged 1 commit into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ object HeaderPlugin extends AutoPlugin {

val HeaderCommentStyle = CommentStyle

val headerEndYear: SettingKey[Option[Int]] =
settingKey(
"The end of the range of years to specify in the header. Defaults to None (only the `startYear` is used)."
)

val headerLicense: SettingKey[Option[License]] =
settingKey(
"The license to apply to files; None by default (enabling auto detection from project settings)"
Expand Down Expand Up @@ -159,9 +164,11 @@ object HeaderPlugin extends AutoPlugin {
headerLicense := LicenseDetection(
licenses.value.toList,
organizationName.value,
startYear.value.map(_.toString),
startYear.value,
headerEndYear.value,
headerLicenseStyle.value
),
headerEndYear := None,
headerLicenseStyle := LicenseStyle.Detailed,
headerSources / includeFilter := (unmanagedSources / includeFilter).value,
headerSources / excludeFilter := (unmanagedSources / excludeFilter).value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ object LicenseDetection {
organizationName: String,
startYear: Option[String],
licenseStyle: LicenseStyle = LicenseStyle.Detailed
): Option[License] =
apply(licenses, organizationName, startYear.map(_.toInt), None, licenseStyle)

private[sbtheader] def apply(
licenses: Seq[(String, URL)],
organizationName: String,
startYear: Option[Int],
endYear: Option[Int],
licenseStyle: LicenseStyle
): Option[License] = {
val licenseName =
licenses match {
Expand All @@ -39,7 +48,14 @@ object LicenseDetection {
for {
name <- licenseName
license <- spdxMapping.get(name)
year <- startYear
year <- combineYears(startYear, endYear)
} yield license(year, organizationName, licenseStyle)
}

private def combineYears(startYear: Option[Int], endYear: Option[Int]): Option[String] =
(startYear, endYear) match {
case (Some(start), Some(end)) if start < end => Some(s"$start-$end")
case (Some(start), _) => Some(start.toString)
case (None, _) => None
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
val pluginVersion =
sys.props
.get("plugin.version")
.getOrElse(sys.error("Sys prop plugin.version must be defined!"))

addSbtPlugin("de.heikoseeberger" % "sbt-header" % pluginVersion)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2015-2022 Heiko Seeberger
*
* Licensed 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.
*/

package de.heikoseeberger.sbtheader.test;

class HasNoHeader
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package de.heikoseeberger.sbtheader.test;

class HasNoHeader
5 changes: 5 additions & 0 deletions src/sbt-test/sbt-header/auto-detection-end-year/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# check if headers get created
-> headerCheck
> headerCreate
> checkFileContents
> headerCheck
26 changes: 26 additions & 0 deletions src/sbt-test/sbt-header/auto-detection-end-year/test.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
organizationName := "Heiko Seeberger"
startYear := Some(2015)
headerEndYear := Some(2022)
licenses := List(("Apache-2.0", new URL("https://www.apache.org/licenses/LICENSE-2.0.txt")))

val checkFileContents = taskKey[Unit]("Verify file contents match expected contents")

checkFileContents := {
checkFile("HasNoHeader.scala")

def checkFile(name: String) = {
val actualPath = (scalaSource.in(Compile).value / name).toString
val expectedPath = (resourceDirectory.in(Compile).value / s"${name}_expected").toString

val actual = scala.io.Source.fromFile(actualPath).mkString
val expected = scala.io.Source.fromFile(expectedPath).mkString

if (actual != expected) sys.error(s"""|Actual file contents do not match expected file contents!
| actual: $actualPath
|$actual
|
| expected: $expectedPath
|$expected
|""".stripMargin)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,32 @@ class LicenseDetectionSpec extends AnyWordSpec with Matchers {
).map(_.text) shouldBe Some(expected.text)
}

"use only the start year even when end year is set and equal" in {
val expected = ALv2(yyyy, organizationName, LicenseStyle.SpdxSyntax)

LicenseDetection(
List(apache),
organizationName,
startYear.map(_.toInt),
startYear.map(_.toInt),
LicenseStyle.SpdxSyntax
).map(_.text) shouldBe Some(expected.text)
}

"detect end year when it is set and larger than start year" in {
val endYYYY = yyyy.toInt + 2
val endYear = Some(endYYYY)
val expected = ALv2(s"$yyyy-$endYYYY", organizationName, LicenseStyle.SpdxSyntax)

LicenseDetection(
List(apache),
organizationName,
startYear.map(_.toInt),
endYear,
LicenseStyle.SpdxSyntax
).map(_.text) shouldBe Some(expected.text)
}

licenses.foreach { case (license, sbtLicense) =>
s"detect ${license.getClass.getSimpleName} license" in {
LicenseDetection(List(sbtLicense), organizationName, startYear)
Expand Down