Skip to content

Commit df7c791

Browse files
Partially replace compat layer with sbt-compat plugin (#1741)
* Partially replace compat layer with sbt-compat plugin * Use published compat plugin * Fix formatting * Use compat plugin in scripted tests * Add workarounds for failing tests * Fix failing tests
1 parent 892a41f commit df7c791

72 files changed

Lines changed: 206 additions & 243 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@ target/
1414
.bloop/*
1515
.metals/*
1616
.bsp/
17+
18+
.java-version
19+
.cursor/
20+
.vscode/
21+
.bloop/

build.sbt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ javacOptions ++= Seq("-source", "1.8", "-target", "1.8")
2727

2828
// put jdeb on the classpath for scripted tests
2929
classpathTypes += "maven-plugin"
30+
addSbtPlugin("com.github.sbt" % "sbt2-compat" % "0.1.0")
31+
3032
libraryDependencies ++= Seq(
3133
// these dependencies have to be explicitly added by the user
3234
"com.spotify" % "docker-client" % "8.16.0" % Provided,

src/main/scala-2.12/com/typesafe/sbt/packager/Compat.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,4 @@ object Compat {
3636
* a CacheStore
3737
*/
3838
implicit def fileToCacheStore(file: java.io.File): CacheStore = CacheStore(file)
39-
40-
implicit class DefOps(private val self: sbt.Def.type) extends AnyVal {
41-
def uncached[A](a: A): A = a
42-
}
4339
}

src/main/scala-2.12/com/typesafe/sbt/packager/PluginCompat.scala

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,14 @@
11
package com.typesafe.sbt.packager
22

3-
import java.nio.file.{Path => NioPath}
43
import java.util.jar.Attributes
54
import sbt.*
6-
import xsbti.FileConverter
75

86
object PluginCompat {
9-
type FileRef = java.io.File
10-
type ArtifactPath = java.io.File
11-
type Out = java.io.File
127
type IncludeArtifact = Artifact => Boolean
138

14-
val artifactStr = sbt.Keys.artifact.key
15-
val moduleIDStr = sbt.Keys.moduleID.key
16-
def parseModuleIDStrAttribute(m: ModuleID): ModuleID = m
17-
def moduleIDToStr(m: ModuleID): ModuleID = m
18-
private[packager] def parseArtifactStrAttribute(a: Artifact): Artifact = a
19-
def artifactToStr(art: Artifact): Artifact = art
20-
21-
private[packager] def toNioPath(a: Attributed[File])(implicit conv: FileConverter): NioPath =
22-
a.data.toPath()
23-
private[packager] def toNioPath(ref: File)(implicit conv: FileConverter): NioPath =
24-
ref.toPath()
25-
def toFile(a: Attributed[File])(implicit conv: FileConverter): File =
26-
a.data
27-
def toFile(ref: File)(implicit conv: FileConverter): File =
28-
ref
29-
private[packager] def artifactPathToFile(ref: File)(implicit conv: FileConverter): File =
30-
ref
31-
private[packager] def toArtifactPath(f: File)(implicit conv: FileConverter): ArtifactPath = f
32-
private[packager] def toNioPaths(cp: Seq[Attributed[File]])(implicit conv: FileConverter): Vector[NioPath] =
33-
cp.map(_.data.toPath()).toVector
34-
private[packager] def toFiles(cp: Seq[Attributed[File]])(implicit conv: FileConverter): Vector[File] =
35-
cp.map(_.data).toVector
36-
def toFileRefsMapping(mappings: Seq[(File, String)])(implicit conv: FileConverter): Seq[(FileRef, String)] =
37-
mappings
38-
def toFileRef(x: File)(implicit conv: FileConverter): FileRef =
39-
x
40-
private[packager] def getName(ref: File): String =
9+
private[packager] def getName(ref: java.io.File): String =
4110
ref.getName()
42-
private[packager] def getArtifactPathName(ref: File): String =
11+
private[packager] def getArtifactPathName(ref: java.io.File): String =
4312
ref.getName()
4413
private[packager] def classpathAttr = Attributes.Name.CLASS_PATH
4514
private[packager] def mainclassAttr = Attributes.Name.MAIN_CLASS

src/main/scala-3/com/typesafe/sbt/packager/PluginCompat.scala

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,15 @@
11
package com.typesafe.sbt.packager
22

3-
import java.io.File
4-
import java.nio.file.{Path => NioPath}
53
import java.util.jar.Attributes
64
import sbt.*
7-
import xsbti.{FileConverter, HashedVirtualFileRef, VirtualFile, VirtualFileRef}
8-
import sbt.internal.RemoteCache
5+
import xsbti.{HashedVirtualFileRef, VirtualFileRef}
96

107
object PluginCompat {
11-
type FileRef = HashedVirtualFileRef
12-
type ArtifactPath = VirtualFileRef
13-
type Out = VirtualFile
148
type IncludeArtifact = Any => Boolean
159

16-
val artifactStr = Keys.artifactStr
17-
val moduleIDStr = Keys.moduleIDStr
18-
def parseModuleIDStrAttribute(str: String): ModuleID =
19-
Classpaths.moduleIdJsonKeyFormat.read(str)
20-
def moduleIDToStr(m: ModuleID): String =
21-
Classpaths.moduleIdJsonKeyFormat.write(m)
22-
23-
private[packager] def parseArtifactStrAttribute(str: String): Artifact =
24-
import sbt.librarymanagement.LibraryManagementCodec.ArtifactFormat
25-
import sjsonnew.support.scalajson.unsafe.*
26-
Converter.fromJsonUnsafe[Artifact](Parser.parseUnsafe(str))
27-
def artifactToStr(art: Artifact): String =
28-
import sbt.librarymanagement.LibraryManagementCodec.ArtifactFormat
29-
import sjsonnew.support.scalajson.unsafe.*
30-
CompactPrinter(Converter.toJsonUnsafe(art))
31-
32-
private[packager] def toNioPath(a: Attributed[HashedVirtualFileRef])(using conv: FileConverter): NioPath =
33-
conv.toPath(a.data)
34-
private[packager] def toNioPath(ref: HashedVirtualFileRef)(using conv: FileConverter): NioPath =
35-
conv.toPath(ref)
36-
def toFile(a: Attributed[HashedVirtualFileRef])(using conv: FileConverter): File =
37-
toNioPath(a).toFile()
38-
def toFile(ref: HashedVirtualFileRef)(using conv: FileConverter): File =
39-
toNioPath(ref).toFile()
40-
private[packager] def artifactPathToFile(ref: VirtualFileRef)(using conv: FileConverter): File =
41-
conv.toPath(ref).toFile()
42-
private[packager] def toArtifactPath(f: File)(using conv: FileConverter): ArtifactPath =
43-
conv.toVirtualFile(f.toPath())
44-
private[packager] def toNioPaths(cp: Seq[Attributed[HashedVirtualFileRef]])(using
45-
conv: FileConverter
46-
): Vector[NioPath] =
47-
cp.map(toNioPath).toVector
48-
private[packager] def toFiles(cp: Seq[Attributed[HashedVirtualFileRef]])(using conv: FileConverter): Vector[File] =
49-
toNioPaths(cp).map(_.toFile())
50-
def toFileRefsMapping(mappings: Seq[(File, String)])(using conv: FileConverter): Seq[(FileRef, String)] =
51-
mappings.map { case (f, name) => toFileRef(f) -> name }
52-
def toFileRef(x: File)(using conv: FileConverter): FileRef =
53-
conv.toVirtualFile(x.toPath())
54-
private[packager] def getName(ref: FileRef): String =
10+
private[packager] def getName(ref: HashedVirtualFileRef): String =
5511
ref.name()
56-
private[packager] def getArtifactPathName(ref: ArtifactPath): String =
12+
private[packager] def getArtifactPathName(ref: VirtualFileRef): String =
5713
ref.name()
5814
private[packager] def classpathAttr: String = Attributes.Name.CLASS_PATH.toString()
5915
private[packager] def mainclassAttr: String = Attributes.Name.MAIN_CLASS.toString()

src/main/scala/com/typesafe/sbt/packager/MappingsHelper.scala

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.typesafe.sbt.packager
22

33
import sbt.{*, given}
44
import sbt.io.*
5+
import sbtcompat.PluginCompat.*
56
import xsbti.FileConverter
67

78
/** A set of helper methods to simplify the writing of mappings */
@@ -38,10 +39,10 @@ object MappingsHelper extends Mapper {
3839
def contentOf(sourceDir: String): Seq[(File, String)] =
3940
contentOf(file(sourceDir))
4041

41-
def contentOf(sourceDir: File, conv0: FileConverter): Seq[(PluginCompat.FileRef, String)] = {
42+
def contentOf(sourceDir: File, conv0: FileConverter): Seq[(FileRef, String)] = {
4243
implicit val conv: FileConverter = conv0
4344
contentOf(sourceDir).map { case (f, p) =>
44-
PluginCompat.toFileRef(f) -> p
45+
toFileRef(f) -> p
4546
}
4647
}
4748

@@ -59,10 +60,7 @@ object MappingsHelper extends Mapper {
5960
* @return
6061
* a list of mappings
6162
*/
62-
def fromClasspath(
63-
entries: Seq[Attributed[PluginCompat.FileRef]],
64-
target: String
65-
): Seq[(PluginCompat.FileRef, String)] =
63+
def fromClasspath(entries: Seq[Attributed[FileRef]], target: String): Seq[(FileRef, String)] =
6664
fromClasspath(entries, target, _ => true)
6765

6866
/**
@@ -89,25 +87,24 @@ object MappingsHelper extends Mapper {
8987
* default is false. When there's no Artifact meta data remove it
9088
*/
9189
def fromClasspath(
92-
entries: Seq[Attributed[PluginCompat.FileRef]],
90+
entries: Seq[Attributed[FileRef]],
9391
target: String,
9492
includeArtifact: PluginCompat.IncludeArtifact,
9593
includeOnNoArtifact: Boolean = false
96-
): Seq[(PluginCompat.FileRef, String)] =
97-
entries.filter(attr => attr.get(PluginCompat.artifactStr).map(includeArtifact) getOrElse includeOnNoArtifact).map {
98-
attribute =>
99-
val file = attribute.data
100-
val name = PluginCompat.getName(file)
101-
file -> s"$target/${name}"
94+
): Seq[(FileRef, String)] =
95+
entries.filter(attr => attr.get(artifactStr).map(includeArtifact) getOrElse includeOnNoArtifact).map { attribute =>
96+
val file = attribute.data
97+
val name = PluginCompat.getName(file)
98+
file -> s"$target/${name}"
10299
}
103100

104101
/**
105102
* Get the mappings for the given files relative to the given directories.
106103
*/
107-
def relative(files: Seq[File], dirs: Seq[File], conv0: FileConverter): Seq[(PluginCompat.FileRef, String)] = {
104+
def relative(files: Seq[File], dirs: Seq[File], conv0: FileConverter): Seq[(FileRef, String)] = {
108105
implicit val conv: FileConverter = conv0
109106
(files --- dirs) pair (relativeTo(dirs) | flat) map { case (f, p) =>
110-
PluginCompat.toFileRef(f) -> p
107+
toFileRef(f) -> p
111108
}
112109
}
113110
}

src/main/scala/com/typesafe/sbt/packager/SettingsHelper.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import sbt.{*, given}
44
import sbt.Keys.*
55
import sbt.librarymanagement.{IvyFileConfiguration, PublishConfiguration}
66
import com.typesafe.sbt.packager.Compat.*
7+
import sbtcompat.PluginCompat.*
78
import xsbti.FileConverter
89

910
/**
@@ -15,7 +16,7 @@ object SettingsHelper {
1516

1617
def addPackage(
1718
config: Configuration,
18-
packageTask: TaskKey[PluginCompat.FileRef],
19+
packageTask: TaskKey[FileRef],
1920
extension: String,
2021
classifier: Option[String] = None
2122
): Seq[Setting[?]] =
@@ -28,7 +29,7 @@ object SettingsHelper {
2829

2930
def makeDeploymentSettings(
3031
config: Configuration,
31-
packageTask: TaskKey[PluginCompat.FileRef],
32+
packageTask: TaskKey[FileRef],
3233
extension: String,
3334
classifier: Option[String] = None
3435
): Seq[Setting[?]] =
@@ -58,7 +59,7 @@ object SettingsHelper {
5859
.withArtifacts(packagedArtifacts.value.toVector.map { case (a, f) =>
5960
val conv0 = fileConverter.value
6061
implicit val conv: FileConverter = conv0
61-
(a, PluginCompat.toFile(f))
62+
(a, toFile(f))
6263
})
6364
.withChecksums(checksums.value.toVector)
6465
.withOverwrite(isSnapshot.value)
@@ -70,7 +71,7 @@ object SettingsHelper {
7071
.withArtifacts(packagedArtifacts.value.toVector.map { case (a, f) =>
7172
val conv0 = fileConverter.value
7273
implicit val conv: FileConverter = conv0
73-
(a, PluginCompat.toFile(f))
74+
(a, toFile(f))
7475
})
7576
.withChecksums(checksums.value.toVector)
7677
.withOverwrite(isSnapshot.value)
@@ -82,7 +83,7 @@ object SettingsHelper {
8283
.withArtifacts(packagedArtifacts.value.toVector.map { case (a, f) =>
8384
val conv0 = fileConverter.value
8485
implicit val conv: FileConverter = conv0
85-
(a, PluginCompat.toFile(f))
86+
(a, toFile(f))
8687
})
8788
.withChecksums(checksums.value.toVector)
8889
.withOverwrite(isSnapshot.value)

src/main/scala/com/typesafe/sbt/packager/Stager.scala

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import sbt.util.CacheStore
66
import java.io.File
77

88
import com.typesafe.sbt.packager.Compat._
9+
import sbtcompat.PluginCompat._
910
import xsbti.FileConverter
1011

1112
object Stager {
@@ -22,14 +23,12 @@ object Stager {
2223
* @param mappings
2324
* staging content
2425
*/
25-
def stageFiles(
26-
config: String
27-
)(cacheDirectory: File, stageDirectory: File, mappings: Seq[(PluginCompat.FileRef, String)])(implicit
26+
def stageFiles(config: String)(cacheDirectory: File, stageDirectory: File, mappings: Seq[(FileRef, String)])(implicit
2827
conv: FileConverter
2928
): File = {
3029
val cache = cacheDirectory / ("packager-mappings-" + config)
3130
val copies = mappings map { case (ref, path) =>
32-
PluginCompat.toFile(ref) -> (stageDirectory / path)
31+
toFile(ref) -> (stageDirectory / path)
3332
}
3433
val store = CacheStore(cache)
3534
Sync.sync(store, FileInfo.hash)(copies)
@@ -48,8 +47,8 @@ object Stager {
4847
* @see
4948
* stageFiles
5049
*/
51-
def stage(config: String)(streams: TaskStreams, stageDirectory: File, mappings: Seq[(PluginCompat.FileRef, String)])(
52-
implicit conv: FileConverter
50+
def stage(config: String)(streams: TaskStreams, stageDirectory: File, mappings: Seq[(FileRef, String)])(implicit
51+
conv: FileConverter
5352
): File =
5453
stageFiles(config)(streams.cacheDirectory, stageDirectory, mappings)
5554

src/main/scala/com/typesafe/sbt/packager/archetypes/JavaAppKeys.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.typesafe.sbt.packager.archetypes
22

33
import sbt.{*, given}
44
import com.typesafe.sbt.packager.PluginCompat
5+
import sbtcompat.PluginCompat.FileRef
56

67
/**
78
* Available settings/tasks for the [[com.typesafe.sbt.packager.archetypes.JavaAppPackaging]] and all depending
@@ -17,10 +18,10 @@ trait JavaAppKeys {
1718
)
1819
@transient
1920
val scriptClasspathOrdering =
20-
taskKey[Seq[(PluginCompat.FileRef, String)]]("The order of the classpath used at runtime for the bat/bash scripts.")
21+
taskKey[Seq[(FileRef, String)]]("The order of the classpath used at runtime for the bat/bash scripts.")
2122
@transient
2223
val projectDependencyArtifacts =
23-
taskKey[Seq[Attributed[PluginCompat.FileRef]]]("The set of exported artifacts from our dependent projects.")
24+
taskKey[Seq[Attributed[FileRef]]]("The set of exported artifacts from our dependent projects.")
2425
@transient
2526
val scriptClasspath = TaskKey[Seq[String]](
2627
"scriptClasspath",

0 commit comments

Comments
 (0)