Skip to content

Commit

Permalink
add location info to log (Scala 2 JVM)
Browse files Browse the repository at this point in the history
  • Loading branch information
rolang committed Jan 20, 2024
1 parent f68f54c commit 8932fa8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ jobs:
run: sbt tlCiRelease

- name: Upload release binaries
if: startsWith(github.ref, 'refs/tags/')
uses: softprops/action-gh-release@v1
with:
files: modules/cli/native/target/bin/*
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ ThisBuild / githubWorkflowPublish += WorkflowStep.Use(
params = Map(
"files" -> "modules/cli/native/target/bin/*"
),
cond = None,
cond = Some("startsWith(github.ref, 'refs/tags/')"),
)

ThisBuild / githubWorkflowBuild += WorkflowStep.Sbt(
Expand Down
41 changes: 25 additions & 16 deletions modules/core/jvm/src/main/scala-2/dumbo/ResourceFilePath.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import java.nio.file.{Path, Paths}
import java.util.zip.ZipFile

import scala.jdk.CollectionConverters.*
import scala.util.{Failure, Success, Try}

import cats.effect.Resource
import cats.effect.kernel.Sync
Expand All @@ -21,23 +22,31 @@ final case class ResourceFilePath(value: String) extends AnyVal {
}

object ResourceFilePath {
def fromResourcesDir[F[_]: Sync](location: String): F[List[ResourceFilePath]] =
Sync[F].delay(getClass().getClassLoader().getResources(location).asScala.toList).flatMap {
case url :: Nil if url.toString.startsWith("jar:") => listInJar(url.toURI(), location)
case url :: Nil =>
Sync[F].delay {
val base = Paths.get(url.toURI())
val resources =
new File(base.toString()).list().map(fileName => apply(Paths.get("/", location, fileName))).toList
resources
}
case Nil => Sync[F].raiseError(new ResourcesLocationNotFund(s"resource ${location} was not found"))
case multiple =>
Sync[F].raiseError(
new MultipleResoucesException(
s"found multiple resource locations for ${location} in:\n${multiple.mkString("\n")}"
)
private[dumbo] def fromResourcesDir[F[_]: Sync](location: String): (String, F[List[ResourceFilePath]]) =
Try(getClass().getClassLoader().getResources(location).asScala.toList) match {
case Failure(err) => ("", Sync[F].raiseError(err))
case Success(Nil) => ("", Sync[F].raiseError(new ResourcesLocationNotFund(s"resource ${location} was not found")))
case Success(url :: Nil) if url.toString.startsWith("jar:") => (url.toString, listInJar(url.toURI(), location))
case Success(url :: Nil) =>
(
url.toString,
Sync[F].delay {
val base = Paths.get(url.toURI())
val resources =
new File(base.toString()).list().map(fileName => apply(Paths.get("/", location, fileName))).toList
resources
},
)
case Success(multiple) =>
(
"",
Sync[F].raiseError(
new MultipleResoucesException(
s"found multiple resource locations for ${location} in:\n${multiple.mkString("\n")}"
)
),
)

}

def apply(p: Path): ResourceFilePath = ResourceFilePath(p.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import cats.effect.Sync
import dumbo.{DumboWithResourcesPartiallyApplied, ResourceFilePath}

private[dumbo] trait DumboPlatform {
def withResourcesIn[F[_]: Sync](location: String): DumboWithResourcesPartiallyApplied[F] =
def withResourcesIn[F[_]: Sync](location: String): DumboWithResourcesPartiallyApplied[F] = {
val (locationInfo, resources) = ResourceFilePath.fromResourcesDir(location)

new DumboWithResourcesPartiallyApplied[F](
ResourceReader.embeddedResources(ResourceFilePath.fromResourcesDir(location))
ResourceReader.embeddedResources(resources, Some(locationInfo))
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ private[dumbo] object ResourceReader {
}
}

def embeddedResources[F[_]: Sync](readResources: F[List[ResourceFilePath]]): ResourceReader[F] =
def embeddedResources[F[_]: Sync](
readResources: F[List[ResourceFilePath]],
locationInfo: Option[String] = None,
): ResourceReader[F] =
new ResourceReader[F] {
override val location: Option[String] = None
override val location: Option[String] = locationInfo

override def list: Stream[F, Path] = Stream.evals(readResources).map(r => Path.fromNioPath(r.toNioPath))

Expand Down

0 comments on commit 8932fa8

Please sign in to comment.