@@ -679,16 +679,17 @@ object Build {
679679 val s = streams.value
680680 val cacheDir = s.cacheDirectory
681681 val dest = (Compile / sourceManaged).value / " downloaded"
682+ val lm = dependencyResolution.value
682683
683- val downloads = Seq (
684- " https://repo1.maven.org/maven2/ com/ lihaoyi/ pprint_3/ 0.9.3/pprint_3-0.9.3-sources.jar " ,
685- " https://repo1.maven.org/maven2/ com/ lihaoyi/ fansi_3/ 0.5.1/fansi_3-0.5.1-sources.jar " ,
686- " https://repo1.maven.org/maven2/ com/ lihaoyi/ sourcecode_3/ 0.4.4/sourcecode_3-0.4.4-sources.jar " ,
684+ val dependencies = Seq (
685+ ( " com. lihaoyi" , " pprint_3" , " 0.9.3" ) ,
686+ ( " com. lihaoyi" , " fansi_3" , " 0.5.1" ) ,
687+ ( " com. lihaoyi" , " sourcecode_3" , " 0.4.4" ) ,
687688 )
688689
689- // Create a marker file that tracks the download URLs for cache invalidation
690+ // Create a marker file that tracks the dependencies for cache invalidation
690691 val markerFile = cacheDir / " shaded-sources-marker"
691- val markerContent = downloads .mkString(" \n " )
692+ val markerContent = dependencies.map { case (org, name, version) => s " $org : $name : $version :sources " } .mkString(" \n " )
692693 if (! markerFile.exists || IO .read(markerFile) != markerContent) {
693694 IO .write(markerFile, markerContent)
694695 }
@@ -702,19 +703,25 @@ object Build {
702703 }
703704 IO .createDirectory(dest)
704705
705- for (url <- downloads) {
706- import java .net .URL
707-
708- // Download jar to a temporary file
709- val jarName = url.substring(url.lastIndexOf('/' ) + 1 )
710- val tempJar = cacheDir / jarName
711-
712- s.log.info(s " Downloading $jarName... " )
713- IO .transfer(new URL (url).openStream(), tempJar)
706+ for ((org, name, version) <- dependencies) {
707+ import sbt .librarymanagement ._
708+
709+ // Retrieve sources jar using dependencyResolution
710+ val moduleId = ModuleID (org, name, version).sources()
711+ val retrieveDir = cacheDir / " retrieved" / s " $org- $name- $version-sources "
712+
713+ s.log.info(s " Retrieving $org: $name: $version:sources... " )
714+ val retrieved = lm.retrieve(moduleId, scalaModuleInfo = None , retrieveDir, s.log)
715+ val jarFiles = retrieved.fold(
716+ w => throw w.resolveException,
717+ files => files.filter(_.getName.contains(" -sources.jar" ))
718+ )
714719
715- // Extract the jar using SBT's IO.unzip
716- s.log.info(s " Extracting $jarName... " )
717- IO .unzip(tempJar, dest)
720+ // Extract each retrieved jar
721+ jarFiles.foreach { jarFile =>
722+ s.log.info(s " Extracting ${jarFile.getName}... " )
723+ IO .unzip(jarFile, dest)
724+ }
718725 }
719726
720727 val scalaFiles = (dest ** " *.scala" ).get
0 commit comments