Skip to content

Commit

Permalink
Temporarily rely on interp.load.cp to back %AddJar (#1094)
Browse files Browse the repository at this point in the history
`import $cp` would be better, as it ensures the class path is updated
prior to compiling and running the current cell. But we need
com-lihaoyi/Ammonite#1340 to make it work.
  • Loading branch information
alexarchambault authored Apr 21, 2023
1 parent 520482d commit f48ea8a
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import cats.effect.IO
import fs2.Stream
import utest._

import scala.collection.JavaConverters._
import scala.collection.compat._
import scala.util.Properties

Expand Down Expand Up @@ -1029,6 +1030,92 @@ object ScalaKernelTests extends TestSuite {
)
}

test("toree AddJar file") {

val interpreter = new ScalaInterpreter(
params = ScalaInterpreterParams(
initialColors = Colors.BlackWhite,
toreeMagics = true
),
logCtx = logCtx
)

val kernel = Kernel.create(interpreter, interpreterEc, threads, logCtx)
.unsafeRunTimedOrThrow()

implicit val sessionId: SessionId = SessionId()

val jar = coursierapi.Fetch.create()
.addDependencies(coursierapi.Dependency.of("info.picocli", "picocli", "4.7.3"))
.fetch()
.asScala
.head
.toURI

kernel.execute(
"import picocli.CommandLine",
errors = Seq(
("", "Compilation Failed", List("Compilation Failed"))
),
ignoreStreams = true
)

kernel.execute(
s"%AddJar $jar",
"",
ignoreStreams = true
)

kernel.execute(
"import picocli.CommandLine",
"import picocli.CommandLine" + maybePostImportNewLine
)
}

test("toree AddJar URL") {

val interpreter = new ScalaInterpreter(
params = ScalaInterpreterParams(
initialColors = Colors.BlackWhite,
toreeMagics = true
),
logCtx = logCtx
)

val kernel = Kernel.create(interpreter, interpreterEc, threads, logCtx)
.unsafeRunTimedOrThrow()

implicit val sessionId: SessionId = SessionId()

val jar = coursierapi.Fetch.create()
.addDependencies(coursierapi.Dependency.of("info.picocli", "picocli", "4.7.3"))
.fetchResult()
.getArtifacts
.asScala
.head
.getKey
.getUrl

kernel.execute(
"import picocli.CommandLine",
errors = Seq(
("", "Compilation Failed", List("Compilation Failed"))
),
ignoreStreams = true
)

kernel.execute(
s"%AddJar $jar",
"",
ignoreStreams = true
)

kernel.execute(
"import picocli.CommandLine",
"import picocli.CommandLine" + maybePostImportNewLine
)
}

test("toree Html") {

val interpreter = new ScalaInterpreter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ object LineMagicHandlers {
System.err.println(s"Warning: ignoring unsupported %AddJar argument --magic")
if (uri.getScheme == "file") {
val path = Paths.get(uri)
Right(s"import $$cp.`$path`")
val q = "\""
Right(s"interp.load.cp(os.Path($q$q$q$path$q$q$q))")
}
else if (uri.getScheme == "http" || uri.getScheme == "https") {
val file = coursierapi.Cache.create().get(coursierapi.Artifact.of(uri.toASCIIString))
val q = "\""
Right(s"interp.load.cp(os.Path($q$q$q$file$q$q$q))")
}
else {
System.err.println(
Expand Down

0 comments on commit f48ea8a

Please sign in to comment.