Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions lib/src/cellar/SourceFetcher.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package cellar

import cats.effect.IO
import cats.effect.{IO, Resource}
import coursierapi.Repository
import fs2.io.file.Path
import fs2.io.readInputStream

import java.util.zip.ZipFile
import scala.jdk.CollectionConverters.*
Expand All @@ -21,28 +22,34 @@ object SourceFetcher:
case None =>
IO.pure(Left(s"No sources JAR published for '${coord.render}'."))
case Some(jar) =>
IO.blocking(extractLines(jar, sourceFilePath, startLine, endLine))
extractLines(jar, sourceFilePath, startLine, endLine)
}

private def extractLines(
jar: Path,
sourceFilePath: String,
startLine: Int,
endLine: Int
): Either[String, SourceResult] =
): IO[Either[String, SourceResult]] =
val normalizedSource = sourceFilePath.replace('\\', '/')
val zip = ZipFile(jar.toNioPath.toFile)
try
val entry = zip.entries().asScala.find { e =>
!e.isDirectory && normalizedSource.endsWith(e.getName)
}
entry match
Resource.fromAutoCloseable(IO.blocking(ZipFile(jar.toNioPath.toFile))).use { zip =>
IO.blocking {
zip.entries().asScala
.find(e => !e.isDirectory && normalizedSource.endsWith(e.getName))
.map(e => (e.getName, zip.getInputStream(e)))
}.flatMap {
case None =>
Left(s"Source file not found in JAR (looked for suffix of '$normalizedSource').")
case Some(e) =>
val allLines = scala.io.Source.fromInputStream(zip.getInputStream(e), "UTF-8")
.getLines().toIndexedSeq
val extracted = allLines.slice(startLine, endLine + 1)
Right(SourceResult(e.getName, startLine, endLine, extracted))
finally
zip.close()
IO.pure(Left(s"Source file not found in JAR (looked for suffix of '$normalizedSource')."))
case Some((name, is)) =>
readInputStream(IO.pure(is), chunkSize = 65536)
.through(fs2.text.utf8.decode)
.through(fs2.text.lines)
.compile
.toVector
.map { allLines =>
val extracted = if endLine == Int.MaxValue then allLines.drop(startLine)
else allLines.slice(startLine, endLine + 1)
Right(SourceResult(name, startLine, endLine, extracted))
}
}
}
6 changes: 4 additions & 2 deletions lib/test/src/cellar/IntegrationTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class IntegrationTest extends CatsEffectSuite:
)
}

test("get-source: Java class returns java code block"):
test("get-source: Java class returns java code block with source body"):
TestFixtures.assumeFixturesAvailable()
val console = CapturingConsole()
given Console[IO] = console
Expand All @@ -230,7 +230,9 @@ class IntegrationTest extends CatsEffectSuite:
)
.map { code =>
assertEquals(code, ExitCode.Success)
assert(console.outBuf.toString.contains("```java"), s"Output: ${console.outBuf}")
val out = console.outBuf.toString
assert(out.contains("```java"), s"Output: $out")
assert(out.contains("getDefault"), s"Expected source body in: $out")
}

test("get-source: trait with same-file companion returns both"):
Expand Down
Loading