From 8932fa819c8db79e852aacaaa81c49f062368862 Mon Sep 17 00:00:00 2001 From: Roman Langolf Date: Sat, 20 Jan 2024 16:51:54 +0700 Subject: [PATCH] add location info to log (Scala 2 JVM) --- .github/workflows/ci.yml | 1 + build.sbt | 2 +- .../main/scala-2/dumbo/ResourceFilePath.scala | 41 +++++++++++-------- .../dumbo/internal/DumboPlatform.scala | 7 +++- .../dumbo/internal/ResourcesReader.scala | 7 +++- 5 files changed, 37 insertions(+), 21 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f4db123..83e2ff9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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/* diff --git a/build.sbt b/build.sbt index 595d4f6..bf45a78 100644 --- a/build.sbt +++ b/build.sbt @@ -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( diff --git a/modules/core/jvm/src/main/scala-2/dumbo/ResourceFilePath.scala b/modules/core/jvm/src/main/scala-2/dumbo/ResourceFilePath.scala index 1f2a736..f242ae1 100644 --- a/modules/core/jvm/src/main/scala-2/dumbo/ResourceFilePath.scala +++ b/modules/core/jvm/src/main/scala-2/dumbo/ResourceFilePath.scala @@ -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 @@ -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()) diff --git a/modules/core/jvm/src/main/scala-2/dumbo/internal/DumboPlatform.scala b/modules/core/jvm/src/main/scala-2/dumbo/internal/DumboPlatform.scala index 135a072..17ec4d0 100644 --- a/modules/core/jvm/src/main/scala-2/dumbo/internal/DumboPlatform.scala +++ b/modules/core/jvm/src/main/scala-2/dumbo/internal/DumboPlatform.scala @@ -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)) ) + } } diff --git a/modules/core/shared/src/main/scala/dumbo/internal/ResourcesReader.scala b/modules/core/shared/src/main/scala/dumbo/internal/ResourcesReader.scala index d3eafd3..ca9d2b1 100644 --- a/modules/core/shared/src/main/scala/dumbo/internal/ResourcesReader.scala +++ b/modules/core/shared/src/main/scala/dumbo/internal/ResourcesReader.scala @@ -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))