Skip to content

Commit

Permalink
No more type errors, yay!
Browse files Browse the repository at this point in the history
  • Loading branch information
BillyAutrey committed Oct 15, 2024
1 parent 8158c60 commit e80bc74
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 21 deletions.
5 changes: 5 additions & 0 deletions src/main/scala-2.12/com/typesafe/sbt/PluginCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ private[sbt] object PluginCompat {
def toNioPath(f: File)(implicit conv: FileConverter): NioPath =
f.toPath
def toFile(f: File)(implicit conv: FileConverter): File = f

def toFileRef(f: File)(implicit conv: FileConverter): FileRef = f

def selectFirstPredicate: Seq[FileRef] => Boolean = files =>
files.forall(_.isFile) && files.map(_.hashString).distinct.size == 1
}
12 changes: 9 additions & 3 deletions src/main/scala-3/com/typesafe/sbt/PluginCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@ private[sbt] object PluginCompat:
def toNioPath(a: Attributed[HashedVirtualFileRef])(using conv: FileConverter): NioPath =
conv.toPath(a.data)
inline def toFile(a: Attributed[HashedVirtualFileRef])(using conv: FileConverter): File =
toNioPath(a).toFile()
toNioPath(a).toFile
def toNioPaths(cp: Seq[Attributed[HashedVirtualFileRef]])(using conv: FileConverter): Vector[NioPath] =
cp.map(toNioPath).toVector
inline def toFiles(cp: Seq[Attributed[HashedVirtualFileRef]])(using conv: FileConverter): Vector[File] =
toNioPaths(cp).map(_.toFile())
toNioPaths(cp).map(_.toFile)
def toSet[A](iterable: Iterable[A]): Set[A] = iterable.to(Set)
inline def classpathToFiles(classpath: Classpath)(using conv: FileConverter): Seq[File] =
toFiles(classpath.to(Seq))
inline def toKey( settingKey: SettingKey[String] ): StringAttributeKey = StringAttributeKey(settingKey.key.label)
def toNioPath(hvf: HashedVirtualFileRef)(using conv: FileConverter): NioPath =
conv.toPath(hvf)
def toFile(hvf: HashedVirtualFileRef)(using conv: FileConverter): File =
toNioPath(hvf).toFile()
toNioPath(hvf).toFile

def toFileRef(file: File)(using conv: FileConverter): FileRef =
conv.toVirtualFile(file.toPath)

inline def selectFirstPredicate(using conv: FileConverter): Seq[FileRef] => Boolean = files =>
files.forall(toFile(_).isFile) && files.map(_.contentHashStr).distinct.size == 1
end PluginCompat
24 changes: 7 additions & 17 deletions src/main/scala/com/typesafe/sbt/web/SbtWeb.scala
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,11 @@ object SbtWeb extends AutoPlugin {
* Create package mappings for assets in the webjar format. Use the webjars path prefix and exclude all web module
* assets.
*/
def createWebJarMappings: Def.Initialize[Task[Seq[(File, String)]]] = Def.task {
def createWebJarMappings: Def.Initialize[Task[Seq[(FileRef, String)]]] = Def.task {
def webModule(file: File) = webModuleDirectories.value.exists(dir => IO.relativize(dir, file).isDefined)
implicit val fc: FileConverter = fileConverter.value
mappings.value flatMap {
case (file, path) if webModule(file) => None
case (file, path) if webModule(toFile(file)) => None
case (file, path) => Some(file -> (webJarsPathPrefix.value + path))
}
}
Expand Down Expand Up @@ -434,7 +435,8 @@ object SbtWeb extends AutoPlugin {
if (state.value.get(disableExportedProducts).getOrElse(false)) {
Seq.empty
} else {
Seq(Attributed.blank(exportTask.value).put(toKey(webModulesLib), moduleName.value))
implicit val fc: FileConverter = fileConverter.value
Seq(Attributed.blank(toFileRef(exportTask.value)).put(toKey(webModulesLib), moduleName.value))
}
}

Expand Down Expand Up @@ -546,9 +548,9 @@ object SbtWeb extends AutoPlugin {
mappings.groupBy(_._2 /*path*/ ).toSeq flatMap { grouped =>
val (path, group) = grouped
if (group.size > 1) {
val files = group.map(_._1)
val files = group.map(mapping => toFile(mapping._1))
val deduplicated = firstResult(deduplicators)(files)
deduplicated.fold(group)(file => Seq((file, path)))
deduplicated.fold(group)(file => Seq((toFileRef(file), path)))
} else {
group
}
Expand All @@ -563,18 +565,6 @@ object SbtWeb extends AutoPlugin {
(fs.toStream flatMap { f => f(a).toSeq }).headOption
}

/**
* Deduplicator that selects the first file contained in the base directory.
*
* @param base
* the base directory to check against
* @return
* a deduplicator function that prefers files in the base directory
*/
def selectFileFrom(base: File): Deduplicator = { (files: Seq[File]) =>
files.find(_.relativeTo(base).isDefined)
}

/**
* Deduplicator that checks whether all duplicates are directories and if so will simply select the first one.
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/com/typesafe/sbt/web/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ package object web {
/**
* A function for possibly selecting a single file from a sequence.
*/
type Deduplicator = Seq[FileRef] => Option[FileRef]
type Deduplicator = Seq[File] => Option[File]
}

0 comments on commit e80bc74

Please sign in to comment.