Skip to content

Commit

Permalink
Make LicenseDetection public and allow choosing license style (#181)
Browse files Browse the repository at this point in the history
* Set `LicenseDetection#startYear` as String so we can use other values

* Allow using `LicenseDetection` publicly

* Add parameter to allow changing license style in `LicenseDetection`

* Create new plugin setting `headerLicenseStyle`

This setting will allow automatically changing the license style when using auto-detection

* Update README with latest changes
  • Loading branch information
alejandrohdezma authored Apr 16, 2020
1 parent 6b0098c commit b73aff0
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 6 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,15 @@ If you want to use the following syntax:
*/
```

You have to pass the style when defining the headerLicense attribute
You have two possibilites:

- If you are using auto-detection, you just need to add the following to your `build.sbt`

```sbt
headerLicenseStyle := HeaderLicenseStyle.SpdxSyntax
```

- On the other hand, if you are defining your license explicitly, you'll have to pass the style when defining the `headerLicense` attribute:

```scala
headerLicense := Some(HeaderLicense.MIT("2015", "Heiko Seeberger", HeaderLicenseStyle.SpdxSyntax))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ object HeaderPlugin extends AutoPlugin {
"The license to apply to files; None by default (enabling auto detection from project settings)"
)

val headerLicenseStyle: SettingKey[LicenseStyle] = settingKey[LicenseStyle] {
"The license style to be used. Can be `Detailed` or `SpdxSyntax`. Defaults to Detailed."
}

val headerMappings: SettingKey[Map[FileType, CommentStyle]] =
settingKey(
"CommentStyles to be used by file extension they should be applied to; C style block comments for Scala and Java files by default"
Expand Down Expand Up @@ -150,8 +154,10 @@ object HeaderPlugin extends AutoPlugin {
headerLicense := LicenseDetection(
licenses.value.toList,
organizationName.value,
startYear.value
startYear.value.map(_.toString),
headerLicenseStyle.value
),
headerLicenseStyle := LicenseStyle.Detailed,
includeFilter.in(headerSources) := includeFilter.in(unmanagedSources).value,
excludeFilter.in(headerSources) := excludeFilter.in(unmanagedSources).value,
includeFilter.in(headerResources) := includeFilter.in(unmanagedResources).value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ package de.heikoseeberger.sbtheader
import sbt.URL
import scala.collection.breakOut

private object LicenseDetection {
object LicenseDetection {

private val spdxMapping =
License.spdxLicenses.map(l => (l.spdxIdentifier, l))(breakOut): Map[String, SpdxLicense]

def apply(
licenses: Seq[(String, URL)],
organizationName: String,
startYear: Option[Int]
startYear: Option[String],
licenseStyle: LicenseStyle = LicenseStyle.Detailed
): Option[License] = {
val licenseName = licenses match {
case (name, _) :: Nil => Some(name)
Expand All @@ -38,6 +39,6 @@ private object LicenseDetection {
name <- licenseName
license <- spdxMapping.get(name)
year <- startYear
} yield license(year.toString, organizationName)
} yield license(year, organizationName, licenseStyle)
}
}
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,9 @@
/*
* Copyright 2015 Heiko Seeberger
*
* SPDX-License-Identifier: Apache-2.0
*/

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# check if headers get created with different style
-> headerCheck
> headerCreate
$ must-mirror src/main/scala/HasNoHeader.scala src/main/resources/HasNoHeader.scala_expected
> headerCheck
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

organizationName := "Heiko Seeberger"
startYear := Some(2015)
licenses := List(("Apache-2.0", new URL("https://www.apache.org/licenses/LICENSE-2.0.txt")))
headerLicenseStyle := HeaderLicenseStyle.SpdxSyntax
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import sbt.URL
class LicenseDetectionSpec extends WordSpec with Matchers {

val organizationName = "Heiko Seeberger"
val yyyy = 2017
val yyyy = "2017"
val startYear = Some(yyyy)
val apache: (String, URL) =
("Apache-2.0", new URL("https://spdx.org/licenses/Apache-2.0.html#licenseText"))
Expand Down Expand Up @@ -92,6 +92,17 @@ class LicenseDetectionSpec extends WordSpec with Matchers {
) shouldBe None
}

"allow changing license style" in {
val expected = ALv2(yyyy, organizationName, LicenseStyle.SpdxSyntax)

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

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

0 comments on commit b73aff0

Please sign in to comment.