Skip to content

Replaces scala.util.Try by cats.effect.Resource #85

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

Merged
merged 1 commit into from
Jan 23, 2020
Merged
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
62 changes: 31 additions & 31 deletions server/src/main/scala/org/scalaexercises/evaluator/evaluation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import java.math.BigInteger
import java.security.MessageDigest
import java.util.jar.JarFile

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

import scala.concurrent.duration._
import scala.language.reflectiveCalls
import scala.reflect.internal.util.{AbstractFileClassLoader, BatchSourceFile, Position}
import scala.reflect.internal.util.ScalaClassLoader.URLClassLoader
import scala.reflect.internal.util.{AbstractFileClassLoader, BatchSourceFile, Position}
import scala.tools.nsc.io.{AbstractFile, VirtualDirectory}
import scala.tools.nsc.reporters._
import scala.tools.nsc.{Global, Settings}
import scala.util.{Failure, Success, Try}
import scala.util.Try
import scala.util.control.NonFatal

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

def checkCurrentClassPath: List[String] = currentClassPath match {
case List(jarFile) if jarFile.endsWith(".jar") =>
val relativeRoot = new File(jarFile).getParentFile
val jarResource: Resource[IO, JarFile] =
Resource.make(IO(new JarFile(jarFile)))(jar => IO(jar.close()))

val nestedClassPath: IO[String] =
jarResource
.use(jar => IO(jar.getManifest.getMainAttributes.getValue("Class-Path")))
.handleError { throwable: Throwable =>
throw new CompilerException(List(List(throwable.getMessage)))
}

nestedClassPath.map {
case ncp if ncp eq null => Nil
case ncp =>
ncp
.split(" ")
.map { f =>
new File(relativeRoot, f).getAbsolutePath
}
.toList

}.unsafeRunSync
case _ => Nil
}

// if there's just one thing in the classpath, and it's a jar, assume an executable jar.
currentClassPath ::: (if (currentClassPath.size == 1 && currentClassPath.head
.endsWith(".jar")) {
val jarFile = currentClassPath.head
val relativeRoot =
new File(jarFile).getParentFile
val nestedClassPath = Try {
val jar = new JarFile(jarFile)
val CP = jar.getManifest.getMainAttributes.getValue("Class-Path")
jar.close()
CP
} match {
case Success(classPath) => classPath
case Failure(throwable) =>
throw new CompilerException(List(List(throwable.getMessage)))
}
if (nestedClassPath eq null) {
Nil
} else {
nestedClassPath
.split(" ")
.map { f =>
new File(relativeRoot, f).getAbsolutePath
}
.toList
}
} else {
Nil
}) ::: classPath.tail.flatten
currentClassPath ::: checkCurrentClassPath ::: classPath.tail.flatten
}

lazy val compilerOutputDir = target match {
Expand Down