Skip to content

Commit

Permalink
Support files without extensions / hidden files (#297)
Browse files Browse the repository at this point in the history
* Test for backwards compatibility

* Support files without extensions / hidden files

* Scripted test for (hidden) files without extension and exact file names
  • Loading branch information
mkurz authored Oct 20, 2022
1 parent c777233 commit 3882166
Show file tree
Hide file tree
Showing 18 changed files with 149 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/main/scala/de/heikoseeberger/sbtheader/FileType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ package de.heikoseeberger.sbtheader

import scala.util.matching.Regex

final case class FileType(extension: String, firstLinePattern: Option[Regex] = None)
final case class FileType(
extension: String,
firstLinePattern: Option[Regex] = None,
name: String = ""
)

object FileType {

Expand Down
16 changes: 12 additions & 4 deletions src/main/scala/de/heikoseeberger/sbtheader/HeaderPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,18 @@ object HeaderPlugin extends AutoPlugin {
) =
files
.groupBy(_.extension)
.collect { case (Some(ext), groupedFiles) =>
headerMappings.collect { case key @ (FileType(`ext`, _), _) =>
key -> groupedFiles
}
.collect {
case (Some(ext), groupedFiles) =>
headerMappings.collect {
case key @ (FileType(`ext`, _, ""), _) =>
key -> groupedFiles.filter(_.isFile)
case key @ (FileType(`ext`, _, name), _) =>
key -> groupedFiles.filter(file => file.getName == s"$name.$ext" && file.isFile)
}
case (None, groupedFiles) =>
headerMappings.collect { case key @ (FileType("", _, name), _) =>
key -> groupedFiles.filter(file => file.getName == name && file.isFile)
}
}
.flatten
}
6 changes: 6 additions & 0 deletions src/sbt-test/sbt-header/no-file-extensions/project/test.sbt
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 @@
GET / HomeController.index
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2015 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.

GET / HomeController.index
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GET / HomeController.index
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2015 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.
*/

GET / HomeController.index
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GET / HomeController.index
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright 2015 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.

GET / HomeController.index
5 changes: 5 additions & 0 deletions src/sbt-test/sbt-header/no-file-extensions/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# check if headers get created
-> headerCheck
> headerCreate
> checkFileContents
> headerCheck
37 changes: 37 additions & 0 deletions src/sbt-test/sbt-header/no-file-extensions/test.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
headerLicense := Some(HeaderLicense.ALv2("2015", "Heiko Seeberger"))

// Files named exactly "routes" (with no extension)
headerMappings := headerMappings.value + (HeaderFileType("", None, "routes") -> HeaderCommentStyle.hashLineComment)
// Files named exactly "more.routes"
headerMappings := headerMappings.value + (HeaderFileType("routes", None, "more") -> HeaderCommentStyle.cStyleBlockComment)
// Hidden files named exactly ".routes"
headerMappings := headerMappings.value + (HeaderFileType("", None, ".routes") -> HeaderCommentStyle.cppStyleLineComment)

// Do not exclude hidden files (.filename)
unmanagedResources / excludeFilter := (_ => false)


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

checkFileContents := {
checkFile("routes")
checkFile("more.routes")
checkFile(".routes")

def checkFile(name: String) = {
val actualPath = (resourceDirectory.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)
}
}
1 change: 1 addition & 0 deletions src/sbt-test/sbt-header/simple/src/main/resources/.routes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GET / HomeController.index
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GET / HomeController.index
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GET / HomeController.index
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2015 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.

GET / HomeController.index
1 change: 1 addition & 0 deletions src/sbt-test/sbt-header/simple/src/main/resources/routes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GET / HomeController.index
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GET / HomeController.index
18 changes: 14 additions & 4 deletions src/sbt-test/sbt-header/simple/test.sbt
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
headerLicense := Some(HeaderLicense.ALv2("2015", "Heiko Seeberger"))

// A file named exactly "routes" will NOT be taken into account even if the header extension is "routes"
// A hidden file .routes must be treated with file name ".routes" and without extension
// A hidden file .some.routes must be treated with extension "*.routes"
headerMappings := headerMappings.value + (HeaderFileType("routes") -> HeaderCommentStyle.cppStyleLineComment)
// Do not exclude hidden files (.filename)
unmanagedResources / excludeFilter := (_ => false)

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

checkFileContents := {
checkFile("HasHeader.scala")
checkFile("HasNoHeader.scala")
checkFile("HasHeader.scala", scalaSource.in(Compile).value)
checkFile("HasNoHeader.scala", scalaSource.in(Compile).value)
checkFile("routes", resourceDirectory.in(Compile).value)
checkFile(".routes", resourceDirectory.in(Compile).value) // does not have extension, file name is only ".routes"
checkFile(".some.routes", resourceDirectory.in(Compile).value) // hidden file with extension .routes

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

val actual = scala.io.Source.fromFile(actualPath).mkString
Expand Down

0 comments on commit 3882166

Please sign in to comment.