Skip to content

Commit

Permalink
Change type of PathMapping, add key converter for StringAttributeKey
Browse files Browse the repository at this point in the history
  • Loading branch information
BillyAutrey committed Oct 12, 2024
1 parent 03419ce commit 8158c60
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
7 changes: 6 additions & 1 deletion src/main/scala-2.12/com/typesafe/sbt/PluginCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@ private[sbt] object PluginCompat {
def toFiles(cp: Seq[Attributed[File]])(implicit conv: FileConverter): Vector[File] =
cp.map(_.data).toVector
def toSet[A](iterable: Iterable[A]): Set[A] = iterable.to[Set]
def classpathToFiles(classpath: Classpath): Seq[FileRef] = classpath.files
def classpathToFiles(classpath: Classpath)(implicit conv: FileConverter): Seq[FileRef] =
classpath.files
def toKey( settingKey: SettingKey[String] ): AttributeKey[String] = settingKey.key
def toNioPath(f: File)(implicit conv: FileConverter): NioPath =
f.toPath
def toFile(f: File)(implicit conv: FileConverter): File = f
}
8 changes: 7 additions & 1 deletion src/main/scala-3/com/typesafe/sbt/PluginCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@ private[sbt] object PluginCompat:
inline def toFiles(cp: Seq[Attributed[HashedVirtualFileRef]])(using conv: FileConverter): Vector[File] =
toNioPaths(cp).map(_.toFile())
def toSet[A](iterable: Iterable[A]): Set[A] = iterable.to(Set)
def classpathToFiles(classpath: Classpath): Seq[File] = toFiles(classpath)
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()
end PluginCompat
41 changes: 24 additions & 17 deletions src/main/scala/com/typesafe/sbt/web/SbtWeb.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.typesafe.sbt.web

import sbt.{ Def, *, given }
import sbt.{Def, given, *}
import sbt.internal.inc.Analysis
import sbt.internal.io.Source
import sbt.Keys.*
Expand All @@ -10,9 +10,9 @@ import org.webjars.WebJarExtractor
import org.webjars.WebJarAssetLocator.WEBJARS_PATH_PREFIX
import com.typesafe.sbt.web.pipeline.Pipeline
import com.typesafe.sbt.web.incremental.{ OpResult, OpSuccess, toStringInputHasher }
import xsbti.Reporter
import xsbti.{ Reporter, FileConverter }

import PluginCompat.*
import com.typesafe.sbt.PluginCompat.*

object Import {

Expand Down Expand Up @@ -234,8 +234,8 @@ object SbtWeb extends AutoPlugin {
(TestAssets / webJars / webJarsCache) := webTarget.value / "web-modules" / "webjars-test.cache",
(Assets / nodeModules / webJarsCache) := webTarget.value / "node-modules" / "webjars-main.cache",
(TestAssets / nodeModules / webJarsCache) := webTarget.value / "node-modules" / "webjars-test.cache",
(Assets / webJarsClassLoader) := classLoader((Compile / dependencyClasspath).value),
(TestAssets / webJarsClassLoader) := classLoader((Test / dependencyClasspath).value),
(Assets / webJarsClassLoader) := classLoader((Compile / dependencyClasspath).value, fileConverter.value),
(TestAssets / webJarsClassLoader) := classLoader((Test / dependencyClasspath).value, fileConverter.value),
assets := (Assets / assets).value,
(Compile / packageBin / mappings) ++= {
if ((Assets / addExportedMappingsToPackageBinMappings).value) {
Expand Down Expand Up @@ -270,12 +270,13 @@ object SbtWeb extends AutoPlugin {
allPipelineStages := Pipeline.chain(pipelineStages).value,
pipeline := allPipelineStages.value((Assets / mappings).value),
deduplicators := Nil,
pipeline := deduplicateMappings(pipeline.value, deduplicators.value),
pipeline := deduplicateMappings(pipeline.value, deduplicators.value, fileConverter.value),
stagingDirectory := webTarget.value / "stage",
stage := syncMappings(
streams.value.cacheStoreFactory.make("sync-stage"),
pipeline.value,
stagingDirectory.value
stagingDirectory.value,
fileConverter.value
)
) ++
inConfig(Assets)(unscopedAssetSettings) ++ inConfig(Assets)(nodeModulesSettings) ++
Expand Down Expand Up @@ -327,11 +328,12 @@ object SbtWeb extends AutoPlugin {
allPipelineStages := Pipeline.chain(pipelineStages).value,
mappings := allPipelineStages.value(mappings.value),
deduplicators := Nil,
mappings := deduplicateMappings(mappings.value, deduplicators.value),
mappings := deduplicateMappings(mappings.value, deduplicators.value, fileConverter.value),
assets := syncMappings(
streams.value.cacheStoreFactory.make(s"sync-assets-" + configuration.value.name),
mappings.value,
public.value
public.value,
fileConverter.value
),
exportedMappings := createWebJarMappings.value,
addExportedMappingsToPackageBinMappings := true,
Expand Down Expand Up @@ -388,7 +390,8 @@ object SbtWeb extends AutoPlugin {
syncMappings(
streams.value.cacheStoreFactory.make("sync-exported-assets-" + configuration.value.name),
exportedMappings.value,
syncTargetDir
syncTargetDir,
fileConverter.value
)
}
else
Expand Down Expand Up @@ -431,7 +434,7 @@ object SbtWeb extends AutoPlugin {
if (state.value.get(disableExportedProducts).getOrElse(false)) {
Seq.empty
} else {
Seq(Attributed.blank(exportTask.value).put(webModulesLib.key, moduleName.value))
Seq(Attributed.blank(exportTask.value).put(toKey(webModulesLib), moduleName.value))
}
}

Expand All @@ -456,14 +459,14 @@ object SbtWeb extends AutoPlugin {
* Get module names for all internal web module dependencies on the classpath.
*/
def getInternalWebModules(conf: Configuration): Def.Initialize[Task[Seq[String]]] = Def.task {
(conf / internalDependencyClasspath).value.flatMap(_.get(WebKeys.webModulesLib.key))
(conf / internalDependencyClasspath).value.flatMap(_.get(toKey(WebKeys.webModulesLib)))
}

/**
* Remove web module dependencies from a classpath. This is a helper method for Play 2.3 transitions.
*/
def classpathWithoutAssets(classpath: Classpath): Classpath = {
classpath.filter(_.get(WebKeys.webModulesLib.key).isEmpty)
classpath.filter(_.get(toKey(WebKeys.webModulesLib)).isEmpty)
}

def flattenDirectWebModules = Def.task {
Expand Down Expand Up @@ -493,8 +496,10 @@ object SbtWeb extends AutoPlugin {
prefixes.find(s.startsWith).fold(s)(s.stripPrefix)
}

private def classLoader(classpath: Classpath): ClassLoader =
private def classLoader(classpath: Classpath, conv: FileConverter): ClassLoader = {
implicit val fc: FileConverter = conv
new java.net.URLClassLoader(Path.toURLs(classpathToFiles(classpath)), null)
}

private def withWebJarExtractor(to: File, cacheFile: File, classLoader: ClassLoader)(
block: (WebJarExtractor, File) => Unit
Expand Down Expand Up @@ -533,7 +538,8 @@ object SbtWeb extends AutoPlugin {
* @return
* the (possibly) deduplicated mappings
*/
def deduplicateMappings(mappings: Seq[PathMapping], deduplicators: Seq[Deduplicator]): Seq[PathMapping] = {
def deduplicateMappings(mappings: Seq[PathMapping], deduplicators: Seq[Deduplicator], conv: FileConverter): Seq[PathMapping] = {
implicit val fc: FileConverter = conv
if (deduplicators.isEmpty) {
mappings
} else {
Expand Down Expand Up @@ -608,9 +614,10 @@ object SbtWeb extends AutoPlugin {
* @return
* the target value
*/
def syncMappings(cacheStore: sbt.util.CacheStore, mappings: Seq[PathMapping], target: File): File = {
def syncMappings(cacheStore: sbt.util.CacheStore, mappings: Seq[PathMapping], target: File, conv: FileConverter): File = {
implicit val fc: FileConverter = conv
val copies = mappings map { case (file, path) =>
file -> (target / path)
toFile(file) -> (target / path)
}
Sync.sync(cacheStore)(copies)
target
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 @@ -9,7 +9,7 @@ package object web {
/**
* Describes a string path relative to a base directory.
*/
type PathMapping = (File, String)
type PathMapping = (FileRef, String)

/**
* A function for possibly selecting a single file from a sequence.
Expand Down

0 comments on commit 8158c60

Please sign in to comment.