-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Support //> using dep "..."
directives in Scala REPL
#24131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lihaoyi
wants to merge
5
commits into
scala:main
Choose a base branch
from
lihaoyi:coursier-interface
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package dotty.tools.repl | ||
|
||
import scala.language.unsafeNulls | ||
|
||
import java.io.File | ||
import java.net.{URL, URLClassLoader} | ||
import scala.jdk.CollectionConverters.* | ||
import scala.util.control.NonFatal | ||
|
||
import coursierapi.{Repository, Dependency, MavenRepository} | ||
import com.virtuslab.using_directives.UsingDirectivesProcessor | ||
import com.virtuslab.using_directives.custom.model.{Path, StringValue, Value} | ||
|
||
/** Handles dependency resolution using Coursier for the REPL */ | ||
object DependencyResolver: | ||
|
||
/** Parse a dependency string of the form `org::artifact:version` or `org:artifact:version` | ||
* and return the (organization, artifact, version) triple if successful. | ||
* | ||
* Supports both Maven-style (single colon) and Scala-style (double colon) notation: | ||
* - Maven: `com.lihaoyi:scalatags_3:0.13.1` | ||
* - Scala: `com.lihaoyi::scalatags:0.13.1` (automatically appends _3) | ||
*/ | ||
def parseDependency(dep: String): Option[(String, String, String)] = | ||
dep match | ||
case s"$org::$artifact:$version" => Some((org, s"${artifact}_3", version)) | ||
case s"$org:$artifact:$version" => Some((org, artifact, version)) | ||
case _ => | ||
System.err.println("Unable to parse dependency \"" + dep + "\"") | ||
None | ||
|
||
/** Extract all dependencies from using directives in source code */ | ||
def extractDependencies(sourceCode: String): List[String] = | ||
try | ||
val directives = new UsingDirectivesProcessor().extract(sourceCode.toCharArray) | ||
val deps = scala.collection.mutable.Buffer[String]() | ||
|
||
for | ||
directive <- directives.asScala | ||
(path, values) <- directive.getFlattenedMap.asScala | ||
do | ||
if path.getPath.asScala.toList == List("dep") then | ||
values.asScala.foreach { | ||
case strValue: StringValue => deps += strValue.get() | ||
case value => System.err.println("Unrecognized directive value " + value) | ||
} | ||
else | ||
System.err.println("Unrecognized directive " + path.getPath) | ||
|
||
deps.toList | ||
catch | ||
case NonFatal(e) => Nil // If parsing fails, fall back to empty list | ||
|
||
/** Resolve dependencies using Coursier Interface and return the classpath as a list of File objects */ | ||
def resolveDependencies(dependencies: List[(String, String, String)]): Either[String, List[File]] = | ||
if dependencies.isEmpty then Right(Nil) | ||
else | ||
try | ||
// Add Maven Central and Sonatype repositories | ||
val repos = Array( | ||
MavenRepository.of("https://repo1.maven.org/maven2"), | ||
MavenRepository.of("https://oss.sonatype.org/content/repositories/releases") | ||
) | ||
|
||
// Create dependency objects | ||
val deps = dependencies | ||
.map { case (org, artifact, version) => Dependency.of(org, artifact, version) } | ||
.toArray | ||
|
||
val fetch = coursierapi.Fetch.create() | ||
.withRepositories(repos*) | ||
.withDependencies(deps*) | ||
|
||
Right(fetch.fetch().asScala.toList) | ||
|
||
catch | ||
case NonFatal(e) => | ||
Left(s"Failed to resolve dependencies: ${e.getMessage}") | ||
|
||
/** Add resolved dependencies to the compiler classpath and classloader. | ||
* Returns the new classloader. | ||
* | ||
* This follows the same pattern as the `:jar` command. | ||
*/ | ||
def addToCompilerClasspath( | ||
files: List[File], | ||
prevClassLoader: ClassLoader, | ||
prevOutputDir: dotty.tools.io.AbstractFile | ||
)(using ctx: dotty.tools.dotc.core.Contexts.Context): AbstractFileClassLoader = | ||
import dotty.tools.dotc.classpath.ClassPathFactory | ||
import dotty.tools.dotc.core.SymbolLoaders | ||
import dotty.tools.dotc.core.Symbols.defn | ||
import dotty.tools.io.* | ||
import dotty.tools.runner.ScalaClassLoader.fromURLsParallelCapable | ||
|
||
// Create a classloader with all the resolved JAR files | ||
val urls = files.map(_.toURI.toURL).toArray | ||
val depsClassLoader = new URLClassLoader(urls, prevClassLoader) | ||
|
||
// Add each JAR to the compiler's classpath | ||
for file <- files do | ||
val jarFile = AbstractFile.getDirectory(file.getAbsolutePath) | ||
if jarFile != null then | ||
val jarClassPath = ClassPathFactory.newClassPath(jarFile) | ||
ctx.platform.addToClassPath(jarClassPath) | ||
SymbolLoaders.mergeNewEntries(defn.RootClass, ClassPath.RootPackage, jarClassPath, ctx.platform.classPath) | ||
|
||
// Create new classloader with previous output dir and resolved dependencies | ||
new AbstractFileClassLoader(prevOutputDir, depsClassLoader) | ||
|
||
end DependencyResolver |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a nod to just a nod to #24119 which has sensitive state towards whatever the current classloader is.