Skip to content

Commit 9c046d4

Browse files
gaeljwCopilot
andauthored
Support simpler syntax without need for implicit conversion in SBT 2.x (#1776)
Co-authored-by: gaeljw <18280708+gaeljw@users.noreply.github.com> fixup Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 2110268 commit 9c046d4

29 files changed

Lines changed: 261 additions & 12 deletions

File tree

‎.github/workflows/validate-pr.yml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ jobs:
3535
java: 8
3636
distribution: zulu
3737
command: +validateUniversal
38+
- os: ubuntu-latest
39+
java: 8
40+
distribution: zulu
41+
command: validateUniversalSbt1 # No cross build, only SBT 1.x
42+
- os: ubuntu-latest
43+
java: 8
44+
distribution: zulu
45+
command: validateUniversalSbt2 # No cross build, only SBT 2.x
3846
- os: ubuntu-latest
3947
java: 8
4048
distribution: zulu
@@ -59,6 +67,14 @@ jobs:
5967
java: 8
6068
distribution: zulu
6169
command: +validateDocker
70+
- os: ubuntu-latest
71+
java: 8
72+
distribution: zulu
73+
command: validateDockerSbt1 # No cross build, only SBT 1.x
74+
- os: ubuntu-latest
75+
java: 8
76+
distribution: zulu
77+
command: validateDockerSbt2 # No cross build, only SBT 2.x
6278
- os: ubuntu-latest
6379
java: 11
6480
distribution: temurin

‎build.sbt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,17 @@ addCommandAlias("validate", "; clean ; update ; validateFormatting ; test")
122122
// tests take quite some time to run.
123123
// Ultimately we should run only those tests that are necessary for a change
124124
addCommandAlias("validateUniversal", "scripted universal/*")
125+
addCommandAlias("validateUniversalSbt1", "++ 2.12.20; scripted universal-sbt1/*")
126+
addCommandAlias("validateUniversalSbt2", "++ 3.7.3; scripted universal-sbt2/*")
125127
addCommandAlias("validateJar", "scripted jar/*")
126128
addCommandAlias("validateBash", "scripted bash/*")
127129
addCommandAlias("validateAsh", "scripted ash/*")
128130
addCommandAlias("validateGraalVMNativeImage", "scripted graalvm-native-image/*")
129131
addCommandAlias("validateRpm", "scripted rpm/*")
130132
addCommandAlias("validateDebian", "scripted debian/*")
131133
addCommandAlias("validateDocker", "scripted docker/*")
134+
addCommandAlias("validateDockerSbt1", "++ 2.12.20; scripted docker-sbt1/*")
135+
addCommandAlias("validateDockerSbt2", "++ 3.7.3; scripted docker-sbt2/*")
132136
addCommandAlias("validateJdkPackager", "scripted jdkpackager/*")
133137
// travis ci's jdk8 version doesn't support nested association elements.
134138
// error: Caused by: class com.sun.javafx.tools.ant.Info doesn't support the nested "association" element.

src/main/scala/com/typesafe/sbt/packager/MappingsHelper.scala renamed to src/main/scala-2.12/com/typesafe/sbt/packager/MappingsHelper.scala

File renamed without changes.
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.typesafe.sbt.packager
2+
3+
import sbt.{*, given}
4+
import sbt.Keys.fileConverter
5+
import sbt.io.*
6+
import sbtcompat.PluginCompat.*
7+
import xsbti.FileConverter
8+
9+
/** A set of helper methods to simplify the writing of mappings */
10+
object MappingsHelper extends Mapper {
11+
12+
/**
13+
* It lightens the build file if one wants to give a string instead of file.
14+
*
15+
* @example
16+
* {{{
17+
* mappings in Universal ++= directory("extra").value
18+
* }}}
19+
*
20+
* @param sourceDir
21+
* @return
22+
* mappings
23+
*/
24+
def directory(sourceDir: String): Def.Initialize[Seq[(FileRef, String)]] =
25+
Def.setting {
26+
implicit val conv: FileConverter = fileConverter.value
27+
directory(file(sourceDir)).map { case (f, p) =>
28+
toFileRef(f) -> p
29+
}
30+
}
31+
32+
/**
33+
* It lightens the build file if one wants to give a string instead of file.
34+
*
35+
* @example
36+
* {{{
37+
* mappings in Universal ++= contentOf("extra").value
38+
* }}}
39+
*
40+
* @param sourceDir
41+
* as string representation
42+
* @return
43+
* mappings
44+
*/
45+
def contentOf(sourceDir: String): Def.Initialize[Seq[(FileRef, String)]] =
46+
Def.setting {
47+
implicit val conv: FileConverter = fileConverter.value
48+
contentOf(sourceDir = file(sourceDir), conv0 = conv)
49+
}
50+
51+
def contentOf(sourceDir: File, conv0: FileConverter): Seq[(FileRef, String)] = {
52+
implicit val conv: FileConverter = conv0
53+
contentOf(sourceDir).map { case (f, p) =>
54+
toFileRef(f) -> p
55+
}
56+
}
57+
58+
/**
59+
* Create mappings from your classpath. For example if you want to add additional dependencies, like test or model.
60+
*
61+
* @example
62+
* Add all test artifacts to a separated test folder
63+
* {{{
64+
* mappings in Universal ++= fromClasspath((managedClasspath in Test).value, target = "test")
65+
* }}}
66+
*
67+
* @param entries
68+
* @param target
69+
* @return
70+
* a list of mappings
71+
*/
72+
def fromClasspath(entries: Seq[Attributed[FileRef]], target: String): Seq[(FileRef, String)] =
73+
fromClasspath(entries, target, _ => true)
74+
75+
/**
76+
* Create mappings from your classpath. For example if you want to add additional dependencies, like test or model.
77+
* You can also filter the artifacts that should be mapped to mappings.
78+
*
79+
* @example
80+
* Filter all osgi bundles
81+
* {{{
82+
* mappings in Universal ++= fromClasspath(
83+
* (managedClasspath in Runtime).value,
84+
* "osgi",
85+
* artifact => artifact.`type` == "bundle"
86+
* )
87+
* }}}
88+
*
89+
* @param entries
90+
* from where mappings should be created from
91+
* @param target
92+
* folder, e.g. `model`. Must not end with a slash
93+
* @param includeArtifact
94+
* function to determine if an artifact should result in a mapping
95+
* @param includeOnNoArtifact
96+
* default is false. When there's no Artifact meta data remove it
97+
*/
98+
def fromClasspath(
99+
entries: Seq[Attributed[FileRef]],
100+
target: String,
101+
includeArtifact: PluginCompat.IncludeArtifact,
102+
includeOnNoArtifact: Boolean = false
103+
): Seq[(FileRef, String)] =
104+
entries.filter(attr => attr.get(artifactStr).map(includeArtifact) getOrElse includeOnNoArtifact).map { attribute =>
105+
val file = attribute.data
106+
val name = PluginCompat.getName(file)
107+
file -> s"$target/${name}"
108+
}
109+
110+
/**
111+
* Get the mappings for the given files relative to the given directories.
112+
*/
113+
def relative(files: Seq[File], dirs: Seq[File], conv0: FileConverter): Seq[(FileRef, String)] = {
114+
implicit val conv: FileConverter = conv0
115+
(files --- dirs) pair (relativeTo(dirs) | flat) map { case (f, p) =>
116+
toFileRef(f) -> p
117+
}
118+
}
119+
}

src/sbt-test/docker/build-command/build.sbt renamed to src/sbt-test/docker-sbt1/build-command/build.sbt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,5 @@ name := "docker-build-command-test"
88

99
version := "0.1.0"
1010

11-
Docker / mappings ++= {
12-
implicit val converter: FileConverter = fileConverter.value
13-
PluginCompat.toFileRefsMapping(directory("src/main/resources/docker-test"))
14-
}
11+
Docker / mappings ++= directory("src/main/resources/docker-test")
1512
dockerBuildCommand := Seq("docker", "build", "-t", "docker-build-command-test:0.1.0", "docker-test/")

src/sbt-test/docker/build-command/project/plugins.sbt renamed to src/sbt-test/docker-sbt1/build-command/project/plugins.sbt

File renamed without changes.

src/sbt-test/docker/build-command/src/main/resources/docker-test/Dockerfile renamed to src/sbt-test/docker-sbt1/build-command/src/main/resources/docker-test/Dockerfile

File renamed without changes.
File renamed without changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import xsbti.FileConverter
2+
import sbtcompat.PluginCompat
3+
import NativePackagerHelper._
4+
5+
enablePlugins(JavaAppPackaging)
6+
7+
name := "docker-build-command-test"
8+
9+
version := "0.1.0"
10+
11+
Docker / mappings ++= directory("src/main/resources/docker-test").value
12+
dockerBuildCommand := Seq("docker", "build", "-t", "docker-build-command-test:0.1.0", "docker-test/")

src/sbt-test/universal/test-mapping-helpers/project/plugins.sbt renamed to src/sbt-test/docker-sbt2/build-command/project/plugins.sbt

File renamed without changes.

0 commit comments

Comments
 (0)