Skip to content

Commit bf7a147

Browse files
authored
fix reading resources from a jar at compile time (Scala 3) (#101)
* fix reading resources from a jar at compile time (Scala 3)
1 parent 01e0f47 commit bf7a147

File tree

7 files changed

+75
-9
lines changed

7 files changed

+75
-9
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,14 @@ jobs:
158158
name: cli-bin-${{ matrix.os }}
159159
path: modules/cli/native/target/bin/*
160160

161-
- name: Run example (covers reading resources from a jar)
161+
- name: Run example (covers reading resources from a jar at runtime)
162162
if: matrix.project == 'rootJVM'
163163
run: sbt 'project ${{ matrix.project }}' '++ ${{ matrix.scala }}' example/run
164164

165+
- name: Test reading resources from a jar at compile time
166+
if: matrix.project == 'rootJVM' && matrix.scala == '3'
167+
run: sbt -Dsample_lib_test 'testLib/clean; sampleLib/publishLocal; testLib/run'
168+
165169
- name: Make target directories
166170
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
167171
run: mkdir -p modules/core/native/target modules/core/jvm/target modules/cli/native/target project/target
@@ -397,5 +401,5 @@ jobs:
397401
- name: Submit Dependencies
398402
uses: scalacenter/sbt-dependency-submission@v2
399403
with:
400-
modules-ignore: dumbo_3 dumbo_2.13 testsflyway_3 testsflyway_2.13 dumbo_3 dumbo_2.13 dumbo_3 dumbo_2.13 tests_3 tests_2.13 example_3 example_2.13 tests_native0.4_3 tests_native0.4_2.13
404+
modules-ignore: sample-lib_3 dumbo_3 dumbo_2.13 testsflyway_3 testsflyway_2.13 dumbo_3 dumbo_2.13 dumbo_3 dumbo_2.13 tests_3 tests_2.13 example_3 example_2.13 sample-lib-test_3 tests_native0.4_3 tests_native0.4_2.13
401405
configs-ignore: test scala-tool scala-doc-tool test-internal

build.sbt

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,16 @@ ThisBuild / githubWorkflowGeneratedCI := (ThisBuild / githubWorkflowGeneratedCI)
191191

192192
ThisBuild / githubWorkflowBuild += WorkflowStep.Sbt(
193193
List("example/run"),
194-
name = Some("Run example (covers reading resources from a jar)"),
194+
name = Some("Run example (covers reading resources from a jar at runtime)"),
195195
cond = Some("matrix.project == 'rootJVM'"),
196196
)
197197

198+
ThisBuild / githubWorkflowBuild += WorkflowStep.Run(
199+
commands = List("sbt -Dsample_lib_test 'testLib/clean; sampleLib/publishLocal; testLib/run'"),
200+
name = Some("Test reading resources from a jar at compile time"),
201+
cond = Some("matrix.project == 'rootJVM' && matrix.scala == '3'"),
202+
)
203+
198204
addCommandAlias("fix", "; +Test/copyResources; +scalafixAll; +scalafmtAll; scalafmtSbt")
199205
addCommandAlias("check", "; +Test/copyResources; +scalafixAll --check; +scalafmtCheckAll; scalafmtSbtCheck")
200206

@@ -369,7 +375,31 @@ lazy val example = project
369375
.settings(commonSettings)
370376
.settings(
371377
Compile / run / fork := true,
372-
publish / skip := true,
373378
Compile / headerCheck := Nil,
374379
scalacOptions -= "-Werror",
375380
)
381+
382+
lazy val sampleLib = project
383+
.in(file("modules/sample-lib"))
384+
.enablePlugins((if (!sys.props.contains("sample_lib_test")) Seq(NoPublishPlugin) else Nil) *)
385+
.settings(
386+
version := "0.0.1-SNAPSHOT",
387+
scalaVersion := `scala-3`,
388+
crossScalaVersions := Seq(`scala-3`),
389+
name := "sample-lib",
390+
Compile / headerCheck := Nil,
391+
)
392+
393+
lazy val testLib = project
394+
.in(file("modules/test-lib"))
395+
.enablePlugins(NoPublishPlugin)
396+
.settings(commonSettings)
397+
.dependsOn(core.jvm)
398+
.settings(
399+
Compile / run / fork := true,
400+
libraryDependencies += "dev.rolang" %% "sample-lib" % "0.0.1-SNAPSHOT",
401+
scalaVersion := `scala-3`,
402+
crossScalaVersions := Seq(`scala-3`),
403+
name := "sample-lib-test",
404+
Compile / headerCheck := Nil,
405+
)

modules/core/jvm/src/main/scala-2/dumbo/ResourceFilePath.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ object ResourceFilePath {
6565
.entries()
6666
.asScala
6767
.toList
68-
.filter(_.getName().startsWith(location))
68+
.filter(e => e.getName().startsWith(location) && !e.isDirectory())
6969
.map(entry => ResourceFilePath(s"/${entry.getName()}"))
7070
}
7171
}

modules/core/shared/src/main/scala-3/dumbo/ResourceFilePath.scala

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,25 @@ object ResourceFilePath:
2323

2424
getClass().getClassLoader().getResources(location).asScala.toList match
2525
case head :: Nil =>
26-
val base = Paths.get(head.toURI())
27-
val resources =
28-
new File(base.toString()).list().map(fileName => s"/$location/$fileName").toList
29-
Expr(resources)
26+
if head.toString.startsWith("jar:") then
27+
val srcUriStr = head.toURI().toString()
28+
val jarFilePath = srcUriStr.slice(srcUriStr.lastIndexOf(":") + 1, srcUriStr.lastIndexOf("!"))
29+
30+
val resources = scala.util.Using.resource(java.util.zip.ZipFile(jarFilePath)) { fs =>
31+
fs
32+
.entries()
33+
.asScala
34+
.toList
35+
.filter(e => e.getName().startsWith(location) && !e.isDirectory())
36+
.map(entry => s"/${entry.getName()}")
37+
}
38+
39+
Expr(resources)
40+
else
41+
val base = Paths.get(head.toURI())
42+
val resources =
43+
new File(base.toString()).list().map(fileName => s"/$location/$fileName").toList
44+
Expr(resources)
3045
case Nil => report.errorAndAbort(s"resource ${location} was not found")
3146
case multiple =>
3247
report.errorAndAbort(s"found multiple resource locations for ${location} in:\n${multiple.mkString("\n")}")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE test_a();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE test_b();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import dumbo.ResourceFilePath
2+
3+
@main def run(args: String*) =
4+
val resources = ResourceFilePath.fromResourcesDir("sample_lib")
5+
val expected = List(
6+
"/sample_lib/V1__test.sql",
7+
"/sample_lib/V2__test_b.sql",
8+
)
9+
10+
if resources == expected then
11+
println("Test ok")
12+
sys.exit(0)
13+
else
14+
Console.err.println(s"Expected resources $expected\nbut got $resources")
15+
sys.exit(1)

0 commit comments

Comments
 (0)