Skip to content

Commit 714374c

Browse files
authored
Merge pull request #85 from scala-exercises/jp-resource-proposal
Replaces scala.util.Try by cats.effect.Resource
2 parents ef2cbc6 + 051ead2 commit 714374c

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

server/src/main/scala/org/scalaexercises/evaluator/evaluation.scala

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import java.math.BigInteger
1212
import java.security.MessageDigest
1313
import java.util.jar.JarFile
1414

15-
import cats.effect.{Concurrent, ConcurrentEffect, Timer}
15+
import cats.effect._
1616
import cats.implicits._
1717
import coursier._
1818
import coursier.cache.{ArtifactError, FileCache}
@@ -22,12 +22,12 @@ import org.scalaexercises.evaluator.{Dependency => EvaluatorDependency}
2222

2323
import scala.concurrent.duration._
2424
import scala.language.reflectiveCalls
25-
import scala.reflect.internal.util.{AbstractFileClassLoader, BatchSourceFile, Position}
2625
import scala.reflect.internal.util.ScalaClassLoader.URLClassLoader
26+
import scala.reflect.internal.util.{AbstractFileClassLoader, BatchSourceFile, Position}
2727
import scala.tools.nsc.io.{AbstractFile, VirtualDirectory}
2828
import scala.tools.nsc.reporters._
2929
import scala.tools.nsc.{Global, Settings}
30-
import scala.util.{Failure, Success, Try}
30+
import scala.util.Try
3131
import scala.util.control.NonFatal
3232

3333
class Evaluator[F[_]: Sync](timeout: FiniteDuration = 20.seconds)(
@@ -462,35 +462,35 @@ class ${className} extends (() => Any) with java.io.Serializable {
462462
val classPath = getClassPath(this.getClass.getClassLoader)
463463
val currentClassPath = classPath.head
464464

465+
def checkCurrentClassPath: List[String] = currentClassPath match {
466+
case List(jarFile) if jarFile.endsWith(".jar") =>
467+
val relativeRoot = new File(jarFile).getParentFile
468+
val jarResource: Resource[IO, JarFile] =
469+
Resource.make(IO(new JarFile(jarFile)))(jar => IO(jar.close()))
470+
471+
val nestedClassPath: IO[String] =
472+
jarResource
473+
.use(jar => IO(jar.getManifest.getMainAttributes.getValue("Class-Path")))
474+
.handleError { throwable: Throwable =>
475+
throw new CompilerException(List(List(throwable.getMessage)))
476+
}
477+
478+
nestedClassPath.map {
479+
case ncp if ncp eq null => Nil
480+
case ncp =>
481+
ncp
482+
.split(" ")
483+
.map { f =>
484+
new File(relativeRoot, f).getAbsolutePath
485+
}
486+
.toList
487+
488+
}.unsafeRunSync
489+
case _ => Nil
490+
}
491+
465492
// if there's just one thing in the classpath, and it's a jar, assume an executable jar.
466-
currentClassPath ::: (if (currentClassPath.size == 1 && currentClassPath.head
467-
.endsWith(".jar")) {
468-
val jarFile = currentClassPath.head
469-
val relativeRoot =
470-
new File(jarFile).getParentFile
471-
val nestedClassPath = Try {
472-
val jar = new JarFile(jarFile)
473-
val CP = jar.getManifest.getMainAttributes.getValue("Class-Path")
474-
jar.close()
475-
CP
476-
} match {
477-
case Success(classPath) => classPath
478-
case Failure(throwable) =>
479-
throw new CompilerException(List(List(throwable.getMessage)))
480-
}
481-
if (nestedClassPath eq null) {
482-
Nil
483-
} else {
484-
nestedClassPath
485-
.split(" ")
486-
.map { f =>
487-
new File(relativeRoot, f).getAbsolutePath
488-
}
489-
.toList
490-
}
491-
} else {
492-
Nil
493-
}) ::: classPath.tail.flatten
493+
currentClassPath ::: checkCurrentClassPath ::: classPath.tail.flatten
494494
}
495495

496496
lazy val compilerOutputDir = target match {

0 commit comments

Comments
 (0)